]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.3.0/src-separate/duk_bi_duktape.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.3.0 / src-separate / duk_bi_duktape.c
1 /*
2 * Duktape built-ins
3 *
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
9 * anyway.
10 */
11
12 #include "duk_internal.h"
13
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.
19 */
20 DUK_INTERNAL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx) {
21 duk_hthread *thr = (duk_hthread *) ctx;
22 duk_tval *tv;
23 duk_heaphdr *h;
24 duk_int_t i, n;
25
26 DUK_UNREF(thr);
27
28 /* result array */
29 duk_push_array(ctx); /* -> [ val arr ] */
30
31 /* type tag (public) */
32 duk_push_int(ctx, duk_get_type(ctx, 0));
33
34 /* address */
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);
40 } else {
41 /* internal type tag */
42 duk_push_int(ctx, (duk_int_t) DUK_TVAL_GET_TAG(tv));
43 goto done;
44 }
45 DUK_ASSERT(h != NULL);
46
47 /* refcount */
48 #ifdef DUK_USE_REFERENCE_COUNTING
49 duk_push_size_t(ctx, DUK_HEAPHDR_GET_REFCOUNT(h));
50 #else
51 duk_push_undefined(ctx);
52 #endif
53
54 /* heaphdr size and additional allocation size, followed by
55 * type specific stuff (with varying value count)
56 */
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));
61 break;
62 }
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);
72 } else {
73 hdr_size = (duk_small_uint_t) sizeof(duk_hobject);
74 }
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
83 * the key list.
84 */
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);
90 if (h_data) {
91 duk_push_uint(ctx, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_data));
92 } else {
93 duk_push_uint(ctx, 0);
94 }
95 }
96 break;
97 }
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)));
103 } else {
104 /* When alloc_size == 0 the second allocation may not
105 * actually exist.
106 */
107 duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_dynamic)));
108 }
109 duk_push_uint(ctx, (duk_uint_t) (DUK_HBUFFER_GET_SIZE(h_buf)));
110 } else {
111 duk_push_uint(ctx, (duk_uint_t) (sizeof(duk_hbuffer_fixed) + DUK_HBUFFER_GET_SIZE(h_buf) + 1));
112 }
113 break;
114
115 }
116 }
117
118 done:
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++) {
123 duk_dup(ctx, i);
124 duk_put_prop_index(ctx, 1, i - 2);
125 }
126 duk_dup(ctx, 1);
127 return 1;
128 }
129
130 DUK_INTERNAL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx) {
131 duk_hthread *thr = (duk_hthread *) ctx;
132 duk_activation *act;
133 duk_uint_fast32_t pc;
134 duk_uint_fast32_t line;
135 duk_int_t level;
136
137 /* -1 = top callstack entry, callstack[callstack_top - 1]
138 * -callstack_top = bottom callstack entry, callstack[0]
139 */
140 level = duk_to_int(ctx, 0);
141 if (level >= 0 || -level > (duk_int_t) thr->callstack_top) {
142 return 0;
143 }
144 DUK_ASSERT(level >= -((duk_int_t) thr->callstack_top) && level <= -1);
145 act = thr->callstack + thr->callstack_top + level;
146
147 duk_push_object(ctx);
148
149 duk_push_tval(ctx, &act->tv_func);
150
151 /* Relevant PC is just before current one because PC is
152 * post-incremented. This should match what error augment
153 * code does.
154 */
155 pc = duk_hthread_get_act_prev_pc(thr, act);
156 duk_push_uint(ctx, (duk_uint_t) pc);
157
158 #if defined(DUK_USE_PC2LINE)
159 line = duk_hobject_pc2line_query(ctx, -2, pc);
160 #else
161 line = 0;
162 #endif
163 duk_push_uint(ctx, (duk_uint_t) line);
164
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.
169 */
170
171 /* [ level obj func pc line ] */
172
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);
177 return 1;
178 }
179
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;
184 duk_bool_t rc;
185
186 flags = (duk_small_uint_t) duk_get_uint(ctx, 0);
187 rc = duk_heap_mark_and_sweep(thr->heap, flags);
188
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).
191 */
192 duk_push_boolean(ctx, !rc);
193 return 1;
194 #else
195 DUK_UNREF(ctx);
196 return 0;
197 #endif
198 }
199
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
207 * be deleted.
208 */
209 duk_set_top(ctx, 2);
210 (void) duk_put_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER);
211 return 0;
212 } else {
213 /* Get. */
214 DUK_ASSERT(duk_get_top(ctx) == 1);
215 duk_get_prop_stridx(ctx, 0, DUK_STRIDX_INT_FINALIZER);
216 return 1;
217 }
218 }
219
220 DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) {
221 duk_hthread *thr = (duk_hthread *) ctx;
222 duk_hstring *h_str;
223
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.
227 */
228
229 h_str = duk_require_hstring(ctx, 0);
230 duk_require_valid_index(ctx, 1);
231
232 if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
233 duk_set_top(ctx, 2);
234 duk_hex_encode(ctx, 1);
235 DUK_ASSERT_TOP(ctx, 2);
236 } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
237 duk_set_top(ctx, 2);
238 duk_base64_encode(ctx, 1);
239 DUK_ASSERT_TOP(ctx, 2);
240 #ifdef DUK_USE_JX
241 } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
242 duk_bi_json_stringify_helper(ctx,
243 1 /*idx_value*/,
244 2 /*idx_replacer*/,
245 3 /*idx_space*/,
246 DUK_JSON_FLAG_EXT_CUSTOM |
247 DUK_JSON_FLAG_ASCII_ONLY |
248 DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
249 #endif
250 #ifdef DUK_USE_JC
251 } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
252 duk_bi_json_stringify_helper(ctx,
253 1 /*idx_value*/,
254 2 /*idx_replacer*/,
255 3 /*idx_space*/,
256 DUK_JSON_FLAG_EXT_COMPATIBLE |
257 DUK_JSON_FLAG_ASCII_ONLY /*flags*/);
258 #endif
259 } else {
260 return DUK_RET_TYPE_ERROR;
261 }
262 return 1;
263 }
264
265 DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx) {
266 duk_hthread *thr = (duk_hthread *) ctx;
267 duk_hstring *h_str;
268
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.
272 */
273
274 h_str = duk_require_hstring(ctx, 0);
275 duk_require_valid_index(ctx, 1);
276
277 if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
278 duk_set_top(ctx, 2);
279 duk_hex_decode(ctx, 1);
280 DUK_ASSERT_TOP(ctx, 2);
281 } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
282 duk_set_top(ctx, 2);
283 duk_base64_decode(ctx, 1);
284 DUK_ASSERT_TOP(ctx, 2);
285 #ifdef DUK_USE_JX
286 } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
287 duk_bi_json_parse_helper(ctx,
288 1 /*idx_value*/,
289 2 /*idx_replacer*/,
290 DUK_JSON_FLAG_EXT_CUSTOM /*flags*/);
291 #endif
292 #ifdef DUK_USE_JC
293 } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
294 duk_bi_json_parse_helper(ctx,
295 1 /*idx_value*/,
296 2 /*idx_replacer*/,
297 DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/);
298 #endif
299 } else {
300 return DUK_RET_TYPE_ERROR;
301 }
302 return 1;
303 }
304
305 /*
306 * Compact an object
307 */
308
309 DUK_INTERNAL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx) {
310 DUK_ASSERT_TOP(ctx, 1);
311 duk_compact(ctx, 0);
312 return 1; /* return the argument object */
313 }