2 * Ecmascript bytecode executor.
5 #include "duk_internal.h"
11 DUK_LOCAL_DECL
void duk__reconfig_valstack(duk_hthread
*thr
, duk_size_t act_idx
, duk_small_uint_t retval_count
);
14 * Arithmetic, binary, and logical helpers.
16 * Note: there is no opcode for logical AND or logical OR; this is on
17 * purpose, because the evalution order semantics for them make such
18 * opcodes pretty pointless (short circuiting means they are most
19 * comfortably implemented as jumps). However, a logical NOT opcode
22 * Note: careful with duk_tval pointers here: they are potentially
23 * invalidated by any DECREF and almost any API call.
26 DUK_LOCAL duk_double_t
duk__compute_mod(duk_double_t d1
, duk_double_t d2
) {
28 * Ecmascript modulus ('%') does not match IEEE 754 "remainder"
29 * operation (implemented by remainder() in C99) but does seem
30 * to match ANSI C fmod().
32 * Compare E5 Section 11.5.3 and "man fmod".
35 return (duk_double_t
) DUK_FMOD((double) d1
, (double) d2
);
38 DUK_LOCAL
void duk__vm_arith_add(duk_hthread
*thr
, duk_tval
*tv_x
, duk_tval
*tv_y
, duk_small_uint_fast_t idx_z
) {
40 * Addition operator is different from other arithmetic
41 * operations in that it also provides string concatenation.
42 * Hence it is implemented separately.
44 * There is a fast path for number addition. Other cases go
45 * through potentially multiple coercions as described in the
46 * E5 specification. It may be possible to reduce the number
47 * of coercions, but this must be done carefully to preserve
48 * the exact semantics.
52 * Custom types also have special behavior implemented here.
55 duk_context
*ctx
= (duk_context
*) thr
;
58 DUK_ASSERT(thr
!= NULL
);
59 DUK_ASSERT(ctx
!= NULL
);
60 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
61 DUK_ASSERT(tv_y
!= NULL
); /* may be reg or const */
62 DUK_ASSERT_DISABLE(idx_z
>= 0); /* unsigned */
63 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
69 #if defined(DUK_USE_FASTINT)
70 if (DUK_TVAL_IS_FASTINT(tv_x
) && DUK_TVAL_IS_FASTINT(tv_y
)) {
71 duk_int64_t v1
, v2
, v3
;
76 /* Input values are signed 48-bit so we can detect overflow
77 * reliably from high bits or just a comparison.
80 v1
= DUK_TVAL_GET_FASTINT(tv_x
);
81 v2
= DUK_TVAL_GET_FASTINT(tv_y
);
83 v3_hi
= (duk_int32_t
) (v3
>> 32);
84 if (DUK_LIKELY(v3_hi
>= -0x8000LL
&& v3_hi
<= 0x7fffLL
)) {
85 tv_z
= thr
->valstack_bottom
+ idx_z
;
86 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
87 DUK_TVAL_SET_FASTINT(tv_z
, v3
);
88 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
89 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
92 /* overflow, fall through */
96 #endif /* DUK_USE_FASTINT */
98 if (DUK_TVAL_IS_NUMBER(tv_x
) && DUK_TVAL_IS_NUMBER(tv_y
)) {
102 du
.d
= DUK_TVAL_GET_NUMBER(tv_x
) + DUK_TVAL_GET_NUMBER(tv_y
);
103 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
);
104 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
106 tv_z
= thr
->valstack_bottom
+ idx_z
;
107 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
108 DUK_TVAL_SET_NUMBER(tv_z
, du
.d
);
109 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
110 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
115 * Slow path: potentially requires function calls for coercion
118 duk_push_tval(ctx
, tv_x
);
119 duk_push_tval(ctx
, tv_y
);
120 duk_to_primitive(ctx
, -2, DUK_HINT_NONE
); /* side effects -> don't use tv_x, tv_y after */
121 duk_to_primitive(ctx
, -1, DUK_HINT_NONE
);
123 /* As a first approximation, buffer values are coerced to strings
124 * for addition. This means that adding two buffers currently
125 * results in a string.
127 if (duk_check_type_mask(ctx
, -2, DUK_TYPE_MASK_STRING
| DUK_TYPE_MASK_BUFFER
) ||
128 duk_check_type_mask(ctx
, -1, DUK_TYPE_MASK_STRING
| DUK_TYPE_MASK_BUFFER
)) {
129 duk_to_string(ctx
, -2);
130 duk_to_string(ctx
, -1);
131 duk_concat(ctx
, 2); /* [... s1 s2] -> [... s1+s2] */
132 duk_replace(ctx
, (duk_idx_t
) idx_z
); /* side effects */
136 d1
= duk_to_number(ctx
, -2);
137 d2
= duk_to_number(ctx
, -1);
138 DUK_ASSERT(duk_is_number(ctx
, -2));
139 DUK_ASSERT(duk_is_number(ctx
, -1));
140 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1
);
141 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2
);
144 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
);
145 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
148 duk_push_number(ctx
, du
.d
);
149 duk_replace(ctx
, (duk_idx_t
) idx_z
); /* side effects */
153 DUK_LOCAL
void duk__vm_arith_binary_op(duk_hthread
*thr
, duk_tval
*tv_x
, duk_tval
*tv_y
, duk_idx_t idx_z
, duk_small_uint_fast_t opcode
) {
155 * Arithmetic operations other than '+' have number-only semantics
156 * and are implemented here. The separate switch-case here means a
157 * "double dispatch" of the arithmetic opcode, but saves code space.
159 * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
162 duk_context
*ctx
= (duk_context
*) thr
;
168 DUK_ASSERT(thr
!= NULL
);
169 DUK_ASSERT(ctx
!= NULL
);
170 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
171 DUK_ASSERT(tv_y
!= NULL
); /* may be reg or const */
172 DUK_ASSERT_DISABLE(idx_z
>= 0); /* unsigned */
173 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
175 #if defined(DUK_USE_FASTINT)
176 if (DUK_TVAL_IS_FASTINT(tv_x
) && DUK_TVAL_IS_FASTINT(tv_y
)) {
177 duk_int64_t v1
, v2
, v3
;
180 v1
= DUK_TVAL_GET_FASTINT(tv_x
);
181 v2
= DUK_TVAL_GET_FASTINT(tv_y
);
189 /* Must ensure result is 64-bit (no overflow); a
190 * simple and sufficient fast path is to allow only
191 * 32-bit inputs. Avoid zero inputs to avoid
192 * negative zero issues (-1 * 0 = -0, for instance).
194 if (v1
>= -0x80000000LL
&& v1
<= 0x7fffffffLL
&& v1
!= 0 &&
195 v2
>= -0x80000000LL
&& v2
<= 0x7fffffffLL
&& v2
!= 0) {
203 /* Don't allow a zero divisor. Fast path check by
204 * "verifying" with multiplication. Also avoid zero
205 * dividend to avoid negative zero issues (0 / -1 = -0
208 if (v1
== 0 || v2
== 0) {
218 /* Don't allow a zero divisor. Restrict both v1 and
219 * v2 to positive values to avoid compiler specific
222 if (v1
< 1 || v2
< 1) {
228 DUK_ASSERT(v1
- (v1
/ v2
) * v2
== v3
);
237 v3_hi
= (duk_int32_t
) (v3
>> 32);
238 if (DUK_LIKELY(v3_hi
>= -0x8000LL
&& v3_hi
<= 0x7fffLL
)) {
239 tv_z
= thr
->valstack_bottom
+ idx_z
;
240 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
241 DUK_TVAL_SET_FASTINT(tv_z
, v3
);
242 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
243 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
246 /* fall through if overflow etc */
249 #endif /* DUK_USE_FASTINT */
251 if (DUK_TVAL_IS_NUMBER(tv_x
) && DUK_TVAL_IS_NUMBER(tv_y
)) {
253 d1
= DUK_TVAL_GET_NUMBER(tv_x
);
254 d2
= DUK_TVAL_GET_NUMBER(tv_y
);
256 duk_push_tval(ctx
, tv_x
);
257 duk_push_tval(ctx
, tv_y
);
258 d1
= duk_to_number(ctx
, -2); /* side effects */
259 d2
= duk_to_number(ctx
, -1);
260 DUK_ASSERT(duk_is_number(ctx
, -2));
261 DUK_ASSERT(duk_is_number(ctx
, -1));
262 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1
);
263 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2
);
281 du
.d
= duk__compute_mod(d1
, d2
);
286 du
.d
= DUK_DOUBLE_NAN
; /* should not happen */
291 /* important to use normalized NaN with 8-byte tagged types */
292 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
);
293 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
295 tv_z
= thr
->valstack_bottom
+ idx_z
;
296 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
297 DUK_TVAL_SET_NUMBER(tv_z
, du
.d
);
298 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
299 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
302 DUK_LOCAL
void duk__vm_bitwise_binary_op(duk_hthread
*thr
, duk_tval
*tv_x
, duk_tval
*tv_y
, duk_small_uint_fast_t idx_z
, duk_small_uint_fast_t opcode
) {
304 * Binary bitwise operations use different coercions (ToInt32, ToUint32)
305 * depending on the operation. We coerce the arguments first using
306 * ToInt32(), and then cast to an 32-bit value if necessary. Note that
307 * such casts must be correct even if there is no native 32-bit type
308 * (e.g., duk_int32_t and duk_uint32_t are 64-bit).
310 * E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3
313 duk_context
*ctx
= (duk_context
*) thr
;
316 duk_int32_t i1
, i2
, i3
;
317 duk_uint32_t u1
, u2
, u3
;
318 #if defined(DUK_USE_FASTINT)
324 DUK_ASSERT(thr
!= NULL
);
325 DUK_ASSERT(ctx
!= NULL
);
326 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
327 DUK_ASSERT(tv_y
!= NULL
); /* may be reg or const */
328 DUK_ASSERT_DISABLE(idx_z
>= 0); /* unsigned */
329 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
331 #if defined(DUK_USE_FASTINT)
332 if (DUK_TVAL_IS_FASTINT(tv_x
) && DUK_TVAL_IS_FASTINT(tv_y
)) {
333 i1
= (duk_int32_t
) DUK_TVAL_GET_FASTINT_I32(tv_x
);
334 i2
= (duk_int32_t
) DUK_TVAL_GET_FASTINT_I32(tv_y
);
337 #endif /* DUK_USE_FASTINT */
339 duk_push_tval(ctx
, tv_x
);
340 duk_push_tval(ctx
, tv_y
);
341 i1
= duk_to_int32(ctx
, -2);
342 i2
= duk_to_int32(ctx
, -1);
360 /* Signed shift, named "arithmetic" (asl) because the result
361 * is signed, e.g. 4294967295 << 1 -> -2. Note that result
365 u2
= ((duk_uint32_t
) i2
) & 0xffffffffUL
;
366 i3
= i1
<< (u2
& 0x1f); /* E5 Section 11.7.1, steps 7 and 8 */
367 i3
= i3
& ((duk_int32_t
) 0xffffffffUL
); /* Note: left shift, should mask */
373 u2
= ((duk_uint32_t
) i2
) & 0xffffffffUL
;
374 i3
= i1
>> (u2
& 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
380 u1
= ((duk_uint32_t
) i1
) & 0xffffffffUL
;
381 u2
= ((duk_uint32_t
) i2
) & 0xffffffffUL
;
383 /* special result value handling */
384 u3
= u1
>> (u2
& 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
385 #if defined(DUK_USE_FASTINT)
386 fi3
= (duk_int64_t
) u3
;
387 goto fastint_result_set
;
389 d3
= (duk_double_t
) u3
;
395 i3
= 0; /* should not happen */
400 #if defined(DUK_USE_FASTINT)
401 /* Result is always fastint compatible. */
402 /* XXX: set 32-bit result (but must handle signed and unsigned) */
403 fi3
= (duk_int64_t
) i3
;
406 tv_z
= thr
->valstack_bottom
+ idx_z
;
407 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
408 DUK_TVAL_SET_FASTINT(tv_z
, fi3
);
409 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
410 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
412 d3
= (duk_double_t
) i3
;
415 DUK_ASSERT(!DUK_ISNAN(d3
)); /* 'd3' is never NaN, so no need to normalize */
416 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3
); /* always normalized */
418 tv_z
= thr
->valstack_bottom
+ idx_z
;
419 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
420 DUK_TVAL_SET_NUMBER(tv_z
, d3
);
421 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
422 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
426 /* In-place unary operation. */
427 DUK_LOCAL
void duk__vm_arith_unary_op(duk_hthread
*thr
, duk_tval
*tv_x
, duk_idx_t idx_x
, duk_small_uint_fast_t opcode
) {
429 * Arithmetic operations other than '+' have number-only semantics
430 * and are implemented here. The separate switch-case here means a
431 * "double dispatch" of the arithmetic opcode, but saves code space.
433 * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
436 duk_context
*ctx
= (duk_context
*) thr
;
440 DUK_ASSERT(thr
!= NULL
);
441 DUK_ASSERT(ctx
!= NULL
);
442 DUK_ASSERT(opcode
== DUK_EXTRAOP_UNM
|| opcode
== DUK_EXTRAOP_UNP
);
444 #if defined(DUK_USE_FASTINT)
445 if (DUK_TVAL_IS_FASTINT(tv_x
)) {
448 v1
= DUK_TVAL_GET_FASTINT(tv_x
);
449 if (opcode
== DUK_EXTRAOP_UNM
) {
450 /* The smallest fastint is no longer 48-bit when
451 * negated. Positive zero becames negative zero
452 * (cannot be represented) when negated.
454 if (DUK_LIKELY(v1
!= DUK_FASTINT_MIN
&& v1
!= 0)) {
456 DUK_TVAL_SET_FASTINT(tv_x
, v2
); /* no refcount changes */
460 /* ToNumber() for a fastint is a no-op. */
461 DUK_ASSERT(opcode
== DUK_EXTRAOP_UNP
);
464 /* fall through if overflow etc */
466 #endif /* DUK_USE_FASTINT */
468 if (!DUK_TVAL_IS_NUMBER(tv_x
)) {
469 duk_to_number(ctx
, idx_x
); /* side effects, perform in-place */
470 tv_x
= duk_get_tval(ctx
, idx_x
);
471 DUK_ASSERT(tv_x
!= NULL
);
472 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x
));
475 d1
= DUK_TVAL_GET_NUMBER(tv_x
);
476 if (opcode
== DUK_EXTRAOP_UNM
) {
479 /* ToNumber() for a double is a no-op. */
480 DUK_ASSERT(opcode
== DUK_EXTRAOP_UNP
);
483 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
); /* mandatory if du.d is a NaN */
485 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
487 #if defined(DUK_USE_FASTINT)
488 /* Unary plus is used to force a fastint check, so must include
491 DUK_TVAL_SET_NUMBER_CHKFAST(tv_x
, du
.d
); /* no refcount changes */
493 DUK_TVAL_SET_NUMBER(tv_x
, du
.d
); /* no refcount changes */
497 DUK_LOCAL
void duk__vm_bitwise_not(duk_hthread
*thr
, duk_tval
*tv_x
, duk_small_uint_fast_t idx_z
) {
502 duk_context
*ctx
= (duk_context
*) thr
;
506 #if !defined(DUK_USE_FASTINT)
510 DUK_ASSERT(thr
!= NULL
);
511 DUK_ASSERT(ctx
!= NULL
);
512 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
513 DUK_ASSERT_DISABLE(idx_z
>= 0);
514 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
516 #if defined(DUK_USE_FASTINT)
517 if (DUK_TVAL_IS_FASTINT(tv_x
)) {
518 i1
= (duk_int32_t
) DUK_TVAL_GET_FASTINT_I32(tv_x
);
521 #endif /* DUK_USE_FASTINT */
523 duk_push_tval(ctx
, tv_x
);
524 i1
= duk_to_int32(ctx
, -1);
530 #if defined(DUK_USE_FASTINT)
531 /* Result is always fastint compatible. */
532 tv_z
= thr
->valstack_bottom
+ idx_z
;
533 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
534 DUK_TVAL_SET_FASTINT_I32(tv_z
, i2
);
535 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
536 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
538 d2
= (duk_double_t
) i2
;
540 DUK_ASSERT(!DUK_ISNAN(d2
)); /* 'val' is never NaN, so no need to normalize */
541 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2
); /* always normalized */
543 tv_z
= thr
->valstack_bottom
+ idx_z
;
544 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
545 DUK_TVAL_SET_NUMBER(tv_z
, d2
);
546 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z
)); /* no need to incref */
547 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
551 DUK_LOCAL
void duk__vm_logical_not(duk_hthread
*thr
, duk_tval
*tv_x
, duk_tval
*tv_z
) {
559 DUK_ASSERT(thr
!= NULL
);
560 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
561 DUK_ASSERT(tv_z
!= NULL
); /* reg */
563 DUK_UNREF(thr
); /* w/o refcounts */
565 /* ToBoolean() does not require any operations with side effects so
566 * we can do it efficiently. For footprint it would be better to use
567 * duk_js_toboolean() and then push+replace to the result slot.
569 res
= duk_js_toboolean(tv_x
); /* does not modify tv_x */
570 DUK_ASSERT(res
== 0 || res
== 1);
572 DUK_TVAL_SET_TVAL(&tv_tmp
, tv_z
);
573 DUK_TVAL_SET_BOOLEAN(tv_z
, res
); /* no need to incref */
574 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
578 * Longjmp handler for the bytecode executor (and a bunch of static
581 * Any type of longjmp() can be caught here, including intra-function
582 * longjmp()s like 'break', 'continue', (slow) 'return', 'yield', etc.
584 * Error policy: should not ordinarily throw errors. Errors thrown
585 * will bubble outwards.
588 * 0 restart execution
589 * 1 bytecode executor finished
593 /* XXX: duk_api operations for cross-thread reg manipulation? */
594 /* XXX: post-condition: value stack must be correct; for ecmascript functions, clamped to 'nregs' */
596 #define DUK__LONGJMP_RESTART 0 /* state updated, restart bytecode execution */
597 #define DUK__LONGJMP_FINISHED 1 /* exit bytecode executor with return value */
598 #define DUK__LONGJMP_RETHROW 2 /* exit bytecode executor by rethrowing an error to caller */
600 /* only called when act_idx points to an Ecmascript function */
601 DUK_LOCAL
void duk__reconfig_valstack(duk_hthread
*thr
, duk_size_t act_idx
, duk_small_uint_t retval_count
) {
602 duk_hcompiledfunction
*h_func
;
604 DUK_ASSERT(thr
!= NULL
);
605 DUK_ASSERT_DISABLE(act_idx
>= 0); /* unsigned */
606 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
) != NULL
);
607 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
)));
608 DUK_ASSERT_DISABLE(thr
->callstack
[act_idx
].idx_retval
>= 0); /* unsigned */
610 thr
->valstack_bottom
= thr
->valstack
+ thr
->callstack
[act_idx
].idx_bottom
;
612 /* clamp so that retval is at the top (retval_count == 1) or register just before
613 * intended retval is at the top (retval_count == 0, happens e.g. with 'finally').
615 duk_set_top((duk_context
*) thr
,
616 (duk_idx_t
) (thr
->callstack
[act_idx
].idx_retval
-
617 thr
->callstack
[act_idx
].idx_bottom
+
621 * When returning to an Ecmascript function, extend the valstack
622 * top to 'nregs' always.
625 h_func
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
);
627 (void) duk_valstack_resize_raw((duk_context
*) thr
,
628 (thr
->valstack_bottom
- thr
->valstack
) + /* bottom of current func */
629 h_func
->nregs
+ /* reg count */
630 DUK_VALSTACK_INTERNAL_EXTRA
, /* + spare */
631 DUK_VSRESIZE_FLAG_SHRINK
| /* flags */
633 DUK_VSRESIZE_FLAG_THROW
);
635 duk_set_top((duk_context
*) thr
, h_func
->nregs
);
638 DUK_LOCAL
void duk__handle_catch_or_finally(duk_hthread
*thr
, duk_size_t cat_idx
, duk_bool_t is_finally
) {
639 duk_context
*ctx
= (duk_context
*) thr
;
643 DUK_DDD(DUK_DDDPRINT("handling catch/finally, cat_idx=%ld, is_finally=%ld",
644 (long) cat_idx
, (long) is_finally
));
647 * Set caught value and longjmp type to catcher regs.
650 DUK_DDD(DUK_DDDPRINT("writing catch registers: idx_base=%ld -> %!T, idx_base+1=%ld -> %!T",
651 (long) thr
->catchstack
[cat_idx
].idx_base
,
652 (duk_tval
*) &thr
->heap
->lj
.value1
,
653 (long) (thr
->catchstack
[cat_idx
].idx_base
+ 1),
654 (duk_tval
*) &thr
->heap
->lj
.value2
));
656 tv1
= thr
->valstack
+ thr
->catchstack
[cat_idx
].idx_base
;
657 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
658 DUK_TVAL_SET_TVAL(tv1
, &thr
->heap
->lj
.value1
);
659 DUK_TVAL_INCREF(thr
, tv1
);
660 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
662 tv1
= thr
->valstack
+ thr
->catchstack
[cat_idx
].idx_base
+ 1;
663 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
664 DUK_TVAL_SET_NUMBER(tv1
, (duk_double_t
) thr
->heap
->lj
.type
); /* XXX: set int */
665 DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv1
)); /* no need to incref */
666 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
669 * Unwind catchstack and callstack.
671 * The 'cat_idx' catcher is always kept, even when executing finally.
674 duk_hthread_catchstack_unwind(thr
, cat_idx
+ 1);
675 duk_hthread_callstack_unwind(thr
, thr
->catchstack
[cat_idx
].callstack_index
+ 1);
678 * Reconfigure valstack to 'nregs' (this is always the case for
679 * Ecmascript functions).
682 DUK_ASSERT(thr
->callstack_top
>= 1);
683 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
);
684 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)));
686 thr
->valstack_bottom
= thr
->valstack
+ (thr
->callstack
+ thr
->callstack_top
- 1)->idx_bottom
;
687 duk_set_top((duk_context
*) thr
, ((duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1))->nregs
);
690 * Reset PC: resume execution from catch or finally jump slot.
693 (thr
->callstack
+ thr
->callstack_top
- 1)->curr_pc
=
694 thr
->catchstack
[cat_idx
].pc_base
+ (is_finally
? 1 : 0);
697 * If entering a 'catch' block which requires an automatic
698 * catch variable binding, create the lexical environment.
700 * The binding is mutable (= writable) but not deletable.
701 * Step 4 for the catch production in E5 Section 12.14;
702 * no value is given for CreateMutableBinding 'D' argument,
703 * which implies the binding is not deletable.
706 if (!is_finally
&& DUK_CAT_HAS_CATCH_BINDING_ENABLED(&thr
->catchstack
[cat_idx
])) {
708 duk_hobject
*new_env
;
709 duk_hobject
*act_lex_env
;
711 DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding"));
713 /* Note: 'act' is dangerous here because it may get invalidate at many
714 * points, so we re-lookup it multiple times.
716 DUK_ASSERT(thr
->callstack_top
>= 1);
717 act
= thr
->callstack
+ thr
->callstack_top
- 1;
719 if (act
->lex_env
== NULL
) {
720 DUK_ASSERT(act
->var_env
== NULL
);
721 DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
723 /* this may have side effects, so re-lookup act */
724 duk_js_init_activation_environment_records_delayed(thr
, act
);
725 act
= thr
->callstack
+ thr
->callstack_top
- 1;
727 DUK_ASSERT(act
->lex_env
!= NULL
);
728 DUK_ASSERT(act
->var_env
!= NULL
);
729 DUK_ASSERT(DUK_ACT_GET_FUNC(act
) != NULL
);
730 DUK_UNREF(act
); /* unreferenced without assertions */
732 act
= thr
->callstack
+ thr
->callstack_top
- 1;
733 act_lex_env
= act
->lex_env
;
734 act
= NULL
; /* invalidated */
736 (void) duk_push_object_helper_proto(ctx
,
737 DUK_HOBJECT_FLAG_EXTENSIBLE
|
738 DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV
),
740 new_env
= duk_require_hobject(ctx
, -1);
741 DUK_ASSERT(new_env
!= NULL
);
742 DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr
*) new_env
));
744 /* Note: currently the catch binding is handled without a register
745 * binding because we don't support dynamic register bindings (they
746 * must be fixed for an entire function). So, there is no need to
747 * record regbases etc.
750 DUK_ASSERT(thr
->catchstack
[cat_idx
].h_varname
!= NULL
);
751 duk_push_hstring(ctx
, thr
->catchstack
[cat_idx
].h_varname
);
752 duk_push_tval(ctx
, &thr
->heap
->lj
.value1
);
753 duk_xdef_prop(ctx
, -3, DUK_PROPDESC_FLAGS_W
); /* writable, not configurable */
755 act
= thr
->callstack
+ thr
->callstack_top
- 1;
756 act
->lex_env
= new_env
;
757 DUK_HOBJECT_INCREF(thr
, new_env
); /* reachable through activation */
759 DUK_CAT_SET_LEXENV_ACTIVE(&thr
->catchstack
[cat_idx
]);
763 DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr
*) new_env
));
767 DUK_CAT_CLEAR_FINALLY_ENABLED(&thr
->catchstack
[cat_idx
]);
769 DUK_CAT_CLEAR_CATCH_ENABLED(&thr
->catchstack
[cat_idx
]);
773 DUK_LOCAL
void duk__handle_label(duk_hthread
*thr
, duk_size_t cat_idx
) {
776 /* no callstack changes, no value stack changes */
778 DUK_ASSERT(thr
!= NULL
);
779 DUK_ASSERT(thr
->callstack_top
>= 1);
781 act
= thr
->callstack
+ thr
->callstack_top
- 1;
783 DUK_ASSERT(DUK_ACT_GET_FUNC(act
) != NULL
);
784 DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act
)));
786 /* +0 = break, +1 = continue */
787 act
->curr_pc
= thr
->catchstack
[cat_idx
].pc_base
+ (thr
->heap
->lj
.type
== DUK_LJ_TYPE_CONTINUE
? 1 : 0);
788 act
= NULL
; /* invalidated */
790 duk_hthread_catchstack_unwind(thr
, cat_idx
+ 1); /* keep label catcher */
791 /* no need to unwind callstack */
793 /* valstack should not need changes */
794 #if defined(DUK_USE_ASSERTIONS)
795 act
= thr
->callstack
+ thr
->callstack_top
- 1;
796 DUK_ASSERT((duk_size_t
) (thr
->valstack_top
- thr
->valstack_bottom
) ==
797 (duk_size_t
) ((duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
))->nregs
);
801 /* Note: called for DUK_LJ_TYPE_YIELD and for DUK_LJ_TYPE_RETURN, when a
802 * return terminates a thread and yields to the resumer.
804 DUK_LOCAL
void duk__handle_yield(duk_hthread
*thr
, duk_hthread
*resumer
, duk_size_t act_idx
) {
808 /* this may also be called for DUK_LJ_TYPE_RETURN; this is OK as long as
809 * lj.value1 is correct.
812 DUK_ASSERT(DUK_ACT_GET_FUNC(resumer
->callstack
+ act_idx
) != NULL
);
813 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer
->callstack
+ act_idx
))); /* resume caller must be an ecmascript func */
815 DUK_DDD(DUK_DDDPRINT("resume idx_retval is %ld", (long) resumer
->callstack
[act_idx
].idx_retval
));
817 tv1
= resumer
->valstack
+ resumer
->callstack
[act_idx
].idx_retval
; /* return value from Duktape.Thread.resume() */
818 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
819 DUK_TVAL_SET_TVAL(tv1
, &thr
->heap
->lj
.value1
);
820 DUK_TVAL_INCREF(thr
, tv1
);
821 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
823 duk_hthread_callstack_unwind(resumer
, act_idx
+ 1); /* unwind to 'resume' caller */
825 /* no need to unwind catchstack */
826 duk__reconfig_valstack(resumer
, act_idx
, 1); /* 1 = have retval */
828 /* caller must change active thread, and set thr->resumer to NULL */
832 duk_small_uint_t
duk__handle_longjmp(duk_hthread
*thr
,
833 duk_hthread
*entry_thread
,
834 duk_size_t entry_callstack_top
) {
836 duk_size_t entry_callstack_index
;
837 duk_small_uint_t retval
= DUK__LONGJMP_RESTART
;
839 DUK_ASSERT(thr
!= NULL
);
840 DUK_ASSERT(entry_thread
!= NULL
);
841 DUK_ASSERT(entry_callstack_top
> 0); /* guarantees entry_callstack_top - 1 >= 0 */
843 entry_callstack_index
= entry_callstack_top
- 1;
845 /* 'thr' is the current thread, as no-one resumes except us and we
846 * switch 'thr' in that case.
850 * (Re)try handling the longjmp.
852 * A longjmp handler may convert the longjmp to a different type and
853 * "virtually" rethrow by goto'ing to 'check_longjmp'. Before the goto,
854 * the following must be updated:
855 * - the heap 'lj' state
856 * - 'thr' must reflect the "throwing" thread
861 DUK_DD(DUK_DDPRINT("handling longjmp: type=%ld, value1=%!T, value2=%!T, iserror=%ld",
862 (long) thr
->heap
->lj
.type
,
863 (duk_tval
*) &thr
->heap
->lj
.value1
,
864 (duk_tval
*) &thr
->heap
->lj
.value2
,
865 (long) thr
->heap
->lj
.iserror
));
867 switch (thr
->heap
->lj
.type
) {
869 case DUK_LJ_TYPE_RESUME
: {
871 * Note: lj.value1 is 'value', lj.value2 is 'resumee'.
872 * This differs from YIELD.
878 duk_hthread
*resumee
;
880 /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
882 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_RUNNING
); /* unchanged by Duktape.Thread.resume() */
883 DUK_ASSERT(thr
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
884 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
&&
885 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)) &&
886 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1))->func
== duk_bi_thread_resume
);
887 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2) != NULL
&&
888 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2))); /* an Ecmascript function */
889 DUK_ASSERT_DISABLE((thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
891 tv
= &thr
->heap
->lj
.value2
; /* resumee */
892 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv
));
893 DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv
) != NULL
);
894 DUK_ASSERT(DUK_HOBJECT_IS_THREAD(DUK_TVAL_GET_OBJECT(tv
)));
895 resumee
= (duk_hthread
*) DUK_TVAL_GET_OBJECT(tv
);
897 DUK_ASSERT(resumee
!= NULL
);
898 DUK_ASSERT(resumee
->resumer
== NULL
);
899 DUK_ASSERT(resumee
->state
== DUK_HTHREAD_STATE_INACTIVE
||
900 resumee
->state
== DUK_HTHREAD_STATE_YIELDED
); /* checked by Duktape.Thread.resume() */
901 DUK_ASSERT(resumee
->state
!= DUK_HTHREAD_STATE_YIELDED
||
902 resumee
->callstack_top
>= 2); /* YIELDED: Ecmascript activation + Duktape.Thread.yield() activation */
903 DUK_ASSERT(resumee
->state
!= DUK_HTHREAD_STATE_YIELDED
||
904 (DUK_ACT_GET_FUNC(resumee
->callstack
+ resumee
->callstack_top
- 1) != NULL
&&
905 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumee
->callstack
+ resumee
->callstack_top
- 1)) &&
906 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(resumee
->callstack
+ resumee
->callstack_top
- 1))->func
== duk_bi_thread_yield
));
907 DUK_ASSERT(resumee
->state
!= DUK_HTHREAD_STATE_YIELDED
||
908 (DUK_ACT_GET_FUNC(resumee
->callstack
+ resumee
->callstack_top
- 2) != NULL
&&
909 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumee
->callstack
+ resumee
->callstack_top
- 2)))); /* an Ecmascript function */
910 DUK_ASSERT_DISABLE(resumee
->state
!= DUK_HTHREAD_STATE_YIELDED
||
911 (resumee
->callstack
+ resumee
->callstack_top
- 2)->idx_retval
>= 0); /* idx_retval unsigned */
912 DUK_ASSERT(resumee
->state
!= DUK_HTHREAD_STATE_INACTIVE
||
913 resumee
->callstack_top
== 0); /* INACTIVE: no activation, single function value on valstack */
914 DUK_ASSERT(resumee
->state
!= DUK_HTHREAD_STATE_INACTIVE
||
915 (resumee
->valstack_top
== resumee
->valstack
+ 1 &&
916 DUK_TVAL_IS_OBJECT(resumee
->valstack_top
- 1) &&
917 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_TVAL_GET_OBJECT(resumee
->valstack_top
- 1))));
919 if (thr
->heap
->lj
.iserror
) {
921 * Throw the error in the resumed thread's context; the
922 * error value is pushed onto the resumee valstack.
924 * Note: the callstack of the target may empty in this case
925 * too (i.e. the target thread has never been resumed). The
926 * value stack will contain the initial function in that case,
927 * which we simply ignore.
930 resumee
->resumer
= thr
;
931 resumee
->state
= DUK_HTHREAD_STATE_RUNNING
;
932 thr
->state
= DUK_HTHREAD_STATE_RESUMED
;
933 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumee
);
936 thr
->heap
->lj
.type
= DUK_LJ_TYPE_THROW
;
938 /* thr->heap->lj.value1 is already the value to throw */
939 /* thr->heap->lj.value2 is 'thread', will be wiped out at the end */
941 DUK_ASSERT(thr
->heap
->lj
.iserror
); /* already set */
943 DUK_DD(DUK_DDPRINT("-> resume with an error, converted to a throw in the resumee, propagate"));
945 } else if (resumee
->state
== DUK_HTHREAD_STATE_YIELDED
) {
946 act_idx
= resumee
->callstack_top
- 2; /* Ecmascript function */
947 DUK_ASSERT_DISABLE(resumee
->callstack
[act_idx
].idx_retval
>= 0); /* unsigned */
949 tv
= resumee
->valstack
+ resumee
->callstack
[act_idx
].idx_retval
; /* return value from Duktape.Thread.yield() */
950 DUK_ASSERT(tv
>= resumee
->valstack
&& tv
< resumee
->valstack_top
);
951 tv2
= &thr
->heap
->lj
.value1
;
952 DUK_TVAL_SET_TVAL(&tv_tmp
, tv
);
953 DUK_TVAL_SET_TVAL(tv
, tv2
);
954 DUK_TVAL_INCREF(thr
, tv
);
955 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
957 duk_hthread_callstack_unwind(resumee
, act_idx
+ 1); /* unwind to 'yield' caller */
959 /* no need to unwind catchstack */
961 duk__reconfig_valstack(resumee
, act_idx
, 1); /* 1 = have retval */
963 resumee
->resumer
= thr
;
964 resumee
->state
= DUK_HTHREAD_STATE_RUNNING
;
965 thr
->state
= DUK_HTHREAD_STATE_RESUMED
;
966 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumee
);
968 thr
= resumee
; /* not needed, as we exit right away */
970 DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
971 retval
= DUK__LONGJMP_RESTART
;
972 goto wipe_and_return
;
974 duk_small_uint_t call_flags
;
977 /* resumee: [... initial_func] (currently actually: [initial_func]) */
979 duk_push_undefined((duk_context
*) resumee
);
980 tv
= &thr
->heap
->lj
.value1
;
981 duk_push_tval((duk_context
*) resumee
, tv
);
983 /* resumee: [... initial_func undefined(= this) resume_value ] */
985 call_flags
= DUK_CALL_FLAG_IS_RESUME
; /* is resume, not a tail call */
987 setup_rc
= duk_handle_ecma_call_setup(resumee
,
988 1, /* num_stack_args */
989 call_flags
); /* call_flags */
991 /* Shouldn't happen but check anyway. */
992 DUK_ERROR(thr
, DUK_ERR_INTERNAL_ERROR
, DUK_STR_INTERNAL_ERROR
);
995 resumee
->resumer
= thr
;
996 resumee
->state
= DUK_HTHREAD_STATE_RUNNING
;
997 thr
->state
= DUK_HTHREAD_STATE_RESUMED
;
998 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumee
);
1000 thr
= resumee
; /* not needed, as we exit right away */
1002 DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
1003 retval
= DUK__LONGJMP_RESTART
;
1004 goto wipe_and_return
;
1007 break; /* never here */
1010 case DUK_LJ_TYPE_YIELD
: {
1012 * Currently only allowed only if yielding thread has only
1013 * Ecmascript activations (except for the Duktape.Thread.yield()
1014 * call at the callstack top) and none of them constructor
1017 * This excludes the 'entry' thread which will always have
1018 * a preventcount > 0.
1021 duk_hthread
*resumer
;
1023 /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
1025 DUK_ASSERT(thr
!= entry_thread
); /* Duktape.Thread.yield() should prevent */
1026 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_RUNNING
); /* unchanged from Duktape.Thread.yield() */
1027 DUK_ASSERT(thr
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.yield() activation */
1028 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
&&
1029 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)) &&
1030 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1))->func
== duk_bi_thread_yield
);
1031 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2) != NULL
&&
1032 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2))); /* an Ecmascript function */
1033 DUK_ASSERT_DISABLE((thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
1035 resumer
= thr
->resumer
;
1037 DUK_ASSERT(resumer
!= NULL
);
1038 DUK_ASSERT(resumer
->state
== DUK_HTHREAD_STATE_RESUMED
); /* written by a previous RESUME handling */
1039 DUK_ASSERT(resumer
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
1040 DUK_ASSERT(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 1) != NULL
&&
1041 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 1)) &&
1042 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 1))->func
== duk_bi_thread_resume
);
1043 DUK_ASSERT(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 2) != NULL
&&
1044 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 2))); /* an Ecmascript function */
1045 DUK_ASSERT_DISABLE((resumer
->callstack
+ resumer
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
1047 if (thr
->heap
->lj
.iserror
) {
1048 thr
->state
= DUK_HTHREAD_STATE_YIELDED
;
1049 thr
->resumer
= NULL
;
1050 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1051 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1054 thr
->heap
->lj
.type
= DUK_LJ_TYPE_THROW
;
1055 /* lj.value1 is already set */
1056 DUK_ASSERT(thr
->heap
->lj
.iserror
); /* already set */
1058 DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate"));
1061 duk__handle_yield(thr
, resumer
, resumer
->callstack_top
- 2);
1063 thr
->state
= DUK_HTHREAD_STATE_YIELDED
;
1064 thr
->resumer
= NULL
;
1065 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1066 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1068 thr
= resumer
; /* not needed, as we exit right away */
1071 DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer"));
1072 retval
= DUK__LONGJMP_RESTART
;
1073 goto wipe_and_return
;
1076 break; /* never here */
1079 case DUK_LJ_TYPE_RETURN
: {
1081 * Four possible outcomes:
1082 * * A 'finally' in the same function catches the 'return'.
1084 * * The return happens at the entry level of the bytecode
1085 * executor, so return from the executor (in C stack).
1087 * * There is a calling (Ecmascript) activation in the call
1088 * stack => return to it.
1090 * * There is no calling activation, and the thread is
1091 * terminated. There is always a resumer in this case,
1092 * which gets the return value similarly to a 'yield'
1093 * (except that the current thread can no longer be
1098 duk_hthread
*resumer
;
1100 duk_size_t orig_callstack_index
;
1102 DUK_ASSERT(thr
!= NULL
);
1103 DUK_ASSERT(thr
->callstack_top
>= 1);
1104 DUK_ASSERT(thr
->catchstack
!= NULL
);
1106 /* XXX: does not work if thr->catchstack is NULL */
1107 /* XXX: does not work if thr->catchstack is allocated but lowest pointer */
1109 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1; /* may be < thr->catchstack initially */
1110 DUK_ASSERT(thr
->callstack_top
> 0); /* ensures callstack_top - 1 >= 0 */
1111 orig_callstack_index
= thr
->callstack_top
- 1;
1113 while (cat
>= thr
->catchstack
) {
1114 if (cat
->callstack_index
!= orig_callstack_index
) {
1117 if (DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
&&
1118 DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
1119 /* 'finally' catches */
1120 duk__handle_catch_or_finally(thr
,
1121 cat
- thr
->catchstack
,
1122 1); /* is_finally */
1124 DUK_DD(DUK_DDPRINT("-> return caught by a finally (in the same function), restart execution"));
1125 retval
= DUK__LONGJMP_RESTART
;
1126 goto wipe_and_return
;
1130 /* if out of catchstack, cat = thr->catchstack - 1 */
1132 DUK_DD(DUK_DDPRINT("no catcher in catch stack, return to calling activation / yield"));
1134 /* return to calling activation (if any) */
1136 if (thr
== entry_thread
&&
1137 thr
->callstack_top
== entry_callstack_top
) {
1138 /* return to the bytecode executor caller */
1140 duk_push_tval((duk_context
*) thr
, &thr
->heap
->lj
.value1
);
1142 /* [ ... retval ] */
1144 DUK_DD(DUK_DDPRINT("-> return propagated up to entry level, exit bytecode executor"));
1145 retval
= DUK__LONGJMP_FINISHED
;
1146 goto wipe_and_return
;
1149 if (thr
->callstack_top
>= 2) {
1150 /* there is a caller; it MUST be an Ecmascript caller (otherwise it would
1151 * match entry level check)
1154 DUK_DDD(DUK_DDDPRINT("slow return to Ecmascript caller, idx_retval=%ld, lj_value1=%!T",
1155 (long) (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
,
1156 (duk_tval
*) &thr
->heap
->lj
.value1
));
1158 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2))); /* must be ecmascript */
1160 tv1
= thr
->valstack
+ (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
;
1161 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
1162 DUK_TVAL_SET_TVAL(tv1
, &thr
->heap
->lj
.value1
);
1163 DUK_TVAL_INCREF(thr
, tv1
);
1164 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
1166 DUK_DDD(DUK_DDDPRINT("return value at idx_retval=%ld is %!T",
1167 (long) (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
,
1168 (duk_tval
*) (thr
->valstack
+ (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
)));
1170 duk_hthread_catchstack_unwind(thr
, (cat
- thr
->catchstack
) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
1171 duk_hthread_callstack_unwind(thr
, thr
->callstack_top
- 1);
1172 duk__reconfig_valstack(thr
, thr
->callstack_top
- 1, 1); /* new top, i.e. callee */
1174 DUK_DD(DUK_DDPRINT("-> return not caught, restart execution in caller"));
1175 retval
= DUK__LONGJMP_RESTART
;
1176 goto wipe_and_return
;
1179 DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)"));
1181 DUK_ASSERT(thr
->resumer
!= NULL
);
1182 DUK_ASSERT(thr
->resumer
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
1183 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1) != NULL
&&
1184 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1)) &&
1185 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1))->func
== duk_bi_thread_resume
); /* Duktape.Thread.resume() */
1186 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2) != NULL
&&
1187 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2))); /* an Ecmascript function */
1188 DUK_ASSERT_DISABLE((thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
1189 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_RUNNING
);
1190 DUK_ASSERT(thr
->resumer
->state
== DUK_HTHREAD_STATE_RESUMED
);
1192 resumer
= thr
->resumer
;
1194 duk__handle_yield(thr
, resumer
, resumer
->callstack_top
- 2);
1196 duk_hthread_terminate(thr
); /* updates thread state, minimizes its allocations */
1197 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_TERMINATED
);
1199 thr
->resumer
= NULL
;
1200 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1201 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1203 thr
= resumer
; /* not needed */
1206 DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer"));
1207 retval
= DUK__LONGJMP_RESTART
;
1208 goto wipe_and_return
;
1211 case DUK_LJ_TYPE_BREAK
:
1212 case DUK_LJ_TYPE_CONTINUE
: {
1214 * Find a matching label catcher or 'finally' catcher in
1215 * the same function.
1217 * A label catcher must always exist and will match unless
1218 * a 'finally' captures the break/continue first. It is the
1219 * compiler's responsibility to ensure that labels are used
1224 duk_size_t orig_callstack_index
;
1225 duk_uint_t lj_label
;
1227 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
1228 orig_callstack_index
= cat
->callstack_index
;
1230 DUK_ASSERT(DUK_TVAL_IS_NUMBER(&thr
->heap
->lj
.value1
));
1231 lj_label
= (duk_uint_t
) DUK_TVAL_GET_NUMBER(&thr
->heap
->lj
.value1
);
1233 DUK_DDD(DUK_DDDPRINT("handling break/continue with label=%ld, callstack index=%ld",
1234 (long) lj_label
, (long) cat
->callstack_index
));
1236 while (cat
>= thr
->catchstack
) {
1237 if (cat
->callstack_index
!= orig_callstack_index
) {
1240 DUK_DDD(DUK_DDDPRINT("considering catcher %ld: type=%ld label=%ld",
1241 (long) (cat
- thr
->catchstack
),
1242 (long) DUK_CAT_GET_TYPE(cat
),
1243 (long) DUK_CAT_GET_LABEL(cat
)));
1245 if (DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
&&
1246 DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
1247 /* finally catches */
1248 duk__handle_catch_or_finally(thr
,
1249 cat
- thr
->catchstack
,
1250 1); /* is_finally */
1252 DUK_DD(DUK_DDPRINT("-> break/continue caught by a finally (in the same function), restart execution"));
1253 retval
= DUK__LONGJMP_RESTART
;
1254 goto wipe_and_return
;
1256 if (DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_LABEL
&&
1257 (duk_uint_t
) DUK_CAT_GET_LABEL(cat
) == lj_label
) {
1259 duk__handle_label(thr
,
1260 cat
- thr
->catchstack
);
1262 DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution"));
1263 retval
= DUK__LONGJMP_RESTART
;
1264 goto wipe_and_return
;
1269 /* should never happen, but be robust */
1270 DUK_D(DUK_DPRINT("break/continue not caught by anything in the current function (should never happen)"));
1271 goto convert_to_internal_error
;
1274 case DUK_LJ_TYPE_THROW
: {
1276 * Three possible outcomes:
1277 * * A try or finally catcher is found => resume there.
1279 * * The error propagates to the bytecode executor entry
1280 * level (and we're in the entry thread) => rethrow
1281 * with a new longjmp(), after restoring the previous
1283 * * The error is not caught in the current thread, so
1284 * the thread finishes with an error. This works like
1285 * a yielded error, except that the thread is finished
1286 * and can no longer be resumed. (There is always a
1287 * resumer in this case.)
1289 * Note: until we hit the entry level, there can only be
1290 * Ecmascript activations.
1294 duk_hthread
*resumer
;
1296 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
1297 while (cat
>= thr
->catchstack
) {
1298 if (thr
== entry_thread
&&
1299 cat
->callstack_index
< entry_callstack_index
) {
1300 /* entry level reached */
1304 if (DUK_CAT_HAS_CATCH_ENABLED(cat
)) {
1306 DUK_ASSERT(DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
);
1308 duk__handle_catch_or_finally(thr
,
1309 cat
- thr
->catchstack
,
1310 0); /* is_finally */
1312 DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution"));
1313 retval
= DUK__LONGJMP_RESTART
;
1314 goto wipe_and_return
;
1317 if (DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
1318 DUK_ASSERT(DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
);
1319 DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat
));
1321 duk__handle_catch_or_finally(thr
,
1322 cat
- thr
->catchstack
,
1323 1); /* is_finally */
1325 DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution"));
1326 retval
= DUK__LONGJMP_RESTART
;
1327 goto wipe_and_return
;
1333 if (thr
== entry_thread
) {
1334 /* not caught by anything before entry level; rethrow and let the
1335 * final catcher unwind everything
1338 duk_hthread_catchstack_unwind(thr
, (cat
- thr
->catchstack
) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
1339 duk_hthread_callstack_unwind(thr
, entry_callstack_index
+ 1);
1342 DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor"));
1343 retval
= DUK__LONGJMP_RETHROW
;
1345 /* Note: MUST NOT wipe_and_return here, as heap->lj must remain intact */
1348 DUK_DD(DUK_DDPRINT("not caught by current thread, yield error to resumer"));
1350 /* not caught by current thread, thread terminates (yield error to resumer);
1351 * note that this may cause a cascade if the resumer terminates with an uncaught
1352 * exception etc (this is OK, but needs careful testing)
1355 DUK_ASSERT(thr
->resumer
!= NULL
);
1356 DUK_ASSERT(thr
->resumer
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
1357 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1) != NULL
&&
1358 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1)) &&
1359 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1))->func
== duk_bi_thread_resume
); /* Duktape.Thread.resume() */
1360 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2) != NULL
&&
1361 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2))); /* an Ecmascript function */
1363 resumer
= thr
->resumer
;
1367 DUK_ASSERT(thr
->heap
->lj
.type
== DUK_LJ_TYPE_THROW
); /* already set */
1368 /* lj.value1 already set */
1370 duk_hthread_terminate(thr
); /* updates thread state, minimizes its allocations */
1371 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_TERMINATED
);
1373 thr
->resumer
= NULL
;
1374 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1375 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1380 case DUK_LJ_TYPE_NORMAL
: {
1381 DUK_D(DUK_DPRINT("caught DUK_LJ_TYPE_NORMAL, should never happen, treat as internal error"));
1382 goto convert_to_internal_error
;
1386 /* should never happen, but be robust */
1387 DUK_D(DUK_DPRINT("caught unknown longjmp type %ld, treat as internal error", (long) thr
->heap
->lj
.type
));
1388 goto convert_to_internal_error
;
1396 /* this is not strictly necessary, but helps debugging */
1397 thr
->heap
->lj
.type
= DUK_LJ_TYPE_UNKNOWN
;
1398 thr
->heap
->lj
.iserror
= 0;
1400 DUK_TVAL_SET_TVAL(&tv_tmp
, &thr
->heap
->lj
.value1
);
1401 DUK_TVAL_SET_UNDEFINED_UNUSED(&thr
->heap
->lj
.value1
);
1402 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
1404 DUK_TVAL_SET_TVAL(&tv_tmp
, &thr
->heap
->lj
.value2
);
1405 DUK_TVAL_SET_UNDEFINED_UNUSED(&thr
->heap
->lj
.value2
);
1406 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
1411 convert_to_internal_error
:
1412 /* This could also be thrown internally (set the error, goto check_longjmp),
1413 * but it's better for internal errors to bubble outwards.
1415 DUK_ERROR(thr
, DUK_ERR_INTERNAL_ERROR
, DUK_STR_INTERNAL_ERROR_EXEC_LONGJMP
);
1420 /* XXX: Disabled for 1.0 release. This needs to handle unwinding for label
1421 * sites (which are created for explicit labels but also for control statements
1422 * like for-loops). At that point it's quite close to the "slow return" handler
1423 * except for longjmp(). Perhaps all returns could initially be handled as fast
1424 * returns and only converted to longjmp()s when basic handling won't do?
1427 /* Try a fast return. Return false if fails, so that a slow return can be done
1431 duk_bool_t
duk__handle_fast_return(duk_hthread
*thr
,
1432 duk_tval
*tv_retval
,
1433 duk_hthread
*entry_thread
,
1434 duk_size_t entry_callstack_top
) {
1438 /* retval == NULL indicates 'undefined' return value */
1440 if (thr
== entry_thread
&& thr
->callstack_top
== entry_callstack_top
) {
1441 DUK_DDD(DUK_DDDPRINT("reject fast return: return would exit bytecode executor to caller"));
1444 if (thr
->callstack_top
<= 1) {
1445 DUK_DDD(DUK_DDDPRINT("reject fast return: there is no caller in this callstack (thread yield)"));
1449 /* There is a caller, and it must be an Ecmascript caller (otherwise
1450 * it would have matched the entry level check).
1452 DUK_ASSERT(thr
->callstack_top
>= 2);
1453 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2))); /* must be ecmascript */
1455 tv1
= thr
->valstack
+ (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
;
1456 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
1458 DUK_TVAL_SET_TVAL(tv1
, tv_retval
);
1459 DUK_TVAL_INCREF(thr
, tv1
);
1461 DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1
);
1462 /* no need to incref */
1464 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
1466 /* No catchstack to unwind. */
1468 duk_hthread_catchstack_unwind(thr
, (cat
- thr
->catchstack
) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
1470 duk_hthread_callstack_unwind(thr
, thr
->callstack_top
- 1);
1471 duk__reconfig_valstack(thr
, thr
->callstack_top
- 1, 1); /* new top, i.e. callee */
1473 DUK_DDD(DUK_DDDPRINT("fast return accepted"));
1479 * Executor interrupt handling
1481 * The handler is called whenever the interrupt countdown reaches zero
1482 * (or below). The handler must perform whatever checks are activated,
1483 * e.g. check for cumulative step count to impose an execution step
1484 * limit or check for breakpoints or other debugger interaction.
1486 * When the actions are done, the handler must reinit the interrupt
1487 * init and counter values. The 'init' value must indicate how many
1488 * bytecode instructions are executed before the next interrupt. The
1489 * counter must interface with the bytecode executor loop. Concretely,
1490 * the new init value is normally one higher than the new counter value.
1491 * For instance, to execute exactly one bytecode instruction the init
1492 * value is set to 1 and the counter to 0. If an error is thrown by the
1493 * interrupt handler, the counters are set to the same value (e.g. both
1494 * to 0 to cause an interrupt when the next bytecode instruction is about
1495 * to be executed after error handling).
1497 * Maintaining the init/counter value properly is important for accurate
1498 * behavior. For instance, executor step limit needs a cumulative step
1499 * count which is simply computed as a sum of 'init' values. This must
1500 * work accurately even when single stepping.
1503 #if defined(DUK_USE_INTERRUPT_COUNTER)
1505 #define DUK__INT_NOACTION 0 /* no specific action, resume normal execution */
1506 #define DUK__INT_RESTART 1 /* must "goto restart_execution", e.g. breakpoints changed */
1508 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1509 DUK_LOCAL
void duk__interrupt_handle_debugger(duk_hthread
*thr
, duk_bool_t
*out_immediate
, duk_small_uint_t
*out_interrupt_retval
) {
1511 duk_activation
*act
;
1513 duk_breakpoint
**bp_active
;
1514 duk_uint_fast32_t line
= 0;
1515 duk_bool_t send_status
;
1516 duk_bool_t process_messages
;
1517 duk_bool_t processed_messages
= 0;
1519 ctx
= (duk_context
*) thr
;
1520 act
= thr
->callstack
+ thr
->callstack_top
- 1;
1522 /* It might seem that replacing 'thr->heap' with just 'heap' below
1523 * might be a good idea, but it increases code size slightly
1524 * (probably due to unnecessary spilling) at least on x64.
1528 * Breakpoint and step state checks
1531 if (act
->flags
& DUK_ACT_FLAG_BREAKPOINT_ACTIVE
||
1532 (thr
->heap
->dbg_step_thread
== thr
&&
1533 thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1)) {
1534 line
= duk_debug_curr_line(thr
);
1536 if (act
->prev_line
!= line
) {
1537 /* Stepped? Step out is handled by callstack unwind. */
1538 if ((thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_INTO
||
1539 thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_OVER
) &&
1540 (thr
->heap
->dbg_step_thread
== thr
) &&
1541 (thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1) &&
1542 (line
!= thr
->heap
->dbg_step_startline
)) {
1543 DUK_D(DUK_DPRINT("STEP STATE TRIGGERED PAUSE at line %ld",
1546 DUK_HEAP_SET_PAUSED(thr
->heap
);
1549 /* Check for breakpoints only on line transition.
1550 * Breakpoint is triggered when we enter the target
1551 * line from a different line, and the previous line
1552 * was within the same function.
1554 * This condition is tricky: the condition used to be
1555 * that transition to -or across- the breakpoint line
1556 * triggered the breakpoint. This seems intuitively
1557 * better because it handles breakpoints on lines with
1558 * no emitted opcodes; but this leads to the issue
1559 * described in: https://github.com/svaarala/duktape/issues/263.
1561 bp_active
= thr
->heap
->dbg_breakpoints_active
;
1568 DUK_ASSERT(bp
->filename
!= NULL
);
1569 if (act
->prev_line
!= bp
->line
&& line
== bp
->line
) {
1570 DUK_D(DUK_DPRINT("BREAKPOINT TRIGGERED at %!O:%ld",
1571 (duk_heaphdr
*) bp
->filename
, (long) bp
->line
));
1573 DUK_HEAP_SET_PAUSED(thr
->heap
);
1580 act
->prev_line
= line
;
1584 * Rate limit check for sending status update or peeking into
1585 * the debug transport. Both can be expensive operations that
1586 * we don't want to do on every opcode.
1588 * Making sure the interval remains reasonable on a wide variety
1589 * of targets and bytecode is difficult without a timestamp, so
1590 * we use a Date-provided timestamp for the rate limit check.
1591 * But since it's also expensive to get a timestamp, a bytecode
1592 * counter is used to rate limit getting timestamps.
1595 if (thr
->heap
->dbg_state_dirty
|| thr
->heap
->dbg_paused
) {
1601 if (thr
->heap
->dbg_paused
) {
1602 process_messages
= 1;
1604 process_messages
= 0;
1607 /* XXX: remove heap->dbg_exec_counter, use heap->inst_count_interrupt instead? */
1608 thr
->heap
->dbg_exec_counter
+= thr
->interrupt_init
;
1609 if (thr
->heap
->dbg_exec_counter
- thr
->heap
->dbg_last_counter
>= DUK_HEAP_DBG_RATELIMIT_OPCODES
) {
1610 /* Overflow of the execution counter is fine and doesn't break
1614 duk_double_t now
, diff_last
;
1616 thr
->heap
->dbg_last_counter
= thr
->heap
->dbg_exec_counter
;
1617 now
= DUK_USE_DATE_GET_NOW(ctx
);
1619 diff_last
= now
- thr
->heap
->dbg_last_time
;
1620 if (diff_last
< 0.0 || diff_last
>= (duk_double_t
) DUK_HEAP_DBG_RATELIMIT_MILLISECS
) {
1621 /* Negative value checked so that a "time jump" works
1624 * Same interval is now used for status sending and
1628 thr
->heap
->dbg_last_time
= now
;
1630 process_messages
= 1;
1638 act
= NULL
; /* may be changed */
1640 duk_debug_send_status(thr
);
1641 thr
->heap
->dbg_state_dirty
= 0;
1645 * Process messages. If we're paused, we'll block for new messages.
1646 * if we're not paused, we'll process anything we can peek but won't
1650 if (process_messages
) {
1651 processed_messages
= duk_debug_process_messages(thr
, 0 /*no_block*/);
1654 /* XXX: any case here where we need to re-send status? */
1656 /* Continue checked execution if there are breakpoints or we're stepping.
1657 * Also use checked execution if paused flag is active - it shouldn't be
1658 * because the debug message loop shouldn't terminate if it was. Step out
1659 * is handled by callstack unwind and doesn't need checked execution.
1660 * Note that debugger may have detached due to error or explicit request
1661 * above, so we must recheck attach status.
1664 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
)) {
1665 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup, may have changed */
1666 if (act
->flags
& DUK_ACT_FLAG_BREAKPOINT_ACTIVE
||
1667 ((thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_INTO
||
1668 thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_OVER
) &&
1669 thr
->heap
->dbg_step_thread
== thr
&&
1670 thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1) ||
1671 thr
->heap
->dbg_paused
) {
1675 /* If we processed any debug messages breakpoints may have
1676 * changed; restart execution to re-check active breakpoints.
1678 if (processed_messages
) {
1679 DUK_D(DUK_DPRINT("processed debug messages, restart execution to recheck possibly changed breakpoints"));
1680 *out_interrupt_retval
= DUK__INT_RESTART
;
1683 DUK_D(DUK_DPRINT("debugger became detached, resume normal execution"));
1686 #endif /* DUK_USE_DEBUGGER_SUPPORT */
1688 DUK_LOCAL duk_small_uint_t
duk__executor_interrupt(duk_hthread
*thr
) {
1690 duk_activation
*act
;
1691 duk_hcompiledfunction
*fun
;
1692 duk_bool_t immediate
= 0;
1693 duk_small_uint_t retval
;
1695 DUK_ASSERT(thr
!= NULL
);
1696 DUK_ASSERT(thr
->heap
!= NULL
);
1697 DUK_ASSERT(thr
->callstack
!= NULL
);
1698 DUK_ASSERT(thr
->callstack_top
> 0);
1700 #if defined(DUK_USE_DEBUG)
1701 thr
->heap
->inst_count_interrupt
+= thr
->interrupt_init
;
1702 DUK_DD(DUK_DDPRINT("execution interrupt, counter=%ld, init=%ld, "
1703 "instruction counts: executor=%ld, interrupt=%ld",
1704 (long) thr
->interrupt_counter
, (long) thr
->interrupt_init
,
1705 (long) thr
->heap
->inst_count_exec
, (long) thr
->heap
->inst_count_interrupt
));
1708 retval
= DUK__INT_NOACTION
;
1709 ctr
= DUK_HTHREAD_INTCTR_DEFAULT
;
1712 * Avoid nested calls. Concretely this happens during debugging, e.g.
1713 * when we eval() an expression.
1716 if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr
->heap
)) {
1717 DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring"));
1719 /* Set a high interrupt counter; the original executor
1720 * interrupt invocation will rewrite before exiting.
1722 thr
->interrupt_init
= ctr
;
1723 thr
->interrupt_counter
= ctr
- 1;
1724 return DUK__INT_NOACTION
;
1726 DUK_HEAP_SET_INTERRUPT_RUNNING(thr
->heap
);
1728 act
= thr
->callstack
+ thr
->callstack_top
- 1;
1729 fun
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
1730 DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION((duk_hobject
*) fun
));
1733 #if defined(DUK_USE_EXEC_TIMEOUT_CHECK)
1735 * Execution timeout check
1738 if (DUK_USE_EXEC_TIMEOUT_CHECK(thr
->heap
->heap_udata
)) {
1739 /* Keep throwing an error whenever we get here. The unusual values
1740 * are set this way because no instruction is ever executed, we just
1741 * throw an error until all try/catch/finally and other catchpoints
1742 * have been exhausted. Duktape/C code gets control at each protected
1743 * call but whenever it enters back into Duktape the RangeError gets
1744 * raised. User exec timeout check must consistently indicate a timeout
1745 * until we've fully bubbled out of Duktape.
1747 DUK_D(DUK_DPRINT("execution timeout, throwing a RangeError"));
1748 thr
->interrupt_init
= 0;
1749 thr
->interrupt_counter
= 0;
1750 DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr
->heap
);
1751 DUK_ERROR(thr
, DUK_ERR_RANGE_ERROR
, "execution timeout");
1753 #endif /* DUK_USE_EXEC_TIMEOUT_CHECK */
1755 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1756 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
)) {
1757 duk__interrupt_handle_debugger(thr
, &immediate
, &retval
);
1758 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup if changed */
1760 #endif /* DUK_USE_DEBUGGER_SUPPORT */
1763 * Update the interrupt counter
1767 /* Cause an interrupt after executing one instruction. */
1771 /* The counter value is one less than the init value: init value should
1772 * indicate how many instructions are executed before interrupt. To
1773 * execute 1 instruction (after interrupt handler return), counter must
1776 thr
->interrupt_init
= ctr
;
1777 thr
->interrupt_counter
= ctr
- 1;
1778 DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr
->heap
);
1782 #endif /* DUK_USE_INTERRUPT_COUNTER */
1785 * Debugger handling for executor restart
1787 * Check for breakpoints, stepping, etc, and figure out if we should execute
1788 * in checked or normal mode. Note that we can't do this when an activation
1789 * is created, because breakpoint status (and stepping status) may change
1790 * later, so we must recheck every time we're executing an activation.
1793 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1794 DUK_LOCAL
void duk__executor_handle_debugger(duk_hthread
*thr
, duk_activation
*act
, duk_hcompiledfunction
*fun
) {
1797 duk_hstring
*filename
;
1798 duk_small_uint_t bp_idx
;
1799 duk_breakpoint
**bp_active
;
1801 DUK_ASSERT(thr
!= NULL
);
1802 DUK_ASSERT(act
!= NULL
);
1803 DUK_ASSERT(fun
!= NULL
);
1806 bp_active
= heap
->dbg_breakpoints_active
;
1807 act
->flags
&= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE
;
1809 tv_tmp
= duk_hobject_find_existing_entry_tval_ptr(thr
->heap
, (duk_hobject
*) fun
, DUK_HTHREAD_STRING_FILE_NAME(thr
));
1810 if (tv_tmp
&& DUK_TVAL_IS_STRING(tv_tmp
)) {
1811 filename
= DUK_TVAL_GET_STRING(tv_tmp
);
1813 /* Figure out all active breakpoints. A breakpoint is
1814 * considered active if the current function's fileName
1815 * matches the breakpoint's fileName, AND there is no
1816 * inner function that has matching line numbers
1817 * (otherwise a breakpoint would be triggered both
1818 * inside and outside of the inner function which would
1819 * be confusing). Example:
1823 * function bar() { <-. breakpoints in these
1824 * print('bar'); | lines should not affect
1825 * } <-' foo() execution
1829 * We need a few things that are only available when
1830 * debugger support is enabled: (1) a line range for
1831 * each function, and (2) access to the function
1832 * template to access the inner functions (and their
1835 * It's important to have a narrow match for active
1836 * breakpoints so that we don't enter checked execution
1837 * when that's not necessary. For instance, if we're
1838 * running inside a certain function and there's
1839 * breakpoint outside in (after the call site), we
1840 * don't want to slow down execution of the function.
1843 for (bp_idx
= 0; bp_idx
< heap
->dbg_breakpoint_count
; bp_idx
++) {
1844 duk_breakpoint
*bp
= heap
->dbg_breakpoints
+ bp_idx
;
1845 duk_hobject
**funcs
, **funcs_end
;
1846 duk_hcompiledfunction
*inner_fun
;
1847 duk_bool_t bp_match
;
1849 if (bp
->filename
== filename
&&
1850 bp
->line
>= fun
->start_line
&& bp
->line
<= fun
->end_line
) {
1852 DUK_DD(DUK_DDPRINT("breakpoint filename and line match: "
1853 "%s:%ld vs. %s (line %ld vs. %ld-%ld)",
1854 DUK_HSTRING_GET_DATA(bp
->filename
),
1856 DUK_HSTRING_GET_DATA(filename
),
1858 (long) fun
->start_line
,
1859 (long) fun
->end_line
));
1861 funcs
= DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr
->heap
, fun
);
1862 funcs_end
= DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr
->heap
, fun
);
1863 while (funcs
!= funcs_end
) {
1864 inner_fun
= (duk_hcompiledfunction
*) *funcs
;
1865 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject
*) inner_fun
));
1866 if (bp
->line
>= inner_fun
->start_line
&& bp
->line
<= inner_fun
->end_line
) {
1867 DUK_DD(DUK_DDPRINT("inner function masks ('captures') breakpoint"));
1875 /* No need to check for size of bp_active list,
1876 * it's always larger than maximum number of
1879 act
->flags
|= DUK_ACT_FLAG_BREAKPOINT_ACTIVE
;
1880 *bp_active
= heap
->dbg_breakpoints
+ bp_idx
;
1887 *bp_active
= NULL
; /* terminate */
1889 DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active
- thr
->heap
->dbg_breakpoints_active
)));
1891 /* Force pause if we were doing "step into" in another activation. */
1892 if (thr
->heap
->dbg_step_thread
!= NULL
&&
1893 thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_INTO
&&
1894 (thr
->heap
->dbg_step_thread
!= thr
||
1895 thr
->heap
->dbg_step_csindex
!= thr
->callstack_top
- 1)) {
1896 DUK_D(DUK_DPRINT("STEP INTO ACTIVE, FORCE PAUSED"));
1897 DUK_HEAP_SET_PAUSED(thr
->heap
);
1900 /* Force interrupt right away if we're paused or in "checked mode".
1901 * Step out is handled by callstack unwind.
1903 if (act
->flags
& (DUK_ACT_FLAG_BREAKPOINT_ACTIVE
) ||
1904 thr
->heap
->dbg_paused
||
1905 (thr
->heap
->dbg_step_type
!= DUK_STEP_TYPE_OUT
&&
1906 thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1)) {
1907 /* We'll need to interrupt early so recompute the init
1908 * counter to reflect the number of bytecode instructions
1909 * executed so that step counts for e.g. debugger rate
1910 * limiting are accurate.
1912 DUK_ASSERT(thr
->interrupt_counter
<= thr
->interrupt_init
);
1913 thr
->interrupt_init
= thr
->interrupt_init
- thr
->interrupt_counter
;
1914 thr
->interrupt_counter
= 0;
1917 #endif /* DUK_USE_DEBUGGER_SUPPORT */
1920 * Ecmascript bytecode executor.
1922 * Resume execution for the current thread from its current activation.
1923 * Returns when execution would return from the entry level activation,
1924 * leaving a single return value on top of the stack. Function calls
1925 * and thread resumptions are handled internally. If an error occurs,
1926 * a longjmp() with type DUK_LJ_TYPE_THROW is called on the entry level
1929 * Ecmascript function calls and coroutine resumptions are handled
1930 * internally without recursive C calls. Other function calls are
1931 * handled using duk_handle_call(), increasing C recursion depth.
1933 * There are many other tricky control flow situations, such as:
1935 * - Break and continue (fast and slow)
1936 * - Return (fast and slow)
1938 * - Thread resume and yield
1940 * For more detailed notes, see doc/execution.rst.
1942 * Also see doc/code-issues.rst for discussion of setjmp(), longjmp(),
1946 #define DUK__STRICT() (DUK_HOBJECT_HAS_STRICT(&(fun)->obj))
1947 #define DUK__REG(x) (*(thr->valstack_bottom + (x)))
1948 #define DUK__REGP(x) (thr->valstack_bottom + (x))
1949 #define DUK__CONST(x) (*(consts + (x)))
1950 #define DUK__CONSTP(x) (consts + (x))
1951 #define DUK__REGCONST(x) ((x) < DUK_BC_REGLIMIT ? DUK__REG((x)) : DUK__CONST((x) - DUK_BC_REGLIMIT))
1952 #define DUK__REGCONSTP(x) ((x) < DUK_BC_REGLIMIT ? DUK__REGP((x)) : DUK__CONSTP((x) - DUK_BC_REGLIMIT))
1954 #ifdef DUK_USE_VERBOSE_EXECUTOR_ERRORS
1955 #define DUK__INTERNAL_ERROR(msg) do { \
1956 DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, (msg)); \
1959 #define DUK__INTERNAL_ERROR(msg) do { \
1960 goto internal_error; \
1964 #define DUK__SYNC_CURR_PC() do { \
1965 duk_activation *act; \
1966 act = thr->callstack + thr->callstack_top - 1; \
1967 act->curr_pc = curr_pc; \
1969 #define DUK__SYNC_AND_NULL_CURR_PC() do { \
1970 duk_activation *act; \
1971 act = thr->callstack + thr->callstack_top - 1; \
1972 act->curr_pc = curr_pc; \
1973 thr->ptr_curr_pc = NULL; \
1976 DUK_INTERNAL
void duk_js_execute_bytecode(duk_hthread
*exec_thr
) {
1977 /* Entry level info. Although these are assigned to before setjmp()
1978 * a 'volatile' seems to be needed. Note placement of "volatile" for
1979 * pointers. See doc/code-issues.rst for more discussion.
1981 duk_hthread
* volatile entry_thread
; /* volatile copy of exec_thr */
1982 volatile duk_size_t entry_callstack_top
;
1983 volatile duk_int_t entry_call_recursion_depth
;
1984 duk_jmpbuf
* volatile entry_jmpbuf_ptr
;
1986 /* current PC, volatile because it is accessed by other functions
1987 * through thr->ptr_to_curr_pc. Critical for performance. It would
1988 * be safest to make this volatile, but that eliminates performance
1989 * benefits. Aliasing guarantees should be enough though.
1991 duk_instr_t
*curr_pc
; /* stable */
1993 /* "hot" variables for interpretation -- not volatile, value not guaranteed in setjmp error handling */
1994 duk_hthread
*thr
; /* stable */
1995 duk_hcompiledfunction
*fun
; /* stable */
1996 duk_tval
*consts
; /* stable */
1997 /* 'funcs' is quite rarely used, so no local for it */
1999 /* "hot" temps for interpretation -- not volatile, value not guaranteed in setjmp error handling */
2000 duk_uint_fast32_t ins
; /* XXX: check performance impact on x64 between fast/non-fast variant */
2005 #ifdef DUK_USE_INTERRUPT_COUNTER
2009 #ifdef DUK_USE_ASSERTIONS
2010 duk_size_t valstack_top_base
; /* valstack top, should match before interpreting each op (no leftovers) */
2013 /* XXX: document assumptions on setjmp and volatile variables
2014 * (see duk_handle_call()).
2021 DUK_ASSERT(exec_thr
!= NULL
);
2022 DUK_ASSERT(exec_thr
->heap
!= NULL
);
2023 DUK_ASSERT(exec_thr
->heap
->curr_thread
!= NULL
);
2024 DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR((duk_heaphdr
*) exec_thr
);
2025 DUK_ASSERT(exec_thr
->callstack_top
>= 1); /* at least one activation, ours */
2026 DUK_ASSERT(DUK_ACT_GET_FUNC(exec_thr
->callstack
+ exec_thr
->callstack_top
- 1) != NULL
);
2027 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(exec_thr
->callstack
+ exec_thr
->callstack_top
- 1)));
2029 entry_thread
= exec_thr
; /* volatile copy */
2030 thr
= (duk_hthread
*) entry_thread
;
2031 entry_callstack_top
= thr
->callstack_top
;
2032 entry_call_recursion_depth
= thr
->heap
->call_recursion_depth
;
2033 entry_jmpbuf_ptr
= thr
->heap
->lj
.jmpbuf_ptr
;
2036 * Setjmp catchpoint setup.
2038 * Note: we currently assume that the setjmp() catchpoint is
2039 * not re-entrant (longjmp() cannot be called more than once
2040 * for a single setjmp()).
2043 reset_setjmp_catchpoint
:
2045 DUK_ASSERT(thr
!= NULL
);
2046 thr
->heap
->lj
.jmpbuf_ptr
= &jmpbuf
;
2047 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
);
2049 if (DUK_SETJMP(thr
->heap
->lj
.jmpbuf_ptr
->jb
)) {
2051 * Note: any local variables accessed here must have their value
2052 * assigned *before* the setjmp() call, OR they must be declared
2053 * volatile. Otherwise their value is not guaranteed to be correct.
2055 * 'thr' might seem to be a risky variable because it is changed
2056 * for yield and resume. However, yield and resume are handled
2060 duk_small_uint_t lj_ret
;
2061 duk_hthread
*tmp_entry_thread
;
2062 duk_size_t tmp_entry_callstack_top
;
2064 DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor"));
2066 /* Relookup 'thr': it's not volatile so its value is not
2067 * guaranteed. The heap->curr_thread value should always be
2068 * valid here because longjmp callers don't switch threads,
2069 * only the longjmp handler does that (even for RESUME and
2072 DUK_ASSERT(entry_thread
!= NULL
);
2073 thr
= entry_thread
->heap
->curr_thread
;
2075 /* Don't sync curr_pc when unwinding: with recursive executor
2076 * calls thr->ptr_curr_pc may be dangling.
2079 /* XXX: signalling the need to shrink check (only if unwound) */
2081 /* Must be restored here to handle e.g. yields properly. */
2082 thr
->heap
->call_recursion_depth
= entry_call_recursion_depth
;
2084 /* Switch to caller's setjmp() catcher so that if an error occurs
2085 * during error handling, it is always propagated outwards instead
2086 * of causing an infinite loop in our own handler.
2089 DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p",
2090 (void *) ((thr
&& thr
->heap
) ? thr
->heap
->lj
.jmpbuf_ptr
: NULL
),
2091 (void *) entry_jmpbuf_ptr
));
2092 thr
->heap
->lj
.jmpbuf_ptr
= (duk_jmpbuf
*) entry_jmpbuf_ptr
;
2094 tmp_entry_thread
= (duk_hthread
*) entry_thread
; /* use temps to avoid GH-318 */
2095 tmp_entry_callstack_top
= (duk_size_t
) entry_callstack_top
;
2096 lj_ret
= duk__handle_longjmp(thr
, tmp_entry_thread
, tmp_entry_callstack_top
);
2098 if (lj_ret
== DUK__LONGJMP_RESTART
) {
2100 * Restart bytecode execution, possibly with a changed thread.
2102 thr
= thr
->heap
->curr_thread
;
2103 goto reset_setjmp_catchpoint
;
2104 } else if (lj_ret
== DUK__LONGJMP_RETHROW
) {
2106 * Rethrow error to calling state.
2109 /* thread may have changed (e.g. YIELD converted to THROW) */
2110 thr
= thr
->heap
->curr_thread
;
2112 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
== entry_jmpbuf_ptr
);
2114 duk_err_longjmp(thr
);
2118 * Return from bytecode executor with a return value.
2120 DUK_ASSERT(lj_ret
== DUK__LONGJMP_FINISHED
);
2121 /* XXX: return assertions for valstack, callstack, catchstack */
2123 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
== entry_jmpbuf_ptr
);
2130 * Restart execution by reloading thread state.
2132 * Note that 'thr' and any thread configuration may have changed,
2133 * so all local variables are suspect.
2135 * The number of local variables should be kept to a minimum: if
2136 * the variables are spilled, they will need to be loaded from
2139 * Any 'goto restart_execution;' code path in opcode dispatch must
2140 * ensure 'curr_pc' is synced back to act->curr_pc before the goto
2146 /* Lookup current thread; use the volatile 'entry_thread' for this to
2147 * avoid clobber warnings. (Any valid, reachable 'thr' value would be
2148 * fine for this, so using 'entry_thread' is just to silence warnings.)
2150 thr
= entry_thread
->heap
->curr_thread
;
2151 DUK_ASSERT(thr
!= NULL
);
2152 DUK_ASSERT(thr
->callstack_top
>= 1);
2153 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
);
2154 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)));
2156 thr
->ptr_curr_pc
= &curr_pc
;
2158 /* Assume interrupt init/counter are properly initialized here. */
2160 /* assume that thr->valstack_bottom has been set-up before getting here */
2162 duk_activation
*act
;
2164 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2165 fun
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
2166 DUK_ASSERT(fun
!= NULL
);
2167 DUK_ASSERT(thr
->valstack_top
- thr
->valstack_bottom
== fun
->nregs
);
2168 consts
= DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr
->heap
, fun
);
2169 DUK_ASSERT(consts
!= NULL
);
2172 #if defined(DUK_USE_DEBUGGER_SUPPORT)
2173 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
) && !thr
->heap
->dbg_processing
) {
2174 duk_activation
*act
;
2176 thr
->heap
->dbg_processing
= 1;
2177 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2178 duk__executor_handle_debugger(thr
, act
, fun
);
2179 thr
->heap
->dbg_processing
= 0;
2181 #endif /* DUK_USE_DEBUGGER_SUPPORT */
2183 /* XXX: shrink check flag? */
2186 * Bytecode interpreter.
2188 * The interpreter must be very careful with memory pointers, as
2189 * many pointers are not guaranteed to be 'stable' and may be
2190 * reallocated and relocated on-the-fly quite easily (e.g. by a
2191 * memory allocation or a property access).
2193 * The following are assumed to have stable pointers:
2194 * - the current thread
2195 * - the current function
2196 * - the bytecode, constant table, inner function table of the
2197 * current function (as they are a part of the function allocation)
2199 * The following are assumed to have semi-stable pointers:
2200 * - the current activation entry: stable as long as callstack
2201 * is not changed (reallocated by growing or shrinking), or
2202 * by any garbage collection invocation (through finalizers)
2203 * - Note in particular that ANY DECREF can invalidate the
2204 * activation pointer
2206 * The following are not assumed to have stable pointers at all:
2207 * - the value stack (registers) of the current thread
2208 * - the catch stack of the current thread
2210 * See execution.rst for discussion.
2213 DUK_ASSERT(thr
!= NULL
);
2214 DUK_ASSERT(fun
!= NULL
);
2216 DUK_DD(DUK_DDPRINT("restarting execution, thr %p, act idx %ld, fun %p,"
2217 "consts %p, funcs %p, lev %ld, regbot %ld, regtop %ld, catchstack_top=%ld, "
2220 (long) (thr
->callstack_top
- 1),
2222 (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr
->heap
, fun
),
2223 (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr
->heap
, fun
),
2224 (long) (thr
->callstack_top
- 1),
2225 (long) (thr
->valstack_bottom
- thr
->valstack
),
2226 (long) (thr
->valstack_top
- thr
->valstack
),
2227 (long) thr
->catchstack_top
,
2228 (long) thr
->callstack_preventcount
));
2230 #ifdef DUK_USE_ASSERTIONS
2231 valstack_top_base
= (duk_size_t
) (thr
->valstack_top
- thr
->valstack
);
2234 /* Set up curr_pc for opcode dispatch. */
2236 duk_activation
*act
;
2237 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2238 curr_pc
= act
->curr_pc
;
2242 DUK_ASSERT(thr
->callstack_top
>= 1);
2243 DUK_ASSERT(thr
->valstack_top
- thr
->valstack_bottom
== fun
->nregs
);
2244 DUK_ASSERT((duk_size_t
) (thr
->valstack_top
- thr
->valstack
) == valstack_top_base
);
2246 /* Executor interrupt counter check, used to implement breakpoints,
2247 * debugging interface, execution timeouts, etc. The counter is heap
2248 * specific but is maintained in the current thread to make the check
2249 * as fast as possible. The counter is copied back to the heap struct
2250 * whenever a thread switch occurs by the DUK_HEAP_SWITCH_THREAD() macro.
2252 #ifdef DUK_USE_INTERRUPT_COUNTER
2253 int_ctr
= thr
->interrupt_counter
;
2254 if (DUK_LIKELY(int_ctr
> 0)) {
2255 thr
->interrupt_counter
= int_ctr
- 1;
2257 /* Trigger at zero or below */
2258 duk_small_uint_t exec_int_ret
;
2260 /* Write curr_pc back for the debugger. */
2261 DUK_ASSERT(thr
->callstack_top
> 0);
2263 duk_activation
*act
;
2264 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2265 act
->curr_pc
= (duk_instr_t
*) curr_pc
;
2268 /* Force restart caused by a function return; must recheck
2269 * debugger breakpoints before checking line transitions,
2270 * see GH-303. Restart and then handle interrupt_counter
2273 #if defined(DUK_USE_DEBUGGER_SUPPORT)
2274 if (thr
->heap
->dbg_force_restart
) {
2275 DUK_DD(DUK_DDPRINT("dbg_force_restart flag forced restart execution")); /* GH-303 */
2276 thr
->heap
->dbg_force_restart
= 0;
2277 goto restart_execution
;
2281 exec_int_ret
= duk__executor_interrupt(thr
);
2282 if (exec_int_ret
== DUK__INT_RESTART
) {
2283 /* curr_pc synced back above */
2284 goto restart_execution
;
2288 #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
2289 thr
->heap
->inst_count_exec
++;
2292 #if defined(DUK_USE_ASSERTIONS) || defined(DUK_USE_DEBUG)
2294 duk_activation
*act
;
2295 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2296 DUK_ASSERT(curr_pc
>= DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr
->heap
, fun
));
2297 DUK_ASSERT(curr_pc
< DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr
->heap
, fun
));
2298 DUK_UNREF(act
); /* if debugging disabled */
2300 DUK_DDD(DUK_DDDPRINT("executing bytecode: pc=%ld, ins=0x%08lx, op=%ld, valstack_top=%ld/%ld, nregs=%ld --> %!I",
2301 (long) (curr_pc
- DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr
->heap
, fun
)),
2302 (unsigned long) *curr_pc
,
2303 (long) DUK_DEC_OP(*curr_pc
),
2304 (long) (thr
->valstack_top
- thr
->valstack
),
2305 (long) (thr
->valstack_end
- thr
->valstack
),
2306 (long) (fun
? fun
->nregs
: -1),
2307 (duk_instr_t
) *curr_pc
));
2311 #if defined(DUK_USE_ASSERTIONS)
2312 /* Quite heavy assert: check that valstack is in correctly
2313 * initialized state. Improper shuffle instructions can
2314 * write beyond valstack_end so this check catches them in
2319 tv
= thr
->valstack_top
;
2320 while (tv
!= thr
->valstack_end
) {
2321 DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(tv
));
2329 /* Typing: use duk_small_(u)int_fast_t when decoding small
2330 * opcode fields (op, A, B, C) and duk_(u)int_fast_t when
2331 * decoding larger fields (e.g. BC which is 18 bits). Use
2332 * unsigned variant by default, signed when the value is used
2333 * in signed arithmetic. Using variable names such as 'a', 'b',
2334 * 'c', 'bc', etc makes it easier to spot typing mismatches.
2337 /* XXX: the best typing needs to be validated by perf measurement:
2338 * e.g. using a small type which is the cast to a larger duk_idx_t
2339 * may be slower than declaring the variable as a duk_idx_t in the
2343 /* XXX: use macros for the repetitive tval/refcount handling. */
2345 switch ((int) DUK_DEC_OP(ins
)) {
2346 /* XXX: switch cast? */
2348 case DUK_OP_LDREG
: {
2349 duk_small_uint_fast_t a
;
2352 duk_tval
*tv1
, *tv2
;
2354 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2355 bc
= DUK_DEC_BC(ins
); tv2
= DUK__REGP(bc
);
2356 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
2357 DUK_TVAL_SET_TVAL(tv1
, tv2
);
2358 DUK_TVAL_INCREF(thr
, tv1
);
2359 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
2363 case DUK_OP_STREG
: {
2364 duk_small_uint_fast_t a
;
2367 duk_tval
*tv1
, *tv2
;
2369 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2370 bc
= DUK_DEC_BC(ins
); tv2
= DUK__REGP(bc
);
2371 DUK_TVAL_SET_TVAL(&tv_tmp
, tv2
);
2372 DUK_TVAL_SET_TVAL(tv2
, tv1
);
2373 DUK_TVAL_INCREF(thr
, tv2
);
2374 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
2378 case DUK_OP_LDCONST
: {
2379 duk_small_uint_fast_t a
;
2382 duk_tval
*tv1
, *tv2
;
2384 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2385 bc
= DUK_DEC_BC(ins
); tv2
= DUK__CONSTP(bc
);
2386 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
2387 DUK_TVAL_SET_TVAL(tv1
, tv2
);
2388 DUK_TVAL_INCREF(thr
, tv2
); /* may be e.g. string */
2389 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
2393 case DUK_OP_LDINT
: {
2394 duk_small_uint_fast_t a
;
2398 #if defined(DUK_USE_FASTINT)
2404 #if defined(DUK_USE_FASTINT)
2405 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2406 bc
= DUK_DEC_BC(ins
); val
= (duk_int32_t
) (bc
- DUK_BC_LDINT_BIAS
);
2407 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
2408 DUK_TVAL_SET_FASTINT_I32(tv1
, val
);
2409 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
2411 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2412 bc
= DUK_DEC_BC(ins
); val
= (duk_double_t
) (bc
- DUK_BC_LDINT_BIAS
);
2413 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
2414 DUK_TVAL_SET_NUMBER(tv1
, val
);
2415 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
2420 case DUK_OP_LDINTX
: {
2421 duk_small_uint_fast_t a
;
2425 /* LDINTX is not necessarily in FASTINT range, so
2426 * no fast path for now.
2428 * XXX: perhaps restrict LDINTX to fastint range, wider
2429 * range very rarely needed.
2432 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2433 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
2434 val
= DUK_TVAL_GET_NUMBER(tv1
) * ((duk_double_t
) (1L << DUK_BC_LDINTX_SHIFT
)) +
2435 (duk_double_t
) DUK_DEC_BC(ins
);
2436 #if defined(DUK_USE_FASTINT)
2437 DUK_TVAL_SET_NUMBER_CHKFAST(tv1
, val
);
2439 DUK_TVAL_SET_NUMBER(tv1
, val
);
2444 case DUK_OP_MPUTOBJ
:
2445 case DUK_OP_MPUTOBJI
: {
2446 duk_context
*ctx
= (duk_context
*) thr
;
2447 duk_small_uint_fast_t a
;
2450 duk_uint_fast_t idx
;
2451 duk_small_uint_fast_t count
;
2453 /* A -> register of target object
2454 * B -> first register of key/value pair list
2455 * C -> number of key/value pairs
2458 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2459 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1
));
2460 obj
= DUK_TVAL_GET_OBJECT(tv1
);
2462 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
2463 if (DUK_DEC_OP(ins
) == DUK_OP_MPUTOBJI
) {
2464 duk_tval
*tv_ind
= DUK__REGP(idx
);
2465 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2466 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2469 count
= (duk_small_uint_fast_t
) DUK_DEC_C(ins
);
2471 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2472 if (DUK_UNLIKELY(idx
+ count
* 2 > (duk_uint_fast_t
) duk_get_top(ctx
))) {
2473 /* XXX: use duk_is_valid_index() instead? */
2474 /* XXX: improve check; check against nregs, not against top */
2475 DUK__INTERNAL_ERROR("MPUTOBJ out of bounds");
2479 duk_push_hobject(ctx
, obj
);
2482 /* XXX: faster initialization (direct access or better primitives) */
2484 duk_push_tval(ctx
, DUK__REGP(idx
));
2485 DUK_ASSERT(duk_is_string(ctx
, -1));
2486 duk_push_tval(ctx
, DUK__REGP(idx
+ 1)); /* -> [... obj key value] */
2487 duk_xdef_prop_wec(ctx
, -3); /* -> [... obj] */
2493 duk_pop(ctx
); /* [... obj] -> [...] */
2497 case DUK_OP_MPUTARR
:
2498 case DUK_OP_MPUTARRI
: {
2499 duk_context
*ctx
= (duk_context
*) thr
;
2500 duk_small_uint_fast_t a
;
2503 duk_uint_fast_t idx
;
2504 duk_small_uint_fast_t count
;
2505 duk_uint32_t arr_idx
;
2507 /* A -> register of target object
2508 * B -> first register of value data (start_index, value1, value2, ..., valueN)
2509 * C -> number of key/value pairs (N)
2512 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2513 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1
));
2514 obj
= DUK_TVAL_GET_OBJECT(tv1
);
2515 DUK_ASSERT(obj
!= NULL
);
2517 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
2518 if (DUK_DEC_OP(ins
) == DUK_OP_MPUTARRI
) {
2519 duk_tval
*tv_ind
= DUK__REGP(idx
);
2520 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2521 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2524 count
= (duk_small_uint_fast_t
) DUK_DEC_C(ins
);
2526 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2527 if (idx
+ count
+ 1 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2528 /* XXX: use duk_is_valid_index() instead? */
2529 /* XXX: improve check; check against nregs, not against top */
2530 DUK__INTERNAL_ERROR("MPUTARR out of bounds");
2534 tv1
= DUK__REGP(idx
);
2535 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
2536 arr_idx
= (duk_uint32_t
) DUK_TVAL_GET_NUMBER(tv1
);
2539 duk_push_hobject(ctx
, obj
);
2542 /* duk_xdef_prop() will define an own property without any array
2543 * special behaviors. We'll need to set the array length explicitly
2544 * in the end. For arrays with elisions, the compiler will emit an
2545 * explicit SETALEN which will update the length.
2548 /* XXX: because we're dealing with 'own' properties of a fresh array,
2549 * the array initializer should just ensure that the array has a large
2550 * enough array part and write the values directly into array part,
2551 * and finally set 'length' manually in the end (as already happens now).
2554 duk_push_tval(ctx
, DUK__REGP(idx
)); /* -> [... obj value] */
2555 duk_xdef_prop_index_wec(ctx
, -2, arr_idx
); /* -> [... obj] */
2557 /* XXX: could use at least one fewer loop counters */
2563 /* XXX: E5.1 Section 11.1.4 coerces the final length through
2564 * ToUint32() which is odd but happens now as a side effect of
2567 duk_hobject_set_length(thr
, obj
, (duk_uint32_t
) arr_idx
);
2569 duk_pop(ctx
); /* [... obj] -> [...] */
2575 duk_context
*ctx
= (duk_context
*) thr
;
2576 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2577 duk_uint_fast_t idx
;
2578 duk_small_uint_fast_t i
;
2580 /* A -> unused (reserved for flags, for consistency with DUK_OP_CALL)
2581 * B -> target register and start reg: constructor, arg1, ..., argN
2582 * (for DUK_OP_NEWI, 'b' is indirect)
2586 /* duk_new() will call the constuctor using duk_handle_call().
2587 * A constructor call prevents a yield from inside the constructor,
2588 * even if the constructor is an Ecmascript function.
2591 /* Don't need to sync curr_pc here; duk_new() will do that
2592 * when it augments the created error.
2595 /* XXX: unnecessary copying of values? Just set 'top' to
2596 * b + c, and let the return handling fix up the stack frame?
2599 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
2600 if (DUK_DEC_OP(ins
) == DUK_OP_NEWI
) {
2601 duk_tval
*tv_ind
= DUK__REGP(idx
);
2602 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2603 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2606 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2607 if (idx
+ c
+ 1 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2608 /* XXX: use duk_is_valid_index() instead? */
2609 /* XXX: improve check; check against nregs, not against top */
2610 DUK__INTERNAL_ERROR("NEW out of bounds");
2614 duk_require_stack(ctx
, (duk_idx_t
) c
);
2615 duk_push_tval(ctx
, DUK__REGP(idx
));
2616 for (i
= 0; i
< c
; i
++) {
2617 duk_push_tval(ctx
, DUK__REGP(idx
+ i
+ 1));
2619 duk_new(ctx
, (duk_idx_t
) c
); /* [... constructor arg1 ... argN] -> [retval] */
2620 DUK_DDD(DUK_DDDPRINT("NEW -> %!iT", (duk_tval
*) duk_get_tval(ctx
, -1)));
2621 duk_replace(ctx
, (duk_idx_t
) idx
);
2623 /* When debugger is enabled, we need to recheck the activation
2624 * status after returning. This is now handled by call handling
2625 * and heap->dbg_force_restart.
2630 case DUK_OP_REGEXP
: {
2631 #ifdef DUK_USE_REGEXP_SUPPORT
2632 duk_context
*ctx
= (duk_context
*) thr
;
2633 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2634 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2635 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2637 /* A -> target register
2638 * B -> bytecode (also contains flags)
2639 * C -> escaped source
2642 duk_push_tval(ctx
, DUK__REGCONSTP(c
));
2643 duk_push_tval(ctx
, DUK__REGCONSTP(b
)); /* -> [ ... escaped_source bytecode ] */
2644 duk_regexp_create_instance(thr
); /* -> [ ... regexp_instance ] */
2645 DUK_DDD(DUK_DDDPRINT("regexp instance: %!iT", (duk_tval
*) duk_get_tval(ctx
, -1)));
2646 duk_replace(ctx
, (duk_idx_t
) a
);
2648 /* The compiler should never emit DUK_OP_REGEXP if there is no
2651 DUK__INTERNAL_ERROR("no regexp support");
2658 case DUK_OP_CSREGI
: {
2660 * Assuming a register binds to a variable declared within this
2661 * function (a declarative binding), the 'this' for the call
2662 * setup is always 'undefined'. E5 Section 10.2.1.1.6.
2665 duk_context
*ctx
= (duk_context
*) thr
;
2666 duk_small_uint_fast_t b
= DUK_DEC_B(ins
); /* restricted to regs */
2667 duk_uint_fast_t idx
;
2669 /* A -> target register (A, A+1) for call setup
2670 * (for DUK_OP_CSREGI, 'a' is indirect)
2671 * B -> register containing target function (not type checked here)
2674 /* XXX: direct manipulation, or duk_replace_tval() */
2676 /* Note: target registers a and a+1 may overlap with DUK__REGP(b).
2680 idx
= (duk_uint_fast_t
) DUK_DEC_A(ins
);
2681 if (DUK_DEC_OP(ins
) == DUK_OP_CSREGI
) {
2682 duk_tval
*tv_ind
= DUK__REGP(idx
);
2683 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2684 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2687 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2688 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2689 /* XXX: use duk_is_valid_index() instead? */
2690 /* XXX: improve check; check against nregs, not against top */
2691 DUK__INTERNAL_ERROR("CSREG out of bounds");
2695 duk_push_tval(ctx
, DUK__REGP(b
));
2696 duk_replace(ctx
, (duk_idx_t
) idx
);
2697 duk_push_undefined(ctx
);
2698 duk_replace(ctx
, (duk_idx_t
) (idx
+ 1));
2702 case DUK_OP_GETVAR
: {
2703 duk_context
*ctx
= (duk_context
*) thr
;
2704 duk_activation
*act
;
2705 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2706 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
2710 tv1
= DUK__CONSTP(bc
);
2711 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2712 name
= DUK_TVAL_GET_STRING(tv1
);
2713 DUK_ASSERT(name
!= NULL
);
2714 DUK_DDD(DUK_DDDPRINT("GETVAR: '%!O'", (duk_heaphdr
*) name
));
2715 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2716 (void) duk_js_getvar_activation(thr
, act
, name
, 1 /*throw*/); /* -> [... val this] */
2718 duk_pop(ctx
); /* 'this' binding is not needed here */
2719 duk_replace(ctx
, (duk_idx_t
) a
);
2723 case DUK_OP_PUTVAR
: {
2724 duk_activation
*act
;
2725 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2726 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
2730 tv1
= DUK__CONSTP(bc
);
2731 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2732 name
= DUK_TVAL_GET_STRING(tv1
);
2733 DUK_ASSERT(name
!= NULL
);
2735 /* XXX: putvar takes a duk_tval pointer, which is awkward and
2736 * should be reworked.
2739 tv1
= DUK__REGP(a
); /* val */
2740 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2741 duk_js_putvar_activation(thr
, act
, name
, tv1
, DUK__STRICT());
2745 case DUK_OP_DECLVAR
: {
2746 duk_activation
*act
;
2747 duk_context
*ctx
= (duk_context
*) thr
;
2748 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2749 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2750 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2753 duk_small_uint_t prop_flags
;
2754 duk_bool_t is_func_decl
;
2755 duk_bool_t is_undef_value
;
2757 tv1
= DUK__REGCONSTP(b
);
2758 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2759 name
= DUK_TVAL_GET_STRING(tv1
);
2760 DUK_ASSERT(name
!= NULL
);
2762 is_undef_value
= ((a
& DUK_BC_DECLVAR_FLAG_UNDEF_VALUE
) != 0);
2763 is_func_decl
= ((a
& DUK_BC_DECLVAR_FLAG_FUNC_DECL
) != 0);
2765 /* XXX: declvar takes an duk_tval pointer, which is awkward and
2766 * should be reworked.
2769 /* Compiler is responsible for selecting property flags (configurability,
2770 * writability, etc).
2772 prop_flags
= a
& DUK_PROPDESC_FLAGS_MASK
;
2774 if (is_undef_value
) {
2775 duk_push_undefined(ctx
);
2777 duk_push_tval(ctx
, DUK__REGCONSTP(c
));
2779 tv1
= duk_get_tval(ctx
, -1);
2781 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2782 if (duk_js_declvar_activation(thr
, act
, name
, tv1
, prop_flags
, is_func_decl
)) {
2783 /* already declared, must update binding value */
2784 tv1
= duk_get_tval(ctx
, -1);
2785 duk_js_putvar_activation(thr
, act
, name
, tv1
, DUK__STRICT());
2792 case DUK_OP_DELVAR
: {
2793 duk_activation
*act
;
2794 duk_context
*ctx
= (duk_context
*) thr
;
2795 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2796 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2801 tv1
= DUK__REGCONSTP(b
);
2802 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2803 name
= DUK_TVAL_GET_STRING(tv1
);
2804 DUK_ASSERT(name
!= NULL
);
2805 DUK_DDD(DUK_DDDPRINT("DELVAR '%!O'", (duk_heaphdr
*) name
));
2806 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2807 rc
= duk_js_delvar_activation(thr
, act
, name
);
2809 duk_push_boolean(ctx
, rc
);
2810 duk_replace(ctx
, (duk_idx_t
) a
);
2815 case DUK_OP_CSVARI
: {
2819 * The only (standard) case where the 'this' binding is non-null is when
2820 * (1) the variable is found in an object environment record, and
2821 * (2) that object environment record is a 'with' block.
2825 duk_context
*ctx
= (duk_context
*) thr
;
2826 duk_activation
*act
;
2827 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2828 duk_uint_fast_t idx
;
2832 tv1
= DUK__REGCONSTP(b
);
2833 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2834 name
= DUK_TVAL_GET_STRING(tv1
);
2835 DUK_ASSERT(name
!= NULL
);
2836 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2837 (void) duk_js_getvar_activation(thr
, act
, name
, 1 /*throw*/); /* -> [... val this] */
2839 /* Note: target registers a and a+1 may overlap with DUK__REGCONSTP(b)
2840 * and DUK__REGCONSTP(c). Careful here.
2843 idx
= (duk_uint_fast_t
) DUK_DEC_A(ins
);
2844 if (DUK_DEC_OP(ins
) == DUK_OP_CSVARI
) {
2845 duk_tval
*tv_ind
= DUK__REGP(idx
);
2846 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2847 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2850 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2851 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2852 /* XXX: use duk_is_valid_index() instead? */
2853 /* XXX: improve check; check against nregs, not against top */
2854 DUK__INTERNAL_ERROR("CSVAR out of bounds");
2858 duk_replace(ctx
, (duk_idx_t
) (idx
+ 1)); /* 'this' binding */
2859 duk_replace(ctx
, (duk_idx_t
) idx
); /* variable value (function, we hope, not checked here) */
2863 case DUK_OP_CLOSURE
: {
2864 duk_context
*ctx
= (duk_context
*) thr
;
2865 duk_activation
*act
;
2866 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2867 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
2868 duk_hobject
*fun_temp
;
2871 * BC -> inner function index
2874 DUK_DDD(DUK_DDDPRINT("CLOSURE to target register %ld, fnum %ld (count %ld)",
2875 (long) a
, (long) bc
, (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr
->heap
, fun
)));
2877 DUK_ASSERT_DISABLE(bc
>= 0); /* unsigned */
2878 DUK_ASSERT((duk_uint_t
) bc
< (duk_uint_t
) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr
->heap
, fun
));
2879 fun_temp
= DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr
->heap
, fun
)[bc
];
2880 DUK_ASSERT(fun_temp
!= NULL
);
2881 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(fun_temp
));
2883 DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O",
2884 (void *) fun_temp
, (duk_heaphdr
*) fun_temp
));
2886 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2887 if (act
->lex_env
== NULL
) {
2888 DUK_ASSERT(act
->var_env
== NULL
);
2889 duk_js_init_activation_environment_records_delayed(thr
, act
);
2891 DUK_ASSERT(act
->lex_env
!= NULL
);
2892 DUK_ASSERT(act
->var_env
!= NULL
);
2894 /* functions always have a NEWENV flag, i.e. they get a
2895 * new variable declaration environment, so only lex_env
2898 duk_js_push_closure(thr
,
2899 (duk_hcompiledfunction
*) fun_temp
,
2902 duk_replace(ctx
, (duk_idx_t
) a
);
2907 case DUK_OP_GETPROP
: {
2908 duk_context
*ctx
= (duk_context
*) thr
;
2909 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2910 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2911 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2917 * B -> object reg/const (may be const e.g. in "'foo'[1]")
2918 * C -> key reg/const
2921 tv_obj
= DUK__REGCONSTP(b
);
2922 tv_key
= DUK__REGCONSTP(c
);
2923 DUK_DDD(DUK_DDDPRINT("GETPROP: a=%ld obj=%!T, key=%!T",
2925 (duk_tval
*) DUK__REGCONSTP(b
),
2926 (duk_tval
*) DUK__REGCONSTP(c
)));
2927 rc
= duk_hobject_getprop(thr
, tv_obj
, tv_key
); /* -> [val] */
2928 DUK_UNREF(rc
); /* ignore */
2929 DUK_DDD(DUK_DDDPRINT("GETPROP --> %!T",
2930 (duk_tval
*) duk_get_tval(ctx
, -1)));
2931 tv_obj
= NULL
; /* invalidated */
2932 tv_key
= NULL
; /* invalidated */
2934 duk_replace(ctx
, (duk_idx_t
) a
); /* val */
2938 case DUK_OP_PUTPROP
: {
2939 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2940 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2941 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2948 * B -> key reg/const
2949 * C -> value reg/const
2951 * Note: intentional difference to register arrangement
2952 * of e.g. GETPROP; 'A' must contain a register-only value.
2955 tv_obj
= DUK__REGP(a
);
2956 tv_key
= DUK__REGCONSTP(b
);
2957 tv_val
= DUK__REGCONSTP(c
);
2958 DUK_DDD(DUK_DDDPRINT("PUTPROP: obj=%!T, key=%!T, val=%!T",
2959 (duk_tval
*) DUK__REGP(a
),
2960 (duk_tval
*) DUK__REGCONSTP(b
),
2961 (duk_tval
*) DUK__REGCONSTP(c
)));
2962 rc
= duk_hobject_putprop(thr
, tv_obj
, tv_key
, tv_val
, DUK__STRICT());
2963 DUK_UNREF(rc
); /* ignore */
2964 DUK_DDD(DUK_DDDPRINT("PUTPROP --> obj=%!T, key=%!T, val=%!T",
2965 (duk_tval
*) DUK__REGP(a
),
2966 (duk_tval
*) DUK__REGCONSTP(b
),
2967 (duk_tval
*) DUK__REGCONSTP(c
)));
2968 tv_obj
= NULL
; /* invalidated */
2969 tv_key
= NULL
; /* invalidated */
2970 tv_val
= NULL
; /* invalidated */
2975 case DUK_OP_DELPROP
: {
2976 duk_context
*ctx
= (duk_context
*) thr
;
2977 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2978 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2979 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2986 * C -> key reg/const
2989 tv_obj
= DUK__REGP(b
);
2990 tv_key
= DUK__REGCONSTP(c
);
2991 rc
= duk_hobject_delprop(thr
, tv_obj
, tv_key
, DUK__STRICT());
2992 tv_obj
= NULL
; /* invalidated */
2993 tv_key
= NULL
; /* invalidated */
2995 duk_push_boolean(ctx
, rc
);
2996 duk_replace(ctx
, (duk_idx_t
) a
); /* result */
3001 case DUK_OP_CSPROPI
: {
3002 duk_context
*ctx
= (duk_context
*) thr
;
3003 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3004 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3005 duk_uint_fast_t idx
;
3010 /* E5 Section 11.2.3, step 6.a.i */
3011 /* E5 Section 10.4.3 */
3013 /* XXX: allow object to be a const, e.g. in 'foo'.toString()?
3014 * On the other hand, DUK_REGCONSTP() is slower and generates
3018 tv_obj
= DUK__REGP(b
);
3019 tv_key
= DUK__REGCONSTP(c
);
3020 rc
= duk_hobject_getprop(thr
, tv_obj
, tv_key
); /* -> [val] */
3021 DUK_UNREF(rc
); /* unused */
3022 tv_obj
= NULL
; /* invalidated */
3023 tv_key
= NULL
; /* invalidated */
3025 /* Note: target registers a and a+1 may overlap with DUK__REGP(b)
3026 * and DUK__REGCONSTP(c). Careful here.
3029 idx
= (duk_uint_fast_t
) DUK_DEC_A(ins
);
3030 if (DUK_DEC_OP(ins
) == DUK_OP_CSPROPI
) {
3031 duk_tval
*tv_ind
= DUK__REGP(idx
);
3032 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
3033 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
3036 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
3037 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
3038 /* XXX: use duk_is_valid_index() instead? */
3039 /* XXX: improve check; check against nregs, not against top */
3040 DUK__INTERNAL_ERROR("CSPROP out of bounds");
3044 duk_push_tval(ctx
, DUK__REGP(b
)); /* [ ... val obj ] */
3045 duk_replace(ctx
, (duk_idx_t
) (idx
+ 1)); /* 'this' binding */
3046 duk_replace(ctx
, (duk_idx_t
) idx
); /* val */
3055 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3056 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3057 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3058 duk_small_uint_fast_t op
= DUK_DEC_OP(ins
);
3060 if (op
== DUK_OP_ADD
) {
3062 * Handling DUK_OP_ADD this way is more compact (experimentally)
3063 * than a separate case with separate argument decoding.
3065 duk__vm_arith_add(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
), a
);
3067 duk__vm_arith_binary_op(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
), a
, op
);
3078 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3079 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3080 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3081 duk_small_uint_fast_t op
= DUK_DEC_OP(ins
);
3083 duk__vm_bitwise_binary_op(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
), a
, op
);
3089 duk_context
*ctx
= (duk_context
*) thr
;
3090 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3091 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3092 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3095 /* E5 Sections 11.9.1, 11.9.3 */
3096 tmp
= duk_js_equals(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
));
3097 if (DUK_DEC_OP(ins
) == DUK_OP_NEQ
) {
3100 duk_push_boolean(ctx
, tmp
);
3101 duk_replace(ctx
, (duk_idx_t
) a
);
3107 duk_context
*ctx
= (duk_context
*) thr
;
3108 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3109 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3110 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3113 /* E5 Sections 11.9.1, 11.9.3 */
3114 tmp
= duk_js_strict_equals(DUK__REGCONSTP(b
), DUK__REGCONSTP(c
));
3115 if (DUK_DEC_OP(ins
) == DUK_OP_SNEQ
) {
3118 duk_push_boolean(ctx
, tmp
);
3119 duk_replace(ctx
, (duk_idx_t
) a
);
3123 /* Note: combining comparison ops must be done carefully because
3124 * of uncomparable values (NaN): it's not necessarily true that
3125 * (x >= y) === !(x < y). Also, evaluation order matters, and
3126 * although it would only seem to affect the compiler this is
3127 * actually not the case, because there are also run-time coercions
3128 * of the arguments (with potential side effects).
3130 * XXX: can be combined; check code size.
3134 duk_context
*ctx
= (duk_context
*) thr
;
3135 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3136 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3137 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3140 /* x > y --> y < x */
3141 tmp
= duk_js_compare_helper(thr
,
3142 DUK__REGCONSTP(c
), /* y */
3143 DUK__REGCONSTP(b
), /* x */
3146 duk_push_boolean(ctx
, tmp
);
3147 duk_replace(ctx
, (duk_idx_t
) a
);
3152 duk_context
*ctx
= (duk_context
*) thr
;
3153 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3154 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3155 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3158 /* x >= y --> not (x < y) */
3159 tmp
= duk_js_compare_helper(thr
,
3160 DUK__REGCONSTP(b
), /* x */
3161 DUK__REGCONSTP(c
), /* y */
3162 DUK_COMPARE_FLAG_EVAL_LEFT_FIRST
|
3163 DUK_COMPARE_FLAG_NEGATE
); /* flags */
3165 duk_push_boolean(ctx
, tmp
);
3166 duk_replace(ctx
, (duk_idx_t
) a
);
3171 duk_context
*ctx
= (duk_context
*) thr
;
3172 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3173 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3174 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3178 tmp
= duk_js_compare_helper(thr
,
3179 DUK__REGCONSTP(b
), /* x */
3180 DUK__REGCONSTP(c
), /* y */
3181 DUK_COMPARE_FLAG_EVAL_LEFT_FIRST
); /* flags */
3183 duk_push_boolean(ctx
, tmp
);
3184 duk_replace(ctx
, (duk_idx_t
) a
);
3189 duk_context
*ctx
= (duk_context
*) thr
;
3190 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3191 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3192 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3195 /* x <= y --> not (x > y) --> not (y < x) */
3196 tmp
= duk_js_compare_helper(thr
,
3197 DUK__REGCONSTP(c
), /* y */
3198 DUK__REGCONSTP(b
), /* x */
3199 DUK_COMPARE_FLAG_NEGATE
); /* flags */
3201 duk_push_boolean(ctx
, tmp
);
3202 duk_replace(ctx
, (duk_idx_t
) a
);
3207 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3208 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3211 tmp
= duk_js_toboolean(DUK__REGCONSTP(b
));
3212 if (tmp
== (duk_bool_t
) a
) {
3213 /* if boolean matches A, skip next inst */
3222 duk_int_fast_t abc
= DUK_DEC_ABC(ins
);
3224 curr_pc
+= abc
- DUK_BC_JUMP_BIAS
;
3228 case DUK_OP_RETURN
: {
3229 duk_context
*ctx
= (duk_context
*) thr
;
3230 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3231 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3232 /* duk_small_uint_fast_t c = DUK_DEC_C(ins); */
3236 * B -> return value reg/const
3237 * C -> currently unused
3240 /* A fast return avoids full longjmp handling for a set of
3241 * scenarios which hopefully represents the common cases.
3242 * The compiler is responsible for emitting fast returns
3243 * only when they are safe. Currently this means that there
3244 * is nothing on the catch stack (not even label catchers).
3245 * The speed advantage of fast returns (avoiding longjmp) is
3246 * not very high, around 10-15%.
3248 #if 0 /* XXX: Disabled for 1.0 release */
3249 if (a
& DUK_BC_RETURN_FLAG_FAST
) {
3250 DUK_DDD(DUK_DDDPRINT("FASTRETURN attempt a=%ld b=%ld", (long) a
, (long) b
));
3252 if (duk__handle_fast_return(thr
,
3253 (a
& DUK_BC_RETURN_FLAG_HAVE_RETVAL
) ? DUK__REGCONSTP(b
) : NULL
,
3255 entry_callstack_top
)) {
3256 DUK_DDD(DUK_DDDPRINT("FASTRETURN success a=%ld b=%ld", (long) a
, (long) b
));
3257 DUK__SYNC_CURR_PC();
3258 goto restart_execution
;
3263 /* No fast return, slow path. */
3264 DUK_DDD(DUK_DDDPRINT("SLOWRETURN a=%ld b=%ld", (long) a
, (long) b
));
3266 if (a
& DUK_BC_RETURN_FLAG_HAVE_RETVAL
) {
3267 tv_val
= DUK__REGCONSTP(b
);
3268 #if defined(DUK_USE_FASTINT)
3269 /* Explicit check for fastint downgrade. Do
3270 * it also for consts for now, which is odd
3273 /* XXX: restrict to reg values only? */
3275 DUK_TVAL_CHKFAST_INPLACE(tv_val
);
3277 duk_push_tval(ctx
, tv_val
);
3279 duk_push_undefined(ctx
);
3282 duk_err_setup_heap_ljstate(thr
, DUK_LJ_TYPE_RETURN
);
3284 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* in bytecode executor, should always be set */
3285 DUK__SYNC_AND_NULL_CURR_PC();
3286 duk_err_longjmp(thr
);
3292 case DUK_OP_CALLI
: {
3293 duk_context
*ctx
= (duk_context
*) thr
;
3294 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3295 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3296 duk_uint_fast_t idx
;
3297 duk_small_uint_t call_flags
;
3298 duk_small_uint_t flag_tailcall
;
3299 duk_small_uint_t flag_evalcall
;
3301 duk_hobject
*obj_func
;
3302 duk_bool_t setup_rc
;
3303 duk_idx_t num_stack_args
;
3306 * B -> base register for call (base -> func, base+1 -> this, base+2 -> arg1 ... base+2+N-1 -> argN)
3307 * (for DUK_OP_CALLI, 'b' is indirect)
3311 /* these are not necessarily 0 or 1 (may be other non-zero), that's ok */
3312 flag_tailcall
= (a
& DUK_BC_CALL_FLAG_TAILCALL
);
3313 flag_evalcall
= (a
& DUK_BC_CALL_FLAG_EVALCALL
);
3315 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
3316 if (DUK_DEC_OP(ins
) == DUK_OP_CALLI
) {
3317 duk_tval
*tv_ind
= DUK__REGP(idx
);
3318 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
3319 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
3322 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
3323 if (!duk_is_valid_index(ctx
, (duk_idx_t
) idx
)) {
3324 /* XXX: improve check; check against nregs, not against top */
3325 DUK__INTERNAL_ERROR("CALL out of bounds");
3330 * To determine whether to use an optimized Ecmascript-to-Ecmascript
3331 * call, we need to know whether the final, non-bound function is an
3332 * Ecmascript function.
3334 * This is now implemented so that we start to do an ecma-to-ecma call
3335 * setup which will resolve the bound chain as the first thing. If the
3336 * final function is not eligible, the return value indicates that the
3337 * ecma-to-ecma call is not possible. The setup will overwrite the call
3338 * target at DUK__REGP(idx) with the final, non-bound function (which
3339 * may be a lightfunc), and fudge arguments if necessary.
3341 * XXX: If an ecma-to-ecma call is not possible, this initial call
3342 * setup will do bound function chain resolution but won't do the
3343 * "effective this binding" resolution which is quite confusing.
3344 * Perhaps add a helper for doing bound function and effective this
3345 * binding resolution - and call that explicitly? Ecma-to-ecma call
3346 * setup and normal function handling can then assume this prestep has
3347 * been done by the caller.
3350 duk_set_top(ctx
, (duk_idx_t
) (idx
+ c
+ 2)); /* [ ... func this arg1 ... argN ] */
3353 if (flag_tailcall
) {
3354 /* We request a tail call, but in some corner cases
3355 * call handling can decide that a tail call is
3356 * actually not possible.
3357 * See: test-bug-tailcall-preventyield-assert.c.
3359 call_flags
|= DUK_CALL_FLAG_IS_TAILCALL
;
3362 /* Compared to duk_handle_call():
3363 * - protected call: never
3364 * - ignore recursion limit: never
3367 setup_rc
= duk_handle_ecma_call_setup(thr
,
3372 /* Ecma-to-ecma call possible, may or may not be a tail call.
3373 * Avoid C recursion by being clever.
3375 DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call setup possible, restart execution"));
3376 /* curr_pc synced by duk_handle_ecma_call_setup() */
3377 goto restart_execution
;
3379 DUK_ASSERT(thr
->ptr_curr_pc
!= NULL
); /* restored if ecma-to-ecma setup fails */
3381 DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call not possible, target is native (may be lightfunc)"));
3383 /* Recompute argument count: bound function handling may have shifted. */
3384 num_stack_args
= duk_get_top(ctx
) - (idx
+ 2);
3385 DUK_DDD(DUK_DDDPRINT("recomputed arg count: %ld\n", (long) num_stack_args
));
3387 tv_func
= DUK__REGP(idx
); /* Relookup if relocated */
3388 if (DUK_TVAL_IS_LIGHTFUNC(tv_func
)) {
3389 call_flags
= 0; /* not protected, respect reclimit, not constructor */
3391 /* There is no eval() special handling here: eval() is never
3392 * automatically converted to a lightfunc.
3394 DUK_ASSERT(DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func
) != duk_bi_global_object_eval
);
3396 duk_handle_call(thr
,
3400 /* duk_js_call.c is required to restore the stack reserve
3401 * so we only need to reset the top.
3403 duk_set_top(ctx
, (duk_idx_t
) fun
->nregs
);
3405 /* No need to reinit setjmp() catchpoint, as call handling
3406 * will store and restore our state.
3409 /* Call setup checks callability. */
3410 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_func
));
3411 obj_func
= DUK_TVAL_GET_OBJECT(tv_func
);
3412 DUK_ASSERT(obj_func
!= NULL
);
3413 DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(obj_func
));
3416 * Other cases, use C recursion.
3418 * If a tail call was requested we ignore it and execute a normal call.
3419 * Since Duktape 0.11.0 the compiler emits a RETURN opcode even after
3420 * a tail call to avoid test-bug-tailcall-thread-yield-resume.js.
3422 * Direct eval call: (1) call target (before following bound function
3423 * chain) is the built-in eval() function, and (2) call was made with
3424 * the identifier 'eval'.
3427 call_flags
= 0; /* not protected, respect reclimit, not constructor */
3429 if (DUK_HOBJECT_IS_NATIVEFUNCTION(obj_func
) &&
3430 ((duk_hnativefunction
*) obj_func
)->func
== duk_bi_global_object_eval
) {
3431 if (flag_evalcall
) {
3432 DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was 'eval' -> direct eval"));
3433 call_flags
|= DUK_CALL_FLAG_DIRECT_EVAL
;
3435 DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was not 'eval' -> indirect eval"));
3439 duk_handle_call(thr
,
3443 /* duk_js_call.c is required to restore the stack reserve
3444 * so we only need to reset the top.
3446 duk_set_top(ctx
, (duk_idx_t
) fun
->nregs
);
3448 /* No need to reinit setjmp() catchpoint, as call handling
3449 * will store and restore our state.
3453 /* When debugger is enabled, we need to recheck the activation
3454 * status after returning. This is now handled by call handling
3455 * and heap->dbg_force_restart.
3460 case DUK_OP_TRYCATCH
: {
3461 duk_context
*ctx
= (duk_context
*) thr
;
3462 duk_activation
*act
;
3465 duk_small_uint_fast_t a
;
3469 * BC -> reg_catch; base register for two registers used both during
3470 * trycatch setup and when catch is triggered
3472 * If DUK_BC_TRYCATCH_FLAG_CATCH_BINDING set:
3473 * reg_catch + 0: catch binding variable name (string).
3474 * Automatic declarative environment is established for
3475 * the duration of the 'catch' clause.
3477 * If DUK_BC_TRYCATCH_FLAG_WITH_BINDING set:
3478 * reg_catch + 0: with 'target value', which is coerced to
3479 * an object and then used as a bindind object for an
3480 * environment record. The binding is initialized here, for
3483 * Note that a TRYCATCH generated for a 'with' statement has no
3484 * catch or finally parts.
3487 /* XXX: TRYCATCH handling should be reworked to avoid creating
3488 * an explicit scope unless it is actually needed (e.g. function
3489 * instances or eval is executed inside the catch block). This
3490 * rework is not trivial because the compiler doesn't have an
3491 * intermediate representation. When the rework is done, the
3492 * opcode format can also be made more straightforward.
3495 /* XXX: side effect handling is quite awkward here */
3497 DUK_DDD(DUK_DDDPRINT("TRYCATCH: reg_catch=%ld, have_catch=%ld, "
3498 "have_finally=%ld, catch_binding=%ld, with_binding=%ld (flags=0x%02lx)",
3499 (long) DUK_DEC_BC(ins
),
3500 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH
? 1 : 0),
3501 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY
? 1 : 0),
3502 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING
? 1 : 0),
3503 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_WITH_BINDING
? 1 : 0),
3504 (unsigned long) DUK_DEC_A(ins
)));
3507 bc
= DUK_DEC_BC(ins
);
3509 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3510 DUK_ASSERT(thr
->callstack_top
>= 1);
3512 /* 'with' target must be created first, in case we run out of memory */
3513 /* XXX: refactor out? */
3515 if (a
& DUK_BC_TRYCATCH_FLAG_WITH_BINDING
) {
3516 DUK_DDD(DUK_DDDPRINT("need to initialize a with binding object"));
3518 if (act
->lex_env
== NULL
) {
3519 DUK_ASSERT(act
->var_env
== NULL
);
3520 DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
3522 /* must relookup act in case of side effects */
3523 duk_js_init_activation_environment_records_delayed(thr
, act
);
3524 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3526 DUK_ASSERT(act
->lex_env
!= NULL
);
3527 DUK_ASSERT(act
->var_env
!= NULL
);
3529 (void) duk_push_object_helper(ctx
,
3530 DUK_HOBJECT_FLAG_EXTENSIBLE
|
3531 DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV
),
3532 -1); /* no prototype, updated below */
3534 duk_push_tval(ctx
, DUK__REGP(bc
));
3535 duk_to_object(ctx
, -1);
3538 /* [ ... env target ] */
3539 /* [ ... env target target ] */
3541 duk_xdef_prop_stridx(thr
, -3, DUK_STRIDX_INT_TARGET
, DUK_PROPDESC_FLAGS_NONE
);
3542 duk_xdef_prop_stridx(thr
, -2, DUK_STRIDX_INT_THIS
, DUK_PROPDESC_FLAGS_NONE
); /* always provideThis=true */
3546 DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iT",
3547 (duk_tval
*) duk_get_tval(ctx
, -1)));
3550 /* allocate catcher and populate it (should be atomic) */
3552 duk_hthread_catchstack_grow(thr
);
3553 cat
= thr
->catchstack
+ thr
->catchstack_top
;
3554 DUK_ASSERT(thr
->catchstack_top
+ 1 <= thr
->catchstack_size
);
3555 thr
->catchstack_top
++;
3557 cat
->flags
= DUK_CAT_TYPE_TCF
;
3558 cat
->h_varname
= NULL
;
3560 if (a
& DUK_BC_TRYCATCH_FLAG_HAVE_CATCH
) {
3561 cat
->flags
|= DUK_CAT_FLAG_CATCH_ENABLED
;
3563 if (a
& DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY
) {
3564 cat
->flags
|= DUK_CAT_FLAG_FINALLY_ENABLED
;
3566 if (a
& DUK_BC_TRYCATCH_FLAG_CATCH_BINDING
) {
3567 DUK_DDD(DUK_DDDPRINT("catch binding flag set to catcher"));
3568 cat
->flags
|= DUK_CAT_FLAG_CATCH_BINDING_ENABLED
;
3569 tv1
= DUK__REGP(bc
);
3570 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
3572 /* borrowed reference; although 'tv1' comes from a register,
3573 * its value was loaded using LDCONST so the constant will
3574 * also exist and be reachable.
3576 cat
->h_varname
= DUK_TVAL_GET_STRING(tv1
);
3577 } else if (a
& DUK_BC_TRYCATCH_FLAG_WITH_BINDING
) {
3578 /* env created above to stack top */
3579 duk_hobject
*new_env
;
3581 DUK_DDD(DUK_DDDPRINT("lexenv active flag set to catcher"));
3582 cat
->flags
|= DUK_CAT_FLAG_LEXENV_ACTIVE
;
3584 DUK_DDD(DUK_DDDPRINT("activating object env: %!iT",
3585 (duk_tval
*) duk_get_tval(ctx
, -1)));
3586 DUK_ASSERT(act
->lex_env
!= NULL
);
3587 new_env
= duk_get_hobject(ctx
, -1);
3588 DUK_ASSERT(new_env
!= NULL
);
3590 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup (side effects) */
3591 DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr
, new_env
, act
->lex_env
);
3593 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup (side effects) */
3594 act
->lex_env
= new_env
;
3595 DUK_HOBJECT_INCREF(thr
, new_env
);
3601 /* Registers 'bc' and 'bc + 1' are written in longjmp handling
3602 * and if their previous values (which are temporaries) become
3603 * unreachable -and- have a finalizer, there'll be a function
3604 * call during error handling which is not supported now (GH-287).
3605 * Ensure that both 'bc' and 'bc + 1' have primitive values to
3606 * guarantee no finalizer calls in error handling. Scrubbing also
3607 * ensures finalizers for the previous values run here rather than
3608 * later. Error handling related values are also written to 'bc'
3609 * and 'bc + 1' but those values never become unreachable during
3610 * error handling, so there's no side effect problem even if the
3611 * error value has a finalizer.
3613 duk_to_undefined(ctx
, bc
);
3614 duk_to_undefined(ctx
, bc
+ 1);
3616 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1; /* relookup (side effects) */
3617 cat
->callstack_index
= thr
->callstack_top
- 1;
3618 cat
->pc_base
= (duk_instr_t
*) curr_pc
; /* pre-incremented, points to first jump slot */
3619 cat
->idx_base
= (duk_size_t
) (thr
->valstack_bottom
- thr
->valstack
) + bc
;
3621 DUK_DDD(DUK_DDDPRINT("TRYCATCH catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
3622 "idx_base=%ld, h_varname=%!O",
3623 (unsigned long) cat
->flags
, (long) cat
->callstack_index
,
3624 (long) cat
->pc_base
, (long) cat
->idx_base
, (duk_heaphdr
*) cat
->h_varname
));
3626 curr_pc
+= 2; /* skip jump slots */
3630 /* Pre/post inc/dec for register variables, important for loops. */
3631 case DUK_OP_PREINCR
:
3632 case DUK_OP_PREDECR
:
3633 case DUK_OP_POSTINCR
:
3634 case DUK_OP_POSTDECR
: {
3635 duk_context
*ctx
= (duk_context
*) thr
;
3636 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3637 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3638 duk_tval
*tv1
, *tv2
;
3640 duk_double_t x
, y
, z
;
3642 /* Two lowest bits of opcode are used to distinguish
3643 * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3645 DUK_ASSERT((DUK_OP_PREINCR
& 0x03) == 0x00);
3646 DUK_ASSERT((DUK_OP_PREDECR
& 0x03) == 0x01);
3647 DUK_ASSERT((DUK_OP_POSTINCR
& 0x03) == 0x02);
3648 DUK_ASSERT((DUK_OP_POSTDECR
& 0x03) == 0x03);
3650 tv1
= DUK__REGP(bc
);
3651 #if defined(DUK_USE_FASTINT)
3652 if (DUK_TVAL_IS_FASTINT(tv1
)) {
3653 duk_int64_t x_fi
, y_fi
, z_fi
;
3654 x_fi
= DUK_TVAL_GET_FASTINT(tv1
);
3655 if (ins
& DUK_ENC_OP(0x01)) {
3656 if (x_fi
== DUK_FASTINT_MIN
) {
3661 if (x_fi
== DUK_FASTINT_MAX
) {
3667 DUK_TVAL_SET_FASTINT(tv1
, y_fi
); /* no need for refcount update */
3670 DUK_TVAL_SET_TVAL(&tv_tmp
, tv2
);
3671 z_fi
= (ins
& DUK_ENC_OP(0x02)) ? x_fi
: y_fi
;
3672 DUK_TVAL_SET_FASTINT(tv2
, z_fi
); /* no need for incref */
3673 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
3678 if (DUK_TVAL_IS_NUMBER(tv1
)) {
3679 /* Fast path for the case where the register
3680 * is a number (e.g. loop counter).
3683 x
= DUK_TVAL_GET_NUMBER(tv1
);
3684 if (ins
& DUK_ENC_OP(0x01)) {
3690 DUK_TVAL_SET_NUMBER(tv1
, y
); /* no need for refcount update */
3692 x
= duk_to_number(ctx
, bc
);
3694 if (ins
& DUK_ENC_OP(0x01)) {
3700 duk_push_number(ctx
, y
);
3701 duk_replace(ctx
, bc
);
3705 DUK_TVAL_SET_TVAL(&tv_tmp
, tv2
);
3706 z
= (ins
& DUK_ENC_OP(0x02)) ? x
: y
;
3707 DUK_TVAL_SET_NUMBER(tv2
, z
); /* no need for incref */
3708 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
3712 /* Preinc/predec for var-by-name, slow path. */
3713 case DUK_OP_PREINCV
:
3714 case DUK_OP_PREDECV
:
3715 case DUK_OP_POSTINCV
:
3716 case DUK_OP_POSTDECV
: {
3717 duk_context
*ctx
= (duk_context
*) thr
;
3718 duk_activation
*act
;
3719 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3720 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3725 /* Two lowest bits of opcode are used to distinguish
3726 * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3728 DUK_ASSERT((DUK_OP_PREINCV
& 0x03) == 0x00);
3729 DUK_ASSERT((DUK_OP_PREDECV
& 0x03) == 0x01);
3730 DUK_ASSERT((DUK_OP_POSTINCV
& 0x03) == 0x02);
3731 DUK_ASSERT((DUK_OP_POSTDECV
& 0x03) == 0x03);
3733 tv1
= DUK__CONSTP(bc
);
3734 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
3735 name
= DUK_TVAL_GET_STRING(tv1
);
3736 DUK_ASSERT(name
!= NULL
);
3737 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3738 (void) duk_js_getvar_activation(thr
, act
, name
, 1 /*throw*/); /* -> [... val this] */
3740 /* XXX: fastint fast path would be very useful here */
3742 x
= duk_to_number(ctx
, -2);
3744 if (ins
& DUK_ENC_OP(0x01)) {
3750 duk_push_number(ctx
, y
);
3751 tv1
= duk_get_tval(ctx
, -1);
3752 DUK_ASSERT(tv1
!= NULL
);
3753 duk_js_putvar_activation(thr
, act
, name
, tv1
, DUK__STRICT());
3756 duk_push_number(ctx
, (ins
& DUK_ENC_OP(0x02)) ? x
: y
);
3757 duk_replace(ctx
, (duk_idx_t
) a
);
3761 /* Preinc/predec for object properties. */
3762 case DUK_OP_PREINCP
:
3763 case DUK_OP_PREDECP
:
3764 case DUK_OP_POSTINCP
:
3765 case DUK_OP_POSTDECP
: {
3766 duk_context
*ctx
= (duk_context
*) thr
;
3767 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3768 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3769 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3777 * B -> object reg/const (may be const e.g. in "'foo'[1]")
3778 * C -> key reg/const
3781 /* Two lowest bits of opcode are used to distinguish
3782 * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3784 DUK_ASSERT((DUK_OP_PREINCP
& 0x03) == 0x00);
3785 DUK_ASSERT((DUK_OP_PREDECP
& 0x03) == 0x01);
3786 DUK_ASSERT((DUK_OP_POSTINCP
& 0x03) == 0x02);
3787 DUK_ASSERT((DUK_OP_POSTDECP
& 0x03) == 0x03);
3789 tv_obj
= DUK__REGCONSTP(b
);
3790 tv_key
= DUK__REGCONSTP(c
);
3791 rc
= duk_hobject_getprop(thr
, tv_obj
, tv_key
); /* -> [val] */
3792 DUK_UNREF(rc
); /* ignore */
3793 tv_obj
= NULL
; /* invalidated */
3794 tv_key
= NULL
; /* invalidated */
3796 x
= duk_to_number(ctx
, -1);
3798 if (ins
& DUK_ENC_OP(0x01)) {
3804 duk_push_number(ctx
, y
);
3805 tv_val
= duk_get_tval(ctx
, -1);
3806 DUK_ASSERT(tv_val
!= NULL
);
3807 tv_obj
= DUK__REGCONSTP(b
);
3808 tv_key
= DUK__REGCONSTP(c
);
3809 rc
= duk_hobject_putprop(thr
, tv_obj
, tv_key
, tv_val
, DUK__STRICT());
3810 DUK_UNREF(rc
); /* ignore */
3811 tv_obj
= NULL
; /* invalidated */
3812 tv_key
= NULL
; /* invalidated */
3815 duk_push_number(ctx
, (ins
& DUK_ENC_OP(0x02)) ? x
: y
);
3816 duk_replace(ctx
, (duk_idx_t
) a
);
3820 case DUK_OP_EXTRA
: {
3821 /* XXX: shared decoding of 'b' and 'c'? */
3823 duk_small_uint_fast_t extraop
= DUK_DEC_A(ins
);
3824 switch ((int) extraop
) {
3825 /* XXX: switch cast? */
3827 case DUK_EXTRAOP_NOP
: {
3832 case DUK_EXTRAOP_INVALID
: {
3833 DUK_ERROR(thr
, DUK_ERR_INTERNAL_ERROR
, "INVALID opcode (%ld)", (long) DUK_DEC_BC(ins
));
3837 case DUK_EXTRAOP_LDTHIS
: {
3838 /* Note: 'this' may be bound to any value, not just an object */
3839 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3841 duk_tval
*tv1
, *tv2
;
3843 tv1
= DUK__REGP(bc
);
3844 tv2
= thr
->valstack_bottom
- 1; /* 'this binding' is just under bottom */
3845 DUK_ASSERT(tv2
>= thr
->valstack
);
3847 DUK_DDD(DUK_DDDPRINT("LDTHIS: %!T to r%ld", (duk_tval
*) tv2
, (long) bc
));
3849 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
3850 DUK_TVAL_SET_TVAL(tv1
, tv2
);
3851 DUK_TVAL_INCREF(thr
, tv1
);
3852 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
3856 case DUK_EXTRAOP_LDUNDEF
: {
3857 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3861 tv1
= DUK__REGP(bc
);
3862 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
3863 DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1
);
3864 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
3868 case DUK_EXTRAOP_LDNULL
: {
3869 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3873 tv1
= DUK__REGP(bc
);
3874 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
3875 DUK_TVAL_SET_NULL(tv1
);
3876 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
3880 case DUK_EXTRAOP_LDTRUE
:
3881 case DUK_EXTRAOP_LDFALSE
: {
3882 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3885 duk_small_uint_fast_t bval
= (extraop
== DUK_EXTRAOP_LDTRUE
? 1 : 0);
3887 tv1
= DUK__REGP(bc
);
3888 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
3889 DUK_TVAL_SET_BOOLEAN(tv1
, bval
);
3890 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
3894 case DUK_EXTRAOP_NEWOBJ
: {
3895 duk_context
*ctx
= (duk_context
*) thr
;
3896 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3898 duk_push_object(ctx
);
3899 duk_replace(ctx
, (duk_idx_t
) b
);
3903 case DUK_EXTRAOP_NEWARR
: {
3904 duk_context
*ctx
= (duk_context
*) thr
;
3905 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3907 duk_push_array(ctx
);
3908 duk_replace(ctx
, (duk_idx_t
) b
);
3912 case DUK_EXTRAOP_SETALEN
: {
3913 duk_small_uint_fast_t b
;
3914 duk_small_uint_fast_t c
;
3919 b
= DUK_DEC_B(ins
); tv1
= DUK__REGP(b
);
3920 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1
));
3921 h
= DUK_TVAL_GET_OBJECT(tv1
);
3923 c
= DUK_DEC_C(ins
); tv1
= DUK__REGP(c
);
3924 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
3925 len
= (duk_uint32_t
) DUK_TVAL_GET_NUMBER(tv1
);
3927 duk_hobject_set_length(thr
, h
, len
);
3932 case DUK_EXTRAOP_TYPEOF
: {
3933 duk_context
*ctx
= (duk_context
*) thr
;
3934 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3935 duk_push_hstring(ctx
, duk_js_typeof(thr
, DUK__REGP(bc
)));
3936 duk_replace(ctx
, (duk_idx_t
) bc
);
3940 case DUK_EXTRAOP_TYPEOFID
: {
3941 duk_context
*ctx
= (duk_context
*) thr
;
3942 duk_activation
*act
;
3943 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3944 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3948 /* B -> target register
3949 * C -> constant index of identifier name
3952 tv
= DUK__REGCONSTP(c
); /* XXX: this could be a DUK__CONSTP instead */
3953 DUK_ASSERT(DUK_TVAL_IS_STRING(tv
));
3954 name
= DUK_TVAL_GET_STRING(tv
);
3955 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3956 if (duk_js_getvar_activation(thr
, act
, name
, 0 /*throw*/)) {
3957 /* -> [... val this] */
3958 tv
= duk_get_tval(ctx
, -2);
3959 duk_push_hstring(ctx
, duk_js_typeof(thr
, tv
));
3960 duk_replace(ctx
, (duk_idx_t
) b
);
3963 /* unresolvable, no stack changes */
3964 duk_push_hstring_stridx(ctx
, DUK_STRIDX_LC_UNDEFINED
);
3965 duk_replace(ctx
, (duk_idx_t
) b
);
3971 case DUK_EXTRAOP_INITENUM
: {
3972 duk_context
*ctx
= (duk_context
*) thr
;
3973 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3974 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3977 * Enumeration semantics come from for-in statement, E5 Section 12.6.4.
3978 * If called with 'null' or 'undefined', this opcode returns 'null' as
3979 * the enumerator, which is special cased in NEXTENUM. This simplifies
3983 /* B -> register for writing enumerator object
3984 * C -> value to be enumerated (register)
3987 if (duk_is_null_or_undefined(ctx
, (duk_idx_t
) c
)) {
3989 duk_replace(ctx
, (duk_idx_t
) b
);
3991 duk_dup(ctx
, (duk_idx_t
) c
);
3992 duk_to_object(ctx
, -1);
3993 duk_hobject_enumerator_create(ctx
, 0 /*enum_flags*/); /* [ ... val ] --> [ ... enum ] */
3994 duk_replace(ctx
, (duk_idx_t
) b
);
3999 case DUK_EXTRAOP_NEXTENUM
: {
4000 duk_context
*ctx
= (duk_context
*) thr
;
4001 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4002 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
4005 * NEXTENUM checks whether the enumerator still has unenumerated
4006 * keys. If so, the next key is loaded to the target register
4007 * and the next instruction is skipped. Otherwise the next instruction
4008 * will be executed, jumping out of the enumeration loop.
4011 /* B -> target register for next key
4012 * C -> enum register
4015 DUK_DDD(DUK_DDDPRINT("NEXTENUM: b->%!T, c->%!T",
4016 (duk_tval
*) duk_get_tval(ctx
, (duk_idx_t
) b
),
4017 (duk_tval
*) duk_get_tval(ctx
, (duk_idx_t
) c
)));
4019 if (duk_is_object(ctx
, (duk_idx_t
) c
)) {
4020 /* XXX: assert 'c' is an enumerator */
4021 duk_dup(ctx
, (duk_idx_t
) c
);
4022 if (duk_hobject_enumerator_next(ctx
, 0 /*get_value*/)) {
4023 /* [ ... enum ] -> [ ... next_key ] */
4024 DUK_DDD(DUK_DDDPRINT("enum active, next key is %!T, skip jump slot ",
4025 (duk_tval
*) duk_get_tval(ctx
, -1)));
4028 /* [ ... enum ] -> [ ... ] */
4029 DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot"));
4030 duk_push_undefined(ctx
);
4032 duk_replace(ctx
, (duk_idx_t
) b
);
4034 /* 'null' enumerator case -> behave as with an empty enumerator */
4035 DUK_ASSERT(duk_is_null(ctx
, (duk_idx_t
) c
));
4036 DUK_DDD(DUK_DDDPRINT("enum is null, execute jump slot"));
4041 case DUK_EXTRAOP_INITSET
:
4042 case DUK_EXTRAOP_INITSETI
:
4043 case DUK_EXTRAOP_INITGET
:
4044 case DUK_EXTRAOP_INITGETI
: {
4045 duk_context
*ctx
= (duk_context
*) thr
;
4046 duk_bool_t is_set
= (extraop
== DUK_EXTRAOP_INITSET
|| extraop
== DUK_EXTRAOP_INITSETI
);
4047 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4048 duk_uint_fast_t idx
;
4050 /* B -> object register
4051 * C -> C+0 contains key, C+1 closure (value)
4055 * INITSET/INITGET are only used to initialize object literal keys.
4056 * The compiler ensures that there cannot be a previous data property
4057 * of the same name. It also ensures that setter and getter can only
4058 * be initialized once (or not at all).
4061 idx
= (duk_uint_fast_t
) DUK_DEC_C(ins
);
4062 if (extraop
== DUK_EXTRAOP_INITSETI
|| extraop
== DUK_EXTRAOP_INITGETI
) {
4063 duk_tval
*tv_ind
= DUK__REGP(idx
);
4064 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
4065 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
4068 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
4069 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
4070 /* XXX: use duk_is_valid_index() instead? */
4071 /* XXX: improve check; check against nregs, not against top */
4072 DUK__INTERNAL_ERROR("INITSET/INITGET out of bounds");
4076 /* XXX: this is now a very unoptimal implementation -- this can be
4077 * made very simple by direct manipulation of the object internals,
4078 * given the guarantees above.
4081 duk_push_hobject_bidx(ctx
, DUK_BIDX_OBJECT_CONSTRUCTOR
);
4082 duk_get_prop_stridx(ctx
, -1, DUK_STRIDX_DEFINE_PROPERTY
);
4083 duk_push_undefined(ctx
);
4084 duk_dup(ctx
, (duk_idx_t
) b
);
4085 duk_dup(ctx
, (duk_idx_t
) (idx
+ 0));
4086 duk_push_object(ctx
); /* -> [ Object defineProperty undefined obj key desc ] */
4089 duk_put_prop_stridx(ctx
, -2, DUK_STRIDX_ENUMERABLE
);
4091 duk_put_prop_stridx(ctx
, -2, DUK_STRIDX_CONFIGURABLE
);
4092 duk_dup(ctx
, (duk_idx_t
) (idx
+ 1));
4093 duk_put_prop_stridx(ctx
, -2, (is_set
? DUK_STRIDX_SET
: DUK_STRIDX_GET
));
4095 DUK_DDD(DUK_DDDPRINT("INITGET/INITSET: obj=%!T, key=%!T, desc=%!T",
4096 (duk_tval
*) duk_get_tval(ctx
, -3),
4097 (duk_tval
*) duk_get_tval(ctx
, -2),
4098 (duk_tval
*) duk_get_tval(ctx
, -1)));
4100 duk_call_method(ctx
, 3); /* -> [ Object res ] */
4103 DUK_DDD(DUK_DDDPRINT("INITGET/INITSET AFTER: obj=%!T",
4104 (duk_tval
*) duk_get_tval(ctx
, (duk_idx_t
) b
)));
4108 case DUK_EXTRAOP_ENDTRY
: {
4113 DUK_ASSERT(thr
->catchstack_top
>= 1);
4114 DUK_ASSERT(thr
->callstack_top
>= 1);
4115 DUK_ASSERT(thr
->catchstack
[thr
->catchstack_top
- 1].callstack_index
== thr
->callstack_top
- 1);
4117 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4119 DUK_DDD(DUK_DDDPRINT("ENDTRY: clearing catch active flag (regardless of whether it was set or not)"));
4120 DUK_CAT_CLEAR_CATCH_ENABLED(cat
);
4122 if (DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
4123 DUK_DDD(DUK_DDDPRINT("ENDTRY: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
4125 tv1
= thr
->valstack
+ cat
->idx_base
;
4126 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4127 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
4128 DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1
);
4129 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
4132 tv1
= thr
->valstack
+ cat
->idx_base
+ 1;
4133 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4134 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
4135 DUK_TVAL_SET_NUMBER(tv1
, (duk_double_t
) DUK_LJ_TYPE_NORMAL
); /* XXX: set int */
4136 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
4139 DUK_CAT_CLEAR_FINALLY_ENABLED(cat
);
4141 DUK_DDD(DUK_DDDPRINT("ENDTRY: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
4142 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4143 /* no need to unwind callstack */
4146 curr_pc
= cat
->pc_base
+ 1;
4150 case DUK_EXTRAOP_ENDCATCH
: {
4151 duk_activation
*act
;
4156 DUK_ASSERT(thr
->catchstack_top
>= 1);
4157 DUK_ASSERT(thr
->callstack_top
>= 1);
4158 DUK_ASSERT(thr
->catchstack
[thr
->catchstack_top
- 1].callstack_index
== thr
->callstack_top
- 1);
4160 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4161 DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat
)); /* cleared before entering catch part */
4163 act
= thr
->callstack
+ thr
->callstack_top
- 1;
4165 if (DUK_CAT_HAS_LEXENV_ACTIVE(cat
)) {
4166 duk_hobject
*prev_env
;
4168 /* 'with' binding has no catch clause, so can't be here unless a normal try-catch */
4169 DUK_ASSERT(DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat
));
4170 DUK_ASSERT(act
->lex_env
!= NULL
);
4172 DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment"));
4174 prev_env
= act
->lex_env
;
4175 DUK_ASSERT(prev_env
!= NULL
);
4176 act
->lex_env
= DUK_HOBJECT_GET_PROTOTYPE(thr
->heap
, prev_env
);
4177 DUK_CAT_CLEAR_LEXENV_ACTIVE(cat
);
4178 DUK_HOBJECT_DECREF(thr
, prev_env
); /* side effects */
4181 if (DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
4182 DUK_DDD(DUK_DDDPRINT("ENDCATCH: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
4184 tv1
= thr
->valstack
+ cat
->idx_base
;
4185 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4186 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
4187 DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1
);
4188 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
4191 tv1
= thr
->valstack
+ cat
->idx_base
+ 1;
4192 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4193 DUK_TVAL_SET_TVAL(&tv_tmp
, tv1
);
4194 DUK_TVAL_SET_NUMBER(tv1
, (duk_double_t
) DUK_LJ_TYPE_NORMAL
); /* XXX: set int */
4195 DUK_TVAL_DECREF(thr
, &tv_tmp
); /* side effects */
4198 DUK_CAT_CLEAR_FINALLY_ENABLED(cat
);
4200 DUK_DDD(DUK_DDDPRINT("ENDCATCH: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
4201 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4202 /* no need to unwind callstack */
4205 curr_pc
= cat
->pc_base
+ 1;
4209 case DUK_EXTRAOP_ENDFIN
: {
4210 duk_context
*ctx
= (duk_context
*) thr
;
4213 duk_small_uint_fast_t cont_type
;
4215 DUK_ASSERT(thr
->catchstack_top
>= 1);
4216 DUK_ASSERT(thr
->callstack_top
>= 1);
4217 DUK_ASSERT(thr
->catchstack
[thr
->catchstack_top
- 1].callstack_index
== thr
->callstack_top
- 1);
4219 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4221 /* CATCH flag may be enabled or disabled here; it may be enabled if
4222 * the statement has a catch block but the try block does not throw
4225 DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat
)); /* cleared before entering finally */
4226 /* XXX: assert idx_base */
4228 DUK_DDD(DUK_DDDPRINT("ENDFIN: completion value=%!T, type=%!T",
4229 (duk_tval
*) (thr
->valstack
+ cat
->idx_base
+ 0),
4230 (duk_tval
*) (thr
->valstack
+ cat
->idx_base
+ 1)));
4232 tv1
= thr
->valstack
+ cat
->idx_base
+ 1; /* type */
4233 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
4234 cont_type
= (duk_small_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv1
);
4236 if (cont_type
== DUK_LJ_TYPE_NORMAL
) {
4237 DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'normal' (non-abrupt) completion -> "
4238 "dismantle catcher, resume execution after ENDFIN"));
4239 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4240 /* no need to unwind callstack */
4242 DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> "
4243 "dismantle catcher, re-throw error",
4246 duk_push_tval(ctx
, thr
->valstack
+ cat
->idx_base
);
4248 /* XXX: assert lj type valid */
4249 duk_err_setup_heap_ljstate(thr
, (duk_small_int_t
) cont_type
);
4251 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* always in executor */
4252 DUK__SYNC_AND_NULL_CURR_PC();
4253 duk_err_longjmp(thr
);
4257 /* continue execution after ENDFIN */
4261 case DUK_EXTRAOP_THROW
: {
4262 duk_context
*ctx
= (duk_context
*) thr
;
4263 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4265 /* Note: errors are augmented when they are created, not
4266 * when they are thrown. So, don't augment here, it would
4267 * break re-throwing for instance.
4270 /* Sync so that augmentation sees up-to-date activations, NULL
4271 * thr->ptr_curr_pc so that it's not used if side effects occur
4272 * in augmentation or longjmp handling.
4274 DUK__SYNC_AND_NULL_CURR_PC();
4276 duk_dup(ctx
, (duk_idx_t
) bc
);
4277 DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (before throw augment)",
4278 (duk_tval
*) duk_get_tval(ctx
, -1)));
4279 #if defined(DUK_USE_AUGMENT_ERROR_THROW)
4280 duk_err_augment_error_throw(thr
);
4281 DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (after throw augment)",
4282 (duk_tval
*) duk_get_tval(ctx
, -1)));
4285 duk_err_setup_heap_ljstate(thr
, DUK_LJ_TYPE_THROW
);
4287 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* always in executor */
4288 duk_err_longjmp(thr
);
4293 case DUK_EXTRAOP_INVLHS
: {
4294 DUK_ERROR(thr
, DUK_ERR_REFERENCE_ERROR
, "invalid lvalue");
4300 case DUK_EXTRAOP_UNM
:
4301 case DUK_EXTRAOP_UNP
: {
4302 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4303 duk__vm_arith_unary_op(thr
, DUK__REGP(bc
), bc
, extraop
);
4307 case DUK_EXTRAOP_DEBUGGER
: {
4308 /* Opcode only emitted by compiler when debugger
4309 * support is enabled. Ignore it silently without
4310 * debugger support, in case it has been loaded
4311 * from precompiled bytecode.
4313 #if defined(DUK_USE_DEBUGGER_SUPPORT)
4314 DUK_D(DUK_DPRINT("DEBUGGER statement encountered, halt execution"));
4315 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
)) {
4316 DUK_HEAP_SET_PAUSED(thr
->heap
);
4317 DUK__SYNC_CURR_PC();
4318 goto restart_execution
;
4321 DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support"));
4326 case DUK_EXTRAOP_BREAK
: {
4327 duk_context
*ctx
= (duk_context
*) thr
;
4328 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4330 /* always the "slow break" variant (longjmp'ing); a "fast break" is
4331 * simply an DUK_OP_JUMP.
4334 DUK_DDD(DUK_DDDPRINT("BREAK: %ld", (long) bc
));
4336 duk_push_uint(ctx
, (duk_uint_t
) bc
);
4337 duk_err_setup_heap_ljstate(thr
, DUK_LJ_TYPE_BREAK
);
4339 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* always in executor */
4340 DUK__SYNC_AND_NULL_CURR_PC();
4341 duk_err_longjmp(thr
);
4346 case DUK_EXTRAOP_CONTINUE
: {
4347 duk_context
*ctx
= (duk_context
*) thr
;
4348 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4350 /* always the "slow continue" variant (longjmp'ing); a "fast continue" is
4351 * simply an DUK_OP_JUMP.
4354 DUK_DDD(DUK_DDDPRINT("CONTINUE: %ld", (long) bc
));
4356 duk_push_uint(ctx
, (duk_uint_t
) bc
);
4357 duk_err_setup_heap_ljstate(thr
, DUK_LJ_TYPE_CONTINUE
);
4359 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* always in executor */
4360 DUK__SYNC_AND_NULL_CURR_PC();
4361 duk_err_longjmp(thr
);
4366 case DUK_EXTRAOP_BNOT
: {
4367 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4369 duk__vm_bitwise_not(thr
, DUK__REGP(bc
), bc
);
4373 case DUK_EXTRAOP_LNOT
: {
4374 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4377 tv1
= DUK__REGP(bc
);
4378 duk__vm_logical_not(thr
, tv1
, tv1
);
4382 case DUK_EXTRAOP_INSTOF
: {
4383 duk_context
*ctx
= (duk_context
*) thr
;
4384 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4385 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
4388 tmp
= duk_js_instanceof(thr
, DUK__REGP(b
), DUK__REGCONSTP(c
));
4389 duk_push_boolean(ctx
, tmp
);
4390 duk_replace(ctx
, (duk_idx_t
) b
);
4394 case DUK_EXTRAOP_IN
: {
4395 duk_context
*ctx
= (duk_context
*) thr
;
4396 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4397 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
4400 tmp
= duk_js_in(thr
, DUK__REGP(b
), DUK__REGCONSTP(c
));
4401 duk_push_boolean(ctx
, tmp
);
4402 duk_replace(ctx
, (duk_idx_t
) b
);
4406 case DUK_EXTRAOP_LABEL
: {
4408 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4410 /* allocate catcher and populate it (should be atomic) */
4412 duk_hthread_catchstack_grow(thr
);
4413 cat
= thr
->catchstack
+ thr
->catchstack_top
;
4414 thr
->catchstack_top
++;
4416 cat
->flags
= DUK_CAT_TYPE_LABEL
| (bc
<< DUK_CAT_LABEL_SHIFT
);
4417 cat
->callstack_index
= thr
->callstack_top
- 1;
4418 cat
->pc_base
= (duk_instr_t
*) curr_pc
; /* pre-incremented, points to first jump slot */
4419 cat
->idx_base
= 0; /* unused for label */
4420 cat
->h_varname
= NULL
;
4422 DUK_DDD(DUK_DDDPRINT("LABEL catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
4423 "idx_base=%ld, h_varname=%!O, label_id=%ld",
4424 (long) cat
->flags
, (long) cat
->callstack_index
, (long) cat
->pc_base
,
4425 (long) cat
->idx_base
, (duk_heaphdr
*) cat
->h_varname
, (long) DUK_CAT_GET_LABEL(cat
)));
4427 curr_pc
+= 2; /* skip jump slots */
4431 case DUK_EXTRAOP_ENDLABEL
: {
4433 #if defined(DUK_USE_DDDPRINT) || defined(DUK_USE_ASSERTIONS)
4434 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4436 #if defined(DUK_USE_DDDPRINT)
4437 DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc
));
4440 DUK_ASSERT(thr
->catchstack_top
>= 1);
4442 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4444 DUK_ASSERT(DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_LABEL
);
4445 DUK_ASSERT((duk_uint_fast_t
) DUK_CAT_GET_LABEL(cat
) == bc
);
4447 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4448 /* no need to unwind callstack */
4453 DUK__INTERNAL_ERROR("invalid extra opcode");
4462 /* this should never be possible, because the switch-case is
4465 DUK__INTERNAL_ERROR("invalid opcode");
4473 #ifndef DUK_USE_VERBOSE_EXECUTOR_ERRORS
4475 DUK_ERROR(thr
, DUK_ERR_INTERNAL_ERROR
, "internal error in bytecode executor");
4479 #undef DUK__INTERNAL_ERROR
4480 #undef DUK__SYNC_CURR_PC
4481 #undef DUK__SYNC_AND_NULL_CURR_PC