4 * Size optimization note: it might seem that vararg multipurpose functions
5 * like fin(), enc(), and dec() are not very size optimal, but using a single
6 * user-visible Ecmascript function saves a lot of run-time footprint; each
7 * Function instance takes >100 bytes. Using a shared native helper and a
8 * 'magic' value won't save much if there are multiple Function instances
12 #include "duk_internal.h"
14 /* Raw helper to extract internal information / statistics about a value.
15 * The return values are version specific and must not expose anything
16 * that would lead to security issues (e.g. exposing compiled function
17 * 'data' buffer might be an issue). Currently only counts and sizes and
18 * such are given so there should not be a security impact.
20 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_info(duk_context
*ctx
) {
21 duk_hthread
*thr
= (duk_hthread
*) ctx
;
29 duk_push_array(ctx
); /* -> [ val arr ] */
31 /* type tag (public) */
32 duk_push_int(ctx
, duk_get_type(ctx
, 0));
35 tv
= duk_get_tval(ctx
, 0);
36 DUK_ASSERT(tv
!= NULL
); /* because arg count is 1 */
37 if (DUK_TVAL_IS_HEAP_ALLOCATED(tv
)) {
38 h
= DUK_TVAL_GET_HEAPHDR(tv
);
39 duk_push_pointer(ctx
, (void *) h
);
41 /* internal type tag */
42 duk_push_int(ctx
, (duk_int_t
) DUK_TVAL_GET_TAG(tv
));
45 DUK_ASSERT(h
!= NULL
);
48 #ifdef DUK_USE_REFERENCE_COUNTING
49 duk_push_size_t(ctx
, DUK_HEAPHDR_GET_REFCOUNT(h
));
51 duk_push_undefined(ctx
);
54 /* heaphdr size and additional allocation size, followed by
55 * type specific stuff (with varying value count)
57 switch ((duk_small_int_t
) DUK_HEAPHDR_GET_TYPE(h
)) {
58 case DUK_HTYPE_STRING
: {
59 duk_hstring
*h_str
= (duk_hstring
*) h
;
60 duk_push_uint(ctx
, (duk_uint_t
) (sizeof(duk_hstring
) + DUK_HSTRING_GET_BYTELEN(h_str
) + 1));
63 case DUK_HTYPE_OBJECT
: {
64 duk_hobject
*h_obj
= (duk_hobject
*) h
;
65 duk_small_uint_t hdr_size
;
66 if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj
)) {
67 hdr_size
= (duk_small_uint_t
) sizeof(duk_hcompiledfunction
);
68 } else if (DUK_HOBJECT_IS_NATIVEFUNCTION(h_obj
)) {
69 hdr_size
= (duk_small_uint_t
) sizeof(duk_hnativefunction
);
70 } else if (DUK_HOBJECT_IS_THREAD(h_obj
)) {
71 hdr_size
= (duk_small_uint_t
) sizeof(duk_hthread
);
73 hdr_size
= (duk_small_uint_t
) sizeof(duk_hobject
);
75 duk_push_uint(ctx
, (duk_uint_t
) hdr_size
);
76 duk_push_uint(ctx
, (duk_uint_t
) DUK_HOBJECT_E_ALLOC_SIZE(h_obj
));
77 duk_push_uint(ctx
, (duk_uint_t
) DUK_HOBJECT_GET_ESIZE(h_obj
));
78 /* Note: e_next indicates the number of gc-reachable entries
79 * in the entry part, and also indicates the index where the
80 * next new property would be inserted. It does *not* indicate
81 * the number of non-NULL keys present in the object. That
82 * value could be counted separately but requires a pass through
85 duk_push_uint(ctx
, (duk_uint_t
) DUK_HOBJECT_GET_ENEXT(h_obj
));
86 duk_push_uint(ctx
, (duk_uint_t
) DUK_HOBJECT_GET_ASIZE(h_obj
));
87 duk_push_uint(ctx
, (duk_uint_t
) DUK_HOBJECT_GET_HSIZE(h_obj
));
88 if (DUK_HOBJECT_IS_COMPILEDFUNCTION(h_obj
)) {
89 duk_hbuffer
*h_data
= (duk_hbuffer
*) DUK_HCOMPILEDFUNCTION_GET_DATA(thr
->heap
, (duk_hcompiledfunction
*) h_obj
);
91 duk_push_uint(ctx
, (duk_uint_t
) DUK_HBUFFER_GET_SIZE(h_data
));
93 duk_push_uint(ctx
, 0);
98 case DUK_HTYPE_BUFFER
: {
99 duk_hbuffer
*h_buf
= (duk_hbuffer
*) h
;
100 if (DUK_HBUFFER_HAS_DYNAMIC(h_buf
)) {
101 if (DUK_HBUFFER_HAS_EXTERNAL(h_buf
)) {
102 duk_push_uint(ctx
, (duk_uint_t
) (sizeof(duk_hbuffer_external
)));
104 /* When alloc_size == 0 the second allocation may not
107 duk_push_uint(ctx
, (duk_uint_t
) (sizeof(duk_hbuffer_dynamic
)));
109 duk_push_uint(ctx
, (duk_uint_t
) (DUK_HBUFFER_GET_SIZE(h_buf
)));
111 duk_push_uint(ctx
, (duk_uint_t
) (sizeof(duk_hbuffer_fixed
) + DUK_HBUFFER_GET_SIZE(h_buf
) + 1));
119 /* set values into ret array */
120 /* XXX: primitive to make array from valstack slice */
121 n
= duk_get_top(ctx
);
122 for (i
= 2; i
< n
; i
++) {
124 duk_put_prop_index(ctx
, 1, i
- 2);
130 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_act(duk_context
*ctx
) {
131 duk_hthread
*thr
= (duk_hthread
*) ctx
;
133 duk_uint_fast32_t pc
;
134 duk_uint_fast32_t line
;
137 /* -1 = top callstack entry, callstack[callstack_top - 1]
138 * -callstack_top = bottom callstack entry, callstack[0]
140 level
= duk_to_int(ctx
, 0);
141 if (level
>= 0 || -level
> (duk_int_t
) thr
->callstack_top
) {
144 DUK_ASSERT(level
>= -((duk_int_t
) thr
->callstack_top
) && level
<= -1);
145 act
= thr
->callstack
+ thr
->callstack_top
+ level
;
147 duk_push_object(ctx
);
149 duk_push_tval(ctx
, &act
->tv_func
);
151 /* Relevant PC is just before current one because PC is
152 * post-incremented. This should match what error augment
155 pc
= duk_hthread_get_act_prev_pc(thr
, act
);
156 duk_push_uint(ctx
, (duk_uint_t
) pc
);
158 #if defined(DUK_USE_PC2LINE)
159 line
= duk_hobject_pc2line_query(ctx
, -2, pc
);
163 duk_push_uint(ctx
, (duk_uint_t
) line
);
165 /* Providing access to e.g. act->lex_env would be dangerous: these
166 * internal structures must never be accessible to the application.
167 * Duktape relies on them having consistent data, and this consistency
168 * is only asserted for, not checked for.
171 /* [ level obj func pc line ] */
173 /* XXX: version specific array format instead? */
174 duk_xdef_prop_stridx_wec(ctx
, -4, DUK_STRIDX_LINE_NUMBER
);
175 duk_xdef_prop_stridx_wec(ctx
, -3, DUK_STRIDX_PC
);
176 duk_xdef_prop_stridx_wec(ctx
, -2, DUK_STRIDX_LC_FUNCTION
);
180 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_gc(duk_context
*ctx
) {
181 #ifdef DUK_USE_MARK_AND_SWEEP
182 duk_hthread
*thr
= (duk_hthread
*) ctx
;
183 duk_small_uint_t flags
;
186 flags
= (duk_small_uint_t
) duk_get_uint(ctx
, 0);
187 rc
= duk_heap_mark_and_sweep(thr
->heap
, flags
);
189 /* XXX: Not sure what the best return value would be in the API.
190 * Return a boolean for now. Note that rc == 0 is success (true).
192 duk_push_boolean(ctx
, !rc
);
200 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_fin(duk_context
*ctx
) {
201 (void) duk_require_hobject(ctx
, 0);
202 if (duk_get_top(ctx
) >= 2) {
203 /* Set: currently a finalizer is disabled by setting it to
204 * undefined; this does not remove the property at the moment.
205 * The value could be type checked to be either a function
206 * or something else; if something else, the property could
210 (void) duk_put_prop_stridx(ctx
, 0, DUK_STRIDX_INT_FINALIZER
);
214 DUK_ASSERT(duk_get_top(ctx
) == 1);
215 duk_get_prop_stridx(ctx
, 0, DUK_STRIDX_INT_FINALIZER
);
220 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_enc(duk_context
*ctx
) {
221 duk_hthread
*thr
= (duk_hthread
*) ctx
;
224 /* Vararg function: must be careful to check/require arguments.
225 * The JSON helpers accept invalid indices and treat them like
226 * non-existent optional parameters.
229 h_str
= duk_require_hstring(ctx
, 0);
230 duk_require_valid_index(ctx
, 1);
232 if (h_str
== DUK_HTHREAD_STRING_HEX(thr
)) {
234 duk_hex_encode(ctx
, 1);
235 DUK_ASSERT_TOP(ctx
, 2);
236 } else if (h_str
== DUK_HTHREAD_STRING_BASE64(thr
)) {
238 duk_base64_encode(ctx
, 1);
239 DUK_ASSERT_TOP(ctx
, 2);
241 } else if (h_str
== DUK_HTHREAD_STRING_JX(thr
)) {
242 duk_bi_json_stringify_helper(ctx
,
246 DUK_JSON_FLAG_EXT_CUSTOM
|
247 DUK_JSON_FLAG_ASCII_ONLY
|
248 DUK_JSON_FLAG_AVOID_KEY_QUOTES
/*flags*/);
251 } else if (h_str
== DUK_HTHREAD_STRING_JC(thr
)) {
252 duk_bi_json_stringify_helper(ctx
,
256 DUK_JSON_FLAG_EXT_COMPATIBLE
|
257 DUK_JSON_FLAG_ASCII_ONLY
/*flags*/);
260 return DUK_RET_TYPE_ERROR
;
265 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_dec(duk_context
*ctx
) {
266 duk_hthread
*thr
= (duk_hthread
*) ctx
;
269 /* Vararg function: must be careful to check/require arguments.
270 * The JSON helpers accept invalid indices and treat them like
271 * non-existent optional parameters.
274 h_str
= duk_require_hstring(ctx
, 0);
275 duk_require_valid_index(ctx
, 1);
277 if (h_str
== DUK_HTHREAD_STRING_HEX(thr
)) {
279 duk_hex_decode(ctx
, 1);
280 DUK_ASSERT_TOP(ctx
, 2);
281 } else if (h_str
== DUK_HTHREAD_STRING_BASE64(thr
)) {
283 duk_base64_decode(ctx
, 1);
284 DUK_ASSERT_TOP(ctx
, 2);
286 } else if (h_str
== DUK_HTHREAD_STRING_JX(thr
)) {
287 duk_bi_json_parse_helper(ctx
,
290 DUK_JSON_FLAG_EXT_CUSTOM
/*flags*/);
293 } else if (h_str
== DUK_HTHREAD_STRING_JC(thr
)) {
294 duk_bi_json_parse_helper(ctx
,
297 DUK_JSON_FLAG_EXT_COMPATIBLE
/*flags*/);
300 return DUK_RET_TYPE_ERROR
;
309 DUK_INTERNAL duk_ret_t
duk_bi_duktape_object_compact(duk_context
*ctx
) {
310 DUK_ASSERT_TOP(ctx
, 1);
312 return 1; /* return the argument object */