]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_boolean.c
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.5.2 / src-separate / duk_bi_boolean.c
1 /*
2 * Boolean built-ins
3 */
4
5 #include "duk_internal.h"
6
7 /* Shared helper to provide toString() and valueOf(). Checks 'this', gets
8 * the primitive value to stack top, and optionally coerces with ToString().
9 */
10 DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx) {
11 duk_tval *tv;
12 duk_hobject *h;
13 duk_small_int_t coerce_tostring = duk_get_current_magic(ctx);
14
15 /* XXX: there is room to use a shared helper here, many built-ins
16 * check the 'this' type, and if it's an object, check its class,
17 * then get its internal value, etc.
18 */
19
20 duk_push_this(ctx);
21 tv = duk_get_tval(ctx, -1);
22 DUK_ASSERT(tv != NULL);
23
24 if (DUK_TVAL_IS_BOOLEAN(tv)) {
25 goto type_ok;
26 } else if (DUK_TVAL_IS_OBJECT(tv)) {
27 h = DUK_TVAL_GET_OBJECT(tv);
28 DUK_ASSERT(h != NULL);
29
30 if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_BOOLEAN) {
31 duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
32 DUK_ASSERT(duk_is_boolean(ctx, -1));
33 goto type_ok;
34 }
35 }
36
37 return DUK_RET_TYPE_ERROR;
38
39 type_ok:
40 if (coerce_tostring) {
41 duk_to_string(ctx, -1);
42 }
43 return 1;
44 }
45
46 DUK_INTERNAL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx) {
47 duk_hthread *thr = (duk_hthread *) ctx;
48 duk_hobject *h_this;
49
50 DUK_UNREF(thr);
51
52 duk_to_boolean(ctx, 0);
53
54 if (duk_is_constructor_call(ctx)) {
55 /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */
56 duk_push_this(ctx);
57 h_this = duk_get_hobject(ctx, -1);
58 DUK_ASSERT(h_this != NULL);
59 DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]);
60
61 DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN);
62
63 duk_dup(ctx, 0); /* -> [ val obj val ] */
64 duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* XXX: proper flags? */
65 } /* unbalanced stack */
66
67 return 1;
68 }