]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | /* |
2 | * Misc support functions | |
3 | */ | |
4 | ||
5 | #include "duk_internal.h" | |
6 | ||
7 | DUK_INTERNAL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop) { | |
8 | duk_uint_t sanity; | |
9 | ||
10 | DUK_ASSERT(thr != NULL); | |
11 | ||
12 | /* False if the object is NULL or the prototype 'p' is NULL. | |
13 | * In particular, false if both are NULL (don't compare equal). | |
14 | */ | |
15 | if (h == NULL || p == NULL) { | |
16 | return 0; | |
17 | } | |
18 | ||
19 | sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY; | |
20 | do { | |
21 | if (h == p) { | |
22 | return 1; | |
23 | } | |
24 | ||
25 | if (sanity-- == 0) { | |
26 | if (ignore_loop) { | |
27 | break; | |
28 | } else { | |
29 | DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT); | |
30 | } | |
31 | } | |
32 | h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h); | |
33 | } while (h); | |
34 | ||
35 | return 0; | |
36 | } | |
37 | ||
38 | DUK_INTERNAL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p) { | |
39 | #ifdef DUK_USE_REFERENCE_COUNTING | |
40 | duk_hobject *tmp; | |
41 | ||
42 | DUK_ASSERT(h); | |
43 | tmp = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h); | |
44 | DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p); | |
45 | DUK_HOBJECT_INCREF_ALLOWNULL(thr, p); /* avoid problems if p == h->prototype */ | |
46 | DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); | |
47 | #else | |
48 | DUK_ASSERT(h); | |
49 | DUK_UNREF(thr); | |
50 | DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p); | |
51 | #endif | |
52 | } |