]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * Pointer built-ins | |
3 | */ | |
4 | ||
5 | #include "duk_internal.h" | |
6 | ||
7 | /* | |
8 | * Constructor | |
9 | */ | |
10 | ||
11 | DUK_INTERNAL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx) { | |
12 | /* XXX: this behavior is quite useless now; it would be nice to be able | |
13 | * to create pointer values from e.g. numbers or strings. Numbers are | |
14 | * problematic on 64-bit platforms though. Hex encoded strings? | |
15 | */ | |
16 | if (duk_get_top(ctx) == 0) { | |
17 | duk_push_pointer(ctx, NULL); | |
18 | } else { | |
19 | duk_to_pointer(ctx, 0); | |
20 | } | |
21 | DUK_ASSERT(duk_is_pointer(ctx, 0)); | |
22 | duk_set_top(ctx, 1); | |
23 | ||
24 | if (duk_is_constructor_call(ctx)) { | |
25 | duk_push_object_helper(ctx, | |
26 | DUK_HOBJECT_FLAG_EXTENSIBLE | | |
27 | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER), | |
28 | DUK_BIDX_POINTER_PROTOTYPE); | |
29 | ||
30 | /* Pointer object internal value is immutable */ | |
31 | duk_dup(ctx, 0); | |
32 | duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); | |
33 | } | |
34 | /* Note: unbalanced stack on purpose */ | |
35 | ||
36 | return 1; | |
37 | } | |
38 | ||
39 | /* | |
40 | * toString(), valueOf() | |
41 | */ | |
42 | ||
43 | DUK_INTERNAL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx) { | |
44 | duk_tval *tv; | |
45 | duk_small_int_t to_string = duk_get_current_magic(ctx); | |
46 | ||
47 | duk_push_this(ctx); | |
48 | tv = duk_require_tval(ctx, -1); | |
49 | DUK_ASSERT(tv != NULL); | |
50 | ||
51 | if (DUK_TVAL_IS_POINTER(tv)) { | |
52 | /* nop */ | |
53 | } else if (DUK_TVAL_IS_OBJECT(tv)) { | |
54 | duk_hobject *h = DUK_TVAL_GET_OBJECT(tv); | |
55 | DUK_ASSERT(h != NULL); | |
56 | ||
57 | /* Must be a "pointer object", i.e. class "Pointer" */ | |
58 | if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_POINTER) { | |
59 | goto type_error; | |
60 | } | |
61 | ||
62 | duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE); | |
63 | } else { | |
64 | goto type_error; | |
65 | } | |
66 | ||
67 | if (to_string) { | |
68 | duk_to_string(ctx, -1); | |
69 | } | |
70 | return 1; | |
71 | ||
72 | type_error: | |
73 | return DUK_RET_TYPE_ERROR; | |
74 | } |