5 #include "duk_internal.h"
7 /* Shared helper to provide toString() and valueOf(). Checks 'this', gets
8 * the primitive value to stack top, and optionally coerces with ToString().
10 DUK_INTERNAL duk_ret_t
duk_bi_boolean_prototype_tostring_shared(duk_context
*ctx
) {
13 duk_small_int_t coerce_tostring
= duk_get_current_magic(ctx
);
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.
21 tv
= duk_get_tval(ctx
, -1);
22 DUK_ASSERT(tv
!= NULL
);
24 if (DUK_TVAL_IS_BOOLEAN(tv
)) {
26 } else if (DUK_TVAL_IS_OBJECT(tv
)) {
27 h
= DUK_TVAL_GET_OBJECT(tv
);
28 DUK_ASSERT(h
!= NULL
);
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));
37 return DUK_RET_TYPE_ERROR
;
40 if (coerce_tostring
) {
41 duk_to_string(ctx
, -1);
46 DUK_INTERNAL duk_ret_t
duk_bi_boolean_constructor(duk_context
*ctx
) {
47 duk_hthread
*thr
= (duk_hthread
*) ctx
;
52 duk_to_boolean(ctx
, 0);
54 if (duk_is_constructor_call(ctx
)) {
55 /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */
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
]);
61 DUK_HOBJECT_SET_CLASS_NUMBER(h_this
, DUK_HOBJECT_CLASS_BOOLEAN
);
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 */