4 * Protected variants should avoid ever throwing an error.
7 #include "duk_internal.h"
9 /* Prepare value stack for a method call through an object property.
10 * May currently throw an error e.g. when getting the property.
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
);
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
)));
18 /* [... key arg1 ... argN] */
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
);
24 DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval
*) duk_get_tval(ctx
, -1)));
26 /* [... key arg1 ... argN func] */
28 duk_replace(ctx
, -nargs
- 2);
30 /* [... func arg1 ... argN] */
32 duk_dup(ctx
, normalized_obj_index
);
33 duk_insert(ctx
, -nargs
- 1);
35 /* [... func this arg1 ... argN] */
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
;
43 DUK_ASSERT_CTX_VALID(ctx
);
44 DUK_ASSERT(thr
!= NULL
);
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
);
52 /* XXX: awkward; we assume there is space for this, overwrite
55 duk_push_undefined(ctx
);
56 duk_insert(ctx
, idx_func
+ 1);
58 call_flags
= 0; /* not protected, respect reclimit, not constructor */
60 duk_handle_call_unprotected(thr
, /* thread */
61 nargs
, /* num_stack_args */
62 call_flags
); /* call_flags */
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
;
70 DUK_ASSERT_CTX_VALID(ctx
);
71 DUK_ASSERT(thr
!= NULL
);
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
);
79 call_flags
= 0; /* not protected, respect reclimit, not constructor */
81 duk_handle_call_unprotected(thr
, /* thread */
82 nargs
, /* num_stack_args */
83 call_flags
); /* call_flags */
86 DUK_EXTERNAL
void duk_call_prop(duk_context
*ctx
, duk_idx_t obj_index
, duk_idx_t nargs
) {
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.
94 DUK_ASSERT_CTX_VALID(ctx
);
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
);
101 duk__call_prop_prep_stack(ctx
, obj_index
, nargs
);
103 duk_call_method(ctx
, nargs
);
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
;
112 DUK_ASSERT_CTX_VALID(ctx
);
113 DUK_ASSERT(thr
!= NULL
);
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
127 DUK_ERROR_API(thr
, DUK_STR_INVALID_CALL_ARGS
);
128 return DUK_EXEC_ERROR
; /* unreachable */
131 /* awkward; we assume there is space for this */
132 duk_push_undefined(ctx
);
133 duk_insert(ctx
, idx_func
+ 1);
135 call_flags
= 0; /* respect reclimit, not constructor */
137 rc
= duk_handle_call_protected(thr
, /* thread */
138 nargs
, /* num_stack_args */
139 call_flags
); /* call_flags */
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
;
150 DUK_ASSERT_CTX_VALID(ctx
);
151 DUK_ASSERT(thr
!= NULL
);
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 */
160 call_flags
= 0; /* respect reclimit, not constructor */
162 rc
= duk_handle_call_protected(thr
, /* thread */
163 nargs
, /* num_stack_args */
164 call_flags
); /* call_flags */
169 DUK_LOCAL duk_ret_t
duk__pcall_prop_raw(duk_context
*ctx
) {
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.
177 DUK_ASSERT_CTX_VALID(ctx
);
179 obj_index
= (duk_idx_t
) duk_get_int(ctx
, -2);
180 nargs
= (duk_idx_t
) duk_get_int(ctx
, -1);
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
);
189 DUK_EXTERNAL duk_int_t
duk_pcall_prop(duk_context
*ctx
, duk_idx_t obj_index
, duk_idx_t nargs
) {
191 * Must be careful to catch errors related to value stack manipulation
192 * and property lookup, not just the call itself.
195 DUK_ASSERT_CTX_VALID(ctx
);
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
);
201 duk_push_idx(ctx
, nargs
);
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.
207 return duk_safe_call(ctx
, duk__pcall_prop_raw
, nargs
+ 1 + 2 /*nargs*/, 1 /*nrets*/);
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
;
214 DUK_ASSERT_CTX_VALID(ctx
);
215 DUK_ASSERT(thr
!= NULL
);
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 */
223 rc
= duk_handle_safe_call(thr
, /* thread */
225 nargs
, /* num_stack_args */
226 nrets
); /* num_stack_res */
231 DUK_EXTERNAL
void duk_new(duk_context
*ctx
, duk_idx_t nargs
) {
233 * There are two [[Construct]] operations in the specification:
235 * - E5 Section 13.2.2: for Function objects
236 * - E5 Section 15.3.4.5.2: for "bound" Function objects
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.
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.
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
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.
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.
266 /* XXX: merge this with duk_js_call.c, as this function implements
267 * core semantics (or perhaps merge the two files altogether).
270 duk_hthread
*thr
= (duk_hthread
*) ctx
;
273 duk_hobject
*fallback
;
275 duk_small_uint_t call_flags
;
277 DUK_ASSERT_CTX_VALID(ctx
);
279 /* [... constructor arg1 ... argN] */
281 idx_cons
= duk_require_normalize_index(ctx
, -nargs
- 1);
283 DUK_DDD(DUK_DDDPRINT("top=%ld, nargs=%ld, idx_cons=%ld",
284 (long) duk_get_top(ctx
), (long) nargs
, (long) idx_cons
));
286 /* XXX: code duplication */
289 * Figure out the final, non-bound constructor, to get "prototype"
293 duk_dup(ctx
, idx_cons
);
296 tv
= DUK_GET_TVAL_NEGIDX(ctx
, -1);
297 DUK_ASSERT(tv
!= NULL
);
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.
312 goto not_constructable
;
314 if (!DUK_HOBJECT_HAS_BOUND(cons
)) {
317 } else if (DUK_TVAL_IS_LIGHTFUNC(tv
)) {
318 /* Lightfuncs cannot be bound. */
321 /* Anything else is not constructable. */
322 goto not_constructable
;
324 duk_get_prop_stridx(ctx
, -1, DUK_STRIDX_INT_TARGET
); /* -> [... cons target] */
325 duk_remove(ctx
, -2); /* -> [... target] */
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))));
331 /* [... constructor arg1 ... argN final_cons] */
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.
340 duk_push_object(ctx
); /* class Object, extensible */
342 /* [... constructor arg1 ... argN final_cons fallback] */
344 duk_get_prop_stridx(ctx
, -2, DUK_STRIDX_PROTOTYPE
);
345 proto
= duk_get_hobject(ctx
, -1);
347 DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object "
348 "-> leave standard Object prototype as fallback prototype"));
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
);
358 /* [... constructor arg1 ... argN final_cons fallback] */
361 * Manipulate callstack for the call.
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)
369 duk_pop(ctx
); /* pop final_cons */
372 /* [... fallback constructor fallback(this) arg1 ... argN];
373 * Note: idx_cons points to first 'fallback', not 'constructor'.
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),
381 (long) duk_get_top(ctx
)));
384 * Call the constructor function (called in "constructor mode").
387 call_flags
= DUK_CALL_FLAG_CONSTRUCTOR_CALL
; /* not protected, respect reclimit, is a constructor call */
389 duk_handle_call_unprotected(thr
, /* thread */
390 nargs
, /* num_stack_args */
391 call_flags
); /* call_flags */
393 /* [... fallback retval] */
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)));
400 * Determine whether to use the constructor return value as the created
401 * object instance or not.
404 if (duk_is_object(ctx
, -1)) {
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.
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*/);
426 DUK_ERROR_TYPE(thr
, DUK_STR_NOT_CONSTRUCTABLE
);
429 DUK_LOCAL duk_ret_t
duk__pnew_helper(duk_context
*ctx
) {
432 nargs
= duk_to_uint(ctx
, -1);
439 DUK_EXTERNAL duk_int_t
duk_pnew(duk_context
*ctx
, duk_idx_t nargs
) {
442 DUK_ASSERT_CTX_VALID(ctx
);
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
452 if (DUK_UNLIKELY(nargs
< 0)) {
453 DUK_ERROR_API((duk_hthread
*) ctx
, DUK_STR_INVALID_CALL_ARGS
);
455 duk_push_uint(ctx
, nargs
);
456 rc
= duk_safe_call(ctx
, duk__pnew_helper
, nargs
+ 2 /*nargs*/, 1 /*nrets*/);
460 DUK_EXTERNAL duk_bool_t
duk_is_constructor_call(duk_context
*ctx
) {
461 duk_hthread
*thr
= (duk_hthread
*) ctx
;
464 DUK_ASSERT_CTX_VALID(ctx
);
465 DUK_ASSERT(thr
!= NULL
);
466 DUK_ASSERT_DISABLE(thr
->callstack_top
>= 0);
468 act
= duk_hthread_get_current_activation(thr
);
470 return ((act
->flags
& DUK_ACT_FLAG_CONSTRUCT
) != 0 ? 1 : 0);
475 DUK_EXTERNAL duk_bool_t
duk_is_strict_call(duk_context
*ctx
) {
476 duk_hthread
*thr
= (duk_hthread
*) ctx
;
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.
488 DUK_ASSERT_CTX_VALID(ctx
);
489 DUK_ASSERT(thr
!= NULL
);
490 DUK_ASSERT_DISABLE(thr
->callstack_top
>= 0);
492 act
= duk_hthread_get_current_activation(thr
);
494 /* Strict by default. */
497 return ((act
->flags
& DUK_ACT_FLAG_STRICT
) != 0 ? 1 : 0);
501 * Duktape/C function magic
504 DUK_EXTERNAL duk_int_t
duk_get_current_magic(duk_context
*ctx
) {
505 duk_hthread
*thr
= (duk_hthread
*) ctx
;
509 DUK_ASSERT_CTX_VALID(ctx
);
510 DUK_ASSERT(thr
!= NULL
);
511 DUK_ASSERT_DISABLE(thr
->callstack_top
>= 0);
513 act
= duk_hthread_get_current_activation(thr
);
515 func
= DUK_ACT_GET_FUNC(act
);
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
);
522 DUK_ASSERT(func
!= NULL
);
524 if (DUK_HOBJECT_IS_NATIVEFUNCTION(func
)) {
525 duk_hnativefunction
*nf
= (duk_hnativefunction
*) func
;
526 return (duk_int_t
) nf
->magic
;
532 DUK_EXTERNAL duk_int_t
duk_get_magic(duk_context
*ctx
, duk_idx_t index
) {
533 duk_hthread
*thr
= (duk_hthread
*) ctx
;
537 DUK_ASSERT_CTX_VALID(ctx
);
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
)) {
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
);
554 DUK_ERROR_TYPE(thr
, DUK_STR_UNEXPECTED_TYPE
);
558 DUK_EXTERNAL
void duk_set_magic(duk_context
*ctx
, duk_idx_t index
, duk_int_t magic
) {
559 duk_hnativefunction
*nf
;
561 DUK_ASSERT_CTX_VALID(ctx
);
563 nf
= duk_require_hnativefunction(ctx
, index
);
564 DUK_ASSERT(nf
!= NULL
);
565 nf
->magic
= (duk_int16_t
) magic
;