]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.8.0/src-separate/duk_api_call.c
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.8.0 / src-separate / duk_api_call.c
1 /*
2 * Calls.
3 *
4 * Protected variants should avoid ever throwing an error.
5 */
6
7 #include "duk_internal.h"
8
9 /* Prepare value stack for a method call through an object property.
10 * May currently throw an error e.g. when getting the property.
11 */
12 DUK_LOCAL void duk__call_prop_prep_stack(duk_context *ctx, duk_idx_t normalized_obj_index, duk_idx_t nargs) {
13 DUK_ASSERT_CTX_VALID(ctx);
14
15 DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_index=%ld, nargs=%ld, stacktop=%ld",
16 (long) normalized_obj_index, (long) nargs, (long) duk_get_top(ctx)));
17
18 /* [... key arg1 ... argN] */
19
20 /* duplicate key */
21 duk_dup(ctx, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */
22 duk_get_prop(ctx, normalized_obj_index);
23
24 DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
25
26 /* [... key arg1 ... argN func] */
27
28 duk_replace(ctx, -nargs - 2);
29
30 /* [... func arg1 ... argN] */
31
32 duk_dup(ctx, normalized_obj_index);
33 duk_insert(ctx, -nargs - 1);
34
35 /* [... func this arg1 ... argN] */
36 }
37
38 DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs) {
39 duk_hthread *thr = (duk_hthread *) ctx;
40 duk_small_uint_t call_flags;
41 duk_idx_t idx_func;
42
43 DUK_ASSERT_CTX_VALID(ctx);
44 DUK_ASSERT(thr != NULL);
45
46 idx_func = duk_get_top(ctx) - nargs - 1;
47 if (idx_func < 0 || nargs < 0) {
48 /* note that we can't reliably pop anything here */
49 DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS);
50 }
51
52 /* XXX: awkward; we assume there is space for this, overwrite
53 * directly instead?
54 */
55 duk_push_undefined(ctx);
56 duk_insert(ctx, idx_func + 1);
57
58 call_flags = 0; /* not protected, respect reclimit, not constructor */
59
60 duk_handle_call_unprotected(thr, /* thread */
61 nargs, /* num_stack_args */
62 call_flags); /* call_flags */
63 }
64
65 DUK_EXTERNAL void duk_call_method(duk_context *ctx, duk_idx_t nargs) {
66 duk_hthread *thr = (duk_hthread *) ctx;
67 duk_small_uint_t call_flags;
68 duk_idx_t idx_func;
69
70 DUK_ASSERT_CTX_VALID(ctx);
71 DUK_ASSERT(thr != NULL);
72
73 idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
74 if (idx_func < 0 || nargs < 0) {
75 /* note that we can't reliably pop anything here */
76 DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS);
77 }
78
79 call_flags = 0; /* not protected, respect reclimit, not constructor */
80
81 duk_handle_call_unprotected(thr, /* thread */
82 nargs, /* num_stack_args */
83 call_flags); /* call_flags */
84 }
85
86 DUK_EXTERNAL void duk_call_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) {
87 /*
88 * XXX: if duk_handle_call() took values through indices, this could be
89 * made much more sensible. However, duk_handle_call() needs to fudge
90 * the 'this' and 'func' values to handle bound function chains, which
91 * is now done "in-place", so this is not a trivial change.
92 */
93
94 DUK_ASSERT_CTX_VALID(ctx);
95
96 obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */
97 if (DUK_UNLIKELY(nargs < 0)) {
98 DUK_ERROR_API((duk_hthread *) ctx, DUK_STR_INVALID_CALL_ARGS);
99 }
100
101 duk__call_prop_prep_stack(ctx, obj_index, nargs);
102
103 duk_call_method(ctx, nargs);
104 }
105
106 DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs) {
107 duk_hthread *thr = (duk_hthread *) ctx;
108 duk_small_uint_t call_flags;
109 duk_idx_t idx_func;
110 duk_int_t rc;
111
112 DUK_ASSERT_CTX_VALID(ctx);
113 DUK_ASSERT(thr != NULL);
114
115 idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */
116 if (idx_func < 0 || nargs < 0) {
117 /* We can't reliably pop anything here because the stack input
118 * shape is incorrect. So we throw an error; if the caller has
119 * no catch point for this, a fatal error will occur. Another
120 * alternative would be to just return an error. But then the
121 * stack would be in an unknown state which might cause some
122 * very hard to diagnose problems later on. Also note that even
123 * if we did not throw an error here, the underlying call handler
124 * might STILL throw an out-of-memory error or some other internal
125 * fatal error.
126 */
127 DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS);
128 return DUK_EXEC_ERROR; /* unreachable */
129 }
130
131 /* awkward; we assume there is space for this */
132 duk_push_undefined(ctx);
133 duk_insert(ctx, idx_func + 1);
134
135 call_flags = 0; /* respect reclimit, not constructor */
136
137 rc = duk_handle_call_protected(thr, /* thread */
138 nargs, /* num_stack_args */
139 call_flags); /* call_flags */
140
141 return rc;
142 }
143
144 DUK_EXTERNAL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs) {
145 duk_hthread *thr = (duk_hthread *) ctx;
146 duk_small_uint_t call_flags;
147 duk_idx_t idx_func;
148 duk_int_t rc;
149
150 DUK_ASSERT_CTX_VALID(ctx);
151 DUK_ASSERT(thr != NULL);
152
153 idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
154 if (idx_func < 0 || nargs < 0) {
155 /* See comments in duk_pcall(). */
156 DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS);
157 return DUK_EXEC_ERROR; /* unreachable */
158 }
159
160 call_flags = 0; /* respect reclimit, not constructor */
161
162 rc = duk_handle_call_protected(thr, /* thread */
163 nargs, /* num_stack_args */
164 call_flags); /* call_flags */
165
166 return rc;
167 }
168
169 DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_context *ctx) {
170 duk_idx_t obj_index;
171 duk_idx_t nargs;
172
173 /* Get the original arguments. Note that obj_index may be a relative
174 * index so the stack must have the same top when we use it.
175 */
176
177 DUK_ASSERT_CTX_VALID(ctx);
178
179 obj_index = (duk_idx_t) duk_get_int(ctx, -2);
180 nargs = (duk_idx_t) duk_get_int(ctx, -1);
181 duk_pop_2(ctx);
182
183 obj_index = duk_require_normalize_index(ctx, obj_index); /* make absolute */
184 duk__call_prop_prep_stack(ctx, obj_index, nargs);
185 duk_call_method(ctx, nargs);
186 return 1;
187 }
188
189 DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs) {
190 /*
191 * Must be careful to catch errors related to value stack manipulation
192 * and property lookup, not just the call itself.
193 */
194
195 DUK_ASSERT_CTX_VALID(ctx);
196
197 duk_push_idx(ctx, obj_index);
198 if (DUK_UNLIKELY(nargs < 0)) {
199 DUK_ERROR_API((duk_hthread *) ctx, DUK_STR_INVALID_CALL_ARGS);
200 }
201 duk_push_idx(ctx, nargs);
202
203 /* Inputs: explicit arguments (nargs), +1 for key, +2 for obj_index/nargs passing.
204 * If the value stack does not contain enough args, an error is thrown; this matches
205 * behavior of the other protected call API functions.
206 */
207 return duk_safe_call(ctx, duk__pcall_prop_raw, nargs + 1 + 2 /*nargs*/, 1 /*nrets*/);
208 }
209
210 DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets) {
211 duk_hthread *thr = (duk_hthread *) ctx;
212 duk_int_t rc;
213
214 DUK_ASSERT_CTX_VALID(ctx);
215 DUK_ASSERT(thr != NULL);
216
217 if (duk_get_top(ctx) < nargs || nargs < 0 || nrets < 0) {
218 /* See comments in duk_pcall(). */
219 DUK_ERROR_API(thr, DUK_STR_INVALID_CALL_ARGS);
220 return DUK_EXEC_ERROR; /* unreachable */
221 }
222
223 rc = duk_handle_safe_call(thr, /* thread */
224 func, /* func */
225 nargs, /* num_stack_args */
226 nrets); /* num_stack_res */
227
228 return rc;
229 }
230
231 DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) {
232 /*
233 * There are two [[Construct]] operations in the specification:
234 *
235 * - E5 Section 13.2.2: for Function objects
236 * - E5 Section 15.3.4.5.2: for "bound" Function objects
237 *
238 * The chain of bound functions is resolved in Section 15.3.4.5.2,
239 * with arguments "piling up" until the [[Construct]] internal
240 * method is called on the final, actual Function object. Note
241 * that the "prototype" property is looked up *only* from the
242 * final object, *before* calling the constructor.
243 *
244 * Currently we follow the bound function chain here to get the
245 * "prototype" property value from the final, non-bound function.
246 * However, we let duk_handle_call() handle the argument "piling"
247 * when the constructor is called. The bound function chain is
248 * thus now processed twice.
249 *
250 * When constructing new Array instances, an unnecessary object is
251 * created and discarded now: the standard [[Construct]] creates an
252 * object, and calls the Array constructor. The Array constructor
253 * returns an Array instance, which is used as the result value for
254 * the "new" operation; the object created before the Array constructor
255 * call is discarded.
256 *
257 * This would be easy to fix, e.g. by knowing that the Array constructor
258 * will always create a replacement object and skip creating the fallback
259 * object in that case.
260 *
261 * Note: functions called via "new" need to know they are called as a
262 * constructor. For instance, built-in constructors behave differently
263 * depending on how they are called.
264 */
265
266 /* XXX: merge this with duk_js_call.c, as this function implements
267 * core semantics (or perhaps merge the two files altogether).
268 */
269
270 duk_hthread *thr = (duk_hthread *) ctx;
271 duk_hobject *proto;
272 duk_hobject *cons;
273 duk_hobject *fallback;
274 duk_idx_t idx_cons;
275 duk_small_uint_t call_flags;
276
277 DUK_ASSERT_CTX_VALID(ctx);
278
279 /* [... constructor arg1 ... argN] */
280
281 idx_cons = duk_require_normalize_index(ctx, -nargs - 1);
282
283 DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld",
284 (long) duk_get_top(ctx), (long) nargs, (long) idx_cons));
285
286 /* XXX: code duplication */
287
288 /*
289 * Figure out the final, non-bound constructor, to get "prototype"
290 * property.
291 */
292
293 duk_dup(ctx, idx_cons);
294 for (;;) {
295 duk_tval *tv;
296 tv = DUK_GET_TVAL_NEGIDX(ctx, -1);
297 DUK_ASSERT(tv != NULL);
298
299 if (DUK_TVAL_IS_OBJECT(tv)) {
300 cons = DUK_TVAL_GET_OBJECT(tv);
301 DUK_ASSERT(cons != NULL);
302 if (!DUK_HOBJECT_IS_CALLABLE(cons) || !DUK_HOBJECT_HAS_CONSTRUCTABLE(cons)) {
303 /* Checking callability of the immediate target
304 * is important, same for constructability.
305 * Checking it for functions down the bound
306 * function chain is not strictly necessary
307 * because .bind() should normally reject them.
308 * But it's good to check anyway because it's
309 * technically possible to edit the bound function
310 * chain via internal keys.
311 */
312 goto not_constructable;
313 }
314 if (!DUK_HOBJECT_HAS_BOUND(cons)) {
315 break;
316 }
317 } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
318 /* Lightfuncs cannot be bound. */
319 break;
320 } else {
321 /* Anything else is not constructable. */
322 goto not_constructable;
323 }
324 duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); /* -> [... cons target] */
325 duk_remove(ctx, -2); /* -> [... target] */
326 }
327 DUK_ASSERT(duk_is_callable(ctx, -1));
328 DUK_ASSERT(duk_is_lightfunc(ctx, -1) ||
329 (duk_get_hobject(ctx, -1) != NULL && !DUK_HOBJECT_HAS_BOUND(duk_get_hobject(ctx, -1))));
330
331 /* [... constructor arg1 ... argN final_cons] */
332
333 /*
334 * Create "fallback" object to be used as the object instance,
335 * unless the constructor returns a replacement value.
336 * Its internal prototype needs to be set based on "prototype"
337 * property of the constructor.
338 */
339
340 duk_push_object(ctx); /* class Object, extensible */
341
342 /* [... constructor arg1 ... argN final_cons fallback] */
343
344 duk_get_prop_stridx(ctx, -2, DUK_STRIDX_PROTOTYPE);
345 proto = duk_get_hobject(ctx, -1);
346 if (!proto) {
347 DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object "
348 "-> leave standard Object prototype as fallback prototype"));
349 } else {
350 DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value "
351 "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto));
352 fallback = duk_get_hobject(ctx, -2);
353 DUK_ASSERT(fallback != NULL);
354 DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto);
355 }
356 duk_pop(ctx);
357
358 /* [... constructor arg1 ... argN final_cons fallback] */
359
360 /*
361 * Manipulate callstack for the call.
362 */
363
364 duk_dup_top(ctx);
365 duk_insert(ctx, idx_cons + 1); /* use fallback as 'this' value */
366 duk_insert(ctx, idx_cons); /* also stash it before constructor,
367 * in case we need it (as the fallback value)
368 */
369 duk_pop(ctx); /* pop final_cons */
370
371
372 /* [... fallback constructor fallback(this) arg1 ... argN];
373 * Note: idx_cons points to first 'fallback', not 'constructor'.
374 */
375
376 DUK_DDD(DUK_DDDPRINT("before call, idx_cons+1 (constructor) -> %!T, idx_cons+2 (fallback/this) -> %!T, "
377 "nargs=%ld, top=%ld",
378 (duk_tval *) duk_get_tval(ctx, idx_cons + 1),
379 (duk_tval *) duk_get_tval(ctx, idx_cons + 2),
380 (long) nargs,
381 (long) duk_get_top(ctx)));
382
383 /*
384 * Call the constructor function (called in "constructor mode").
385 */
386
387 call_flags = DUK_CALL_FLAG_CONSTRUCTOR_CALL; /* not protected, respect reclimit, is a constructor call */
388
389 duk_handle_call_unprotected(thr, /* thread */
390 nargs, /* num_stack_args */
391 call_flags); /* call_flags */
392
393 /* [... fallback retval] */
394
395 DUK_DDD(DUK_DDDPRINT("constructor call finished, fallback=%!iT, retval=%!iT",
396 (duk_tval *) duk_get_tval(ctx, -2),
397 (duk_tval *) duk_get_tval(ctx, -1)));
398
399 /*
400 * Determine whether to use the constructor return value as the created
401 * object instance or not.
402 */
403
404 if (duk_is_object(ctx, -1)) {
405 duk_remove(ctx, -2);
406 } else {
407 duk_pop(ctx);
408 }
409
410 /*
411 * Augment created errors upon creation (not when they are thrown or
412 * rethrown). __FILE__ and __LINE__ are not desirable here; the call
413 * stack reflects the caller which is correct.
414 */
415
416 #ifdef DUK_USE_AUGMENT_ERROR_CREATE
417 duk_hthread_sync_currpc(thr);
418 duk_err_augment_error_create(thr, thr, NULL, 0, 1 /*noblame_fileline*/);
419 #endif
420
421 /* [... retval] */
422
423 return;
424
425 not_constructable:
426 DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONSTRUCTABLE);
427 }
428
429 DUK_LOCAL duk_ret_t duk__pnew_helper(duk_context *ctx) {
430 duk_uint_t nargs;
431
432 nargs = duk_to_uint(ctx, -1);
433 duk_pop(ctx);
434
435 duk_new(ctx, nargs);
436 return 1;
437 }
438
439 DUK_EXTERNAL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs) {
440 duk_int_t rc;
441
442 DUK_ASSERT_CTX_VALID(ctx);
443
444 /* For now, just use duk_safe_call() to wrap duk_new(). We can't
445 * simply use a protected duk_handle_call() because there's post
446 * processing which might throw. It should be possible to ensure
447 * the post processing never throws (except in internal errors and
448 * out of memory etc which are always allowed) and then remove this
449 * wrapper.
450 */
451
452 if (DUK_UNLIKELY(nargs < 0)) {
453 DUK_ERROR_API((duk_hthread *) ctx, DUK_STR_INVALID_CALL_ARGS);
454 }
455 duk_push_uint(ctx, nargs);
456 rc = duk_safe_call(ctx, duk__pnew_helper, nargs + 2 /*nargs*/, 1 /*nrets*/);
457 return rc;
458 }
459
460 DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx) {
461 duk_hthread *thr = (duk_hthread *) ctx;
462 duk_activation *act;
463
464 DUK_ASSERT_CTX_VALID(ctx);
465 DUK_ASSERT(thr != NULL);
466 DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
467
468 act = duk_hthread_get_current_activation(thr);
469 if (act != NULL) {
470 return ((act->flags & DUK_ACT_FLAG_CONSTRUCT) != 0 ? 1 : 0);
471 }
472 return 0;
473 }
474
475 DUK_EXTERNAL duk_bool_t duk_is_strict_call(duk_context *ctx) {
476 duk_hthread *thr = (duk_hthread *) ctx;
477 duk_activation *act;
478
479 /* For user code this could just return 1 (strict) always
480 * because all Duktape/C functions are considered strict,
481 * and strict is also the default when nothing is running.
482 * However, Duktape may call this function internally when
483 * the current activation is an Ecmascript function, so
484 * this cannot be replaced by a 'return 1' without fixing
485 * the internal call sites.
486 */
487
488 DUK_ASSERT_CTX_VALID(ctx);
489 DUK_ASSERT(thr != NULL);
490 DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
491
492 act = duk_hthread_get_current_activation(thr);
493 if (act == NULL) {
494 /* Strict by default. */
495 return 1;
496 }
497 return ((act->flags & DUK_ACT_FLAG_STRICT) != 0 ? 1 : 0);
498 }
499
500 /*
501 * Duktape/C function magic
502 */
503
504 DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_context *ctx) {
505 duk_hthread *thr = (duk_hthread *) ctx;
506 duk_activation *act;
507 duk_hobject *func;
508
509 DUK_ASSERT_CTX_VALID(ctx);
510 DUK_ASSERT(thr != NULL);
511 DUK_ASSERT_DISABLE(thr->callstack_top >= 0);
512
513 act = duk_hthread_get_current_activation(thr);
514 if (act) {
515 func = DUK_ACT_GET_FUNC(act);
516 if (!func) {
517 duk_tval *tv = &act->tv_func;
518 duk_small_uint_t lf_flags;
519 lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
520 return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
521 }
522 DUK_ASSERT(func != NULL);
523
524 if (DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
525 duk_hnativefunction *nf = (duk_hnativefunction *) func;
526 return (duk_int_t) nf->magic;
527 }
528 }
529 return 0;
530 }
531
532 DUK_EXTERNAL duk_int_t duk_get_magic(duk_context *ctx, duk_idx_t index) {
533 duk_hthread *thr = (duk_hthread *) ctx;
534 duk_tval *tv;
535 duk_hobject *h;
536
537 DUK_ASSERT_CTX_VALID(ctx);
538
539 tv = duk_require_tval(ctx, index);
540 if (DUK_TVAL_IS_OBJECT(tv)) {
541 h = DUK_TVAL_GET_OBJECT(tv);
542 DUK_ASSERT(h != NULL);
543 if (!DUK_HOBJECT_HAS_NATIVEFUNCTION(h)) {
544 goto type_error;
545 }
546 return (duk_int_t) ((duk_hnativefunction *) h)->magic;
547 } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
548 duk_small_uint_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
549 return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
550 }
551
552 /* fall through */
553 type_error:
554 DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE);
555 return 0;
556 }
557
558 DUK_EXTERNAL void duk_set_magic(duk_context *ctx, duk_idx_t index, duk_int_t magic) {
559 duk_hnativefunction *nf;
560
561 DUK_ASSERT_CTX_VALID(ctx);
562
563 nf = duk_require_hnativefunction(ctx, index);
564 DUK_ASSERT(nf != NULL);
565 nf->magic = (duk_int16_t) magic;
566 }