2 * Ecmascript bytecode executor.
5 #include "duk_internal.h"
11 DUK_LOCAL_DECL
void duk__js_execute_bytecode_inner(duk_hthread
*entry_thread
, duk_size_t entry_callstack_top
);
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. It's still
24 * preferable to work without making a copy but that's not always
28 DUK_LOCAL duk_double_t
duk__compute_mod(duk_double_t d1
, duk_double_t d2
) {
30 * Ecmascript modulus ('%') does not match IEEE 754 "remainder"
31 * operation (implemented by remainder() in C99) but does seem
32 * to match ANSI C fmod().
34 * Compare E5 Section 11.5.3 and "man fmod".
37 return (duk_double_t
) DUK_FMOD((double) d1
, (double) d2
);
40 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
) {
42 * Addition operator is different from other arithmetic
43 * operations in that it also provides string concatenation.
44 * Hence it is implemented separately.
46 * There is a fast path for number addition. Other cases go
47 * through potentially multiple coercions as described in the
48 * E5 specification. It may be possible to reduce the number
49 * of coercions, but this must be done carefully to preserve
50 * the exact semantics.
54 * Custom types also have special behavior implemented here.
57 duk_context
*ctx
= (duk_context
*) thr
;
60 DUK_ASSERT(thr
!= NULL
);
61 DUK_ASSERT(ctx
!= NULL
);
62 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
63 DUK_ASSERT(tv_y
!= NULL
); /* may be reg or const */
64 DUK_ASSERT_DISABLE(idx_z
>= 0); /* unsigned */
65 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
71 #if defined(DUK_USE_FASTINT)
72 if (DUK_TVAL_IS_FASTINT(tv_x
) && DUK_TVAL_IS_FASTINT(tv_y
)) {
73 duk_int64_t v1
, v2
, v3
;
77 /* Input values are signed 48-bit so we can detect overflow
78 * reliably from high bits or just a comparison.
81 v1
= DUK_TVAL_GET_FASTINT(tv_x
);
82 v2
= DUK_TVAL_GET_FASTINT(tv_y
);
84 v3_hi
= (duk_int32_t
) (v3
>> 32);
85 if (DUK_LIKELY(v3_hi
>= -0x8000LL
&& v3_hi
<= 0x7fffLL
)) {
86 tv_z
= thr
->valstack_bottom
+ idx_z
;
87 DUK_TVAL_SET_FASTINT_UPDREF(thr
, tv_z
, v3
); /* side effects */
90 /* overflow, fall through */
94 #endif /* DUK_USE_FASTINT */
96 if (DUK_TVAL_IS_NUMBER(tv_x
) && DUK_TVAL_IS_NUMBER(tv_y
)) {
99 du
.d
= DUK_TVAL_GET_NUMBER(tv_x
) + DUK_TVAL_GET_NUMBER(tv_y
);
100 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
);
101 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
103 tv_z
= thr
->valstack_bottom
+ idx_z
;
104 DUK_TVAL_SET_NUMBER_UPDREF(thr
, tv_z
, du
.d
); /* side effects */
109 * Slow path: potentially requires function calls for coercion
112 duk_push_tval(ctx
, tv_x
);
113 duk_push_tval(ctx
, tv_y
);
114 duk_to_primitive(ctx
, -2, DUK_HINT_NONE
); /* side effects -> don't use tv_x, tv_y after */
115 duk_to_primitive(ctx
, -1, DUK_HINT_NONE
);
117 /* As a first approximation, buffer values are coerced to strings
118 * for addition. This means that adding two buffers currently
119 * results in a string.
121 if (duk_check_type_mask(ctx
, -2, DUK_TYPE_MASK_STRING
| DUK_TYPE_MASK_BUFFER
) ||
122 duk_check_type_mask(ctx
, -1, DUK_TYPE_MASK_STRING
| DUK_TYPE_MASK_BUFFER
)) {
123 duk_to_string(ctx
, -2);
124 duk_to_string(ctx
, -1);
125 duk_concat(ctx
, 2); /* [... s1 s2] -> [... s1+s2] */
126 duk_replace(ctx
, (duk_idx_t
) idx_z
); /* side effects */
130 d1
= duk_to_number(ctx
, -2);
131 d2
= duk_to_number(ctx
, -1);
132 DUK_ASSERT(duk_is_number(ctx
, -2));
133 DUK_ASSERT(duk_is_number(ctx
, -1));
134 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1
);
135 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2
);
138 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
);
139 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
142 duk_push_number(ctx
, du
.d
);
143 duk_replace(ctx
, (duk_idx_t
) idx_z
); /* side effects */
147 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
) {
149 * Arithmetic operations other than '+' have number-only semantics
150 * and are implemented here. The separate switch-case here means a
151 * "double dispatch" of the arithmetic opcode, but saves code space.
153 * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
156 duk_context
*ctx
= (duk_context
*) thr
;
161 DUK_ASSERT(thr
!= NULL
);
162 DUK_ASSERT(ctx
!= NULL
);
163 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
164 DUK_ASSERT(tv_y
!= NULL
); /* may be reg or const */
165 DUK_ASSERT_DISABLE(idx_z
>= 0); /* unsigned */
166 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
168 #if defined(DUK_USE_FASTINT)
169 if (DUK_TVAL_IS_FASTINT(tv_x
) && DUK_TVAL_IS_FASTINT(tv_y
)) {
170 duk_int64_t v1
, v2
, v3
;
173 v1
= DUK_TVAL_GET_FASTINT(tv_x
);
174 v2
= DUK_TVAL_GET_FASTINT(tv_y
);
182 /* Must ensure result is 64-bit (no overflow); a
183 * simple and sufficient fast path is to allow only
184 * 32-bit inputs. Avoid zero inputs to avoid
185 * negative zero issues (-1 * 0 = -0, for instance).
187 if (v1
>= -0x80000000LL
&& v1
<= 0x7fffffffLL
&& v1
!= 0 &&
188 v2
>= -0x80000000LL
&& v2
<= 0x7fffffffLL
&& v2
!= 0) {
196 /* Don't allow a zero divisor. Fast path check by
197 * "verifying" with multiplication. Also avoid zero
198 * dividend to avoid negative zero issues (0 / -1 = -0
201 if (v1
== 0 || v2
== 0) {
211 /* Don't allow a zero divisor. Restrict both v1 and
212 * v2 to positive values to avoid compiler specific
215 if (v1
< 1 || v2
< 1) {
221 DUK_ASSERT(v1
- (v1
/ v2
) * v2
== v3
);
230 v3_hi
= (duk_int32_t
) (v3
>> 32);
231 if (DUK_LIKELY(v3_hi
>= -0x8000LL
&& v3_hi
<= 0x7fffLL
)) {
232 tv_z
= thr
->valstack_bottom
+ idx_z
;
233 DUK_TVAL_SET_FASTINT_UPDREF(thr
, tv_z
, v3
); /* side effects */
236 /* fall through if overflow etc */
239 #endif /* DUK_USE_FASTINT */
241 if (DUK_TVAL_IS_NUMBER(tv_x
) && DUK_TVAL_IS_NUMBER(tv_y
)) {
243 d1
= DUK_TVAL_GET_NUMBER(tv_x
);
244 d2
= DUK_TVAL_GET_NUMBER(tv_y
);
246 duk_push_tval(ctx
, tv_x
);
247 duk_push_tval(ctx
, tv_y
);
248 d1
= duk_to_number(ctx
, -2); /* side effects */
249 d2
= duk_to_number(ctx
, -1);
250 DUK_ASSERT(duk_is_number(ctx
, -2));
251 DUK_ASSERT(duk_is_number(ctx
, -1));
252 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1
);
253 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2
);
271 du
.d
= duk__compute_mod(d1
, d2
);
276 du
.d
= DUK_DOUBLE_NAN
; /* should not happen */
281 /* important to use normalized NaN with 8-byte tagged types */
282 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
);
283 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
285 tv_z
= thr
->valstack_bottom
+ idx_z
;
286 DUK_TVAL_SET_NUMBER_UPDREF(thr
, tv_z
, du
.d
); /* side effects */
289 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
) {
291 * Binary bitwise operations use different coercions (ToInt32, ToUint32)
292 * depending on the operation. We coerce the arguments first using
293 * ToInt32(), and then cast to an 32-bit value if necessary. Note that
294 * such casts must be correct even if there is no native 32-bit type
295 * (e.g., duk_int32_t and duk_uint32_t are 64-bit).
297 * E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3
300 duk_context
*ctx
= (duk_context
*) thr
;
302 duk_int32_t i1
, i2
, i3
;
303 duk_uint32_t u1
, u2
, u3
;
304 #if defined(DUK_USE_FASTINT)
310 DUK_ASSERT(thr
!= NULL
);
311 DUK_ASSERT(ctx
!= NULL
);
312 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
313 DUK_ASSERT(tv_y
!= NULL
); /* may be reg or const */
314 DUK_ASSERT_DISABLE(idx_z
>= 0); /* unsigned */
315 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
317 #if defined(DUK_USE_FASTINT)
318 if (DUK_TVAL_IS_FASTINT(tv_x
) && DUK_TVAL_IS_FASTINT(tv_y
)) {
319 i1
= (duk_int32_t
) DUK_TVAL_GET_FASTINT_I32(tv_x
);
320 i2
= (duk_int32_t
) DUK_TVAL_GET_FASTINT_I32(tv_y
);
323 #endif /* DUK_USE_FASTINT */
325 duk_push_tval(ctx
, tv_x
);
326 duk_push_tval(ctx
, tv_y
);
327 i1
= duk_to_int32(ctx
, -2);
328 i2
= duk_to_int32(ctx
, -1);
346 /* Signed shift, named "arithmetic" (asl) because the result
347 * is signed, e.g. 4294967295 << 1 -> -2. Note that result
351 u2
= ((duk_uint32_t
) i2
) & 0xffffffffUL
;
352 i3
= i1
<< (u2
& 0x1f); /* E5 Section 11.7.1, steps 7 and 8 */
353 i3
= i3
& ((duk_int32_t
) 0xffffffffUL
); /* Note: left shift, should mask */
359 u2
= ((duk_uint32_t
) i2
) & 0xffffffffUL
;
360 i3
= i1
>> (u2
& 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
366 u1
= ((duk_uint32_t
) i1
) & 0xffffffffUL
;
367 u2
= ((duk_uint32_t
) i2
) & 0xffffffffUL
;
369 /* special result value handling */
370 u3
= u1
>> (u2
& 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */
371 #if defined(DUK_USE_FASTINT)
372 fi3
= (duk_int64_t
) u3
;
373 goto fastint_result_set
;
375 d3
= (duk_double_t
) u3
;
381 i3
= 0; /* should not happen */
386 #if defined(DUK_USE_FASTINT)
387 /* Result is always fastint compatible. */
388 /* XXX: Set 32-bit result (but must then handle signed and
389 * unsigned results separately).
391 fi3
= (duk_int64_t
) i3
;
394 tv_z
= thr
->valstack_bottom
+ idx_z
;
395 DUK_TVAL_SET_FASTINT_UPDREF(thr
, tv_z
, fi3
); /* side effects */
397 d3
= (duk_double_t
) i3
;
400 DUK_ASSERT(!DUK_ISNAN(d3
)); /* 'd3' is never NaN, so no need to normalize */
401 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3
); /* always normalized */
403 tv_z
= thr
->valstack_bottom
+ idx_z
;
404 DUK_TVAL_SET_NUMBER_UPDREF(thr
, tv_z
, d3
); /* side effects */
408 /* In-place unary operation. */
409 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
) {
411 * Arithmetic operations other than '+' have number-only semantics
412 * and are implemented here. The separate switch-case here means a
413 * "double dispatch" of the arithmetic opcode, but saves code space.
415 * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
418 duk_context
*ctx
= (duk_context
*) thr
;
422 DUK_ASSERT(thr
!= NULL
);
423 DUK_ASSERT(ctx
!= NULL
);
424 DUK_ASSERT(opcode
== DUK_EXTRAOP_UNM
|| opcode
== DUK_EXTRAOP_UNP
);
425 DUK_ASSERT(tv_x
!= NULL
);
426 DUK_ASSERT(idx_x
>= 0);
428 #if defined(DUK_USE_FASTINT)
429 if (DUK_TVAL_IS_FASTINT(tv_x
)) {
432 v1
= DUK_TVAL_GET_FASTINT(tv_x
);
433 if (opcode
== DUK_EXTRAOP_UNM
) {
434 /* The smallest fastint is no longer 48-bit when
435 * negated. Positive zero becames negative zero
436 * (cannot be represented) when negated.
438 if (DUK_LIKELY(v1
!= DUK_FASTINT_MIN
&& v1
!= 0)) {
440 DUK_TVAL_SET_FASTINT(tv_x
, v2
); /* no refcount changes */
444 /* ToNumber() for a fastint is a no-op. */
445 DUK_ASSERT(opcode
== DUK_EXTRAOP_UNP
);
448 /* fall through if overflow etc */
450 #endif /* DUK_USE_FASTINT */
452 if (!DUK_TVAL_IS_NUMBER(tv_x
)) {
453 duk_to_number(ctx
, idx_x
); /* side effects, perform in-place */
454 tv_x
= DUK_GET_TVAL_POSIDX(ctx
, idx_x
);
455 DUK_ASSERT(tv_x
!= NULL
);
456 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x
));
459 d1
= DUK_TVAL_GET_NUMBER(tv_x
);
460 if (opcode
== DUK_EXTRAOP_UNM
) {
463 /* ToNumber() for a double is a no-op. */
464 DUK_ASSERT(opcode
== DUK_EXTRAOP_UNP
);
467 DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du
); /* mandatory if du.d is a NaN */
469 DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du
));
471 #if defined(DUK_USE_FASTINT)
472 /* Unary plus is used to force a fastint check, so must include
475 DUK_TVAL_SET_NUMBER_CHKFAST(tv_x
, du
.d
); /* no refcount changes */
477 DUK_TVAL_SET_NUMBER(tv_x
, du
.d
); /* no refcount changes */
481 DUK_LOCAL
void duk__vm_bitwise_not(duk_hthread
*thr
, duk_tval
*tv_x
, duk_uint_fast_t idx_z
) {
486 duk_context
*ctx
= (duk_context
*) thr
;
489 #if !defined(DUK_USE_FASTINT)
493 DUK_ASSERT(thr
!= NULL
);
494 DUK_ASSERT(ctx
!= NULL
);
495 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
496 DUK_ASSERT_DISABLE(idx_z
>= 0);
497 DUK_ASSERT((duk_uint_t
) idx_z
< (duk_uint_t
) duk_get_top(ctx
));
499 #if defined(DUK_USE_FASTINT)
500 if (DUK_TVAL_IS_FASTINT(tv_x
)) {
501 i1
= (duk_int32_t
) DUK_TVAL_GET_FASTINT_I32(tv_x
);
504 #endif /* DUK_USE_FASTINT */
506 duk_push_tval(ctx
, tv_x
);
507 i1
= duk_to_int32(ctx
, -1);
513 #if defined(DUK_USE_FASTINT)
514 /* Result is always fastint compatible. */
515 tv_z
= thr
->valstack_bottom
+ idx_z
;
516 DUK_TVAL_SET_FASTINT_I32_UPDREF(thr
, tv_z
, i2
); /* side effects */
518 d2
= (duk_double_t
) i2
;
520 DUK_ASSERT(!DUK_ISNAN(d2
)); /* 'val' is never NaN, so no need to normalize */
521 DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2
); /* always normalized */
523 tv_z
= thr
->valstack_bottom
+ idx_z
;
524 DUK_TVAL_SET_NUMBER_UPDREF(thr
, tv_z
, d2
); /* side effects */
528 DUK_LOCAL
void duk__vm_logical_not(duk_hthread
*thr
, duk_tval
*tv_x
, duk_tval
*tv_z
) {
535 DUK_ASSERT(thr
!= NULL
);
536 DUK_ASSERT(tv_x
!= NULL
); /* may be reg or const */
537 DUK_ASSERT(tv_z
!= NULL
); /* reg */
539 DUK_UNREF(thr
); /* w/o refcounts */
541 /* ToBoolean() does not require any operations with side effects so
542 * we can do it efficiently. For footprint it would be better to use
543 * duk_js_toboolean() and then push+replace to the result slot.
545 res
= duk_js_toboolean(tv_x
); /* does not modify tv_x */
546 DUK_ASSERT(res
== 0 || res
== 1);
548 DUK_TVAL_SET_BOOLEAN_UPDREF(thr
, tv_z
, res
); /* side effects */
552 * Longjmp and other control flow transfer for the bytecode executor.
554 * The longjmp handler can handle all longjmp types: error, yield, and
555 * resume (pseudotypes are never actually thrown).
557 * Error policy for longjmp: should not ordinarily throw errors; if errors
558 * occur (e.g. due to out-of-memory) they bubble outwards rather than being
559 * handled recursively.
562 #define DUK__LONGJMP_RESTART 0 /* state updated, restart bytecode execution */
563 #define DUK__LONGJMP_RETHROW 1 /* exit bytecode executor by rethrowing an error to caller */
565 #define DUK__RETHAND_RESTART 0 /* state updated, restart bytecode execution */
566 #define DUK__RETHAND_FINISHED 1 /* exit bytecode execution with return value */
568 /* XXX: optimize reconfig valstack operations so that resize, clamp, and setting
569 * top are combined into one pass.
572 /* Reconfigure value stack for return to an Ecmascript function at 'act_idx'. */
573 DUK_LOCAL
void duk__reconfig_valstack_ecma_return(duk_hthread
*thr
, duk_size_t act_idx
) {
575 duk_hcompiledfunction
*h_func
;
578 DUK_ASSERT(thr
!= NULL
);
579 DUK_ASSERT_DISABLE(act_idx
>= 0); /* unsigned */
580 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
) != NULL
);
581 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
)));
582 DUK_ASSERT_DISABLE(thr
->callstack
[act_idx
].idx_retval
>= 0); /* unsigned */
584 /* Clamp so that values at 'clamp_top' and above are wiped and won't
585 * retain reachable garbage. Then extend to 'nregs' because we're
586 * returning to an Ecmascript function.
589 act
= thr
->callstack
+ act_idx
;
590 h_func
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
592 thr
->valstack_bottom
= thr
->valstack
+ act
->idx_bottom
;
593 DUK_ASSERT(act
->idx_retval
>= act
->idx_bottom
);
594 clamp_top
= (duk_idx_t
) (act
->idx_retval
- act
->idx_bottom
+ 1); /* +1 = one retval */
595 duk_set_top((duk_context
*) thr
, clamp_top
);
598 (void) duk_valstack_resize_raw((duk_context
*) thr
,
599 (thr
->valstack_bottom
- thr
->valstack
) + /* bottom of current func */
600 h_func
->nregs
+ /* reg count */
601 DUK_VALSTACK_INTERNAL_EXTRA
, /* + spare */
602 DUK_VSRESIZE_FLAG_SHRINK
| /* flags */
604 DUK_VSRESIZE_FLAG_THROW
);
606 duk_set_top((duk_context
*) thr
, h_func
->nregs
);
609 DUK_LOCAL
void duk__reconfig_valstack_ecma_catcher(duk_hthread
*thr
, duk_size_t act_idx
, duk_size_t cat_idx
) {
612 duk_hcompiledfunction
*h_func
;
615 DUK_ASSERT(thr
!= NULL
);
616 DUK_ASSERT_DISABLE(act_idx
>= 0); /* unsigned */
617 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
) != NULL
);
618 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ act_idx
)));
619 DUK_ASSERT_DISABLE(thr
->callstack
[act_idx
].idx_retval
>= 0); /* unsigned */
621 act
= thr
->callstack
+ act_idx
;
622 cat
= thr
->catchstack
+ cat_idx
;
623 h_func
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
625 thr
->valstack_bottom
= thr
->valstack
+ act
->idx_bottom
;
626 DUK_ASSERT(cat
->idx_base
>= act
->idx_bottom
);
627 clamp_top
= (duk_idx_t
) (cat
->idx_base
- act
->idx_bottom
+ 2); /* +2 = catcher value, catcher lj_type */
628 duk_set_top((duk_context
*) thr
, clamp_top
);
632 (void) duk_valstack_resize_raw((duk_context
*) thr
,
633 (thr
->valstack_bottom
- thr
->valstack
) + /* bottom of current func */
634 h_func
->nregs
+ /* reg count */
635 DUK_VALSTACK_INTERNAL_EXTRA
, /* + spare */
636 DUK_VSRESIZE_FLAG_SHRINK
| /* flags */
638 DUK_VSRESIZE_FLAG_THROW
);
640 duk_set_top((duk_context
*) thr
, h_func
->nregs
);
643 /* Set catcher regs: idx_base+0 = value, idx_base+1 = lj_type. */
644 DUK_LOCAL
void duk__set_catcher_regs(duk_hthread
*thr
, duk_size_t cat_idx
, duk_tval
*tv_val_unstable
, duk_small_uint_t lj_type
) {
647 DUK_ASSERT(thr
!= NULL
);
648 DUK_ASSERT(tv_val_unstable
!= NULL
);
650 tv1
= thr
->valstack
+ thr
->catchstack
[cat_idx
].idx_base
;
651 DUK_ASSERT(tv1
< thr
->valstack_top
);
652 DUK_TVAL_SET_TVAL_UPDREF(thr
, tv1
, tv_val_unstable
); /* side effects */
654 tv1
= thr
->valstack
+ thr
->catchstack
[cat_idx
].idx_base
+ 1;
655 DUK_ASSERT(tv1
< thr
->valstack_top
);
657 DUK_TVAL_SET_FASTINT_U32_UPDREF(thr
, tv1
, (duk_uint32_t
) lj_type
); /* side effects */
660 DUK_LOCAL
void duk__handle_catch(duk_hthread
*thr
, duk_size_t cat_idx
, duk_tval
*tv_val_unstable
, duk_small_uint_t lj_type
) {
664 DUK_ASSERT(thr
!= NULL
);
665 DUK_ASSERT(tv_val_unstable
!= NULL
);
666 ctx
= (duk_context
*) thr
;
668 duk__set_catcher_regs(thr
, cat_idx
, tv_val_unstable
, lj_type
);
670 duk_hthread_catchstack_unwind(thr
, cat_idx
+ 1);
671 duk_hthread_callstack_unwind(thr
, thr
->catchstack
[cat_idx
].callstack_index
+ 1);
673 DUK_ASSERT(thr
->callstack_top
>= 1);
674 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
);
675 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)));
677 duk__reconfig_valstack_ecma_catcher(thr
, thr
->callstack_top
- 1, cat_idx
);
679 DUK_ASSERT(thr
->callstack_top
>= 1);
680 act
= thr
->callstack
+ thr
->callstack_top
- 1;
681 act
->curr_pc
= thr
->catchstack
[cat_idx
].pc_base
+ 0; /* +0 = catch */
685 * If entering a 'catch' block which requires an automatic
686 * catch variable binding, create the lexical environment.
688 * The binding is mutable (= writable) but not deletable.
689 * Step 4 for the catch production in E5 Section 12.14;
690 * no value is given for CreateMutableBinding 'D' argument,
691 * which implies the binding is not deletable.
694 if (DUK_CAT_HAS_CATCH_BINDING_ENABLED(&thr
->catchstack
[cat_idx
])) {
695 duk_hobject
*new_env
;
696 duk_hobject
*act_lex_env
;
698 DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding"));
700 /* Note: 'act' is dangerous here because it may get invalidate at many
701 * points, so we re-lookup it multiple times.
703 DUK_ASSERT(thr
->callstack_top
>= 1);
704 act
= thr
->callstack
+ thr
->callstack_top
- 1;
706 if (act
->lex_env
== NULL
) {
707 DUK_ASSERT(act
->var_env
== NULL
);
708 DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
710 /* this may have side effects, so re-lookup act */
711 duk_js_init_activation_environment_records_delayed(thr
, act
);
712 act
= thr
->callstack
+ thr
->callstack_top
- 1;
714 DUK_ASSERT(act
->lex_env
!= NULL
);
715 DUK_ASSERT(act
->var_env
!= NULL
);
716 DUK_ASSERT(DUK_ACT_GET_FUNC(act
) != NULL
);
717 DUK_UNREF(act
); /* unreferenced without assertions */
719 act
= thr
->callstack
+ thr
->callstack_top
- 1;
720 act_lex_env
= act
->lex_env
;
721 act
= NULL
; /* invalidated */
723 (void) duk_push_object_helper_proto(ctx
,
724 DUK_HOBJECT_FLAG_EXTENSIBLE
|
725 DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV
),
727 new_env
= DUK_GET_HOBJECT_NEGIDX(ctx
, -1);
728 DUK_ASSERT(new_env
!= NULL
);
729 DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr
*) new_env
));
731 /* Note: currently the catch binding is handled without a register
732 * binding because we don't support dynamic register bindings (they
733 * must be fixed for an entire function). So, there is no need to
734 * record regbases etc.
737 DUK_ASSERT(thr
->catchstack
[cat_idx
].h_varname
!= NULL
);
738 duk_push_hstring(ctx
, thr
->catchstack
[cat_idx
].h_varname
);
739 duk_push_tval(ctx
, thr
->valstack
+ thr
->catchstack
[cat_idx
].idx_base
);
740 duk_xdef_prop(ctx
, -3, DUK_PROPDESC_FLAGS_W
); /* writable, not configurable */
742 act
= thr
->callstack
+ thr
->callstack_top
- 1;
743 act
->lex_env
= new_env
;
744 DUK_HOBJECT_INCREF(thr
, new_env
); /* reachable through activation */
746 DUK_CAT_SET_LEXENV_ACTIVE(&thr
->catchstack
[cat_idx
]);
750 DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr
*) new_env
));
753 DUK_CAT_CLEAR_CATCH_ENABLED(&thr
->catchstack
[cat_idx
]);
756 DUK_LOCAL
void duk__handle_finally(duk_hthread
*thr
, duk_size_t cat_idx
, duk_tval
*tv_val_unstable
, duk_small_uint_t lj_type
) {
759 DUK_ASSERT(thr
!= NULL
);
760 DUK_ASSERT(tv_val_unstable
!= NULL
);
762 duk__set_catcher_regs(thr
, cat_idx
, tv_val_unstable
, lj_type
);
764 duk_hthread_catchstack_unwind(thr
, cat_idx
+ 1); /* cat_idx catcher is kept, even for finally */
765 duk_hthread_callstack_unwind(thr
, thr
->catchstack
[cat_idx
].callstack_index
+ 1);
767 DUK_ASSERT(thr
->callstack_top
>= 1);
768 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
);
769 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)));
771 duk__reconfig_valstack_ecma_catcher(thr
, thr
->callstack_top
- 1, cat_idx
);
773 DUK_ASSERT(thr
->callstack_top
>= 1);
774 act
= thr
->callstack
+ thr
->callstack_top
- 1;
775 act
->curr_pc
= thr
->catchstack
[cat_idx
].pc_base
+ 1; /* +1 = finally */
778 DUK_CAT_CLEAR_FINALLY_ENABLED(&thr
->catchstack
[cat_idx
]);
781 DUK_LOCAL
void duk__handle_label(duk_hthread
*thr
, duk_size_t cat_idx
, duk_small_uint_t lj_type
) {
784 DUK_ASSERT(thr
!= NULL
);
786 DUK_ASSERT(thr
->callstack_top
>= 1);
787 act
= thr
->callstack
+ thr
->callstack_top
- 1;
789 DUK_ASSERT(DUK_ACT_GET_FUNC(act
) != NULL
);
790 DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act
)));
792 /* +0 = break, +1 = continue */
793 act
->curr_pc
= thr
->catchstack
[cat_idx
].pc_base
+ (lj_type
== DUK_LJ_TYPE_CONTINUE
? 1 : 0);
794 act
= NULL
; /* invalidated */
796 duk_hthread_catchstack_unwind(thr
, cat_idx
+ 1); /* keep label catcher */
797 /* no need to unwind callstack */
799 /* valstack should not need changes */
800 #if defined(DUK_USE_ASSERTIONS)
801 DUK_ASSERT(thr
->callstack_top
>= 1);
802 act
= thr
->callstack
+ thr
->callstack_top
- 1;
803 DUK_ASSERT((duk_size_t
) (thr
->valstack_top
- thr
->valstack_bottom
) ==
804 (duk_size_t
) ((duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
))->nregs
);
808 /* Called for handling both a longjmp() with type DUK_LJ_TYPE_YIELD and
809 * when a RETURN opcode terminates a thread and yields to the resumer.
811 DUK_LOCAL
void duk__handle_yield(duk_hthread
*thr
, duk_hthread
*resumer
, duk_size_t act_idx
, duk_tval
*tv_val_unstable
) {
814 DUK_ASSERT(thr
!= NULL
);
815 DUK_ASSERT(resumer
!= NULL
);
816 DUK_ASSERT(tv_val_unstable
!= NULL
);
817 DUK_ASSERT(DUK_ACT_GET_FUNC(resumer
->callstack
+ act_idx
) != NULL
);
818 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer
->callstack
+ act_idx
))); /* resume caller must be an ecmascript func */
820 tv1
= resumer
->valstack
+ resumer
->callstack
[act_idx
].idx_retval
; /* return value from Duktape.Thread.resume() */
821 DUK_TVAL_SET_TVAL_UPDREF(thr
, tv1
, tv_val_unstable
); /* 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_ecma_return(resumer
, act_idx
);
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
) {
835 duk_size_t entry_callstack_index
;
836 duk_small_uint_t retval
= DUK__LONGJMP_RESTART
;
838 DUK_ASSERT(thr
!= NULL
);
839 DUK_ASSERT(entry_thread
!= NULL
);
840 DUK_ASSERT(entry_callstack_top
> 0); /* guarantees entry_callstack_top - 1 >= 0 */
842 entry_callstack_index
= entry_callstack_top
- 1;
844 /* 'thr' is the current thread, as no-one resumes except us and we
845 * switch 'thr' in that case.
847 DUK_ASSERT(thr
== thr
->heap
->curr_thread
);
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_UPDREF(thr
, tv
, tv2
); /* side effects */
954 duk_hthread_callstack_unwind(resumee
, act_idx
+ 1); /* unwind to 'yield' caller */
956 /* no need to unwind catchstack */
958 duk__reconfig_valstack_ecma_return(resumee
, act_idx
);
960 resumee
->resumer
= thr
;
961 resumee
->state
= DUK_HTHREAD_STATE_RUNNING
;
962 thr
->state
= DUK_HTHREAD_STATE_RESUMED
;
963 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumee
);
965 thr
= resumee
; /* not needed, as we exit right away */
967 DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
968 retval
= DUK__LONGJMP_RESTART
;
969 goto wipe_and_return
;
971 duk_small_uint_t call_flags
;
974 /* resumee: [... initial_func] (currently actually: [initial_func]) */
976 duk_push_undefined((duk_context
*) resumee
);
977 tv
= &thr
->heap
->lj
.value1
;
978 duk_push_tval((duk_context
*) resumee
, tv
);
980 /* resumee: [... initial_func undefined(= this) resume_value ] */
982 call_flags
= DUK_CALL_FLAG_IS_RESUME
; /* is resume, not a tail call */
984 setup_rc
= duk_handle_ecma_call_setup(resumee
,
985 1, /* num_stack_args */
986 call_flags
); /* call_flags */
988 /* Shouldn't happen but check anyway. */
989 DUK_ERROR_INTERNAL_DEFMSG(thr
);
992 resumee
->resumer
= thr
;
993 resumee
->state
= DUK_HTHREAD_STATE_RUNNING
;
994 thr
->state
= DUK_HTHREAD_STATE_RESUMED
;
995 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumee
);
997 thr
= resumee
; /* not needed, as we exit right away */
999 DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
1000 retval
= DUK__LONGJMP_RESTART
;
1001 goto wipe_and_return
;
1004 break; /* never here */
1007 case DUK_LJ_TYPE_YIELD
: {
1009 * Currently only allowed only if yielding thread has only
1010 * Ecmascript activations (except for the Duktape.Thread.yield()
1011 * call at the callstack top) and none of them constructor
1014 * This excludes the 'entry' thread which will always have
1015 * a preventcount > 0.
1018 duk_hthread
*resumer
;
1020 /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
1022 DUK_ASSERT(thr
!= entry_thread
); /* Duktape.Thread.yield() should prevent */
1023 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_RUNNING
); /* unchanged from Duktape.Thread.yield() */
1024 DUK_ASSERT(thr
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.yield() activation */
1025 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
&&
1026 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)) &&
1027 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1))->func
== duk_bi_thread_yield
);
1028 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2) != NULL
&&
1029 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2))); /* an Ecmascript function */
1030 DUK_ASSERT_DISABLE((thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
1032 resumer
= thr
->resumer
;
1034 DUK_ASSERT(resumer
!= NULL
);
1035 DUK_ASSERT(resumer
->state
== DUK_HTHREAD_STATE_RESUMED
); /* written by a previous RESUME handling */
1036 DUK_ASSERT(resumer
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
1037 DUK_ASSERT(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 1) != NULL
&&
1038 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 1)) &&
1039 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 1))->func
== duk_bi_thread_resume
);
1040 DUK_ASSERT(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 2) != NULL
&&
1041 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer
->callstack
+ resumer
->callstack_top
- 2))); /* an Ecmascript function */
1042 DUK_ASSERT_DISABLE((resumer
->callstack
+ resumer
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
1044 if (thr
->heap
->lj
.iserror
) {
1045 thr
->state
= DUK_HTHREAD_STATE_YIELDED
;
1046 thr
->resumer
= NULL
;
1047 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1048 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1051 thr
->heap
->lj
.type
= DUK_LJ_TYPE_THROW
;
1052 /* lj.value1 is already set */
1053 DUK_ASSERT(thr
->heap
->lj
.iserror
); /* already set */
1055 DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate"));
1058 duk__handle_yield(thr
, resumer
, resumer
->callstack_top
- 2, &thr
->heap
->lj
.value1
);
1060 thr
->state
= DUK_HTHREAD_STATE_YIELDED
;
1061 thr
->resumer
= NULL
;
1062 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1063 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1065 thr
= resumer
; /* not needed, as we exit right away */
1068 DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer"));
1069 retval
= DUK__LONGJMP_RESTART
;
1070 goto wipe_and_return
;
1073 break; /* never here */
1076 case DUK_LJ_TYPE_THROW
: {
1078 * Three possible outcomes:
1079 * * A try or finally catcher is found => resume there.
1081 * * The error propagates to the bytecode executor entry
1082 * level (and we're in the entry thread) => rethrow
1083 * with a new longjmp(), after restoring the previous
1085 * * The error is not caught in the current thread, so
1086 * the thread finishes with an error. This works like
1087 * a yielded error, except that the thread is finished
1088 * and can no longer be resumed. (There is always a
1089 * resumer in this case.)
1091 * Note: until we hit the entry level, there can only be
1092 * Ecmascript activations.
1096 duk_hthread
*resumer
;
1098 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
1099 while (cat
>= thr
->catchstack
) {
1100 if (thr
== entry_thread
&&
1101 cat
->callstack_index
< entry_callstack_index
) {
1102 /* entry level reached */
1106 if (DUK_CAT_HAS_CATCH_ENABLED(cat
)) {
1107 DUK_ASSERT(DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
);
1109 duk__handle_catch(thr
,
1110 cat
- thr
->catchstack
,
1111 &thr
->heap
->lj
.value1
,
1114 DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution"));
1115 retval
= DUK__LONGJMP_RESTART
;
1116 goto wipe_and_return
;
1119 if (DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
1120 DUK_ASSERT(DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
);
1121 DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat
));
1123 duk__handle_finally(thr
,
1124 cat
- thr
->catchstack
,
1125 &thr
->heap
->lj
.value1
,
1128 DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution"));
1129 retval
= DUK__LONGJMP_RESTART
;
1130 goto wipe_and_return
;
1136 if (thr
== entry_thread
) {
1137 /* not caught by anything before entry level; rethrow and let the
1138 * final catcher unwind everything
1141 duk_hthread_catchstack_unwind(thr
, (cat
- thr
->catchstack
) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
1142 duk_hthread_callstack_unwind(thr
, entry_callstack_index
+ 1);
1145 DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor"));
1146 retval
= DUK__LONGJMP_RETHROW
;
1148 /* Note: MUST NOT wipe_and_return here, as heap->lj must remain intact */
1151 DUK_DD(DUK_DDPRINT("-> throw not caught by current thread, yield error to resumer and recheck longjmp"));
1153 /* not caught by current thread, thread terminates (yield error to resumer);
1154 * note that this may cause a cascade if the resumer terminates with an uncaught
1155 * exception etc (this is OK, but needs careful testing)
1158 DUK_ASSERT(thr
->resumer
!= NULL
);
1159 DUK_ASSERT(thr
->resumer
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
1160 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1) != NULL
&&
1161 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1)) &&
1162 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1))->func
== duk_bi_thread_resume
); /* Duktape.Thread.resume() */
1163 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2) != NULL
&&
1164 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2))); /* an Ecmascript function */
1166 resumer
= thr
->resumer
;
1170 DUK_ASSERT(thr
->heap
->lj
.type
== DUK_LJ_TYPE_THROW
); /* already set */
1171 /* lj.value1 already set */
1173 duk_hthread_terminate(thr
); /* updates thread state, minimizes its allocations */
1174 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_TERMINATED
);
1176 thr
->resumer
= NULL
;
1177 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1178 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1183 case DUK_LJ_TYPE_BREAK
: /* pseudotypes, not used in actual longjmps */
1184 case DUK_LJ_TYPE_CONTINUE
:
1185 case DUK_LJ_TYPE_RETURN
:
1186 case DUK_LJ_TYPE_NORMAL
:
1188 /* should never happen, but be robust */
1189 DUK_D(DUK_DPRINT("caught unknown longjmp type %ld, treat as internal error", (long) thr
->heap
->lj
.type
));
1190 goto convert_to_internal_error
;
1198 /* this is not strictly necessary, but helps debugging */
1199 thr
->heap
->lj
.type
= DUK_LJ_TYPE_UNKNOWN
;
1200 thr
->heap
->lj
.iserror
= 0;
1202 DUK_TVAL_SET_UNDEFINED_UPDREF(thr
, &thr
->heap
->lj
.value1
); /* side effects */
1203 DUK_TVAL_SET_UNDEFINED_UPDREF(thr
, &thr
->heap
->lj
.value2
); /* side effects */
1208 convert_to_internal_error
:
1209 /* This could also be thrown internally (set the error, goto check_longjmp),
1210 * but it's better for internal errors to bubble outwards so that we won't
1211 * infinite loop in this catchpoint.
1213 DUK_ERROR_INTERNAL_DEFMSG(thr
);
1218 /* Handle a BREAK/CONTINUE opcode. Avoid using longjmp() for BREAK/CONTINUE
1219 * handling because it has a measurable performance impact in ordinary
1220 * environments and an extreme impact in Emscripten (GH-342).
1222 DUK_LOCAL
void duk__handle_break_or_continue(duk_hthread
*thr
,
1223 duk_uint_t label_id
,
1224 duk_small_uint_t lj_type
) {
1226 duk_size_t orig_callstack_index
;
1228 DUK_ASSERT(thr
!= NULL
);
1231 * Find a matching label catcher or 'finally' catcher in
1232 * the same function.
1234 * A label catcher must always exist and will match unless
1235 * a 'finally' captures the break/continue first. It is the
1236 * compiler's responsibility to ensure that labels are used
1240 /* Note: thr->catchstack_top may be 0, so that cat < thr->catchstack
1241 * initially. This is OK and intended.
1243 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
1244 DUK_ASSERT(thr
->callstack_top
> 0);
1245 orig_callstack_index
= thr
->callstack_top
- 1;
1247 DUK_DDD(DUK_DDDPRINT("handling break/continue with label=%ld, callstack index=%ld",
1248 (long) label_id
, (long) cat
->callstack_index
));
1250 while (cat
>= thr
->catchstack
) {
1251 if (cat
->callstack_index
!= orig_callstack_index
) {
1254 DUK_DDD(DUK_DDDPRINT("considering catcher %ld: type=%ld label=%ld",
1255 (long) (cat
- thr
->catchstack
),
1256 (long) DUK_CAT_GET_TYPE(cat
),
1257 (long) DUK_CAT_GET_LABEL(cat
)));
1259 if (DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
&&
1260 DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
1264 cat_idx
= (duk_size_t
) (cat
- thr
->catchstack
); /* get before side effects */
1266 DUK_TVAL_SET_FASTINT_U32(&tv_tmp
, (duk_uint32_t
) label_id
);
1267 duk__handle_finally(thr
, cat_idx
, &tv_tmp
, lj_type
);
1269 DUK_DD(DUK_DDPRINT("-> break/continue caught by 'finally', restart execution"));
1272 if (DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_LABEL
&&
1273 (duk_uint_t
) DUK_CAT_GET_LABEL(cat
) == label_id
) {
1276 cat_idx
= (duk_size_t
) (cat
- thr
->catchstack
);
1277 duk__handle_label(thr
, cat_idx
, lj_type
);
1279 DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution"));
1285 /* should never happen, but be robust */
1286 DUK_D(DUK_DPRINT("-> break/continue not caught by anything in the current function (should never happen), throw internal error"));
1287 DUK_ERROR_INTERNAL_DEFMSG(thr
);
1291 /* Handle a RETURN opcode. Avoid using longjmp() for return handling because
1292 * it has a measurable performance impact in ordinary environments and an extreme
1293 * impact in Emscripten (GH-342). Return value is on value stack top.
1295 DUK_LOCAL duk_small_uint_t
duk__handle_return(duk_hthread
*thr
,
1296 duk_hthread
*entry_thread
,
1297 duk_size_t entry_callstack_top
) {
1300 duk_hthread
*resumer
;
1302 duk_size_t new_cat_top
;
1303 duk_size_t orig_callstack_index
;
1305 /* We can directly access value stack here. */
1307 DUK_ASSERT(thr
!= NULL
);
1308 DUK_ASSERT(entry_thread
!= NULL
);
1309 DUK_ASSERT(thr
->valstack_top
- 1 >= thr
->valstack_bottom
);
1310 tv1
= thr
->valstack_top
- 1;
1311 DUK_TVAL_CHKFAST_INPLACE(tv1
); /* fastint downgrade check for return values */
1314 * Four possible outcomes:
1316 * 1. A 'finally' in the same function catches the 'return'.
1317 * It may continue to propagate when 'finally' is finished,
1318 * or it may be neutralized by 'finally' (both handled by
1321 * 2. The return happens at the entry level of the bytecode
1322 * executor, so return from the executor (in C stack).
1324 * 3. There is a calling (Ecmascript) activation in the call
1325 * stack => return to it, in the same executor instance.
1327 * 4. There is no calling activation, and the thread is
1328 * terminated. There is always a resumer in this case,
1329 * which gets the return value similarly to a 'yield'
1330 * (except that the current thread can no longer be
1334 DUK_ASSERT(thr
!= NULL
);
1335 DUK_ASSERT(thr
->callstack_top
>= 1);
1336 DUK_ASSERT(thr
->catchstack
!= NULL
);
1338 /* XXX: does not work if thr->catchstack is NULL */
1339 /* XXX: does not work if thr->catchstack is allocated but lowest pointer */
1341 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1; /* may be < thr->catchstack initially */
1342 DUK_ASSERT(thr
->callstack_top
> 0); /* ensures callstack_top - 1 >= 0 */
1343 orig_callstack_index
= thr
->callstack_top
- 1;
1345 while (cat
>= thr
->catchstack
) {
1346 if (cat
->callstack_index
!= orig_callstack_index
) {
1349 if (DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_TCF
&&
1350 DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
1353 cat_idx
= (duk_size_t
) (cat
- thr
->catchstack
); /* get before side effects */
1355 DUK_ASSERT(thr
->valstack_top
- 1 >= thr
->valstack_bottom
);
1356 duk__handle_finally(thr
, cat_idx
, thr
->valstack_top
- 1, DUK_LJ_TYPE_RETURN
);
1358 DUK_DD(DUK_DDPRINT("-> return caught by 'finally', restart execution"));
1359 return DUK__RETHAND_RESTART
;
1363 /* If out of catchstack, cat = thr->catchstack - 1;
1364 * new_cat_top will be 0 in that case.
1366 new_cat_top
= (duk_size_t
) ((cat
+ 1) - thr
->catchstack
);
1367 cat
= NULL
; /* avoid referencing, invalidated */
1369 DUK_DDD(DUK_DDDPRINT("no catcher in catch stack, return to calling activation / yield"));
1371 if (thr
== entry_thread
&&
1372 thr
->callstack_top
== entry_callstack_top
) {
1373 /* Return to the bytecode executor caller which will unwind stacks.
1374 * Return value is already on the stack top: [ ... retval ].
1377 /* XXX: could unwind catchstack here, so that call handling
1378 * didn't need to do that?
1380 DUK_DDD(DUK_DDDPRINT("-> return propagated up to entry level, exit bytecode executor"));
1381 return DUK__RETHAND_FINISHED
;
1384 if (thr
->callstack_top
>= 2) {
1385 /* There is a caller; it MUST be an Ecmascript caller (otherwise it would
1386 * match entry level check)
1389 DUK_DDD(DUK_DDDPRINT("return to Ecmascript caller, idx_retval=%ld, lj_value1=%!T",
1390 (long) (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
,
1391 (duk_tval
*) &thr
->heap
->lj
.value1
));
1393 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 2))); /* must be ecmascript */
1395 tv1
= thr
->valstack
+ (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
;
1396 DUK_ASSERT(thr
->valstack_top
- 1 >= thr
->valstack_bottom
);
1397 tv2
= thr
->valstack_top
- 1;
1398 DUK_TVAL_SET_TVAL_UPDREF(thr
, tv1
, tv2
); /* side effects */
1400 DUK_DDD(DUK_DDDPRINT("return value at idx_retval=%ld is %!T",
1401 (long) (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
,
1402 (duk_tval
*) (thr
->valstack
+ (thr
->callstack
+ thr
->callstack_top
- 2)->idx_retval
)));
1404 duk_hthread_catchstack_unwind(thr
, new_cat_top
); /* leave 'cat' as top catcher (also works if catchstack exhausted) */
1405 duk_hthread_callstack_unwind(thr
, thr
->callstack_top
- 1);
1406 duk__reconfig_valstack_ecma_return(thr
, thr
->callstack_top
- 1);
1408 DUK_DD(DUK_DDPRINT("-> return not intercepted, restart execution in caller"));
1409 return DUK__RETHAND_RESTART
;
1412 DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)"));
1414 DUK_ASSERT(thr
->resumer
!= NULL
);
1415 DUK_ASSERT(thr
->resumer
->callstack_top
>= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */
1416 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1) != NULL
&&
1417 DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1)) &&
1418 ((duk_hnativefunction
*) DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 1))->func
== duk_bi_thread_resume
); /* Duktape.Thread.resume() */
1419 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2) != NULL
&&
1420 DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2))); /* an Ecmascript function */
1421 DUK_ASSERT_DISABLE((thr
->resumer
->callstack
+ thr
->resumer
->callstack_top
- 2)->idx_retval
>= 0); /* unsigned */
1422 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_RUNNING
);
1423 DUK_ASSERT(thr
->resumer
->state
== DUK_HTHREAD_STATE_RESUMED
);
1425 resumer
= thr
->resumer
;
1427 /* Share yield longjmp handler. */
1428 DUK_ASSERT(thr
->valstack_top
- 1 >= thr
->valstack_bottom
);
1429 duk__handle_yield(thr
, resumer
, resumer
->callstack_top
- 2, thr
->valstack_top
- 1);
1431 duk_hthread_terminate(thr
); /* updates thread state, minimizes its allocations */
1432 DUK_ASSERT(thr
->state
== DUK_HTHREAD_STATE_TERMINATED
);
1434 thr
->resumer
= NULL
;
1435 resumer
->state
= DUK_HTHREAD_STATE_RUNNING
;
1436 DUK_HEAP_SWITCH_THREAD(thr
->heap
, resumer
);
1438 thr
= resumer
; /* not needed */
1441 DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer"));
1442 return DUK__RETHAND_RESTART
;
1446 * Executor interrupt handling
1448 * The handler is called whenever the interrupt countdown reaches zero
1449 * (or below). The handler must perform whatever checks are activated,
1450 * e.g. check for cumulative step count to impose an execution step
1451 * limit or check for breakpoints or other debugger interaction.
1453 * When the actions are done, the handler must reinit the interrupt
1454 * init and counter values. The 'init' value must indicate how many
1455 * bytecode instructions are executed before the next interrupt. The
1456 * counter must interface with the bytecode executor loop. Concretely,
1457 * the new init value is normally one higher than the new counter value.
1458 * For instance, to execute exactly one bytecode instruction the init
1459 * value is set to 1 and the counter to 0. If an error is thrown by the
1460 * interrupt handler, the counters are set to the same value (e.g. both
1461 * to 0 to cause an interrupt when the next bytecode instruction is about
1462 * to be executed after error handling).
1464 * Maintaining the init/counter value properly is important for accurate
1465 * behavior. For instance, executor step limit needs a cumulative step
1466 * count which is simply computed as a sum of 'init' values. This must
1467 * work accurately even when single stepping.
1470 #if defined(DUK_USE_INTERRUPT_COUNTER)
1472 #define DUK__INT_NOACTION 0 /* no specific action, resume normal execution */
1473 #define DUK__INT_RESTART 1 /* must "goto restart_execution", e.g. breakpoints changed */
1475 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1476 DUK_LOCAL
void duk__interrupt_handle_debugger(duk_hthread
*thr
, duk_bool_t
*out_immediate
, duk_small_uint_t
*out_interrupt_retval
) {
1478 duk_activation
*act
;
1480 duk_breakpoint
**bp_active
;
1481 duk_uint_fast32_t line
= 0;
1482 duk_bool_t process_messages
;
1483 duk_bool_t processed_messages
= 0;
1485 DUK_ASSERT(thr
->heap
->dbg_processing
== 0); /* don't re-enter e.g. during Eval */
1487 ctx
= (duk_context
*) thr
;
1488 act
= thr
->callstack
+ thr
->callstack_top
- 1;
1490 /* It might seem that replacing 'thr->heap' with just 'heap' below
1491 * might be a good idea, but it increases code size slightly
1492 * (probably due to unnecessary spilling) at least on x64.
1496 * Breakpoint and step state checks
1499 if (act
->flags
& DUK_ACT_FLAG_BREAKPOINT_ACTIVE
||
1500 (thr
->heap
->dbg_step_thread
== thr
&&
1501 thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1)) {
1502 line
= duk_debug_curr_line(thr
);
1504 if (act
->prev_line
!= line
) {
1505 /* Stepped? Step out is handled by callstack unwind. */
1506 if ((thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_INTO
||
1507 thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_OVER
) &&
1508 (thr
->heap
->dbg_step_thread
== thr
) &&
1509 (thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1) &&
1510 (line
!= thr
->heap
->dbg_step_startline
)) {
1511 DUK_D(DUK_DPRINT("STEP STATE TRIGGERED PAUSE at line %ld",
1514 DUK_HEAP_SET_PAUSED(thr
->heap
);
1517 /* Check for breakpoints only on line transition.
1518 * Breakpoint is triggered when we enter the target
1519 * line from a different line, and the previous line
1520 * was within the same function.
1522 * This condition is tricky: the condition used to be
1523 * that transition to -or across- the breakpoint line
1524 * triggered the breakpoint. This seems intuitively
1525 * better because it handles breakpoints on lines with
1526 * no emitted opcodes; but this leads to the issue
1527 * described in: https://github.com/svaarala/duktape/issues/263.
1529 bp_active
= thr
->heap
->dbg_breakpoints_active
;
1536 DUK_ASSERT(bp
->filename
!= NULL
);
1537 if (act
->prev_line
!= bp
->line
&& line
== bp
->line
) {
1538 DUK_D(DUK_DPRINT("BREAKPOINT TRIGGERED at %!O:%ld",
1539 (duk_heaphdr
*) bp
->filename
, (long) bp
->line
));
1541 DUK_HEAP_SET_PAUSED(thr
->heap
);
1548 act
->prev_line
= line
;
1552 * Rate limit check for sending status update or peeking into
1553 * the debug transport. Both can be expensive operations that
1554 * we don't want to do on every opcode.
1556 * Making sure the interval remains reasonable on a wide variety
1557 * of targets and bytecode is difficult without a timestamp, so
1558 * we use a Date-provided timestamp for the rate limit check.
1559 * But since it's also expensive to get a timestamp, a bytecode
1560 * counter is used to rate limit getting timestamps.
1563 process_messages
= 0;
1564 if (thr
->heap
->dbg_state_dirty
|| thr
->heap
->dbg_paused
|| thr
->heap
->dbg_detaching
) {
1565 /* Enter message processing loop for sending Status notifys and
1566 * to finish a pending detach.
1568 process_messages
= 1;
1571 /* XXX: remove heap->dbg_exec_counter, use heap->inst_count_interrupt instead? */
1572 thr
->heap
->dbg_exec_counter
+= thr
->interrupt_init
;
1573 if (thr
->heap
->dbg_exec_counter
- thr
->heap
->dbg_last_counter
>= DUK_HEAP_DBG_RATELIMIT_OPCODES
) {
1574 /* Overflow of the execution counter is fine and doesn't break
1578 duk_double_t now
, diff_last
;
1580 thr
->heap
->dbg_last_counter
= thr
->heap
->dbg_exec_counter
;
1581 now
= DUK_USE_DATE_GET_NOW(ctx
);
1583 diff_last
= now
- thr
->heap
->dbg_last_time
;
1584 if (diff_last
< 0.0 || diff_last
>= (duk_double_t
) DUK_HEAP_DBG_RATELIMIT_MILLISECS
) {
1585 /* Negative value checked so that a "time jump" works
1588 * Same interval is now used for status sending and
1592 thr
->heap
->dbg_last_time
= now
;
1593 thr
->heap
->dbg_state_dirty
= 1;
1594 process_messages
= 1;
1599 * Process messages and send status if necessary.
1601 * If we're paused, we'll block for new messages. If we're not
1602 * paused, we'll process anything we can peek but won't block
1603 * for more. Detach (and re-attach) handling is all localized
1604 * to duk_debug_process_messages() too.
1606 * Debugger writes outside the message loop may cause debugger
1607 * detach1 phase to run, after which dbg_read_cb == NULL and
1608 * dbg_detaching != 0. The message loop will finish the detach
1609 * by running detach2 phase, so enter the message loop also when
1613 act
= NULL
; /* may be changed */
1614 if (process_messages
) {
1615 DUK_ASSERT(thr
->heap
->dbg_processing
== 0);
1616 processed_messages
= duk_debug_process_messages(thr
, 0 /*no_block*/);
1617 DUK_ASSERT(thr
->heap
->dbg_processing
== 0);
1620 /* Continue checked execution if there are breakpoints or we're stepping.
1621 * Also use checked execution if paused flag is active - it shouldn't be
1622 * because the debug message loop shouldn't terminate if it was. Step out
1623 * is handled by callstack unwind and doesn't need checked execution.
1624 * Note that debugger may have detached due to error or explicit request
1625 * above, so we must recheck attach status.
1628 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
)) {
1629 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup, may have changed */
1630 if (act
->flags
& DUK_ACT_FLAG_BREAKPOINT_ACTIVE
||
1631 ((thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_INTO
||
1632 thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_OVER
) &&
1633 thr
->heap
->dbg_step_thread
== thr
&&
1634 thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1) ||
1635 thr
->heap
->dbg_paused
) {
1639 /* If we processed any debug messages breakpoints may have
1640 * changed; restart execution to re-check active breakpoints.
1642 if (processed_messages
) {
1643 DUK_D(DUK_DPRINT("processed debug messages, restart execution to recheck possibly changed breakpoints"));
1644 *out_interrupt_retval
= DUK__INT_RESTART
;
1647 DUK_D(DUK_DPRINT("debugger became detached, resume normal execution"));
1650 #endif /* DUK_USE_DEBUGGER_SUPPORT */
1652 DUK_LOCAL duk_small_uint_t
duk__executor_interrupt(duk_hthread
*thr
) {
1654 duk_activation
*act
;
1655 duk_hcompiledfunction
*fun
;
1656 duk_bool_t immediate
= 0;
1657 duk_small_uint_t retval
;
1659 DUK_ASSERT(thr
!= NULL
);
1660 DUK_ASSERT(thr
->heap
!= NULL
);
1661 DUK_ASSERT(thr
->callstack
!= NULL
);
1662 DUK_ASSERT(thr
->callstack_top
> 0);
1664 #if defined(DUK_USE_DEBUG)
1665 thr
->heap
->inst_count_interrupt
+= thr
->interrupt_init
;
1666 DUK_DD(DUK_DDPRINT("execution interrupt, counter=%ld, init=%ld, "
1667 "instruction counts: executor=%ld, interrupt=%ld",
1668 (long) thr
->interrupt_counter
, (long) thr
->interrupt_init
,
1669 (long) thr
->heap
->inst_count_exec
, (long) thr
->heap
->inst_count_interrupt
));
1672 retval
= DUK__INT_NOACTION
;
1673 ctr
= DUK_HTHREAD_INTCTR_DEFAULT
;
1676 * Avoid nested calls. Concretely this happens during debugging, e.g.
1677 * when we eval() an expression.
1679 * Also don't interrupt if we're currently doing debug processing
1680 * (which can be initiated outside the bytecode executor) as this
1681 * may cause the debugger to be called recursively. Check required
1682 * for correct operation of throw intercept and other "exotic" halting
1686 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1687 if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr
->heap
) || thr
->heap
->dbg_processing
) {
1689 if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr
->heap
)) {
1691 DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring"));
1693 /* Set a high interrupt counter; the original executor
1694 * interrupt invocation will rewrite before exiting.
1696 thr
->interrupt_init
= ctr
;
1697 thr
->interrupt_counter
= ctr
- 1;
1698 return DUK__INT_NOACTION
;
1700 DUK_HEAP_SET_INTERRUPT_RUNNING(thr
->heap
);
1702 act
= thr
->callstack
+ thr
->callstack_top
- 1;
1704 fun
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
1705 DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION((duk_hobject
*) fun
));
1709 #if defined(DUK_USE_EXEC_TIMEOUT_CHECK)
1711 * Execution timeout check
1714 if (DUK_USE_EXEC_TIMEOUT_CHECK(thr
->heap
->heap_udata
)) {
1715 /* Keep throwing an error whenever we get here. The unusual values
1716 * are set this way because no instruction is ever executed, we just
1717 * throw an error until all try/catch/finally and other catchpoints
1718 * have been exhausted. Duktape/C code gets control at each protected
1719 * call but whenever it enters back into Duktape the RangeError gets
1720 * raised. User exec timeout check must consistently indicate a timeout
1721 * until we've fully bubbled out of Duktape.
1723 DUK_D(DUK_DPRINT("execution timeout, throwing a RangeError"));
1724 thr
->interrupt_init
= 0;
1725 thr
->interrupt_counter
= 0;
1726 DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr
->heap
);
1727 DUK_ERROR_RANGE(thr
, "execution timeout");
1729 #endif /* DUK_USE_EXEC_TIMEOUT_CHECK */
1731 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1732 if (!thr
->heap
->dbg_processing
&&
1733 (thr
->heap
->dbg_read_cb
!= NULL
|| thr
->heap
->dbg_detaching
)) {
1734 /* Avoid recursive re-entry; enter when we're attached or
1735 * detaching (to finish off the pending detach).
1737 duk__interrupt_handle_debugger(thr
, &immediate
, &retval
);
1738 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup if changed */
1739 DUK_UNREF(act
); /* 'act' is no longer accessed, scanbuild fix */
1741 #endif /* DUK_USE_DEBUGGER_SUPPORT */
1744 * Update the interrupt counter
1748 /* Cause an interrupt after executing one instruction. */
1752 /* The counter value is one less than the init value: init value should
1753 * indicate how many instructions are executed before interrupt. To
1754 * execute 1 instruction (after interrupt handler return), counter must
1757 DUK_ASSERT(ctr
>= 1);
1758 thr
->interrupt_init
= ctr
;
1759 thr
->interrupt_counter
= ctr
- 1;
1760 DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr
->heap
);
1764 #endif /* DUK_USE_INTERRUPT_COUNTER */
1767 * Debugger handling for executor restart
1769 * Check for breakpoints, stepping, etc, and figure out if we should execute
1770 * in checked or normal mode. Note that we can't do this when an activation
1771 * is created, because breakpoint status (and stepping status) may change
1772 * later, so we must recheck every time we're executing an activation.
1773 * This primitive should be side effect free to avoid changes during check.
1776 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1777 DUK_LOCAL
void duk__executor_recheck_debugger(duk_hthread
*thr
, duk_activation
*act
, duk_hcompiledfunction
*fun
) {
1780 duk_hstring
*filename
;
1781 duk_small_uint_t bp_idx
;
1782 duk_breakpoint
**bp_active
;
1784 DUK_ASSERT(thr
!= NULL
);
1785 DUK_ASSERT(act
!= NULL
);
1786 DUK_ASSERT(fun
!= NULL
);
1789 bp_active
= heap
->dbg_breakpoints_active
;
1790 act
->flags
&= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE
;
1792 tv_tmp
= duk_hobject_find_existing_entry_tval_ptr(thr
->heap
, (duk_hobject
*) fun
, DUK_HTHREAD_STRING_FILE_NAME(thr
));
1793 if (tv_tmp
&& DUK_TVAL_IS_STRING(tv_tmp
)) {
1794 filename
= DUK_TVAL_GET_STRING(tv_tmp
);
1796 /* Figure out all active breakpoints. A breakpoint is
1797 * considered active if the current function's fileName
1798 * matches the breakpoint's fileName, AND there is no
1799 * inner function that has matching line numbers
1800 * (otherwise a breakpoint would be triggered both
1801 * inside and outside of the inner function which would
1802 * be confusing). Example:
1806 * function bar() { <-. breakpoints in these
1807 * print('bar'); | lines should not affect
1808 * } <-' foo() execution
1812 * We need a few things that are only available when
1813 * debugger support is enabled: (1) a line range for
1814 * each function, and (2) access to the function
1815 * template to access the inner functions (and their
1818 * It's important to have a narrow match for active
1819 * breakpoints so that we don't enter checked execution
1820 * when that's not necessary. For instance, if we're
1821 * running inside a certain function and there's
1822 * breakpoint outside in (after the call site), we
1823 * don't want to slow down execution of the function.
1826 for (bp_idx
= 0; bp_idx
< heap
->dbg_breakpoint_count
; bp_idx
++) {
1827 duk_breakpoint
*bp
= heap
->dbg_breakpoints
+ bp_idx
;
1828 duk_hobject
**funcs
, **funcs_end
;
1829 duk_hcompiledfunction
*inner_fun
;
1830 duk_bool_t bp_match
;
1832 if (bp
->filename
== filename
&&
1833 bp
->line
>= fun
->start_line
&& bp
->line
<= fun
->end_line
) {
1835 DUK_DD(DUK_DDPRINT("breakpoint filename and line match: "
1836 "%s:%ld vs. %s (line %ld vs. %ld-%ld)",
1837 DUK_HSTRING_GET_DATA(bp
->filename
),
1839 DUK_HSTRING_GET_DATA(filename
),
1841 (long) fun
->start_line
,
1842 (long) fun
->end_line
));
1844 funcs
= DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr
->heap
, fun
);
1845 funcs_end
= DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr
->heap
, fun
);
1846 while (funcs
!= funcs_end
) {
1847 inner_fun
= (duk_hcompiledfunction
*) *funcs
;
1848 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject
*) inner_fun
));
1849 if (bp
->line
>= inner_fun
->start_line
&& bp
->line
<= inner_fun
->end_line
) {
1850 DUK_DD(DUK_DDPRINT("inner function masks ('captures') breakpoint"));
1858 /* No need to check for size of bp_active list,
1859 * it's always larger than maximum number of
1862 act
->flags
|= DUK_ACT_FLAG_BREAKPOINT_ACTIVE
;
1863 *bp_active
= heap
->dbg_breakpoints
+ bp_idx
;
1870 *bp_active
= NULL
; /* terminate */
1872 DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active
- thr
->heap
->dbg_breakpoints_active
)));
1874 /* Force pause if we were doing "step into" in another activation. */
1875 if (thr
->heap
->dbg_step_thread
!= NULL
&&
1876 thr
->heap
->dbg_step_type
== DUK_STEP_TYPE_INTO
&&
1877 (thr
->heap
->dbg_step_thread
!= thr
||
1878 thr
->heap
->dbg_step_csindex
!= thr
->callstack_top
- 1)) {
1879 DUK_D(DUK_DPRINT("STEP INTO ACTIVE, FORCE PAUSED"));
1880 DUK_HEAP_SET_PAUSED(thr
->heap
);
1883 /* Force interrupt right away if we're paused or in "checked mode".
1884 * Step out is handled by callstack unwind.
1886 if (act
->flags
& (DUK_ACT_FLAG_BREAKPOINT_ACTIVE
) ||
1887 thr
->heap
->dbg_paused
||
1888 (thr
->heap
->dbg_step_type
!= DUK_STEP_TYPE_OUT
&&
1889 thr
->heap
->dbg_step_csindex
== thr
->callstack_top
- 1)) {
1890 /* We'll need to interrupt early so recompute the init
1891 * counter to reflect the number of bytecode instructions
1892 * executed so that step counts for e.g. debugger rate
1893 * limiting are accurate.
1895 DUK_ASSERT(thr
->interrupt_counter
<= thr
->interrupt_init
);
1896 thr
->interrupt_init
= thr
->interrupt_init
- thr
->interrupt_counter
;
1897 thr
->interrupt_counter
= 0;
1900 #endif /* DUK_USE_DEBUGGER_SUPPORT */
1903 * Ecmascript bytecode executor.
1905 * Resume execution for the current thread from its current activation.
1906 * Returns when execution would return from the entry level activation,
1907 * leaving a single return value on top of the stack. Function calls
1908 * and thread resumptions are handled internally. If an error occurs,
1909 * a longjmp() with type DUK_LJ_TYPE_THROW is called on the entry level
1912 * Ecmascript function calls and coroutine resumptions are handled
1913 * internally (by the outer executor function) without recursive C calls.
1914 * Other function calls are handled using duk_handle_call(), increasing
1915 * C recursion depth.
1917 * Abrupt completions (= long control tranfers) are handled either
1918 * directly by reconfiguring relevant stacks and restarting execution,
1919 * or via a longjmp. Longjmp-free handling is preferable for performance
1920 * (especially Emscripten performance), and is used for: break, continue,
1923 * For more detailed notes, see doc/execution.rst.
1925 * Also see doc/code-issues.rst for discussion of setjmp(), longjmp(),
1929 /* Presence of 'fun' is config based, there's a marginal performance
1930 * difference and the best option is architecture dependent.
1932 #if defined(DUK_USE_EXEC_FUN_LOCAL)
1933 #define DUK__FUN() fun
1935 #define DUK__FUN() ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC((thr)->callstack + (thr)->callstack_top - 1))
1937 #define DUK__STRICT() (DUK_HOBJECT_HAS_STRICT((duk_hobject *) DUK__FUN()))
1939 /* Reg/const access macros: these are very footprint and performance sensitive
1940 * so modify with care.
1942 #define DUK__REG(x) (*(thr->valstack_bottom + (x)))
1943 #define DUK__REGP(x) (thr->valstack_bottom + (x))
1944 #define DUK__CONST(x) (*(consts + (x)))
1945 #define DUK__CONSTP(x) (consts + (x))
1947 #define DUK__REGCONST(x) ((x) < DUK_BC_REGLIMIT ? DUK__REG((x)) : DUK__CONST((x) - DUK_BC_REGLIMIT))
1948 #define DUK__REGCONSTP(x) ((x) < DUK_BC_REGLIMIT ? DUK__REGP((x)) : DUK__CONSTP((x) - DUK_BC_REGLIMIT))
1949 #define DUK__REGCONST(x) *((((x) < DUK_BC_REGLIMIT ? thr->valstack_bottom : consts2) + (x)))
1950 #define DUK__REGCONSTP(x) (((x) < DUK_BC_REGLIMIT ? thr->valstack_bottom : consts2) + (x))
1952 /* This macro works when a regconst field is 9 bits, [0,0x1ff]. Adding
1953 * DUK_LIKELY/DUK_UNLIKELY increases code footprint and doesn't seem to
1954 * improve performance on x64 (and actually harms performance in some tests).
1956 #define DUK__RCISREG(x) (((x) & 0x100) == 0)
1957 #define DUK__REGCONST(x) (*((DUK__RCISREG((x)) ? thr->valstack_bottom : consts2) + (x)))
1958 #define DUK__REGCONSTP(x) ((DUK__RCISREG((x)) ? thr->valstack_bottom : consts2) + (x))
1960 #ifdef DUK_USE_VERBOSE_EXECUTOR_ERRORS
1961 #define DUK__INTERNAL_ERROR(msg) do { \
1962 DUK_ERROR_INTERNAL(thr, (msg)); \
1965 #define DUK__INTERNAL_ERROR(msg) do { \
1966 goto internal_error; \
1970 #define DUK__SYNC_CURR_PC() do { \
1971 duk_activation *act; \
1972 act = thr->callstack + thr->callstack_top - 1; \
1973 act->curr_pc = curr_pc; \
1975 #define DUK__SYNC_AND_NULL_CURR_PC() do { \
1976 duk_activation *act; \
1977 act = thr->callstack + thr->callstack_top - 1; \
1978 act->curr_pc = curr_pc; \
1979 thr->ptr_curr_pc = NULL; \
1982 DUK_LOCAL
void duk__handle_executor_error(duk_heap
*heap
,
1983 duk_hthread
*entry_thread
,
1984 duk_size_t entry_callstack_top
,
1985 duk_int_t entry_call_recursion_depth
,
1986 duk_jmpbuf
*entry_jmpbuf_ptr
) {
1987 duk_small_uint_t lj_ret
;
1989 /* Longjmp callers are required to sync-and-null thr->ptr_curr_pc
1992 DUK_ASSERT(heap
->curr_thread
!= NULL
);
1993 DUK_ASSERT(heap
->curr_thread
->ptr_curr_pc
== NULL
);
1995 /* XXX: signalling the need to shrink check (only if unwound) */
1997 /* Must be restored here to handle e.g. yields properly. */
1998 heap
->call_recursion_depth
= entry_call_recursion_depth
;
2000 /* Switch to caller's setjmp() catcher so that if an error occurs
2001 * during error handling, it is always propagated outwards instead
2002 * of causing an infinite loop in our own handler.
2004 heap
->lj
.jmpbuf_ptr
= (duk_jmpbuf
*) entry_jmpbuf_ptr
;
2006 lj_ret
= duk__handle_longjmp(heap
->curr_thread
, entry_thread
, entry_callstack_top
);
2008 if (lj_ret
== DUK__LONGJMP_RESTART
) {
2009 /* Restart bytecode execution, possibly with a changed thread. */
2012 /* Rethrow error to calling state. */
2013 DUK_ASSERT(lj_ret
== DUK__LONGJMP_RETHROW
);
2015 /* Longjmp handling has restored jmpbuf_ptr. */
2016 DUK_ASSERT(heap
->lj
.jmpbuf_ptr
== entry_jmpbuf_ptr
);
2018 /* Thread may have changed, e.g. YIELD converted to THROW. */
2019 duk_err_longjmp(heap
->curr_thread
);
2024 /* Outer executor with setjmp/longjmp handling. */
2025 DUK_INTERNAL
void duk_js_execute_bytecode(duk_hthread
*exec_thr
) {
2026 /* Entry level info. */
2027 duk_hthread
*entry_thread
;
2028 duk_size_t entry_callstack_top
;
2029 duk_int_t entry_call_recursion_depth
;
2030 duk_jmpbuf
*entry_jmpbuf_ptr
;
2031 duk_jmpbuf our_jmpbuf
;
2034 DUK_ASSERT(exec_thr
!= NULL
);
2035 DUK_ASSERT(exec_thr
->heap
!= NULL
);
2036 DUK_ASSERT(exec_thr
->heap
->curr_thread
!= NULL
);
2037 DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR((duk_heaphdr
*) exec_thr
);
2038 DUK_ASSERT(exec_thr
->callstack_top
>= 1); /* at least one activation, ours */
2039 DUK_ASSERT(DUK_ACT_GET_FUNC(exec_thr
->callstack
+ exec_thr
->callstack_top
- 1) != NULL
);
2040 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(exec_thr
->callstack
+ exec_thr
->callstack_top
- 1)));
2042 entry_thread
= exec_thr
;
2043 heap
= entry_thread
->heap
;
2044 entry_callstack_top
= entry_thread
->callstack_top
;
2045 entry_call_recursion_depth
= entry_thread
->heap
->call_recursion_depth
;
2046 entry_jmpbuf_ptr
= entry_thread
->heap
->lj
.jmpbuf_ptr
;
2049 * Note: we currently assume that the setjmp() catchpoint is
2050 * not re-entrant (longjmp() cannot be called more than once
2051 * for a single setjmp()).
2053 * See doc/code-issues.rst for notes on variable assignment
2054 * before and after setjmp().
2058 heap
->lj
.jmpbuf_ptr
= &our_jmpbuf
;
2059 DUK_ASSERT(heap
->lj
.jmpbuf_ptr
!= NULL
);
2061 #if defined(DUK_USE_CPP_EXCEPTIONS)
2064 DUK_ASSERT(heap
->lj
.jmpbuf_ptr
== &our_jmpbuf
);
2065 if (DUK_SETJMP(our_jmpbuf
.jb
) == 0) {
2067 /* Execute bytecode until returned or longjmp(). */
2068 duk__js_execute_bytecode_inner(entry_thread
, entry_callstack_top
);
2070 /* Successful return: restore jmpbuf and return to caller. */
2071 heap
->lj
.jmpbuf_ptr
= entry_jmpbuf_ptr
;
2074 #if defined(DUK_USE_CPP_EXCEPTIONS)
2075 } catch (duk_internal_exception
&exc
) {
2079 #if defined(DUK_USE_CPP_EXCEPTIONS)
2082 DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor"));
2084 duk__handle_executor_error(heap
,
2086 entry_callstack_top
,
2087 entry_call_recursion_depth
,
2090 #if defined(DUK_USE_CPP_EXCEPTIONS)
2091 catch (std::exception
&exc
) {
2092 const char *what
= exc
.what();
2096 DUK_D(DUK_DPRINT("unexpected c++ std::exception (perhaps thrown by user code)"));
2098 DUK_ASSERT(heap
->curr_thread
!= NULL
);
2099 DUK_ERROR_FMT1(heap
->curr_thread
, DUK_ERR_API_ERROR
, "caught invalid c++ std::exception '%s' (perhaps thrown by user code)", what
);
2100 } catch (duk_internal_exception exc
) {
2101 DUK_D(DUK_DPRINT("caught api error thrown from unexpected c++ std::exception"));
2103 duk__handle_executor_error(heap
,
2105 entry_callstack_top
,
2106 entry_call_recursion_depth
,
2110 DUK_D(DUK_DPRINT("unexpected c++ exception (perhaps thrown by user code)"));
2112 DUK_ASSERT(heap
->curr_thread
!= NULL
);
2113 DUK_ERROR_API(heap
->curr_thread
, "caught invalid c++ exception (perhaps thrown by user code)");
2114 } catch (duk_internal_exception exc
) {
2115 DUK_D(DUK_DPRINT("caught api error thrown from unexpected c++ exception"));
2117 duk__handle_executor_error(heap
,
2119 entry_callstack_top
,
2120 entry_call_recursion_depth
,
2130 /* Inner executor, performance critical. */
2131 DUK_LOCAL DUK_NOINLINE
void duk__js_execute_bytecode_inner(duk_hthread
*entry_thread
, duk_size_t entry_callstack_top
) {
2132 /* Current PC, accessed by other functions through thr->ptr_to_curr_pc.
2133 * Critical for performance. It would be safest to make this volatile,
2134 * but that eliminates performance benefits; aliasing guarantees
2135 * should be enough though.
2137 duk_instr_t
*curr_pc
; /* bytecode has a stable pointer */
2139 /* Hot variables for interpretation. Critical for performance,
2140 * but must add sparingly to minimize register shuffling.
2142 duk_hthread
*thr
; /* stable */
2143 duk_tval
*consts
; /* stable */
2144 duk_tval
*consts2
; /* stable; precalculated for faster lookups */
2145 duk_uint_fast32_t ins
;
2146 /* 'funcs' is quite rarely used, so no local for it */
2147 #if defined(DUK_USE_EXEC_FUN_LOCAL)
2148 duk_hcompiledfunction
*fun
;
2150 /* 'fun' is quite rarely used, so no local for it */
2153 #ifdef DUK_USE_INTERRUPT_COUNTER
2157 #ifdef DUK_USE_ASSERTIONS
2158 duk_size_t valstack_top_base
; /* valstack top, should match before interpreting each op (no leftovers) */
2162 * Restart execution by reloading thread state.
2164 * Note that 'thr' and any thread configuration may have changed,
2165 * so all local variables are suspect and we need to reinitialize.
2167 * The number of local variables should be kept to a minimum: if
2168 * the variables are spilled, they will need to be loaded from
2171 * Any 'goto restart_execution;' code path in opcode dispatch must
2172 * ensure 'curr_pc' is synced back to act->curr_pc before the goto
2175 * The interpreter must be very careful with memory pointers, as
2176 * many pointers are not guaranteed to be 'stable' and may be
2177 * reallocated and relocated on-the-fly quite easily (e.g. by a
2178 * memory allocation or a property access).
2180 * The following are assumed to have stable pointers:
2181 * - the current thread
2182 * - the current function
2183 * - the bytecode, constant table, inner function table of the
2184 * current function (as they are a part of the function allocation)
2186 * The following are assumed to have semi-stable pointers:
2187 * - the current activation entry: stable as long as callstack
2188 * is not changed (reallocated by growing or shrinking), or
2189 * by any garbage collection invocation (through finalizers)
2190 * - Note in particular that ANY DECREF can invalidate the
2191 * activation pointer, so for the most part a fresh lookup
2194 * The following are not assumed to have stable pointers at all:
2195 * - the value stack (registers) of the current thread
2196 * - the catch stack of the current thread
2198 * See execution.rst for discussion.
2203 /* Lookup current thread; use the stable 'entry_thread' for this to
2204 * avoid clobber warnings. Any valid, reachable 'thr' value would be
2205 * fine for this, so using 'entry_thread' is just to silence warnings.
2207 thr
= entry_thread
->heap
->curr_thread
;
2208 DUK_ASSERT(thr
!= NULL
);
2209 DUK_ASSERT(thr
->callstack_top
>= 1);
2210 DUK_ASSERT(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1) != NULL
);
2211 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr
->callstack
+ thr
->callstack_top
- 1)));
2213 thr
->ptr_curr_pc
= &curr_pc
;
2215 /* Relookup and initialize dispatch loop variables. Debugger check. */
2217 duk_activation
*act
;
2218 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
2219 duk_hcompiledfunction
*fun
;
2222 /* Assume interrupt init/counter are properly initialized here. */
2223 /* Assume that thr->valstack_bottom has been set-up before getting here. */
2225 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2226 fun
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
2227 DUK_ASSERT(fun
!= NULL
);
2228 DUK_ASSERT(thr
->valstack_top
- thr
->valstack_bottom
== fun
->nregs
);
2229 consts
= DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr
->heap
, fun
);
2230 DUK_ASSERT(consts
!= NULL
);
2231 consts2
= consts
- DUK_BC_REGLIMIT
;
2233 #if defined(DUK_USE_DEBUGGER_SUPPORT)
2234 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
) && !thr
->heap
->dbg_processing
) {
2235 duk__executor_recheck_debugger(thr
, act
, fun
);
2236 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup after side effects (no side effects currently however) */
2238 #endif /* DUK_USE_DEBUGGER_SUPPORT */
2240 #ifdef DUK_USE_ASSERTIONS
2241 valstack_top_base
= (duk_size_t
) (thr
->valstack_top
- thr
->valstack
);
2244 /* Set up curr_pc for opcode dispatch. */
2245 curr_pc
= act
->curr_pc
;
2248 DUK_DD(DUK_DDPRINT("restarting execution, thr %p, act idx %ld, fun %p,"
2249 "consts %p, funcs %p, lev %ld, regbot %ld, regtop %ld, catchstack_top=%ld, "
2252 (long) (thr
->callstack_top
- 1),
2253 (void *) DUK__FUN(),
2254 (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr
->heap
, DUK__FUN()),
2255 (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr
->heap
, DUK__FUN()),
2256 (long) (thr
->callstack_top
- 1),
2257 (long) (thr
->valstack_bottom
- thr
->valstack
),
2258 (long) (thr
->valstack_top
- thr
->valstack
),
2259 (long) thr
->catchstack_top
,
2260 (long) thr
->callstack_preventcount
));
2262 /* Dispatch loop. */
2265 DUK_ASSERT(thr
->callstack_top
>= 1);
2266 DUK_ASSERT(thr
->valstack_top
- thr
->valstack_bottom
== DUK__FUN()->nregs
);
2267 DUK_ASSERT((duk_size_t
) (thr
->valstack_top
- thr
->valstack
) == valstack_top_base
);
2269 /* Executor interrupt counter check, used to implement breakpoints,
2270 * debugging interface, execution timeouts, etc. The counter is heap
2271 * specific but is maintained in the current thread to make the check
2272 * as fast as possible. The counter is copied back to the heap struct
2273 * whenever a thread switch occurs by the DUK_HEAP_SWITCH_THREAD() macro.
2275 #if defined(DUK_USE_INTERRUPT_COUNTER)
2276 int_ctr
= thr
->interrupt_counter
;
2277 if (DUK_LIKELY(int_ctr
> 0)) {
2278 thr
->interrupt_counter
= int_ctr
- 1;
2280 /* Trigger at zero or below */
2281 duk_small_uint_t exec_int_ret
;
2283 /* Write curr_pc back for the debugger. */
2284 DUK_ASSERT(thr
->callstack_top
> 0);
2286 duk_activation
*act
;
2287 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2288 act
->curr_pc
= (duk_instr_t
*) curr_pc
;
2291 /* Force restart caused by a function return; must recheck
2292 * debugger breakpoints before checking line transitions,
2293 * see GH-303. Restart and then handle interrupt_counter
2296 #if defined(DUK_USE_DEBUGGER_SUPPORT)
2297 if (thr
->heap
->dbg_force_restart
) {
2298 DUK_DD(DUK_DDPRINT("dbg_force_restart flag forced restart execution")); /* GH-303 */
2299 thr
->heap
->dbg_force_restart
= 0;
2300 goto restart_execution
;
2304 exec_int_ret
= duk__executor_interrupt(thr
);
2305 if (exec_int_ret
== DUK__INT_RESTART
) {
2306 /* curr_pc synced back above */
2307 goto restart_execution
;
2310 #endif /* DUK_USE_INTERRUPT_COUNTER */
2311 #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
2312 /* For cross-checking during development: ensure dispatch count
2313 * matches cumulative interrupt counter init value sums.
2315 thr
->heap
->inst_count_exec
++;
2318 #if defined(DUK_USE_ASSERTIONS) || defined(DUK_USE_DEBUG)
2320 duk_activation
*act
;
2321 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2322 DUK_ASSERT(curr_pc
>= DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr
->heap
, DUK__FUN()));
2323 DUK_ASSERT(curr_pc
< DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr
->heap
, DUK__FUN()));
2324 DUK_UNREF(act
); /* if debugging disabled */
2326 DUK_DDD(DUK_DDDPRINT("executing bytecode: pc=%ld, ins=0x%08lx, op=%ld, valstack_top=%ld/%ld, nregs=%ld --> %!I",
2327 (long) (curr_pc
- DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr
->heap
, DUK__FUN())),
2328 (unsigned long) *curr_pc
,
2329 (long) DUK_DEC_OP(*curr_pc
),
2330 (long) (thr
->valstack_top
- thr
->valstack
),
2331 (long) (thr
->valstack_end
- thr
->valstack
),
2332 (long) (DUK__FUN() ? DUK__FUN()->nregs
: -1),
2333 (duk_instr_t
) *curr_pc
));
2337 #if defined(DUK_USE_ASSERTIONS)
2338 /* Quite heavy assert: check valstack policy. Improper
2339 * shuffle instructions can write beyond valstack_top/end
2340 * so this check catches them in the act.
2344 tv
= thr
->valstack_top
;
2345 while (tv
!= thr
->valstack_end
) {
2346 DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv
));
2354 /* Typing: use duk_small_(u)int_fast_t when decoding small
2355 * opcode fields (op, A, B, C) and duk_(u)int_fast_t when
2356 * decoding larger fields (e.g. BC which is 18 bits). Use
2357 * unsigned variant by default, signed when the value is used
2358 * in signed arithmetic. Using variable names such as 'a', 'b',
2359 * 'c', 'bc', etc makes it easier to spot typing mismatches.
2362 /* XXX: the best typing needs to be validated by perf measurement:
2363 * e.g. using a small type which is the cast to a larger duk_idx_t
2364 * may be slower than declaring the variable as a duk_idx_t in the
2368 /* XXX: use macros for the repetitive tval/refcount handling. */
2370 switch ((int) DUK_DEC_OP(ins
)) {
2371 /* XXX: switch cast? */
2373 case DUK_OP_LDREG
: {
2374 duk_small_uint_fast_t a
;
2376 duk_tval
*tv1
, *tv2
;
2378 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2379 bc
= DUK_DEC_BC(ins
); tv2
= DUK__REGP(bc
);
2380 DUK_TVAL_SET_TVAL_UPDREF_FAST(thr
, tv1
, tv2
); /* side effects */
2384 case DUK_OP_STREG
: {
2385 duk_small_uint_fast_t a
;
2387 duk_tval
*tv1
, *tv2
;
2389 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2390 bc
= DUK_DEC_BC(ins
); tv2
= DUK__REGP(bc
);
2391 DUK_TVAL_SET_TVAL_UPDREF_FAST(thr
, tv2
, tv1
); /* side effects */
2395 case DUK_OP_LDCONST
: {
2396 duk_small_uint_fast_t a
;
2398 duk_tval
*tv1
, *tv2
;
2400 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2401 bc
= DUK_DEC_BC(ins
); tv2
= DUK__CONSTP(bc
);
2402 DUK_TVAL_SET_TVAL_UPDREF_FAST(thr
, tv1
, tv2
); /* side effects */
2406 case DUK_OP_LDINT
: {
2407 duk_small_uint_fast_t a
;
2410 #if defined(DUK_USE_FASTINT)
2416 #if defined(DUK_USE_FASTINT)
2417 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2418 bc
= DUK_DEC_BC(ins
); val
= (duk_int32_t
) (bc
- DUK_BC_LDINT_BIAS
);
2419 DUK_TVAL_SET_FASTINT_I32_UPDREF(thr
, tv1
, val
); /* side effects */
2421 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2422 bc
= DUK_DEC_BC(ins
); val
= (duk_double_t
) (bc
- DUK_BC_LDINT_BIAS
);
2423 DUK_TVAL_SET_NUMBER_UPDREF(thr
, tv1
, val
); /* side effects */
2428 case DUK_OP_LDINTX
: {
2429 duk_small_uint_fast_t a
;
2433 /* LDINTX is not necessarily in FASTINT range, so
2434 * no fast path for now.
2436 * XXX: perhaps restrict LDINTX to fastint range, wider
2437 * range very rarely needed.
2440 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2441 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
2442 val
= DUK_TVAL_GET_NUMBER(tv1
) * ((duk_double_t
) (1L << DUK_BC_LDINTX_SHIFT
)) +
2443 (duk_double_t
) DUK_DEC_BC(ins
);
2444 #if defined(DUK_USE_FASTINT)
2445 DUK_TVAL_SET_NUMBER_CHKFAST(tv1
, val
);
2447 DUK_TVAL_SET_NUMBER(tv1
, val
);
2452 case DUK_OP_MPUTOBJ
:
2453 case DUK_OP_MPUTOBJI
: {
2454 duk_context
*ctx
= (duk_context
*) thr
;
2455 duk_small_uint_fast_t a
;
2458 duk_uint_fast_t idx
;
2459 duk_small_uint_fast_t count
;
2461 /* A -> register of target object
2462 * B -> first register of key/value pair list
2463 * C -> number of key/value pairs
2466 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2467 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1
));
2468 obj
= DUK_TVAL_GET_OBJECT(tv1
);
2470 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
2471 if (DUK_DEC_OP(ins
) == DUK_OP_MPUTOBJI
) {
2472 duk_tval
*tv_ind
= DUK__REGP(idx
);
2473 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2474 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2477 count
= (duk_small_uint_fast_t
) DUK_DEC_C(ins
);
2479 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2480 if (DUK_UNLIKELY(idx
+ count
* 2 > (duk_uint_fast_t
) duk_get_top(ctx
))) {
2481 /* XXX: use duk_is_valid_index() instead? */
2482 /* XXX: improve check; check against nregs, not against top */
2483 DUK__INTERNAL_ERROR("MPUTOBJ out of bounds");
2487 duk_push_hobject(ctx
, obj
);
2490 /* XXX: faster initialization (direct access or better primitives) */
2492 duk_push_tval(ctx
, DUK__REGP(idx
));
2493 DUK_ASSERT(duk_is_string(ctx
, -1));
2494 duk_push_tval(ctx
, DUK__REGP(idx
+ 1)); /* -> [... obj key value] */
2495 duk_xdef_prop_wec(ctx
, -3); /* -> [... obj] */
2501 duk_pop(ctx
); /* [... obj] -> [...] */
2505 case DUK_OP_MPUTARR
:
2506 case DUK_OP_MPUTARRI
: {
2507 duk_context
*ctx
= (duk_context
*) thr
;
2508 duk_small_uint_fast_t a
;
2511 duk_uint_fast_t idx
;
2512 duk_small_uint_fast_t count
;
2513 duk_uint32_t arr_idx
;
2515 /* A -> register of target object
2516 * B -> first register of value data (start_index, value1, value2, ..., valueN)
2517 * C -> number of key/value pairs (N)
2520 a
= DUK_DEC_A(ins
); tv1
= DUK__REGP(a
);
2521 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1
));
2522 obj
= DUK_TVAL_GET_OBJECT(tv1
);
2523 DUK_ASSERT(obj
!= NULL
);
2525 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
2526 if (DUK_DEC_OP(ins
) == DUK_OP_MPUTARRI
) {
2527 duk_tval
*tv_ind
= DUK__REGP(idx
);
2528 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2529 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2532 count
= (duk_small_uint_fast_t
) DUK_DEC_C(ins
);
2534 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2535 if (idx
+ count
+ 1 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2536 /* XXX: use duk_is_valid_index() instead? */
2537 /* XXX: improve check; check against nregs, not against top */
2538 DUK__INTERNAL_ERROR("MPUTARR out of bounds");
2542 tv1
= DUK__REGP(idx
);
2543 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
2544 arr_idx
= (duk_uint32_t
) DUK_TVAL_GET_NUMBER(tv1
);
2547 duk_push_hobject(ctx
, obj
);
2550 /* duk_xdef_prop() will define an own property without any array
2551 * special behaviors. We'll need to set the array length explicitly
2552 * in the end. For arrays with elisions, the compiler will emit an
2553 * explicit SETALEN which will update the length.
2556 /* XXX: because we're dealing with 'own' properties of a fresh array,
2557 * the array initializer should just ensure that the array has a large
2558 * enough array part and write the values directly into array part,
2559 * and finally set 'length' manually in the end (as already happens now).
2562 duk_push_tval(ctx
, DUK__REGP(idx
)); /* -> [... obj value] */
2563 duk_xdef_prop_index_wec(ctx
, -2, arr_idx
); /* -> [... obj] */
2565 /* XXX: could use at least one fewer loop counters */
2571 /* XXX: E5.1 Section 11.1.4 coerces the final length through
2572 * ToUint32() which is odd but happens now as a side effect of
2575 duk_hobject_set_length(thr
, obj
, (duk_uint32_t
) arr_idx
);
2577 duk_pop(ctx
); /* [... obj] -> [...] */
2583 duk_context
*ctx
= (duk_context
*) thr
;
2584 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2585 duk_uint_fast_t idx
;
2586 duk_small_uint_fast_t i
;
2588 /* A -> unused (reserved for flags, for consistency with DUK_OP_CALL)
2589 * B -> target register and start reg: constructor, arg1, ..., argN
2590 * (for DUK_OP_NEWI, 'b' is indirect)
2594 /* duk_new() will call the constuctor using duk_handle_call().
2595 * A constructor call prevents a yield from inside the constructor,
2596 * even if the constructor is an Ecmascript function.
2599 /* Don't need to sync curr_pc here; duk_new() will do that
2600 * when it augments the created error.
2603 /* XXX: unnecessary copying of values? Just set 'top' to
2604 * b + c, and let the return handling fix up the stack frame?
2607 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
2608 if (DUK_DEC_OP(ins
) == DUK_OP_NEWI
) {
2609 duk_tval
*tv_ind
= DUK__REGP(idx
);
2610 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2611 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2614 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2615 if (idx
+ c
+ 1 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2616 /* XXX: use duk_is_valid_index() instead? */
2617 /* XXX: improve check; check against nregs, not against top */
2618 DUK__INTERNAL_ERROR("NEW out of bounds");
2622 duk_require_stack(ctx
, (duk_idx_t
) c
);
2623 duk_push_tval(ctx
, DUK__REGP(idx
));
2624 for (i
= 0; i
< c
; i
++) {
2625 duk_push_tval(ctx
, DUK__REGP(idx
+ i
+ 1));
2627 duk_new(ctx
, (duk_idx_t
) c
); /* [... constructor arg1 ... argN] -> [retval] */
2628 DUK_DDD(DUK_DDDPRINT("NEW -> %!iT", (duk_tval
*) duk_get_tval(ctx
, -1)));
2629 duk_replace(ctx
, (duk_idx_t
) idx
);
2631 /* When debugger is enabled, we need to recheck the activation
2632 * status after returning. This is now handled by call handling
2633 * and heap->dbg_force_restart.
2638 case DUK_OP_REGEXP
: {
2639 #ifdef DUK_USE_REGEXP_SUPPORT
2640 duk_context
*ctx
= (duk_context
*) thr
;
2641 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2642 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2643 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2645 /* A -> target register
2646 * B -> bytecode (also contains flags)
2647 * C -> escaped source
2650 duk_push_tval(ctx
, DUK__REGCONSTP(c
));
2651 duk_push_tval(ctx
, DUK__REGCONSTP(b
)); /* -> [ ... escaped_source bytecode ] */
2652 duk_regexp_create_instance(thr
); /* -> [ ... regexp_instance ] */
2653 DUK_DDD(DUK_DDDPRINT("regexp instance: %!iT", (duk_tval
*) duk_get_tval(ctx
, -1)));
2654 duk_replace(ctx
, (duk_idx_t
) a
);
2656 /* The compiler should never emit DUK_OP_REGEXP if there is no
2659 DUK__INTERNAL_ERROR("no regexp support");
2666 case DUK_OP_CSREGI
: {
2668 * Assuming a register binds to a variable declared within this
2669 * function (a declarative binding), the 'this' for the call
2670 * setup is always 'undefined'. E5 Section 10.2.1.1.6.
2673 duk_context
*ctx
= (duk_context
*) thr
;
2674 duk_small_uint_fast_t b
= DUK_DEC_B(ins
); /* restricted to regs */
2675 duk_uint_fast_t idx
;
2677 /* A -> target register (A, A+1) for call setup
2678 * (for DUK_OP_CSREGI, 'a' is indirect)
2679 * B -> register containing target function (not type checked here)
2682 /* XXX: direct manipulation, or duk_replace_tval() */
2684 /* Note: target registers a and a+1 may overlap with DUK__REGP(b).
2688 idx
= (duk_uint_fast_t
) DUK_DEC_A(ins
);
2689 if (DUK_DEC_OP(ins
) == DUK_OP_CSREGI
) {
2690 duk_tval
*tv_ind
= DUK__REGP(idx
);
2691 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2692 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2695 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2696 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2697 /* XXX: use duk_is_valid_index() instead? */
2698 /* XXX: improve check; check against nregs, not against top */
2699 DUK__INTERNAL_ERROR("CSREG out of bounds");
2703 duk_push_tval(ctx
, DUK__REGP(b
));
2704 duk_replace(ctx
, (duk_idx_t
) idx
);
2705 duk_push_undefined(ctx
);
2706 duk_replace(ctx
, (duk_idx_t
) (idx
+ 1));
2710 case DUK_OP_GETVAR
: {
2711 duk_context
*ctx
= (duk_context
*) thr
;
2712 duk_activation
*act
;
2713 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2714 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
2718 tv1
= DUK__CONSTP(bc
);
2719 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2720 name
= DUK_TVAL_GET_STRING(tv1
);
2721 DUK_ASSERT(name
!= NULL
);
2722 DUK_DDD(DUK_DDDPRINT("GETVAR: '%!O'", (duk_heaphdr
*) name
));
2723 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2724 (void) duk_js_getvar_activation(thr
, act
, name
, 1 /*throw*/); /* -> [... val this] */
2726 duk_pop(ctx
); /* 'this' binding is not needed here */
2727 duk_replace(ctx
, (duk_idx_t
) a
);
2731 case DUK_OP_PUTVAR
: {
2732 duk_activation
*act
;
2733 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2734 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
2738 tv1
= DUK__CONSTP(bc
);
2739 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2740 name
= DUK_TVAL_GET_STRING(tv1
);
2741 DUK_ASSERT(name
!= NULL
);
2743 /* XXX: putvar takes a duk_tval pointer, which is awkward and
2744 * should be reworked.
2747 tv1
= DUK__REGP(a
); /* val */
2748 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2749 duk_js_putvar_activation(thr
, act
, name
, tv1
, DUK__STRICT());
2753 case DUK_OP_DECLVAR
: {
2754 duk_activation
*act
;
2755 duk_context
*ctx
= (duk_context
*) thr
;
2756 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2757 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2758 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2761 duk_small_uint_t prop_flags
;
2762 duk_bool_t is_func_decl
;
2763 duk_bool_t is_undef_value
;
2765 tv1
= DUK__REGCONSTP(b
);
2766 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2767 name
= DUK_TVAL_GET_STRING(tv1
);
2768 DUK_ASSERT(name
!= NULL
);
2770 is_undef_value
= ((a
& DUK_BC_DECLVAR_FLAG_UNDEF_VALUE
) != 0);
2771 is_func_decl
= ((a
& DUK_BC_DECLVAR_FLAG_FUNC_DECL
) != 0);
2773 /* XXX: declvar takes an duk_tval pointer, which is awkward and
2774 * should be reworked.
2777 /* Compiler is responsible for selecting property flags (configurability,
2778 * writability, etc).
2780 prop_flags
= a
& DUK_PROPDESC_FLAGS_MASK
;
2782 if (is_undef_value
) {
2783 duk_push_undefined(ctx
);
2785 duk_push_tval(ctx
, DUK__REGCONSTP(c
));
2787 tv1
= DUK_GET_TVAL_NEGIDX(ctx
, -1);
2789 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2790 if (duk_js_declvar_activation(thr
, act
, name
, tv1
, prop_flags
, is_func_decl
)) {
2791 /* already declared, must update binding value */
2792 tv1
= DUK_GET_TVAL_NEGIDX(ctx
, -1);
2793 duk_js_putvar_activation(thr
, act
, name
, tv1
, DUK__STRICT());
2800 case DUK_OP_DELVAR
: {
2801 duk_activation
*act
;
2802 duk_context
*ctx
= (duk_context
*) thr
;
2803 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2804 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2809 tv1
= DUK__REGCONSTP(b
);
2810 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2811 name
= DUK_TVAL_GET_STRING(tv1
);
2812 DUK_ASSERT(name
!= NULL
);
2813 DUK_DDD(DUK_DDDPRINT("DELVAR '%!O'", (duk_heaphdr
*) name
));
2814 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2815 rc
= duk_js_delvar_activation(thr
, act
, name
);
2817 duk_push_boolean(ctx
, rc
);
2818 duk_replace(ctx
, (duk_idx_t
) a
);
2823 case DUK_OP_CSVARI
: {
2827 * The only (standard) case where the 'this' binding is non-null is when
2828 * (1) the variable is found in an object environment record, and
2829 * (2) that object environment record is a 'with' block.
2833 duk_context
*ctx
= (duk_context
*) thr
;
2834 duk_activation
*act
;
2835 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2836 duk_uint_fast_t idx
;
2840 tv1
= DUK__REGCONSTP(b
);
2841 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
2842 name
= DUK_TVAL_GET_STRING(tv1
);
2843 DUK_ASSERT(name
!= NULL
);
2844 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2845 (void) duk_js_getvar_activation(thr
, act
, name
, 1 /*throw*/); /* -> [... val this] */
2847 /* Note: target registers a and a+1 may overlap with DUK__REGCONSTP(b)
2848 * and DUK__REGCONSTP(c). Careful here.
2851 idx
= (duk_uint_fast_t
) DUK_DEC_A(ins
);
2852 if (DUK_DEC_OP(ins
) == DUK_OP_CSVARI
) {
2853 duk_tval
*tv_ind
= DUK__REGP(idx
);
2854 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
2855 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
2858 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
2859 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
2860 /* XXX: use duk_is_valid_index() instead? */
2861 /* XXX: improve check; check against nregs, not against top */
2862 DUK__INTERNAL_ERROR("CSVAR out of bounds");
2866 duk_replace(ctx
, (duk_idx_t
) (idx
+ 1)); /* 'this' binding */
2867 duk_replace(ctx
, (duk_idx_t
) idx
); /* variable value (function, we hope, not checked here) */
2871 case DUK_OP_CLOSURE
: {
2872 duk_context
*ctx
= (duk_context
*) thr
;
2873 duk_activation
*act
;
2874 duk_hcompiledfunction
*fun
;
2875 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2876 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
2877 duk_hobject
*fun_temp
;
2880 * BC -> inner function index
2883 DUK_DDD(DUK_DDDPRINT("CLOSURE to target register %ld, fnum %ld (count %ld)",
2884 (long) a
, (long) bc
, (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr
->heap
, DUK__FUN())));
2886 DUK_ASSERT_DISABLE(bc
>= 0); /* unsigned */
2887 DUK_ASSERT((duk_uint_t
) bc
< (duk_uint_t
) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr
->heap
, DUK__FUN()));
2889 act
= thr
->callstack
+ thr
->callstack_top
- 1;
2890 fun
= (duk_hcompiledfunction
*) DUK_ACT_GET_FUNC(act
);
2891 fun_temp
= DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr
->heap
, fun
)[bc
];
2892 DUK_ASSERT(fun_temp
!= NULL
);
2893 DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(fun_temp
));
2895 DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O",
2896 (void *) fun_temp
, (duk_heaphdr
*) fun_temp
));
2898 if (act
->lex_env
== NULL
) {
2899 DUK_ASSERT(act
->var_env
== NULL
);
2900 duk_js_init_activation_environment_records_delayed(thr
, act
);
2902 DUK_ASSERT(act
->lex_env
!= NULL
);
2903 DUK_ASSERT(act
->var_env
!= NULL
);
2905 /* functions always have a NEWENV flag, i.e. they get a
2906 * new variable declaration environment, so only lex_env
2909 duk_js_push_closure(thr
,
2910 (duk_hcompiledfunction
*) fun_temp
,
2913 1 /*add_auto_proto*/);
2914 duk_replace(ctx
, (duk_idx_t
) a
);
2919 case DUK_OP_GETPROP
: {
2920 duk_context
*ctx
= (duk_context
*) thr
;
2921 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2922 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2923 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2929 * B -> object reg/const (may be const e.g. in "'foo'[1]")
2930 * C -> key reg/const
2933 tv_obj
= DUK__REGCONSTP(b
);
2934 tv_key
= DUK__REGCONSTP(c
);
2935 DUK_DDD(DUK_DDDPRINT("GETPROP: a=%ld obj=%!T, key=%!T",
2937 (duk_tval
*) DUK__REGCONSTP(b
),
2938 (duk_tval
*) DUK__REGCONSTP(c
)));
2939 rc
= duk_hobject_getprop(thr
, tv_obj
, tv_key
); /* -> [val] */
2940 DUK_UNREF(rc
); /* ignore */
2941 DUK_DDD(DUK_DDDPRINT("GETPROP --> %!T",
2942 (duk_tval
*) duk_get_tval(ctx
, -1)));
2943 tv_obj
= NULL
; /* invalidated */
2944 tv_key
= NULL
; /* invalidated */
2946 duk_replace(ctx
, (duk_idx_t
) a
); /* val */
2950 case DUK_OP_PUTPROP
: {
2951 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2952 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2953 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2960 * B -> key reg/const
2961 * C -> value reg/const
2963 * Note: intentional difference to register arrangement
2964 * of e.g. GETPROP; 'A' must contain a register-only value.
2967 tv_obj
= DUK__REGP(a
);
2968 tv_key
= DUK__REGCONSTP(b
);
2969 tv_val
= DUK__REGCONSTP(c
);
2970 DUK_DDD(DUK_DDDPRINT("PUTPROP: obj=%!T, key=%!T, val=%!T",
2971 (duk_tval
*) DUK__REGP(a
),
2972 (duk_tval
*) DUK__REGCONSTP(b
),
2973 (duk_tval
*) DUK__REGCONSTP(c
)));
2974 rc
= duk_hobject_putprop(thr
, tv_obj
, tv_key
, tv_val
, DUK__STRICT());
2975 DUK_UNREF(rc
); /* ignore */
2976 DUK_DDD(DUK_DDDPRINT("PUTPROP --> obj=%!T, key=%!T, val=%!T",
2977 (duk_tval
*) DUK__REGP(a
),
2978 (duk_tval
*) DUK__REGCONSTP(b
),
2979 (duk_tval
*) DUK__REGCONSTP(c
)));
2980 tv_obj
= NULL
; /* invalidated */
2981 tv_key
= NULL
; /* invalidated */
2982 tv_val
= NULL
; /* invalidated */
2987 case DUK_OP_DELPROP
: {
2988 duk_context
*ctx
= (duk_context
*) thr
;
2989 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
2990 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
2991 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
2998 * C -> key reg/const
3001 tv_obj
= DUK__REGP(b
);
3002 tv_key
= DUK__REGCONSTP(c
);
3003 rc
= duk_hobject_delprop(thr
, tv_obj
, tv_key
, DUK__STRICT());
3004 tv_obj
= NULL
; /* invalidated */
3005 tv_key
= NULL
; /* invalidated */
3007 duk_push_boolean(ctx
, rc
);
3008 duk_replace(ctx
, (duk_idx_t
) a
); /* result */
3013 case DUK_OP_CSPROPI
: {
3014 duk_context
*ctx
= (duk_context
*) thr
;
3015 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3016 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3017 duk_uint_fast_t idx
;
3022 /* E5 Section 11.2.3, step 6.a.i */
3023 /* E5 Section 10.4.3 */
3025 /* XXX: allow object to be a const, e.g. in 'foo'.toString()?
3026 * On the other hand, DUK_REGCONSTP() is slower and generates
3030 tv_obj
= DUK__REGP(b
);
3031 tv_key
= DUK__REGCONSTP(c
);
3032 rc
= duk_hobject_getprop(thr
, tv_obj
, tv_key
); /* -> [val] */
3033 DUK_UNREF(rc
); /* unused */
3034 tv_obj
= NULL
; /* invalidated */
3035 tv_key
= NULL
; /* invalidated */
3037 /* Note: target registers a and a+1 may overlap with DUK__REGP(b)
3038 * and DUK__REGCONSTP(c). Careful here.
3041 idx
= (duk_uint_fast_t
) DUK_DEC_A(ins
);
3042 if (DUK_DEC_OP(ins
) == DUK_OP_CSPROPI
) {
3043 duk_tval
*tv_ind
= DUK__REGP(idx
);
3044 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
3045 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
3048 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
3049 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
3050 /* XXX: use duk_is_valid_index() instead? */
3051 /* XXX: improve check; check against nregs, not against top */
3052 DUK__INTERNAL_ERROR("CSPROP out of bounds");
3056 duk_push_tval(ctx
, DUK__REGP(b
)); /* [ ... val obj ] */
3057 duk_replace(ctx
, (duk_idx_t
) (idx
+ 1)); /* 'this' binding */
3058 duk_replace(ctx
, (duk_idx_t
) idx
); /* val */
3067 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3068 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3069 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3070 duk_small_uint_fast_t op
= DUK_DEC_OP(ins
);
3072 if (op
== DUK_OP_ADD
) {
3074 * Handling DUK_OP_ADD this way is more compact (experimentally)
3075 * than a separate case with separate argument decoding.
3077 duk__vm_arith_add(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
), a
);
3079 duk__vm_arith_binary_op(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
), a
, op
);
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
);
3093 duk_small_uint_fast_t op
= DUK_DEC_OP(ins
);
3095 duk__vm_bitwise_binary_op(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
), a
, op
);
3101 duk_context
*ctx
= (duk_context
*) thr
;
3102 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3103 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3104 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3107 /* E5 Sections 11.9.1, 11.9.3 */
3108 tmp
= duk_js_equals(thr
, DUK__REGCONSTP(b
), DUK__REGCONSTP(c
));
3109 if (DUK_DEC_OP(ins
) == DUK_OP_NEQ
) {
3112 duk_push_boolean(ctx
, tmp
);
3113 duk_replace(ctx
, (duk_idx_t
) a
);
3119 duk_context
*ctx
= (duk_context
*) thr
;
3120 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3121 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3122 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3125 /* E5 Sections 11.9.1, 11.9.3 */
3126 tmp
= duk_js_strict_equals(DUK__REGCONSTP(b
), DUK__REGCONSTP(c
));
3127 if (DUK_DEC_OP(ins
) == DUK_OP_SNEQ
) {
3130 duk_push_boolean(ctx
, tmp
);
3131 duk_replace(ctx
, (duk_idx_t
) a
);
3135 /* Note: combining comparison ops must be done carefully because
3136 * of uncomparable values (NaN): it's not necessarily true that
3137 * (x >= y) === !(x < y). Also, evaluation order matters, and
3138 * although it would only seem to affect the compiler this is
3139 * actually not the case, because there are also run-time coercions
3140 * of the arguments (with potential side effects).
3142 * XXX: can be combined; check code size.
3146 duk_context
*ctx
= (duk_context
*) thr
;
3147 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3148 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3149 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3152 /* x > y --> y < x */
3153 tmp
= duk_js_compare_helper(thr
,
3154 DUK__REGCONSTP(c
), /* y */
3155 DUK__REGCONSTP(b
), /* x */
3158 duk_push_boolean(ctx
, tmp
);
3159 duk_replace(ctx
, (duk_idx_t
) a
);
3164 duk_context
*ctx
= (duk_context
*) thr
;
3165 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3166 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3167 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3170 /* x >= y --> not (x < y) */
3171 tmp
= duk_js_compare_helper(thr
,
3172 DUK__REGCONSTP(b
), /* x */
3173 DUK__REGCONSTP(c
), /* y */
3174 DUK_COMPARE_FLAG_EVAL_LEFT_FIRST
|
3175 DUK_COMPARE_FLAG_NEGATE
); /* flags */
3177 duk_push_boolean(ctx
, tmp
);
3178 duk_replace(ctx
, (duk_idx_t
) a
);
3183 duk_context
*ctx
= (duk_context
*) thr
;
3184 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3185 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3186 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3190 tmp
= duk_js_compare_helper(thr
,
3191 DUK__REGCONSTP(b
), /* x */
3192 DUK__REGCONSTP(c
), /* y */
3193 DUK_COMPARE_FLAG_EVAL_LEFT_FIRST
); /* flags */
3195 duk_push_boolean(ctx
, tmp
);
3196 duk_replace(ctx
, (duk_idx_t
) a
);
3201 duk_context
*ctx
= (duk_context
*) thr
;
3202 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3203 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3204 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3207 /* x <= y --> not (x > y) --> not (y < x) */
3208 tmp
= duk_js_compare_helper(thr
,
3209 DUK__REGCONSTP(c
), /* y */
3210 DUK__REGCONSTP(b
), /* x */
3211 DUK_COMPARE_FLAG_NEGATE
); /* flags */
3213 duk_push_boolean(ctx
, tmp
);
3214 duk_replace(ctx
, (duk_idx_t
) a
);
3219 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3220 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3223 tmp
= duk_js_toboolean(DUK__REGCONSTP(b
));
3224 if (tmp
== (duk_bool_t
) a
) {
3225 /* if boolean matches A, skip next inst */
3234 duk_int_fast_t abc
= DUK_DEC_ABC(ins
);
3236 curr_pc
+= abc
- DUK_BC_JUMP_BIAS
;
3240 case DUK_OP_RETURN
: {
3241 duk_context
*ctx
= (duk_context
*) thr
;
3242 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3243 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3244 /* duk_small_uint_fast_t c = DUK_DEC_C(ins); */
3245 duk_small_uint_t ret_result
;
3248 * B -> return value reg/const
3249 * C -> currently unused
3252 DUK__SYNC_AND_NULL_CURR_PC();
3254 /* duk__handle_return() is guaranteed never to throw, except
3255 * for potential out-of-memory situations which will then
3256 * propagate out of the executor longjmp handler.
3259 if (a
& DUK_BC_RETURN_FLAG_HAVE_RETVAL
) {
3260 duk_push_tval(ctx
, DUK__REGCONSTP(b
));
3262 duk_push_undefined(ctx
);
3264 ret_result
= duk__handle_return(thr
,
3266 entry_callstack_top
);
3267 if (ret_result
== DUK__RETHAND_RESTART
) {
3268 goto restart_execution
;
3270 DUK_ASSERT(ret_result
== DUK__RETHAND_FINISHED
);
3272 DUK_DDD(DUK_DDDPRINT("exiting executor after RETURN handling"));
3277 case DUK_OP_CALLI
: {
3278 duk_context
*ctx
= (duk_context
*) thr
;
3279 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3280 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3281 duk_uint_fast_t idx
;
3282 duk_small_uint_t call_flags
;
3283 duk_small_uint_t flag_tailcall
;
3284 duk_small_uint_t flag_evalcall
;
3286 duk_hobject
*obj_func
;
3287 duk_bool_t setup_rc
;
3288 duk_idx_t num_stack_args
;
3289 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
3290 duk_hcompiledfunction
*fun
;
3294 * B -> base register for call (base -> func, base+1 -> this, base+2 -> arg1 ... base+2+N-1 -> argN)
3295 * (for DUK_OP_CALLI, 'b' is indirect)
3299 /* these are not necessarily 0 or 1 (may be other non-zero), that's ok */
3300 flag_tailcall
= (a
& DUK_BC_CALL_FLAG_TAILCALL
);
3301 flag_evalcall
= (a
& DUK_BC_CALL_FLAG_EVALCALL
);
3303 idx
= (duk_uint_fast_t
) DUK_DEC_B(ins
);
3304 if (DUK_DEC_OP(ins
) == DUK_OP_CALLI
) {
3305 duk_tval
*tv_ind
= DUK__REGP(idx
);
3306 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
3307 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
3310 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
3311 if (!duk_is_valid_index(ctx
, (duk_idx_t
) idx
)) {
3312 /* XXX: improve check; check against nregs, not against top */
3313 DUK__INTERNAL_ERROR("CALL out of bounds");
3318 * To determine whether to use an optimized Ecmascript-to-Ecmascript
3319 * call, we need to know whether the final, non-bound function is an
3320 * Ecmascript function.
3322 * This is now implemented so that we start to do an ecma-to-ecma call
3323 * setup which will resolve the bound chain as the first thing. If the
3324 * final function is not eligible, the return value indicates that the
3325 * ecma-to-ecma call is not possible. The setup will overwrite the call
3326 * target at DUK__REGP(idx) with the final, non-bound function (which
3327 * may be a lightfunc), and fudge arguments if necessary.
3329 * XXX: If an ecma-to-ecma call is not possible, this initial call
3330 * setup will do bound function chain resolution but won't do the
3331 * "effective this binding" resolution which is quite confusing.
3332 * Perhaps add a helper for doing bound function and effective this
3333 * binding resolution - and call that explicitly? Ecma-to-ecma call
3334 * setup and normal function handling can then assume this prestep has
3335 * been done by the caller.
3338 duk_set_top(ctx
, (duk_idx_t
) (idx
+ c
+ 2)); /* [ ... func this arg1 ... argN ] */
3341 if (flag_tailcall
) {
3342 /* We request a tail call, but in some corner cases
3343 * call handling can decide that a tail call is
3344 * actually not possible.
3345 * See: test-bug-tailcall-preventyield-assert.c.
3347 call_flags
|= DUK_CALL_FLAG_IS_TAILCALL
;
3350 /* Compared to duk_handle_call():
3351 * - protected call: never
3352 * - ignore recursion limit: never
3355 setup_rc
= duk_handle_ecma_call_setup(thr
,
3360 /* Ecma-to-ecma call possible, may or may not be a tail call.
3361 * Avoid C recursion by being clever.
3363 DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call setup possible, restart execution"));
3364 /* curr_pc synced by duk_handle_ecma_call_setup() */
3365 goto restart_execution
;
3367 DUK_ASSERT(thr
->ptr_curr_pc
!= NULL
); /* restored if ecma-to-ecma setup fails */
3369 DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call not possible, target is native (may be lightfunc)"));
3371 /* Recompute argument count: bound function handling may have shifted. */
3372 num_stack_args
= duk_get_top(ctx
) - (idx
+ 2);
3373 DUK_DDD(DUK_DDDPRINT("recomputed arg count: %ld\n", (long) num_stack_args
));
3375 tv_func
= DUK__REGP(idx
); /* Relookup if relocated */
3376 if (DUK_TVAL_IS_LIGHTFUNC(tv_func
)) {
3378 call_flags
= 0; /* not protected, respect reclimit, not constructor */
3380 /* There is no eval() special handling here: eval() is never
3381 * automatically converted to a lightfunc.
3383 DUK_ASSERT(DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func
) != duk_bi_global_object_eval
);
3385 duk_handle_call_unprotected(thr
,
3389 /* duk_js_call.c is required to restore the stack reserve
3390 * so we only need to reset the top.
3392 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
3395 duk_set_top(ctx
, (duk_idx_t
) fun
->nregs
);
3397 /* No need to reinit setjmp() catchpoint, as call handling
3398 * will store and restore our state.
3401 /* Call setup checks callability. */
3402 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_func
));
3403 obj_func
= DUK_TVAL_GET_OBJECT(tv_func
);
3404 DUK_ASSERT(obj_func
!= NULL
);
3405 DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(obj_func
));
3408 * Other cases, use C recursion.
3410 * If a tail call was requested we ignore it and execute a normal call.
3411 * Since Duktape 0.11.0 the compiler emits a RETURN opcode even after
3412 * a tail call to avoid test-bug-tailcall-thread-yield-resume.js.
3414 * Direct eval call: (1) call target (before following bound function
3415 * chain) is the built-in eval() function, and (2) call was made with
3416 * the identifier 'eval'.
3419 call_flags
= 0; /* not protected, respect reclimit, not constructor */
3421 if (DUK_HOBJECT_IS_NATIVEFUNCTION(obj_func
) &&
3422 ((duk_hnativefunction
*) obj_func
)->func
== duk_bi_global_object_eval
) {
3423 if (flag_evalcall
) {
3424 DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was 'eval' -> direct eval"));
3425 call_flags
|= DUK_CALL_FLAG_DIRECT_EVAL
;
3427 DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was not 'eval' -> indirect eval"));
3431 duk_handle_call_unprotected(thr
,
3435 /* duk_js_call.c is required to restore the stack reserve
3436 * so we only need to reset the top.
3438 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
3441 duk_set_top(ctx
, (duk_idx_t
) fun
->nregs
);
3443 /* No need to reinit setjmp() catchpoint, as call handling
3444 * will store and restore our state.
3448 /* When debugger is enabled, we need to recheck the activation
3449 * status after returning. This is now handled by call handling
3450 * and heap->dbg_force_restart.
3455 case DUK_OP_TRYCATCH
: {
3456 duk_context
*ctx
= (duk_context
*) thr
;
3457 duk_activation
*act
;
3460 duk_small_uint_fast_t a
;
3464 * BC -> reg_catch; base register for two registers used both during
3465 * trycatch setup and when catch is triggered
3467 * If DUK_BC_TRYCATCH_FLAG_CATCH_BINDING set:
3468 * reg_catch + 0: catch binding variable name (string).
3469 * Automatic declarative environment is established for
3470 * the duration of the 'catch' clause.
3472 * If DUK_BC_TRYCATCH_FLAG_WITH_BINDING set:
3473 * reg_catch + 0: with 'target value', which is coerced to
3474 * an object and then used as a bindind object for an
3475 * environment record. The binding is initialized here, for
3478 * Note that a TRYCATCH generated for a 'with' statement has no
3479 * catch or finally parts.
3482 /* XXX: TRYCATCH handling should be reworked to avoid creating
3483 * an explicit scope unless it is actually needed (e.g. function
3484 * instances or eval is executed inside the catch block). This
3485 * rework is not trivial because the compiler doesn't have an
3486 * intermediate representation. When the rework is done, the
3487 * opcode format can also be made more straightforward.
3490 /* XXX: side effect handling is quite awkward here */
3492 DUK_DDD(DUK_DDDPRINT("TRYCATCH: reg_catch=%ld, have_catch=%ld, "
3493 "have_finally=%ld, catch_binding=%ld, with_binding=%ld (flags=0x%02lx)",
3494 (long) DUK_DEC_BC(ins
),
3495 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH
? 1 : 0),
3496 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY
? 1 : 0),
3497 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING
? 1 : 0),
3498 (long) (DUK_DEC_A(ins
) & DUK_BC_TRYCATCH_FLAG_WITH_BINDING
? 1 : 0),
3499 (unsigned long) DUK_DEC_A(ins
)));
3502 bc
= DUK_DEC_BC(ins
);
3504 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3505 DUK_ASSERT(thr
->callstack_top
>= 1);
3507 /* 'with' target must be created first, in case we run out of memory */
3508 /* XXX: refactor out? */
3510 if (a
& DUK_BC_TRYCATCH_FLAG_WITH_BINDING
) {
3511 DUK_DDD(DUK_DDDPRINT("need to initialize a with binding object"));
3513 if (act
->lex_env
== NULL
) {
3514 DUK_ASSERT(act
->var_env
== NULL
);
3515 DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
3517 /* must relookup act in case of side effects */
3518 duk_js_init_activation_environment_records_delayed(thr
, act
);
3519 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3520 DUK_UNREF(act
); /* 'act' is no longer accessed, scanbuild fix */
3522 DUK_ASSERT(act
->lex_env
!= NULL
);
3523 DUK_ASSERT(act
->var_env
!= NULL
);
3525 (void) duk_push_object_helper(ctx
,
3526 DUK_HOBJECT_FLAG_EXTENSIBLE
|
3527 DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV
),
3528 -1); /* no prototype, updated below */
3530 duk_push_tval(ctx
, DUK__REGP(bc
));
3531 duk_to_object(ctx
, -1);
3534 /* [ ... env target ] */
3535 /* [ ... env target target ] */
3537 duk_xdef_prop_stridx(thr
, -3, DUK_STRIDX_INT_TARGET
, DUK_PROPDESC_FLAGS_NONE
);
3538 duk_xdef_prop_stridx(thr
, -2, DUK_STRIDX_INT_THIS
, DUK_PROPDESC_FLAGS_NONE
); /* always provideThis=true */
3542 DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iT",
3543 (duk_tval
*) duk_get_tval(ctx
, -1)));
3546 /* allocate catcher and populate it (should be atomic) */
3548 duk_hthread_catchstack_grow(thr
);
3549 cat
= thr
->catchstack
+ thr
->catchstack_top
;
3550 DUK_ASSERT(thr
->catchstack_top
+ 1 <= thr
->catchstack_size
);
3551 thr
->catchstack_top
++;
3553 cat
->flags
= DUK_CAT_TYPE_TCF
;
3554 cat
->h_varname
= NULL
;
3556 if (a
& DUK_BC_TRYCATCH_FLAG_HAVE_CATCH
) {
3557 cat
->flags
|= DUK_CAT_FLAG_CATCH_ENABLED
;
3559 if (a
& DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY
) {
3560 cat
->flags
|= DUK_CAT_FLAG_FINALLY_ENABLED
;
3562 if (a
& DUK_BC_TRYCATCH_FLAG_CATCH_BINDING
) {
3563 DUK_DDD(DUK_DDDPRINT("catch binding flag set to catcher"));
3564 cat
->flags
|= DUK_CAT_FLAG_CATCH_BINDING_ENABLED
;
3565 tv1
= DUK__REGP(bc
);
3566 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
3568 /* borrowed reference; although 'tv1' comes from a register,
3569 * its value was loaded using LDCONST so the constant will
3570 * also exist and be reachable.
3572 cat
->h_varname
= DUK_TVAL_GET_STRING(tv1
);
3573 } else if (a
& DUK_BC_TRYCATCH_FLAG_WITH_BINDING
) {
3574 /* env created above to stack top */
3575 duk_hobject
*new_env
;
3577 DUK_DDD(DUK_DDDPRINT("lexenv active flag set to catcher"));
3578 cat
->flags
|= DUK_CAT_FLAG_LEXENV_ACTIVE
;
3580 DUK_DDD(DUK_DDDPRINT("activating object env: %!iT",
3581 (duk_tval
*) duk_get_tval(ctx
, -1)));
3582 DUK_ASSERT(act
->lex_env
!= NULL
);
3583 new_env
= DUK_GET_HOBJECT_NEGIDX(ctx
, -1);
3584 DUK_ASSERT(new_env
!= NULL
);
3586 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup (side effects) */
3587 DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr
, new_env
, act
->lex_env
); /* side effects */
3589 act
= thr
->callstack
+ thr
->callstack_top
- 1; /* relookup (side effects) */
3590 act
->lex_env
= new_env
;
3591 DUK_HOBJECT_INCREF(thr
, new_env
);
3597 /* Registers 'bc' and 'bc + 1' are written in longjmp handling
3598 * and if their previous values (which are temporaries) become
3599 * unreachable -and- have a finalizer, there'll be a function
3600 * call during error handling which is not supported now (GH-287).
3601 * Ensure that both 'bc' and 'bc + 1' have primitive values to
3602 * guarantee no finalizer calls in error handling. Scrubbing also
3603 * ensures finalizers for the previous values run here rather than
3604 * later. Error handling related values are also written to 'bc'
3605 * and 'bc + 1' but those values never become unreachable during
3606 * error handling, so there's no side effect problem even if the
3607 * error value has a finalizer.
3609 duk_to_undefined(ctx
, bc
);
3610 duk_to_undefined(ctx
, bc
+ 1);
3612 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1; /* relookup (side effects) */
3613 cat
->callstack_index
= thr
->callstack_top
- 1;
3614 cat
->pc_base
= (duk_instr_t
*) curr_pc
; /* pre-incremented, points to first jump slot */
3615 cat
->idx_base
= (duk_size_t
) (thr
->valstack_bottom
- thr
->valstack
) + bc
;
3617 DUK_DDD(DUK_DDDPRINT("TRYCATCH catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
3618 "idx_base=%ld, h_varname=%!O",
3619 (unsigned long) cat
->flags
, (long) cat
->callstack_index
,
3620 (long) cat
->pc_base
, (long) cat
->idx_base
, (duk_heaphdr
*) cat
->h_varname
));
3622 curr_pc
+= 2; /* skip jump slots */
3626 /* Pre/post inc/dec for register variables, important for loops. */
3627 case DUK_OP_PREINCR
:
3628 case DUK_OP_PREDECR
:
3629 case DUK_OP_POSTINCR
:
3630 case DUK_OP_POSTDECR
: {
3631 duk_context
*ctx
= (duk_context
*) thr
;
3632 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3633 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3634 duk_tval
*tv1
, *tv2
;
3635 duk_double_t x
, y
, z
;
3637 /* Two lowest bits of opcode are used to distinguish
3638 * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3640 DUK_ASSERT((DUK_OP_PREINCR
& 0x03) == 0x00);
3641 DUK_ASSERT((DUK_OP_PREDECR
& 0x03) == 0x01);
3642 DUK_ASSERT((DUK_OP_POSTINCR
& 0x03) == 0x02);
3643 DUK_ASSERT((DUK_OP_POSTDECR
& 0x03) == 0x03);
3645 tv1
= DUK__REGP(bc
);
3646 #if defined(DUK_USE_FASTINT)
3647 if (DUK_TVAL_IS_FASTINT(tv1
)) {
3648 duk_int64_t x_fi
, y_fi
, z_fi
;
3649 x_fi
= DUK_TVAL_GET_FASTINT(tv1
);
3650 if (ins
& DUK_ENC_OP(0x01)) {
3651 if (x_fi
== DUK_FASTINT_MIN
) {
3656 if (x_fi
== DUK_FASTINT_MAX
) {
3662 DUK_TVAL_SET_FASTINT(tv1
, y_fi
); /* no need for refcount update */
3665 z_fi
= (ins
& DUK_ENC_OP(0x02)) ? x_fi
: y_fi
;
3666 DUK_TVAL_SET_FASTINT_UPDREF(thr
, tv2
, z_fi
); /* side effects */
3671 if (DUK_TVAL_IS_NUMBER(tv1
)) {
3672 /* Fast path for the case where the register
3673 * is a number (e.g. loop counter).
3676 x
= DUK_TVAL_GET_NUMBER(tv1
);
3677 if (ins
& DUK_ENC_OP(0x01)) {
3683 DUK_TVAL_SET_NUMBER(tv1
, y
); /* no need for refcount update */
3685 x
= duk_to_number(ctx
, bc
);
3687 if (ins
& DUK_ENC_OP(0x01)) {
3693 duk_push_number(ctx
, y
);
3694 duk_replace(ctx
, bc
);
3698 z
= (ins
& DUK_ENC_OP(0x02)) ? x
: y
;
3699 DUK_TVAL_SET_NUMBER_UPDREF(thr
, tv2
, z
); /* side effects */
3703 /* Preinc/predec for var-by-name, slow path. */
3704 case DUK_OP_PREINCV
:
3705 case DUK_OP_PREDECV
:
3706 case DUK_OP_POSTINCV
:
3707 case DUK_OP_POSTDECV
: {
3708 duk_context
*ctx
= (duk_context
*) thr
;
3709 duk_activation
*act
;
3710 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3711 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3716 /* Two lowest bits of opcode are used to distinguish
3717 * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3719 DUK_ASSERT((DUK_OP_PREINCV
& 0x03) == 0x00);
3720 DUK_ASSERT((DUK_OP_PREDECV
& 0x03) == 0x01);
3721 DUK_ASSERT((DUK_OP_POSTINCV
& 0x03) == 0x02);
3722 DUK_ASSERT((DUK_OP_POSTDECV
& 0x03) == 0x03);
3724 tv1
= DUK__CONSTP(bc
);
3725 DUK_ASSERT(DUK_TVAL_IS_STRING(tv1
));
3726 name
= DUK_TVAL_GET_STRING(tv1
);
3727 DUK_ASSERT(name
!= NULL
);
3728 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3729 (void) duk_js_getvar_activation(thr
, act
, name
, 1 /*throw*/); /* -> [... val this] */
3731 /* XXX: fastint fast path would be very useful here */
3733 x
= duk_to_number(ctx
, -2);
3735 if (ins
& DUK_ENC_OP(0x01)) {
3741 duk_push_number(ctx
, y
);
3742 tv1
= DUK_GET_TVAL_NEGIDX(ctx
, -1);
3743 DUK_ASSERT(tv1
!= NULL
);
3744 duk_js_putvar_activation(thr
, act
, name
, tv1
, DUK__STRICT());
3747 duk_push_number(ctx
, (ins
& DUK_ENC_OP(0x02)) ? x
: y
);
3748 duk_replace(ctx
, (duk_idx_t
) a
);
3752 /* Preinc/predec for object properties. */
3753 case DUK_OP_PREINCP
:
3754 case DUK_OP_PREDECP
:
3755 case DUK_OP_POSTINCP
:
3756 case DUK_OP_POSTDECP
: {
3757 duk_context
*ctx
= (duk_context
*) thr
;
3758 duk_small_uint_fast_t a
= DUK_DEC_A(ins
);
3759 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3760 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3768 * B -> object reg/const (may be const e.g. in "'foo'[1]")
3769 * C -> key reg/const
3772 /* Two lowest bits of opcode are used to distinguish
3773 * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3775 DUK_ASSERT((DUK_OP_PREINCP
& 0x03) == 0x00);
3776 DUK_ASSERT((DUK_OP_PREDECP
& 0x03) == 0x01);
3777 DUK_ASSERT((DUK_OP_POSTINCP
& 0x03) == 0x02);
3778 DUK_ASSERT((DUK_OP_POSTDECP
& 0x03) == 0x03);
3780 tv_obj
= DUK__REGCONSTP(b
);
3781 tv_key
= DUK__REGCONSTP(c
);
3782 rc
= duk_hobject_getprop(thr
, tv_obj
, tv_key
); /* -> [val] */
3783 DUK_UNREF(rc
); /* ignore */
3784 tv_obj
= NULL
; /* invalidated */
3785 tv_key
= NULL
; /* invalidated */
3787 x
= duk_to_number(ctx
, -1);
3789 if (ins
& DUK_ENC_OP(0x01)) {
3795 duk_push_number(ctx
, y
);
3796 tv_val
= DUK_GET_TVAL_NEGIDX(ctx
, -1);
3797 DUK_ASSERT(tv_val
!= NULL
);
3798 tv_obj
= DUK__REGCONSTP(b
);
3799 tv_key
= DUK__REGCONSTP(c
);
3800 rc
= duk_hobject_putprop(thr
, tv_obj
, tv_key
, tv_val
, DUK__STRICT());
3801 DUK_UNREF(rc
); /* ignore */
3802 tv_obj
= NULL
; /* invalidated */
3803 tv_key
= NULL
; /* invalidated */
3806 duk_push_number(ctx
, (ins
& DUK_ENC_OP(0x02)) ? x
: y
);
3807 duk_replace(ctx
, (duk_idx_t
) a
);
3811 case DUK_OP_EXTRA
: {
3812 /* XXX: shared decoding of 'b' and 'c'? */
3814 duk_small_uint_fast_t extraop
= DUK_DEC_A(ins
);
3815 switch ((int) extraop
) {
3816 /* XXX: switch cast? */
3818 case DUK_EXTRAOP_NOP
: {
3823 case DUK_EXTRAOP_INVALID
: {
3824 DUK_ERROR_FMT1(thr
, DUK_ERR_INTERNAL_ERROR
, "INVALID opcode (%ld)", (long) DUK_DEC_BC(ins
));
3828 case DUK_EXTRAOP_LDTHIS
: {
3829 /* Note: 'this' may be bound to any value, not just an object */
3830 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3831 duk_tval
*tv1
, *tv2
;
3833 tv1
= DUK__REGP(bc
);
3834 tv2
= thr
->valstack_bottom
- 1; /* 'this binding' is just under bottom */
3835 DUK_ASSERT(tv2
>= thr
->valstack
);
3837 DUK_DDD(DUK_DDDPRINT("LDTHIS: %!T to r%ld", (duk_tval
*) tv2
, (long) bc
));
3839 DUK_TVAL_SET_TVAL_UPDREF_FAST(thr
, tv1
, tv2
); /* side effects */
3843 case DUK_EXTRAOP_LDUNDEF
: {
3844 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3847 tv1
= DUK__REGP(bc
);
3848 DUK_TVAL_SET_UNDEFINED_UPDREF(thr
, tv1
); /* side effects */
3852 case DUK_EXTRAOP_LDNULL
: {
3853 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3856 tv1
= DUK__REGP(bc
);
3857 DUK_TVAL_SET_NULL_UPDREF(thr
, tv1
); /* side effects */
3861 case DUK_EXTRAOP_LDTRUE
:
3862 case DUK_EXTRAOP_LDFALSE
: {
3863 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3865 duk_small_uint_fast_t bval
= (extraop
== DUK_EXTRAOP_LDTRUE
? 1 : 0);
3867 tv1
= DUK__REGP(bc
);
3868 DUK_TVAL_SET_BOOLEAN_UPDREF(thr
, tv1
, bval
); /* side effects */
3872 case DUK_EXTRAOP_NEWOBJ
: {
3873 duk_context
*ctx
= (duk_context
*) thr
;
3874 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3876 duk_push_object(ctx
);
3877 duk_replace(ctx
, (duk_idx_t
) b
);
3881 case DUK_EXTRAOP_NEWARR
: {
3882 duk_context
*ctx
= (duk_context
*) thr
;
3883 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3885 duk_push_array(ctx
);
3886 duk_replace(ctx
, (duk_idx_t
) b
);
3890 case DUK_EXTRAOP_SETALEN
: {
3891 duk_small_uint_fast_t b
;
3892 duk_small_uint_fast_t c
;
3897 b
= DUK_DEC_B(ins
); tv1
= DUK__REGP(b
);
3898 DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1
));
3899 h
= DUK_TVAL_GET_OBJECT(tv1
);
3901 c
= DUK_DEC_C(ins
); tv1
= DUK__REGP(c
);
3902 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
3903 len
= (duk_uint32_t
) DUK_TVAL_GET_NUMBER(tv1
);
3905 duk_hobject_set_length(thr
, h
, len
);
3910 case DUK_EXTRAOP_TYPEOF
: {
3911 duk_context
*ctx
= (duk_context
*) thr
;
3912 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
3913 duk_push_hstring(ctx
, duk_js_typeof(thr
, DUK__REGP(bc
)));
3914 duk_replace(ctx
, (duk_idx_t
) bc
);
3918 case DUK_EXTRAOP_TYPEOFID
: {
3919 duk_context
*ctx
= (duk_context
*) thr
;
3920 duk_activation
*act
;
3921 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3922 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3926 /* B -> target register
3927 * C -> constant index of identifier name
3930 tv
= DUK__REGCONSTP(c
); /* XXX: this could be a DUK__CONSTP instead */
3931 DUK_ASSERT(DUK_TVAL_IS_STRING(tv
));
3932 name
= DUK_TVAL_GET_STRING(tv
);
3933 act
= thr
->callstack
+ thr
->callstack_top
- 1;
3934 if (duk_js_getvar_activation(thr
, act
, name
, 0 /*throw*/)) {
3935 /* -> [... val this] */
3936 tv
= DUK_GET_TVAL_NEGIDX(ctx
, -2);
3937 duk_push_hstring(ctx
, duk_js_typeof(thr
, tv
));
3938 duk_replace(ctx
, (duk_idx_t
) b
);
3941 /* unresolvable, no stack changes */
3942 duk_push_hstring_stridx(ctx
, DUK_STRIDX_LC_UNDEFINED
);
3943 duk_replace(ctx
, (duk_idx_t
) b
);
3949 case DUK_EXTRAOP_INITENUM
: {
3950 duk_context
*ctx
= (duk_context
*) thr
;
3951 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3952 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3955 * Enumeration semantics come from for-in statement, E5 Section 12.6.4.
3956 * If called with 'null' or 'undefined', this opcode returns 'null' as
3957 * the enumerator, which is special cased in NEXTENUM. This simplifies
3961 /* B -> register for writing enumerator object
3962 * C -> value to be enumerated (register)
3965 if (duk_is_null_or_undefined(ctx
, (duk_idx_t
) c
)) {
3967 duk_replace(ctx
, (duk_idx_t
) b
);
3969 duk_dup(ctx
, (duk_idx_t
) c
);
3970 duk_to_object(ctx
, -1);
3971 duk_hobject_enumerator_create(ctx
, 0 /*enum_flags*/); /* [ ... val ] --> [ ... enum ] */
3972 duk_replace(ctx
, (duk_idx_t
) b
);
3977 case DUK_EXTRAOP_NEXTENUM
: {
3978 duk_context
*ctx
= (duk_context
*) thr
;
3979 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
3980 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
3983 * NEXTENUM checks whether the enumerator still has unenumerated
3984 * keys. If so, the next key is loaded to the target register
3985 * and the next instruction is skipped. Otherwise the next instruction
3986 * will be executed, jumping out of the enumeration loop.
3989 /* B -> target register for next key
3990 * C -> enum register
3993 DUK_DDD(DUK_DDDPRINT("NEXTENUM: b->%!T, c->%!T",
3994 (duk_tval
*) duk_get_tval(ctx
, (duk_idx_t
) b
),
3995 (duk_tval
*) duk_get_tval(ctx
, (duk_idx_t
) c
)));
3997 if (duk_is_object(ctx
, (duk_idx_t
) c
)) {
3998 /* XXX: assert 'c' is an enumerator */
3999 duk_dup(ctx
, (duk_idx_t
) c
);
4000 if (duk_hobject_enumerator_next(ctx
, 0 /*get_value*/)) {
4001 /* [ ... enum ] -> [ ... next_key ] */
4002 DUK_DDD(DUK_DDDPRINT("enum active, next key is %!T, skip jump slot ",
4003 (duk_tval
*) duk_get_tval(ctx
, -1)));
4006 /* [ ... enum ] -> [ ... ] */
4007 DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot"));
4008 duk_push_undefined(ctx
);
4010 duk_replace(ctx
, (duk_idx_t
) b
);
4012 /* 'null' enumerator case -> behave as with an empty enumerator */
4013 DUK_ASSERT(duk_is_null(ctx
, (duk_idx_t
) c
));
4014 DUK_DDD(DUK_DDDPRINT("enum is null, execute jump slot"));
4019 case DUK_EXTRAOP_INITSET
:
4020 case DUK_EXTRAOP_INITSETI
:
4021 case DUK_EXTRAOP_INITGET
:
4022 case DUK_EXTRAOP_INITGETI
: {
4023 duk_context
*ctx
= (duk_context
*) thr
;
4024 duk_bool_t is_set
= (extraop
== DUK_EXTRAOP_INITSET
|| extraop
== DUK_EXTRAOP_INITSETI
);
4025 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4026 duk_uint_fast_t idx
;
4028 /* B -> object register
4029 * C -> C+0 contains key, C+1 closure (value)
4033 * INITSET/INITGET are only used to initialize object literal keys.
4034 * The compiler ensures that there cannot be a previous data property
4035 * of the same name. It also ensures that setter and getter can only
4036 * be initialized once (or not at all).
4039 idx
= (duk_uint_fast_t
) DUK_DEC_C(ins
);
4040 if (extraop
== DUK_EXTRAOP_INITSETI
|| extraop
== DUK_EXTRAOP_INITGETI
) {
4041 duk_tval
*tv_ind
= DUK__REGP(idx
);
4042 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind
));
4043 idx
= (duk_uint_fast_t
) DUK_TVAL_GET_NUMBER(tv_ind
);
4046 #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
4047 if (idx
+ 2 > (duk_uint_fast_t
) duk_get_top(ctx
)) {
4048 /* XXX: use duk_is_valid_index() instead? */
4049 /* XXX: improve check; check against nregs, not against top */
4050 DUK__INTERNAL_ERROR("INITSET/INITGET out of bounds");
4054 /* XXX: this is now a very unoptimal implementation -- this can be
4055 * made very simple by direct manipulation of the object internals,
4056 * given the guarantees above.
4059 duk_push_hobject_bidx(ctx
, DUK_BIDX_OBJECT_CONSTRUCTOR
);
4060 duk_get_prop_stridx(ctx
, -1, DUK_STRIDX_DEFINE_PROPERTY
);
4061 duk_push_undefined(ctx
);
4062 duk_dup(ctx
, (duk_idx_t
) b
);
4063 duk_dup(ctx
, (duk_idx_t
) (idx
+ 0));
4064 duk_push_object(ctx
); /* -> [ Object defineProperty undefined obj key desc ] */
4067 duk_put_prop_stridx(ctx
, -2, DUK_STRIDX_ENUMERABLE
);
4069 duk_put_prop_stridx(ctx
, -2, DUK_STRIDX_CONFIGURABLE
);
4070 duk_dup(ctx
, (duk_idx_t
) (idx
+ 1));
4071 duk_put_prop_stridx(ctx
, -2, (is_set
? DUK_STRIDX_SET
: DUK_STRIDX_GET
));
4073 DUK_DDD(DUK_DDDPRINT("INITGET/INITSET: obj=%!T, key=%!T, desc=%!T",
4074 (duk_tval
*) duk_get_tval(ctx
, -3),
4075 (duk_tval
*) duk_get_tval(ctx
, -2),
4076 (duk_tval
*) duk_get_tval(ctx
, -1)));
4078 duk_call_method(ctx
, 3); /* -> [ Object res ] */
4081 DUK_DDD(DUK_DDDPRINT("INITGET/INITSET AFTER: obj=%!T",
4082 (duk_tval
*) duk_get_tval(ctx
, (duk_idx_t
) b
)));
4086 case DUK_EXTRAOP_ENDTRY
: {
4090 DUK_ASSERT(thr
->catchstack_top
>= 1);
4091 DUK_ASSERT(thr
->callstack_top
>= 1);
4092 DUK_ASSERT(thr
->catchstack
[thr
->catchstack_top
- 1].callstack_index
== thr
->callstack_top
- 1);
4094 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4096 DUK_DDD(DUK_DDDPRINT("ENDTRY: clearing catch active flag (regardless of whether it was set or not)"));
4097 DUK_CAT_CLEAR_CATCH_ENABLED(cat
);
4099 if (DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
4100 DUK_DDD(DUK_DDDPRINT("ENDTRY: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
4102 tv1
= thr
->valstack
+ cat
->idx_base
;
4103 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4104 DUK_TVAL_SET_UNDEFINED_UPDREF(thr
, tv1
); /* side effects */
4107 tv1
= thr
->valstack
+ cat
->idx_base
+ 1;
4108 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4109 DUK_TVAL_SET_FASTINT_U32_UPDREF(thr
, tv1
, (duk_uint32_t
) DUK_LJ_TYPE_NORMAL
); /* side effects */
4112 DUK_CAT_CLEAR_FINALLY_ENABLED(cat
);
4114 DUK_DDD(DUK_DDDPRINT("ENDTRY: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
4115 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4116 /* no need to unwind callstack */
4119 curr_pc
= cat
->pc_base
+ 1;
4123 case DUK_EXTRAOP_ENDCATCH
: {
4124 duk_activation
*act
;
4128 DUK_ASSERT(thr
->catchstack_top
>= 1);
4129 DUK_ASSERT(thr
->callstack_top
>= 1);
4130 DUK_ASSERT(thr
->catchstack
[thr
->catchstack_top
- 1].callstack_index
== thr
->callstack_top
- 1);
4132 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4133 DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat
)); /* cleared before entering catch part */
4135 act
= thr
->callstack
+ thr
->callstack_top
- 1;
4137 if (DUK_CAT_HAS_LEXENV_ACTIVE(cat
)) {
4138 duk_hobject
*prev_env
;
4140 /* 'with' binding has no catch clause, so can't be here unless a normal try-catch */
4141 DUK_ASSERT(DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat
));
4142 DUK_ASSERT(act
->lex_env
!= NULL
);
4144 DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment"));
4146 prev_env
= act
->lex_env
;
4147 DUK_ASSERT(prev_env
!= NULL
);
4148 act
->lex_env
= DUK_HOBJECT_GET_PROTOTYPE(thr
->heap
, prev_env
);
4149 DUK_CAT_CLEAR_LEXENV_ACTIVE(cat
);
4150 DUK_HOBJECT_DECREF(thr
, prev_env
); /* side effects */
4153 if (DUK_CAT_HAS_FINALLY_ENABLED(cat
)) {
4154 DUK_DDD(DUK_DDDPRINT("ENDCATCH: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
4156 tv1
= thr
->valstack
+ cat
->idx_base
;
4157 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4158 DUK_TVAL_SET_UNDEFINED_UPDREF(thr
, tv1
); /* side effects */
4161 tv1
= thr
->valstack
+ cat
->idx_base
+ 1;
4162 DUK_ASSERT(tv1
>= thr
->valstack
&& tv1
< thr
->valstack_top
);
4163 DUK_TVAL_SET_FASTINT_U32_UPDREF(thr
, tv1
, (duk_uint32_t
) DUK_LJ_TYPE_NORMAL
); /* side effects */
4166 DUK_CAT_CLEAR_FINALLY_ENABLED(cat
);
4168 DUK_DDD(DUK_DDDPRINT("ENDCATCH: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
4169 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4170 /* no need to unwind callstack */
4173 curr_pc
= cat
->pc_base
+ 1;
4177 case DUK_EXTRAOP_ENDFIN
: {
4178 duk_context
*ctx
= (duk_context
*) thr
;
4181 duk_small_uint_t cont_type
;
4182 duk_small_uint_t ret_result
;
4184 /* Sync and NULL early. */
4185 DUK__SYNC_AND_NULL_CURR_PC();
4187 DUK_ASSERT(thr
->catchstack_top
>= 1);
4188 DUK_ASSERT(thr
->callstack_top
>= 1);
4189 DUK_ASSERT(thr
->catchstack
[thr
->catchstack_top
- 1].callstack_index
== thr
->callstack_top
- 1);
4191 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4193 /* CATCH flag may be enabled or disabled here; it may be enabled if
4194 * the statement has a catch block but the try block does not throw
4197 DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat
)); /* cleared before entering finally */
4198 /* XXX: assert idx_base */
4200 DUK_DDD(DUK_DDDPRINT("ENDFIN: completion value=%!T, type=%!T",
4201 (duk_tval
*) (thr
->valstack
+ cat
->idx_base
+ 0),
4202 (duk_tval
*) (thr
->valstack
+ cat
->idx_base
+ 1)));
4204 tv1
= thr
->valstack
+ cat
->idx_base
+ 1; /* type */
4205 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
4206 cont_type
= (duk_small_uint_t
) DUK_TVAL_GET_NUMBER(tv1
);
4208 switch (cont_type
) {
4209 case DUK_LJ_TYPE_NORMAL
: {
4210 DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'normal' (non-abrupt) completion -> "
4211 "dismantle catcher, resume execution after ENDFIN"));
4212 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4213 /* no need to unwind callstack */
4214 goto restart_execution
;
4216 case DUK_LJ_TYPE_RETURN
: {
4217 DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'return' complation -> dismantle "
4218 "catcher, handle return, lj.value1=%!T", thr
->valstack
+ cat
->idx_base
));
4220 /* Not necessary to unwind catchstack: return handling will
4221 * do it. The finally flag of 'cat' is no longer set. The
4222 * catch flag may be set, but it's not checked by return handling.
4224 DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat
)); /* cleared before entering finally */
4226 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4229 duk_push_tval(ctx
, thr
->valstack
+ cat
->idx_base
);
4230 ret_result
= duk__handle_return(thr
,
4232 entry_callstack_top
);
4233 if (ret_result
== DUK__RETHAND_RESTART
) {
4234 goto restart_execution
;
4236 DUK_ASSERT(ret_result
== DUK__RETHAND_FINISHED
);
4238 DUK_DDD(DUK_DDDPRINT("exiting executor after ENDFIN and RETURN (pseudo) longjmp type"));
4241 case DUK_LJ_TYPE_BREAK
:
4242 case DUK_LJ_TYPE_CONTINUE
: {
4243 duk_uint_t label_id
;
4244 duk_small_uint_t lj_type
;
4246 /* Not necessary to unwind catchstack: break/continue
4247 * handling will do it. The finally flag of 'cat' is
4248 * no longer set. The catch flag may be set, but it's
4249 * not checked by break/continue handling.
4252 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4255 tv1
= thr
->valstack
+ cat
->idx_base
;
4256 DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1
));
4257 #if defined(DUK_USE_FASTINT)
4258 DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1
));
4259 label_id
= (duk_small_uint_t
) DUK_TVAL_GET_FASTINT_U32(tv1
);
4261 label_id
= (duk_small_uint_t
) DUK_TVAL_GET_NUMBER(tv1
);
4263 lj_type
= cont_type
;
4264 duk__handle_break_or_continue(thr
, label_id
, lj_type
);
4265 goto restart_execution
;
4268 DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> "
4269 "dismantle catcher, re-throw error",
4272 duk_push_tval(ctx
, thr
->valstack
+ cat
->idx_base
);
4274 duk_err_setup_heap_ljstate(thr
, (duk_small_int_t
) cont_type
);
4276 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* always in executor */
4277 duk_err_longjmp(thr
);
4282 /* Must restart in all cases because we NULLed thr->ptr_curr_pc. */
4287 case DUK_EXTRAOP_THROW
: {
4288 duk_context
*ctx
= (duk_context
*) thr
;
4289 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4291 /* Note: errors are augmented when they are created, not
4292 * when they are thrown. So, don't augment here, it would
4293 * break re-throwing for instance.
4296 /* Sync so that augmentation sees up-to-date activations, NULL
4297 * thr->ptr_curr_pc so that it's not used if side effects occur
4298 * in augmentation or longjmp handling.
4300 DUK__SYNC_AND_NULL_CURR_PC();
4302 duk_dup(ctx
, (duk_idx_t
) bc
);
4303 DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (before throw augment)",
4304 (duk_tval
*) duk_get_tval(ctx
, -1)));
4305 #if defined(DUK_USE_AUGMENT_ERROR_THROW)
4306 duk_err_augment_error_throw(thr
);
4307 DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (after throw augment)",
4308 (duk_tval
*) duk_get_tval(ctx
, -1)));
4311 duk_err_setup_heap_ljstate(thr
, DUK_LJ_TYPE_THROW
);
4313 DUK_ASSERT(thr
->heap
->lj
.jmpbuf_ptr
!= NULL
); /* always in executor */
4314 duk_err_longjmp(thr
);
4319 case DUK_EXTRAOP_INVLHS
: {
4320 DUK_ERROR(thr
, DUK_ERR_REFERENCE_ERROR
, "invalid lvalue");
4326 case DUK_EXTRAOP_UNM
:
4327 case DUK_EXTRAOP_UNP
: {
4328 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4329 duk__vm_arith_unary_op(thr
, DUK__REGP(bc
), bc
, extraop
);
4333 case DUK_EXTRAOP_DEBUGGER
: {
4334 /* Opcode only emitted by compiler when debugger
4335 * support is enabled. Ignore it silently without
4336 * debugger support, in case it has been loaded
4337 * from precompiled bytecode.
4339 #if defined(DUK_USE_DEBUGGER_SUPPORT)
4340 if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
)) {
4341 DUK_D(DUK_DPRINT("DEBUGGER statement encountered, halt execution"));
4342 DUK__SYNC_AND_NULL_CURR_PC();
4343 duk_debug_halt_execution(thr
, 1 /*use_prev_pc*/);
4344 DUK_D(DUK_DPRINT("DEBUGGER statement finished, resume execution"));
4345 goto restart_execution
;
4347 DUK_D(DUK_DPRINT("DEBUGGER statement ignored, debugger not attached"));
4350 DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support"));
4355 case DUK_EXTRAOP_BREAK
: {
4356 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4358 DUK_DDD(DUK_DDDPRINT("BREAK: %ld", (long) bc
));
4360 DUK__SYNC_AND_NULL_CURR_PC();
4361 duk__handle_break_or_continue(thr
, (duk_uint_t
) bc
, DUK_LJ_TYPE_BREAK
);
4362 goto restart_execution
;
4365 case DUK_EXTRAOP_CONTINUE
: {
4366 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4368 DUK_DDD(DUK_DDDPRINT("CONTINUE: %ld", (long) bc
));
4370 DUK__SYNC_AND_NULL_CURR_PC();
4371 duk__handle_break_or_continue(thr
, (duk_uint_t
) bc
, DUK_LJ_TYPE_CONTINUE
);
4372 goto restart_execution
;
4375 case DUK_EXTRAOP_BNOT
: {
4376 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4378 duk__vm_bitwise_not(thr
, DUK__REGP(bc
), bc
);
4382 case DUK_EXTRAOP_LNOT
: {
4383 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4386 tv1
= DUK__REGP(bc
);
4387 duk__vm_logical_not(thr
, tv1
, tv1
);
4391 case DUK_EXTRAOP_INSTOF
: {
4392 duk_context
*ctx
= (duk_context
*) thr
;
4393 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4394 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
4397 tmp
= duk_js_instanceof(thr
, DUK__REGP(b
), DUK__REGCONSTP(c
));
4398 duk_push_boolean(ctx
, tmp
);
4399 duk_replace(ctx
, (duk_idx_t
) b
);
4403 case DUK_EXTRAOP_IN
: {
4404 duk_context
*ctx
= (duk_context
*) thr
;
4405 duk_small_uint_fast_t b
= DUK_DEC_B(ins
);
4406 duk_small_uint_fast_t c
= DUK_DEC_C(ins
);
4409 tmp
= duk_js_in(thr
, DUK__REGP(b
), DUK__REGCONSTP(c
));
4410 duk_push_boolean(ctx
, tmp
);
4411 duk_replace(ctx
, (duk_idx_t
) b
);
4415 case DUK_EXTRAOP_LABEL
: {
4417 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4419 /* allocate catcher and populate it (should be atomic) */
4421 duk_hthread_catchstack_grow(thr
);
4422 cat
= thr
->catchstack
+ thr
->catchstack_top
;
4423 thr
->catchstack_top
++;
4425 cat
->flags
= DUK_CAT_TYPE_LABEL
| (bc
<< DUK_CAT_LABEL_SHIFT
);
4426 cat
->callstack_index
= thr
->callstack_top
- 1;
4427 cat
->pc_base
= (duk_instr_t
*) curr_pc
; /* pre-incremented, points to first jump slot */
4428 cat
->idx_base
= 0; /* unused for label */
4429 cat
->h_varname
= NULL
;
4431 DUK_DDD(DUK_DDDPRINT("LABEL catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, "
4432 "idx_base=%ld, h_varname=%!O, label_id=%ld",
4433 (long) cat
->flags
, (long) cat
->callstack_index
, (long) cat
->pc_base
,
4434 (long) cat
->idx_base
, (duk_heaphdr
*) cat
->h_varname
, (long) DUK_CAT_GET_LABEL(cat
)));
4436 curr_pc
+= 2; /* skip jump slots */
4440 case DUK_EXTRAOP_ENDLABEL
: {
4442 #if defined(DUK_USE_DDDPRINT) || defined(DUK_USE_ASSERTIONS)
4443 duk_uint_fast_t bc
= DUK_DEC_BC(ins
);
4445 #if defined(DUK_USE_DDDPRINT)
4446 DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc
));
4449 DUK_ASSERT(thr
->catchstack_top
>= 1);
4451 cat
= thr
->catchstack
+ thr
->catchstack_top
- 1;
4453 DUK_ASSERT(DUK_CAT_GET_TYPE(cat
) == DUK_CAT_TYPE_LABEL
);
4454 DUK_ASSERT((duk_uint_fast_t
) DUK_CAT_GET_LABEL(cat
) == bc
);
4456 duk_hthread_catchstack_unwind(thr
, thr
->catchstack_top
- 1);
4457 /* no need to unwind callstack */
4462 DUK__INTERNAL_ERROR("invalid extra opcode");
4471 /* this should never be possible, because the switch-case is
4474 DUK__INTERNAL_ERROR("invalid opcode");
4482 #ifndef DUK_USE_VERBOSE_EXECUTOR_ERRORS
4484 DUK_ERROR_INTERNAL(thr
, "internal error in bytecode executor");
4488 #undef DUK__LONGJMP_RESTART
4489 #undef DUK__LONGJMP_FINISHED
4490 #undef DUK__LONGJMP_RETHROW
4492 #undef DUK__RETHAND_RESTART
4493 #undef DUK__RETHAND_FINISHED
4502 #undef DUK__REGCONST
4503 #undef DUK__REGCONSTP
4505 #undef DUK__INTERNAL_ERROR
4506 #undef DUK__SYNC_CURR_PC
4507 #undef DUK__SYNC_AND_NULL_CURR_PC