1 /*
2  *  Ecmascript bytecode executor.
3  */
4 
5 #include "duk_internal.h"
6 
7 /*
8  *  Local declarations.
9  */
10 
11 DUK_LOCAL_DECL void duk__js_execute_bytecode_inner(duk_hthread *entry_thread, duk_size_t entry_callstack_top);
12 
13 /*
14  *  Arithmetic, binary, and logical helpers.
15  *
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
20  *  is useful.
21  *
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
25  *  possible.
26  */
27 
duk__compute_mod(duk_double_t d1,duk_double_t d2)28 DUK_LOCAL duk_double_t duk__compute_mod(duk_double_t d1, duk_double_t d2) {
29 	/*
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().
33 	 *
34 	 *  Compare E5 Section 11.5.3 and "man fmod".
35 	 */
36 
37 	return (duk_double_t) DUK_FMOD((double) d1, (double) d2);
38 }
39 
duk__vm_arith_add(duk_hthread * thr,duk_tval * tv_x,duk_tval * tv_y,duk_small_uint_fast_t idx_z)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) {
41 	/*
42 	 *  Addition operator is different from other arithmetic
43 	 *  operations in that it also provides string concatenation.
44 	 *  Hence it is implemented separately.
45 	 *
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.
51 	 *
52 	 *  E5 Section 11.6.1.
53 	 *
54 	 *  Custom types also have special behavior implemented here.
55 	 */
56 
57 	duk_context *ctx = (duk_context *) thr;
58 	duk_double_union du;
59 
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));
66 
67 	/*
68 	 *  Fast paths
69 	 */
70 
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;
74 		duk_int32_t v3_hi;
75 		duk_tval *tv_z;
76 
77 		/* Input values are signed 48-bit so we can detect overflow
78 		 * reliably from high bits or just a comparison.
79 		 */
80 
81 		v1 = DUK_TVAL_GET_FASTINT(tv_x);
82 		v2 = DUK_TVAL_GET_FASTINT(tv_y);
83 		v3 = v1 + v2;
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 */
88 			return;
89 		} else {
90 			/* overflow, fall through */
91 			;
92 		}
93 	}
94 #endif  /* DUK_USE_FASTINT */
95 
96 	if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
97 		duk_tval *tv_z;
98 
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));
102 
103 		tv_z = thr->valstack_bottom + idx_z;
104 		DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, du.d);  /* side effects */
105 		return;
106 	}
107 
108 	/*
109 	 *  Slow path: potentially requires function calls for coercion
110 	 */
111 
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);
116 
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.
120 	 */
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 */
127 	} else {
128 		duk_double_t d1, d2;
129 
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);
136 
137 		du.d = d1 + d2;
138 		DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
139 		DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
140 
141 		duk_pop_2(ctx);
142 		duk_push_number(ctx, du.d);
143 		duk_replace(ctx, (duk_idx_t) idx_z);  /* side effects */
144 	}
145 }
146 
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)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) {
148 	/*
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.
152 	 *
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.
154 	 */
155 
156 	duk_context *ctx = (duk_context *) thr;
157 	duk_tval *tv_z;
158 	duk_double_t d1, d2;
159 	duk_double_union du;
160 
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));
167 
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;
171 		duk_int32_t v3_hi;
172 
173 		v1 = DUK_TVAL_GET_FASTINT(tv_x);
174 		v2 = DUK_TVAL_GET_FASTINT(tv_y);
175 
176 		switch (opcode) {
177 		case DUK_OP_SUB: {
178 			v3 = v1 - v2;
179 			break;
180 		}
181 		case DUK_OP_MUL: {
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).
186 			 */
187 			if (v1 >= -0x80000000LL && v1 <= 0x7fffffffLL && v1 != 0 &&
188 			    v2 >= -0x80000000LL && v2 <= 0x7fffffffLL && v2 != 0) {
189 				v3 = v1 * v2;
190 			} else {
191 				goto skip_fastint;
192 			}
193 			break;
194 		}
195 		case DUK_OP_DIV: {
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
199 			 * for instance).
200 			 */
201 			if (v1 == 0 || v2 == 0) {
202 				goto skip_fastint;
203 			}
204 			v3 = v1 / v2;
205 			if (v3 * v2 != v1) {
206 				goto skip_fastint;
207 			}
208 			break;
209 		}
210 		case DUK_OP_MOD: {
211 			/* Don't allow a zero divisor.  Restrict both v1 and
212 			 * v2 to positive values to avoid compiler specific
213 			 * behavior.
214 			 */
215 			if (v1 < 1 || v2 < 1) {
216 				goto skip_fastint;
217 			}
218 			v3 = v1 % v2;
219 			DUK_ASSERT(v3 >= 0);
220 			DUK_ASSERT(v3 < v2);
221 			DUK_ASSERT(v1 - (v1 / v2) * v2 == v3);
222 			break;
223 		}
224 		default: {
225 			DUK_UNREACHABLE();
226 			goto skip_fastint;
227 		}
228 		}
229 
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 */
234 			return;
235 		}
236 		/* fall through if overflow etc */
237 	}
238  skip_fastint:
239 #endif  /* DUK_USE_FASTINT */
240 
241 	if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
242 		/* fast path */
243 		d1 = DUK_TVAL_GET_NUMBER(tv_x);
244 		d2 = DUK_TVAL_GET_NUMBER(tv_y);
245 	} else {
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);
254 		duk_pop_2(ctx);
255 	}
256 
257 	switch (opcode) {
258 	case DUK_OP_SUB: {
259 		du.d = d1 - d2;
260 		break;
261 	}
262 	case DUK_OP_MUL: {
263 		du.d = d1 * d2;
264 		break;
265 	}
266 	case DUK_OP_DIV: {
267 		du.d = d1 / d2;
268 		break;
269 	}
270 	case DUK_OP_MOD: {
271 		du.d = duk__compute_mod(d1, d2);
272 		break;
273 	}
274 	default: {
275 		DUK_UNREACHABLE();
276 		du.d = DUK_DOUBLE_NAN;  /* should not happen */
277 		break;
278 	}
279 	}
280 
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));
284 
285 	tv_z = thr->valstack_bottom + idx_z;
286 	DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, du.d);  /* side effects */
287 }
288 
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)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) {
290 	/*
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).
296 	 *
297 	 *  E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3
298 	 */
299 
300 	duk_context *ctx = (duk_context *) thr;
301 	duk_tval *tv_z;
302 	duk_int32_t i1, i2, i3;
303 	duk_uint32_t u1, u2, u3;
304 #if defined(DUK_USE_FASTINT)
305 	duk_int64_t fi3;
306 #else
307 	duk_double_t d3;
308 #endif
309 
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));
316 
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);
321 	}
322 	else
323 #endif  /* DUK_USE_FASTINT */
324 	{
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);
329 		duk_pop_2(ctx);
330 	}
331 
332 	switch (opcode) {
333 	case DUK_OP_BAND: {
334 		i3 = i1 & i2;
335 		break;
336 	}
337 	case DUK_OP_BOR: {
338 		i3 = i1 | i2;
339 		break;
340 	}
341 	case DUK_OP_BXOR: {
342 		i3 = i1 ^ i2;
343 		break;
344 	}
345 	case DUK_OP_BASL: {
346 		/* Signed shift, named "arithmetic" (asl) because the result
347 		 * is signed, e.g. 4294967295 << 1 -> -2.  Note that result
348 		 * must be masked.
349 		 */
350 
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 */
354 		break;
355 	}
356 	case DUK_OP_BASR: {
357 		/* signed shift */
358 
359 		u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
360 		i3 = i1 >> (u2 & 0x1f);                      /* E5 Section 11.7.2, steps 7 and 8 */
361 		break;
362 	}
363 	case DUK_OP_BLSR: {
364 		/* unsigned shift */
365 
366 		u1 = ((duk_uint32_t) i1) & 0xffffffffUL;
367 		u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
368 
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;
374 #else
375 		d3 = (duk_double_t) u3;
376 		goto result_set;
377 #endif
378 	}
379 	default: {
380 		DUK_UNREACHABLE();
381 		i3 = 0;  /* should not happen */
382 		break;
383 	}
384 	}
385 
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).
390 	 */
391 	fi3 = (duk_int64_t) i3;
392 
393  fastint_result_set:
394 	tv_z = thr->valstack_bottom + idx_z;
395 	DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, fi3);  /* side effects */
396 #else
397 	d3 = (duk_double_t) i3;
398 
399  result_set:
400 	DUK_ASSERT(!DUK_ISNAN(d3));            /* 'd3' is never NaN, so no need to normalize */
401 	DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3);   /* always normalized */
402 
403 	tv_z = thr->valstack_bottom + idx_z;
404 	DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, d3);  /* side effects */
405 #endif
406 }
407 
408 /* In-place unary operation. */
duk__vm_arith_unary_op(duk_hthread * thr,duk_tval * tv_x,duk_idx_t idx_x,duk_small_uint_fast_t opcode)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) {
410 	/*
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.
414 	 *
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.
416 	 */
417 
418 	duk_context *ctx = (duk_context *) thr;
419 	duk_double_t d1;
420 	duk_double_union du;
421 
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);
427 
428 #if defined(DUK_USE_FASTINT)
429 	if (DUK_TVAL_IS_FASTINT(tv_x)) {
430 		duk_int64_t v1, v2;
431 
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.
437 			 */
438 			if (DUK_LIKELY(v1 != DUK_FASTINT_MIN && v1 != 0)) {
439 				v2 = -v1;
440 				DUK_TVAL_SET_FASTINT(tv_x, v2);  /* no refcount changes */
441 				return;
442 			}
443 		} else {
444 			/* ToNumber() for a fastint is a no-op. */
445 			DUK_ASSERT(opcode == DUK_EXTRAOP_UNP);
446 			return;
447 		}
448 		/* fall through if overflow etc */
449 	}
450 #endif  /* DUK_USE_FASTINT */
451 
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));
457 	}
458 
459 	d1 = DUK_TVAL_GET_NUMBER(tv_x);
460 	if (opcode == DUK_EXTRAOP_UNM) {
461 		du.d = -d1;
462 	} else {
463 		/* ToNumber() for a double is a no-op. */
464 		DUK_ASSERT(opcode == DUK_EXTRAOP_UNP);
465 		du.d = d1;
466 	}
467 	DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);  /* mandatory if du.d is a NaN */
468 
469 	DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
470 
471 #if defined(DUK_USE_FASTINT)
472 	/* Unary plus is used to force a fastint check, so must include
473 	 * downgrade check.
474 	 */
475 	DUK_TVAL_SET_NUMBER_CHKFAST(tv_x, du.d);  /* no refcount changes */
476 #else
477 	DUK_TVAL_SET_NUMBER(tv_x, du.d);  /* no refcount changes */
478 #endif
479 }
480 
duk__vm_bitwise_not(duk_hthread * thr,duk_tval * tv_x,duk_uint_fast_t idx_z)481 DUK_LOCAL void duk__vm_bitwise_not(duk_hthread *thr, duk_tval *tv_x, duk_uint_fast_t idx_z) {
482 	/*
483 	 *  E5 Section 11.4.8
484 	 */
485 
486 	duk_context *ctx = (duk_context *) thr;
487 	duk_tval *tv_z;
488 	duk_int32_t i1, i2;
489 #if !defined(DUK_USE_FASTINT)
490 	duk_double_t d2;
491 #endif
492 
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));
498 
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);
502 	}
503 	else
504 #endif  /* DUK_USE_FASTINT */
505 	{
506 		duk_push_tval(ctx, tv_x);
507 		i1 = duk_to_int32(ctx, -1);
508 		duk_pop(ctx);
509 	}
510 
511 	i2 = ~i1;
512 
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 */
517 #else
518 	d2 = (duk_double_t) i2;
519 
520 	DUK_ASSERT(!DUK_ISNAN(d2));            /* 'val' is never NaN, so no need to normalize */
521 	DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);   /* always normalized */
522 
523 	tv_z = thr->valstack_bottom + idx_z;
524 	DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, d2);  /* side effects */
525 #endif
526 }
527 
duk__vm_logical_not(duk_hthread * thr,duk_tval * tv_x,duk_tval * tv_z)528 DUK_LOCAL void duk__vm_logical_not(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_z) {
529 	/*
530 	 *  E5 Section 11.4.9
531 	 */
532 
533 	duk_bool_t res;
534 
535 	DUK_ASSERT(thr != NULL);
536 	DUK_ASSERT(tv_x != NULL);  /* may be reg or const */
537 	DUK_ASSERT(tv_z != NULL);  /* reg */
538 
539 	DUK_UNREF(thr);  /* w/o refcounts */
540 
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.
544 	 */
545 	res = duk_js_toboolean(tv_x);  /* does not modify tv_x */
546 	DUK_ASSERT(res == 0 || res == 1);
547 	res ^= 1;
548 	DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv_z, res);  /* side effects */
549 }
550 
551 /*
552  *  Longjmp and other control flow transfer for the bytecode executor.
553  *
554  *  The longjmp handler can handle all longjmp types: error, yield, and
555  *  resume (pseudotypes are never actually thrown).
556  *
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.
560  */
561 
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 */
564 
565 #define DUK__RETHAND_RESTART   0  /* state updated, restart bytecode execution */
566 #define DUK__RETHAND_FINISHED  1  /* exit bytecode execution with return value */
567 
568 /* XXX: optimize reconfig valstack operations so that resize, clamp, and setting
569  * top are combined into one pass.
570  */
571 
572 /* Reconfigure value stack for return to an Ecmascript function at 'act_idx'. */
duk__reconfig_valstack_ecma_return(duk_hthread * thr,duk_size_t act_idx)573 DUK_LOCAL void duk__reconfig_valstack_ecma_return(duk_hthread *thr, duk_size_t act_idx) {
574 	duk_activation *act;
575 	duk_hcompiledfunction *h_func;
576 	duk_idx_t clamp_top;
577 
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 */
583 
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.
587 	 */
588 
589 	act = thr->callstack + act_idx;
590 	h_func = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
591 
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);
596 	act = NULL;
597 
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 */
603 	                               0 /* no compact */ |
604 	                               DUK_VSRESIZE_FLAG_THROW);
605 
606 	duk_set_top((duk_context *) thr, h_func->nregs);
607 }
608 
duk__reconfig_valstack_ecma_catcher(duk_hthread * thr,duk_size_t act_idx,duk_size_t cat_idx)609 DUK_LOCAL void duk__reconfig_valstack_ecma_catcher(duk_hthread *thr, duk_size_t act_idx, duk_size_t cat_idx) {
610 	duk_activation *act;
611 	duk_catcher *cat;
612 	duk_hcompiledfunction *h_func;
613 	duk_idx_t clamp_top;
614 
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 */
620 
621 	act = thr->callstack + act_idx;
622 	cat = thr->catchstack + cat_idx;
623 	h_func = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
624 
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);
629 	act = NULL;
630 	cat = NULL;
631 
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 */
637 	                               0 /* no compact */ |
638 	                               DUK_VSRESIZE_FLAG_THROW);
639 
640 	duk_set_top((duk_context *) thr, h_func->nregs);
641 }
642 
643 /* Set catcher regs: idx_base+0 = value, idx_base+1 = lj_type. */
duk__set_catcher_regs(duk_hthread * thr,duk_size_t cat_idx,duk_tval * tv_val_unstable,duk_small_uint_t 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) {
645 	duk_tval *tv1;
646 
647 	DUK_ASSERT(thr != NULL);
648 	DUK_ASSERT(tv_val_unstable != NULL);
649 
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 */
653 
654 	tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base + 1;
655 	DUK_ASSERT(tv1 < thr->valstack_top);
656 
657 	DUK_TVAL_SET_FASTINT_U32_UPDREF(thr, tv1, (duk_uint32_t) lj_type);  /* side effects */
658 }
659 
duk__handle_catch(duk_hthread * thr,duk_size_t cat_idx,duk_tval * tv_val_unstable,duk_small_uint_t lj_type)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) {
661 	duk_context *ctx;
662 	duk_activation *act;
663 
664 	DUK_ASSERT(thr != NULL);
665 	DUK_ASSERT(tv_val_unstable != NULL);
666 	ctx = (duk_context *) thr;
667 
668 	duk__set_catcher_regs(thr, cat_idx, tv_val_unstable, lj_type);
669 
670 	duk_hthread_catchstack_unwind(thr, cat_idx + 1);
671 	duk_hthread_callstack_unwind(thr, thr->catchstack[cat_idx].callstack_index + 1);
672 
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)));
676 
677 	duk__reconfig_valstack_ecma_catcher(thr, thr->callstack_top - 1, cat_idx);
678 
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 */
682 	act = NULL;
683 
684 	/*
685 	 *  If entering a 'catch' block which requires an automatic
686 	 *  catch variable binding, create the lexical environment.
687 	 *
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.
692 	 */
693 
694 	if (DUK_CAT_HAS_CATCH_BINDING_ENABLED(&thr->catchstack[cat_idx])) {
695 		duk_hobject *new_env;
696 		duk_hobject *act_lex_env;
697 
698 		DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding"));
699 
700 		/* Note: 'act' is dangerous here because it may get invalidate at many
701 		 * points, so we re-lookup it multiple times.
702 		 */
703 		DUK_ASSERT(thr->callstack_top >= 1);
704 		act = thr->callstack + thr->callstack_top - 1;
705 
706 		if (act->lex_env == NULL) {
707 			DUK_ASSERT(act->var_env == NULL);
708 			DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
709 
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;
713 		}
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 */
718 
719 		act = thr->callstack + thr->callstack_top - 1;
720 		act_lex_env = act->lex_env;
721 		act = NULL;  /* invalidated */
722 
723 		(void) duk_push_object_helper_proto(ctx,
724 		                                    DUK_HOBJECT_FLAG_EXTENSIBLE |
725 		                                    DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV),
726 		                                    act_lex_env);
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));
730 
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.
735 		 */
736 
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 */
741 
742 		act = thr->callstack + thr->callstack_top - 1;
743 		act->lex_env = new_env;
744 		DUK_HOBJECT_INCREF(thr, new_env);  /* reachable through activation */
745 
746 		DUK_CAT_SET_LEXENV_ACTIVE(&thr->catchstack[cat_idx]);
747 
748 		duk_pop(ctx);
749 
750 		DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr *) new_env));
751 	}
752 
753 	DUK_CAT_CLEAR_CATCH_ENABLED(&thr->catchstack[cat_idx]);
754 }
755 
duk__handle_finally(duk_hthread * thr,duk_size_t cat_idx,duk_tval * tv_val_unstable,duk_small_uint_t lj_type)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) {
757 	duk_activation *act;
758 
759 	DUK_ASSERT(thr != NULL);
760 	DUK_ASSERT(tv_val_unstable != NULL);
761 
762 	duk__set_catcher_regs(thr, cat_idx, tv_val_unstable, lj_type);
763 
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);
766 
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)));
770 
771 	duk__reconfig_valstack_ecma_catcher(thr, thr->callstack_top - 1, cat_idx);
772 
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 */
776 	act = NULL;
777 
778 	DUK_CAT_CLEAR_FINALLY_ENABLED(&thr->catchstack[cat_idx]);
779 }
780 
duk__handle_label(duk_hthread * thr,duk_size_t cat_idx,duk_small_uint_t lj_type)781 DUK_LOCAL void duk__handle_label(duk_hthread *thr, duk_size_t cat_idx, duk_small_uint_t lj_type) {
782 	duk_activation *act;
783 
784 	DUK_ASSERT(thr != NULL);
785 
786 	DUK_ASSERT(thr->callstack_top >= 1);
787 	act = thr->callstack + thr->callstack_top - 1;
788 
789 	DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
790 	DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act)));
791 
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 */
795 
796 	duk_hthread_catchstack_unwind(thr, cat_idx + 1);  /* keep label catcher */
797 	/* no need to unwind callstack */
798 
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);
805 #endif
806 }
807 
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.
810  */
duk__handle_yield(duk_hthread * thr,duk_hthread * resumer,duk_size_t act_idx,duk_tval * tv_val_unstable)811 DUK_LOCAL void duk__handle_yield(duk_hthread *thr, duk_hthread *resumer, duk_size_t act_idx, duk_tval *tv_val_unstable) {
812 	duk_tval *tv1;
813 
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 */
819 
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 */
822 
823 	duk_hthread_callstack_unwind(resumer, act_idx + 1);  /* unwind to 'resume' caller */
824 
825 	/* no need to unwind catchstack */
826 	duk__reconfig_valstack_ecma_return(resumer, act_idx);
827 
828 	/* caller must change active thread, and set thr->resumer to NULL */
829 }
830 
831 DUK_LOCAL
duk__handle_longjmp(duk_hthread * thr,duk_hthread * entry_thread,duk_size_t entry_callstack_top)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;
837 
838 	DUK_ASSERT(thr != NULL);
839 	DUK_ASSERT(entry_thread != NULL);
840 	DUK_ASSERT(entry_callstack_top > 0);  /* guarantees entry_callstack_top - 1 >= 0 */
841 
842 	entry_callstack_index = entry_callstack_top - 1;
843 
844 	/* 'thr' is the current thread, as no-one resumes except us and we
845 	 * switch 'thr' in that case.
846 	 */
847 	DUK_ASSERT(thr == thr->heap->curr_thread);
848 
849 	/*
850 	 *  (Re)try handling the longjmp.
851 	 *
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
857 	 */
858 
859  check_longjmp:
860 
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));
866 
867 	switch (thr->heap->lj.type) {
868 
869 	case DUK_LJ_TYPE_RESUME: {
870 		/*
871 		 *  Note: lj.value1 is 'value', lj.value2 is 'resumee'.
872 		 *  This differs from YIELD.
873 		 */
874 
875 		duk_tval *tv;
876 		duk_tval *tv2;
877 		duk_size_t act_idx;
878 		duk_hthread *resumee;
879 
880 		/* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
881 
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 */
890 
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);
896 
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))));
918 
919 		if (thr->heap->lj.iserror) {
920 			/*
921 			 *  Throw the error in the resumed thread's context; the
922 			 *  error value is pushed onto the resumee valstack.
923 			 *
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.
928 			 */
929 
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);
934 			thr = resumee;
935 
936 			thr->heap->lj.type = DUK_LJ_TYPE_THROW;
937 
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 */
940 
941 			DUK_ASSERT(thr->heap->lj.iserror);  /* already set */
942 
943 			DUK_DD(DUK_DDPRINT("-> resume with an error, converted to a throw in the resumee, propagate"));
944 			goto check_longjmp;
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 */
948 
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 */
953 
954 			duk_hthread_callstack_unwind(resumee, act_idx + 1);  /* unwind to 'yield' caller */
955 
956 			/* no need to unwind catchstack */
957 
958 			duk__reconfig_valstack_ecma_return(resumee, act_idx);
959 
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);
964 #if 0
965 			thr = resumee;  /* not needed, as we exit right away */
966 #endif
967 			DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
968 			retval = DUK__LONGJMP_RESTART;
969 			goto wipe_and_return;
970 		} else {
971 			duk_small_uint_t call_flags;
972 			duk_bool_t setup_rc;
973 
974 			/* resumee: [... initial_func]  (currently actually: [initial_func]) */
975 
976 			duk_push_undefined((duk_context *) resumee);
977 			tv = &thr->heap->lj.value1;
978 			duk_push_tval((duk_context *) resumee, tv);
979 
980 			/* resumee: [... initial_func undefined(= this) resume_value ] */
981 
982 			call_flags = DUK_CALL_FLAG_IS_RESUME;  /* is resume, not a tail call */
983 
984 			setup_rc = duk_handle_ecma_call_setup(resumee,
985 			                                      1,              /* num_stack_args */
986 			                                      call_flags);    /* call_flags */
987 			if (setup_rc == 0) {
988 				/* Shouldn't happen but check anyway. */
989 				DUK_ERROR_INTERNAL_DEFMSG(thr);
990 			}
991 
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);
996 #if 0
997 			thr = resumee;  /* not needed, as we exit right away */
998 #endif
999 			DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
1000 			retval = DUK__LONGJMP_RESTART;
1001 			goto wipe_and_return;
1002 		}
1003 		DUK_UNREACHABLE();
1004 		break;  /* never here */
1005 	}
1006 
1007 	case DUK_LJ_TYPE_YIELD: {
1008 		/*
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
1012 		 *  calls.
1013 		 *
1014 		 *  This excludes the 'entry' thread which will always have
1015 		 *  a preventcount > 0.
1016 		 */
1017 
1018 		duk_hthread *resumer;
1019 
1020 		/* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
1021 
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 */
1031 
1032 		resumer = thr->resumer;
1033 
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 */
1043 
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);
1049 			thr = resumer;
1050 
1051 			thr->heap->lj.type = DUK_LJ_TYPE_THROW;
1052 			/* lj.value1 is already set */
1053 			DUK_ASSERT(thr->heap->lj.iserror);  /* already set */
1054 
1055 			DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate"));
1056 			goto check_longjmp;
1057 		} else {
1058 			duk__handle_yield(thr, resumer, resumer->callstack_top - 2, &thr->heap->lj.value1);
1059 
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);
1064 #if 0
1065 			thr = resumer;  /* not needed, as we exit right away */
1066 #endif
1067 
1068 			DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer"));
1069 			retval = DUK__LONGJMP_RESTART;
1070 			goto wipe_and_return;
1071 		}
1072 		DUK_UNREACHABLE();
1073 		break;  /* never here */
1074 	}
1075 
1076 	case DUK_LJ_TYPE_THROW: {
1077 		/*
1078 		 *  Three possible outcomes:
1079 		 *    * A try or finally catcher is found => resume there.
1080 		 *      (or)
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
1084 		 *      catchpoint.
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.)
1090 		 *
1091 		 *  Note: until we hit the entry level, there can only be
1092 		 *  Ecmascript activations.
1093 		 */
1094 
1095 		duk_catcher *cat;
1096 		duk_hthread *resumer;
1097 
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 */
1103 				break;
1104 			}
1105 
1106 			if (DUK_CAT_HAS_CATCH_ENABLED(cat)) {
1107 				DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
1108 
1109 				duk__handle_catch(thr,
1110 				                  cat - thr->catchstack,
1111 				                  &thr->heap->lj.value1,
1112 				                  DUK_LJ_TYPE_THROW);
1113 
1114 				DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution"));
1115 				retval = DUK__LONGJMP_RESTART;
1116 				goto wipe_and_return;
1117 			}
1118 
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));
1122 
1123 				duk__handle_finally(thr,
1124 				                    cat - thr->catchstack,
1125 				                    &thr->heap->lj.value1,
1126 				                    DUK_LJ_TYPE_THROW);
1127 
1128 				DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution"));
1129 				retval = DUK__LONGJMP_RESTART;
1130 				goto wipe_and_return;
1131 			}
1132 
1133 			cat--;
1134 		}
1135 
1136 		if (thr == entry_thread) {
1137 			/* not caught by anything before entry level; rethrow and let the
1138 			 * final catcher unwind everything
1139 			 */
1140 #if 0
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);
1143 
1144 #endif
1145 			DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor"));
1146 			retval = DUK__LONGJMP_RETHROW;
1147 			goto just_return;
1148 			/* Note: MUST NOT wipe_and_return here, as heap->lj must remain intact */
1149 		}
1150 
1151 		DUK_DD(DUK_DDPRINT("-> throw not caught by current thread, yield error to resumer and recheck longjmp"));
1152 
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)
1156 		 */
1157 
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 */
1165 
1166 		resumer = thr->resumer;
1167 
1168 		/* reset longjmp */
1169 
1170 		DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);  /* already set */
1171 		/* lj.value1 already set */
1172 
1173 		duk_hthread_terminate(thr);  /* updates thread state, minimizes its allocations */
1174 		DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
1175 
1176 		thr->resumer = NULL;
1177 		resumer->state = DUK_HTHREAD_STATE_RUNNING;
1178 		DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
1179 		thr = resumer;
1180 		goto check_longjmp;
1181 	}
1182 
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:
1187 	default: {
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;
1191 	}
1192 
1193 	}  /* end switch */
1194 
1195 	DUK_UNREACHABLE();
1196 
1197  wipe_and_return:
1198 	/* this is not strictly necessary, but helps debugging */
1199 	thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
1200 	thr->heap->lj.iserror = 0;
1201 
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 */
1204 
1205  just_return:
1206 	return retval;
1207 
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.
1212 	 */
1213 	DUK_ERROR_INTERNAL_DEFMSG(thr);
1214 	DUK_UNREACHABLE();
1215 	return retval;
1216 }
1217 
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).
1221  */
duk__handle_break_or_continue(duk_hthread * thr,duk_uint_t label_id,duk_small_uint_t lj_type)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) {
1225 	duk_catcher *cat;
1226 	duk_size_t orig_callstack_index;
1227 
1228 	DUK_ASSERT(thr != NULL);
1229 
1230 	/*
1231 	 *  Find a matching label catcher or 'finally' catcher in
1232 	 *  the same function.
1233 	 *
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
1237 	 *  correctly.
1238 	 */
1239 
1240 	/* Note: thr->catchstack_top may be 0, so that cat < thr->catchstack
1241 	 * initially.  This is OK and intended.
1242 	 */
1243 	cat = thr->catchstack + thr->catchstack_top - 1;
1244 	DUK_ASSERT(thr->callstack_top > 0);
1245 	orig_callstack_index = thr->callstack_top - 1;
1246 
1247 	DUK_DDD(DUK_DDDPRINT("handling break/continue with label=%ld, callstack index=%ld",
1248 	                     (long) label_id, (long) cat->callstack_index));
1249 
1250 	while (cat >= thr->catchstack) {
1251 		if (cat->callstack_index != orig_callstack_index) {
1252 			break;
1253 		}
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)));
1258 
1259 		if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
1260 		    DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
1261 			duk_size_t cat_idx;
1262 			duk_tval tv_tmp;
1263 
1264 			cat_idx = (duk_size_t) (cat - thr->catchstack);  /* get before side effects */
1265 
1266 			DUK_TVAL_SET_FASTINT_U32(&tv_tmp, (duk_uint32_t) label_id);
1267 			duk__handle_finally(thr, cat_idx, &tv_tmp, lj_type);
1268 
1269 			DUK_DD(DUK_DDPRINT("-> break/continue caught by 'finally', restart execution"));
1270 			return;
1271 		}
1272 		if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL &&
1273 		    (duk_uint_t) DUK_CAT_GET_LABEL(cat) == label_id) {
1274 			duk_size_t cat_idx;
1275 
1276 			cat_idx = (duk_size_t) (cat - thr->catchstack);
1277 			duk__handle_label(thr, cat_idx, lj_type);
1278 
1279 			DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution"));
1280 			return;
1281 		}
1282 		cat--;
1283 	}
1284 
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);
1288 	return;
1289 }
1290 
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.
1294  */
duk__handle_return(duk_hthread * thr,duk_hthread * entry_thread,duk_size_t entry_callstack_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) {
1298 	duk_tval *tv1;
1299 	duk_tval *tv2;
1300 	duk_hthread *resumer;
1301 	duk_catcher *cat;
1302 	duk_size_t new_cat_top;
1303 	duk_size_t orig_callstack_index;
1304 
1305 	/* We can directly access value stack here. */
1306 
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 */
1312 
1313 	/*
1314 	 *  Four possible outcomes:
1315 	 *
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
1319 	 *       ENDFIN).
1320 	 *
1321 	 *    2. The return happens at the entry level of the bytecode
1322 	 *       executor, so return from the executor (in C stack).
1323 	 *
1324 	 *    3. There is a calling (Ecmascript) activation in the call
1325 	 *       stack => return to it, in the same executor instance.
1326 	 *
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
1331 	 *       resumed).
1332 	 */
1333 
1334 	DUK_ASSERT(thr != NULL);
1335 	DUK_ASSERT(thr->callstack_top >= 1);
1336 	DUK_ASSERT(thr->catchstack != NULL);
1337 
1338 	/* XXX: does not work if thr->catchstack is NULL */
1339 	/* XXX: does not work if thr->catchstack is allocated but lowest pointer */
1340 
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;
1344 
1345 	while (cat >= thr->catchstack) {
1346 		if (cat->callstack_index != orig_callstack_index) {
1347 			break;
1348 		}
1349 		if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
1350 		    DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
1351 			duk_size_t cat_idx;
1352 
1353 			cat_idx = (duk_size_t) (cat - thr->catchstack);  /* get before side effects */
1354 
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);
1357 
1358 			DUK_DD(DUK_DDPRINT("-> return caught by 'finally', restart execution"));
1359 			return DUK__RETHAND_RESTART;
1360 		}
1361 		cat--;
1362 	}
1363 	/* If out of catchstack, cat = thr->catchstack - 1;
1364 	 * new_cat_top will be 0 in that case.
1365 	 */
1366 	new_cat_top = (duk_size_t) ((cat + 1) - thr->catchstack);
1367 	cat = NULL;  /* avoid referencing, invalidated */
1368 
1369 	DUK_DDD(DUK_DDDPRINT("no catcher in catch stack, return to calling activation / yield"));
1370 
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 ].
1375 		 */
1376 
1377 		/* XXX: could unwind catchstack here, so that call handling
1378 		 * didn't need to do that?
1379 		 */
1380 		DUK_DDD(DUK_DDDPRINT("-> return propagated up to entry level, exit bytecode executor"));
1381 		return DUK__RETHAND_FINISHED;
1382 	}
1383 
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)
1387 		 */
1388 
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));
1392 
1393 		DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2)));   /* must be ecmascript */
1394 
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 */
1399 
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)));
1403 
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);
1407 
1408 		DUK_DD(DUK_DDPRINT("-> return not intercepted, restart execution in caller"));
1409 		return DUK__RETHAND_RESTART;
1410 	}
1411 
1412 	DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)"));
1413 
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);
1424 
1425 	resumer = thr->resumer;
1426 
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);
1430 
1431 	duk_hthread_terminate(thr);  /* updates thread state, minimizes its allocations */
1432 	DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
1433 
1434 	thr->resumer = NULL;
1435 	resumer->state = DUK_HTHREAD_STATE_RUNNING;
1436 	DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
1437 #if 0
1438 	thr = resumer;  /* not needed */
1439 #endif
1440 
1441 	DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer"));
1442 	return DUK__RETHAND_RESTART;
1443 }
1444 
1445 /*
1446  *  Executor interrupt handling
1447  *
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.
1452  *
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).
1463  *
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.
1468  */
1469 
1470 #if defined(DUK_USE_INTERRUPT_COUNTER)
1471 
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 */
1474 
1475 #if defined(DUK_USE_DEBUGGER_SUPPORT)
duk__interrupt_handle_debugger(duk_hthread * thr,duk_bool_t * out_immediate,duk_small_uint_t * out_interrupt_retval)1476 DUK_LOCAL void duk__interrupt_handle_debugger(duk_hthread *thr, duk_bool_t *out_immediate, duk_small_uint_t *out_interrupt_retval) {
1477 	duk_context *ctx;
1478 	duk_activation *act;
1479 	duk_breakpoint *bp;
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;
1484 
1485 	DUK_ASSERT(thr->heap->dbg_processing == 0);  /* don't re-enter e.g. during Eval */
1486 
1487 	ctx = (duk_context *) thr;
1488 	act = thr->callstack + thr->callstack_top - 1;
1489 
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.
1493 	 */
1494 
1495 	/*
1496 	 *  Breakpoint and step state checks
1497 	 */
1498 
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);
1503 
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",
1512 				                 (long) line));
1513 
1514 				DUK_HEAP_SET_PAUSED(thr->heap);
1515 			}
1516 
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.
1521 			 *
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.
1528 			 */
1529 			bp_active = thr->heap->dbg_breakpoints_active;
1530 			for (;;) {
1531 				bp = *bp_active++;
1532 				if (bp == NULL) {
1533 					break;
1534 				}
1535 
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));
1540 
1541 					DUK_HEAP_SET_PAUSED(thr->heap);
1542 				}
1543 			}
1544 		} else {
1545 			;
1546 		}
1547 
1548 		act->prev_line = line;
1549 	}
1550 
1551 	/*
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.
1555 	 *
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.
1561 	 */
1562 
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.
1567 		 */
1568 		process_messages = 1;
1569 	}
1570 
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
1575 		 * anything here.
1576 		 */
1577 
1578 		duk_double_t now, diff_last;
1579 
1580 		thr->heap->dbg_last_counter = thr->heap->dbg_exec_counter;
1581 		now = DUK_USE_DATE_GET_NOW(ctx);
1582 
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
1586 			 * reasonably.
1587 			 *
1588 			 * Same interval is now used for status sending and
1589 			 * peeking.
1590 			 */
1591 
1592 			thr->heap->dbg_last_time = now;
1593 			thr->heap->dbg_state_dirty = 1;
1594 			process_messages = 1;
1595 		}
1596 	}
1597 
1598 	/*
1599 	 *  Process messages and send status if necessary.
1600 	 *
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.
1605 	 *
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
1610 	 *  detaching.
1611 	 */
1612 
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);
1618 	}
1619 
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.
1626 	 */
1627 
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) {
1636 			*out_immediate = 1;
1637 		}
1638 
1639 		/* If we processed any debug messages breakpoints may have
1640 		 * changed; restart execution to re-check active breakpoints.
1641 		 */
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;
1645 		}
1646 	} else {
1647 		DUK_D(DUK_DPRINT("debugger became detached, resume normal execution"));
1648 	}
1649 }
1650 #endif  /* DUK_USE_DEBUGGER_SUPPORT */
1651 
duk__executor_interrupt(duk_hthread * thr)1652 DUK_LOCAL duk_small_uint_t duk__executor_interrupt(duk_hthread *thr) {
1653 	duk_int_t ctr;
1654 	duk_activation *act;
1655 	duk_hcompiledfunction *fun;
1656 	duk_bool_t immediate = 0;
1657 	duk_small_uint_t retval;
1658 
1659 	DUK_ASSERT(thr != NULL);
1660 	DUK_ASSERT(thr->heap != NULL);
1661 	DUK_ASSERT(thr->callstack != NULL);
1662 	DUK_ASSERT(thr->callstack_top > 0);
1663 
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));
1670 #endif
1671 
1672 	retval = DUK__INT_NOACTION;
1673 	ctr = DUK_HTHREAD_INTCTR_DEFAULT;
1674 
1675 	/*
1676 	 *  Avoid nested calls.  Concretely this happens during debugging, e.g.
1677 	 *  when we eval() an expression.
1678 	 *
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
1683 	 * scenarios.
1684 	 */
1685 
1686 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1687 	if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap) || thr->heap->dbg_processing) {
1688 #else
1689 	if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap)) {
1690 #endif
1691 		DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring"));
1692 
1693 		/* Set a high interrupt counter; the original executor
1694 		 * interrupt invocation will rewrite before exiting.
1695 		 */
1696 		thr->interrupt_init = ctr;
1697 		thr->interrupt_counter = ctr - 1;
1698 		return DUK__INT_NOACTION;
1699 	}
1700 	DUK_HEAP_SET_INTERRUPT_RUNNING(thr->heap);
1701 
1702 	act = thr->callstack + thr->callstack_top - 1;
1703 
1704 	fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act);
1705 	DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION((duk_hobject *) fun));
1706 
1707 	DUK_UNREF(fun);
1708 
1709 #if defined(DUK_USE_EXEC_TIMEOUT_CHECK)
1710 	/*
1711 	 *  Execution timeout check
1712 	 */
1713 
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.
1722 		 */
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");
1728 	}
1729 #endif  /* DUK_USE_EXEC_TIMEOUT_CHECK */
1730 
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).
1736 		 */
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 */
1740 	}
1741 #endif  /* DUK_USE_DEBUGGER_SUPPORT */
1742 
1743 	/*
1744 	 *  Update the interrupt counter
1745 	 */
1746 
1747 	if (immediate) {
1748 		/* Cause an interrupt after executing one instruction. */
1749 		ctr = 1;
1750 	}
1751 
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
1755 	 * be 0.
1756 	 */
1757 	DUK_ASSERT(ctr >= 1);
1758 	thr->interrupt_init = ctr;
1759 	thr->interrupt_counter = ctr - 1;
1760 	DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap);
1761 
1762 	return retval;
1763 }
1764 #endif  /* DUK_USE_INTERRUPT_COUNTER */
1765 
1766 /*
1767  *  Debugger handling for executor restart
1768  *
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.
1774  */
1775 
1776 #if defined(DUK_USE_DEBUGGER_SUPPORT)
1777 DUK_LOCAL void duk__executor_recheck_debugger(duk_hthread *thr, duk_activation *act, duk_hcompiledfunction *fun) {
1778 	duk_heap *heap;
1779 	duk_tval *tv_tmp;
1780 	duk_hstring *filename;
1781 	duk_small_uint_t bp_idx;
1782 	duk_breakpoint **bp_active;
1783 
1784 	DUK_ASSERT(thr != NULL);
1785 	DUK_ASSERT(act != NULL);
1786 	DUK_ASSERT(fun != NULL);
1787 
1788 	heap = thr->heap;
1789 	bp_active = heap->dbg_breakpoints_active;
1790 	act->flags &= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
1791 
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);
1795 
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:
1803 		 *
1804 		 *     function foo() {
1805 		 *         print('foo');
1806 		 *         function bar() {    <-.  breakpoints in these
1807 		 *             print('bar');     |  lines should not affect
1808 		 *         }                   <-'  foo() execution
1809 		 *         bar();
1810 		 *     }
1811 		 *
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
1816 		 * line ranges).
1817 		 *
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.
1824 		 */
1825 
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;
1831 
1832 			if (bp->filename == filename &&
1833 			    bp->line >= fun->start_line && bp->line <= fun->end_line) {
1834 				bp_match = 1;
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),
1838 				                   (long) bp->line,
1839 				                   DUK_HSTRING_GET_DATA(filename),
1840 				                   (long) bp->line,
1841 				                   (long) fun->start_line,
1842 				                   (long) fun->end_line));
1843 
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"));
1851 						bp_match = 0;
1852 						break;
1853 					}
1854 					funcs++;
1855 				}
1856 
1857 				if (bp_match) {
1858 					/* No need to check for size of bp_active list,
1859 					 * it's always larger than maximum number of
1860 					 * breakpoints.
1861 					 */
1862 					act->flags |= DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
1863 					*bp_active = heap->dbg_breakpoints + bp_idx;
1864 					bp_active++;
1865 				}
1866 			}
1867 		}
1868 	}
1869 
1870 	*bp_active = NULL;  /* terminate */
1871 
1872 	DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active - thr->heap->dbg_breakpoints_active)));
1873 
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);
1881 	}
1882 
1883 	/* Force interrupt right away if we're paused or in "checked mode".
1884 	 * Step out is handled by callstack unwind.
1885 	 */
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.
1894 		 */
1895 		DUK_ASSERT(thr->interrupt_counter <= thr->interrupt_init);
1896 		thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter;
1897 		thr->interrupt_counter = 0;
1898 	}
1899 }
1900 #endif  /* DUK_USE_DEBUGGER_SUPPORT */
1901 
1902 /*
1903  *  Ecmascript bytecode executor.
1904  *
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
1910  *  setjmp() jmpbuf.
1911  *
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.
1916  *
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,
1921  *  and return.
1922  *
1923  *  For more detailed notes, see doc/execution.rst.
1924  *
1925  *  Also see doc/code-issues.rst for discussion of setjmp(), longjmp(),
1926  *  and volatile.
1927  */
1928 
1929 /* Presence of 'fun' is config based, there's a marginal performance
1930  * difference and the best option is architecture dependent.
1931  */
1932 #if defined(DUK_USE_EXEC_FUN_LOCAL)
1933 #define DUK__FUN()          fun
1934 #else
1935 #define DUK__FUN()          ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC((thr)->callstack + (thr)->callstack_top - 1))
1936 #endif
1937 #define DUK__STRICT()       (DUK_HOBJECT_HAS_STRICT((duk_hobject *) DUK__FUN()))
1938 
1939 /* Reg/const access macros: these are very footprint and performance sensitive
1940  * so modify with care.
1941  */
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))
1946 #if 0
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))
1951 #endif
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).
1955  */
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))
1959 
1960 #ifdef DUK_USE_VERBOSE_EXECUTOR_ERRORS
1961 #define DUK__INTERNAL_ERROR(msg)  do { \
1962 		DUK_ERROR_INTERNAL(thr, (msg)); \
1963 	} while (0)
1964 #else
1965 #define DUK__INTERNAL_ERROR(msg)  do { \
1966 		goto internal_error; \
1967 	} while (0)
1968 #endif
1969 
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; \
1974 	} while (0)
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; \
1980 	} while (0)
1981 
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;
1988 
1989 	/* Longjmp callers are required to sync-and-null thr->ptr_curr_pc
1990 	 * before longjmp.
1991 	 */
1992 	DUK_ASSERT(heap->curr_thread != NULL);
1993 	DUK_ASSERT(heap->curr_thread->ptr_curr_pc == NULL);
1994 
1995 	/* XXX: signalling the need to shrink check (only if unwound) */
1996 
1997 	/* Must be restored here to handle e.g. yields properly. */
1998 	heap->call_recursion_depth = entry_call_recursion_depth;
1999 
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.
2003 	 */
2004 	heap->lj.jmpbuf_ptr = (duk_jmpbuf *) entry_jmpbuf_ptr;
2005 
2006 	lj_ret = duk__handle_longjmp(heap->curr_thread, entry_thread, entry_callstack_top);
2007 
2008 	if (lj_ret == DUK__LONGJMP_RESTART) {
2009 		/* Restart bytecode execution, possibly with a changed thread. */
2010 		;
2011 	} else {
2012 		/* Rethrow error to calling state. */
2013 		DUK_ASSERT(lj_ret == DUK__LONGJMP_RETHROW);
2014 
2015 		/* Longjmp handling has restored jmpbuf_ptr. */
2016 		DUK_ASSERT(heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr);
2017 
2018 		/* Thread may have changed, e.g. YIELD converted to THROW. */
2019 		duk_err_longjmp(heap->curr_thread);
2020 		DUK_UNREACHABLE();
2021 	}
2022 }
2023 
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;
2032 	duk_heap *heap;
2033 
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)));
2041 
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;
2047 
2048 	/*
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()).
2052 	 *
2053 	 *  See doc/code-issues.rst for notes on variable assignment
2054 	 *  before and after setjmp().
2055 	 */
2056 
2057 	for (;;) {
2058 		heap->lj.jmpbuf_ptr = &our_jmpbuf;
2059 		DUK_ASSERT(heap->lj.jmpbuf_ptr != NULL);
2060 
2061 #if defined(DUK_USE_CPP_EXCEPTIONS)
2062 		try {
2063 #else
2064 		DUK_ASSERT(heap->lj.jmpbuf_ptr == &our_jmpbuf);
2065 		if (DUK_SETJMP(our_jmpbuf.jb) == 0) {
2066 #endif
2067 			/* Execute bytecode until returned or longjmp(). */
2068 			duk__js_execute_bytecode_inner(entry_thread, entry_callstack_top);
2069 
2070 			/* Successful return: restore jmpbuf and return to caller. */
2071 			heap->lj.jmpbuf_ptr = entry_jmpbuf_ptr;
2072 
2073 			return;
2074 #if defined(DUK_USE_CPP_EXCEPTIONS)
2075 		} catch (duk_internal_exception &exc) {
2076 #else
2077 		} else {
2078 #endif
2079 #if defined(DUK_USE_CPP_EXCEPTIONS)
2080 			DUK_UNREF(exc);
2081 #endif
2082 			DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor"));
2083 
2084 			duk__handle_executor_error(heap,
2085 			                           entry_thread,
2086 			                           entry_callstack_top,
2087 			                           entry_call_recursion_depth,
2088 			                           entry_jmpbuf_ptr);
2089 		}
2090 #if defined(DUK_USE_CPP_EXCEPTIONS)
2091 		catch (std::exception &exc) {
2092 			const char *what = exc.what();
2093 			if (!what) {
2094 				what = "unknown";
2095 			}
2096 			DUK_D(DUK_DPRINT("unexpected c++ std::exception (perhaps thrown by user code)"));
2097 			try {
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"));
2102 				DUK_UNREF(exc);
2103 				duk__handle_executor_error(heap,
2104 				                           entry_thread,
2105 				                           entry_callstack_top,
2106 				                           entry_call_recursion_depth,
2107 				                           entry_jmpbuf_ptr);
2108 			}
2109 		} catch (...) {
2110 			DUK_D(DUK_DPRINT("unexpected c++ exception (perhaps thrown by user code)"));
2111 			try {
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"));
2116 				DUK_UNREF(exc);
2117 				duk__handle_executor_error(heap,
2118 				                           entry_thread,
2119 				                           entry_callstack_top,
2120 				                           entry_call_recursion_depth,
2121 				                           entry_jmpbuf_ptr);
2122 			}
2123 		}
2124 #endif
2125 	}
2126 
2127 	DUK_UNREACHABLE();
2128 }
2129 
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.
2136 	 */
2137 	duk_instr_t *curr_pc;         /* bytecode has a stable pointer */
2138 
2139 	/* Hot variables for interpretation.  Critical for performance,
2140 	 * but must add sparingly to minimize register shuffling.
2141 	 */
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;
2149 #else
2150 	/* 'fun' is quite rarely used, so no local for it */
2151 #endif
2152 
2153 #ifdef DUK_USE_INTERRUPT_COUNTER
2154 	duk_int_t int_ctr;
2155 #endif
2156 
2157 #ifdef DUK_USE_ASSERTIONS
2158 	duk_size_t valstack_top_base;    /* valstack top, should match before interpreting each op (no leftovers) */
2159 #endif
2160 
2161 	/*
2162 	 *  Restart execution by reloading thread state.
2163 	 *
2164 	 *  Note that 'thr' and any thread configuration may have changed,
2165 	 *  so all local variables are suspect and we need to reinitialize.
2166 	 *
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
2169 	 *  memory anyway.
2170 	 *
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
2173 	 *  takes place.
2174 	 *
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).
2179 	 *
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)
2185 	 *
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
2192 	 *      is required
2193 	 *
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
2197 	 *
2198 	 *  See execution.rst for discussion.
2199 	 */
2200 
2201  restart_execution:
2202 
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.
2206 	 */
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)));
2212 
2213 	thr->ptr_curr_pc = &curr_pc;
2214 
2215 	/* Relookup and initialize dispatch loop variables.  Debugger check. */
2216 	{
2217 		duk_activation *act;
2218 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
2219 		duk_hcompiledfunction *fun;
2220 #endif
2221 
2222 		/* Assume interrupt init/counter are properly initialized here. */
2223 		/* Assume that thr->valstack_bottom has been set-up before getting here. */
2224 
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;
2232 
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) */
2237 		}
2238 #endif  /* DUK_USE_DEBUGGER_SUPPORT */
2239 
2240 #ifdef DUK_USE_ASSERTIONS
2241 		valstack_top_base = (duk_size_t) (thr->valstack_top - thr->valstack);
2242 #endif
2243 
2244 		/* Set up curr_pc for opcode dispatch. */
2245 		curr_pc = act->curr_pc;
2246 	}
2247 
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, "
2250 	                   "preventcount=%ld",
2251 	                   (void *) thr,
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));
2261 
2262 	/* Dispatch loop. */
2263 
2264 	for (;;) {
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);
2268 
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.
2274 		 */
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;
2279 		} else {
2280 			/* Trigger at zero or below */
2281 			duk_small_uint_t exec_int_ret;
2282 
2283 			/* Write curr_pc back for the debugger. */
2284 			DUK_ASSERT(thr->callstack_top > 0);
2285 			{
2286 				duk_activation *act;
2287 				act = thr->callstack + thr->callstack_top - 1;
2288 				act->curr_pc = (duk_instr_t *) curr_pc;
2289 			}
2290 
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
2294 			 * zero again.
2295 			 */
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;
2301 			}
2302 #endif
2303 
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;
2308 			}
2309 		}
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.
2314 		 */
2315 		thr->heap->inst_count_exec++;
2316 #endif
2317 
2318 #if defined(DUK_USE_ASSERTIONS) || defined(DUK_USE_DEBUG)
2319 		{
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 */
2325 
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));
2334 		}
2335 #endif
2336 
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.
2341 		 */
2342 		{
2343 			duk_tval *tv;
2344 			tv = thr->valstack_top;
2345 			while (tv != thr->valstack_end) {
2346 				DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
2347 				tv++;
2348 			}
2349 		}
2350 #endif
2351 
2352 		ins = *curr_pc++;
2353 
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.
2360 		 */
2361 
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
2365 		 * first place.
2366 		 */
2367 
2368 		/* XXX: use macros for the repetitive tval/refcount handling. */
2369 
2370 		switch ((int) DUK_DEC_OP(ins)) {
2371 		/* XXX: switch cast? */
2372 
2373 		case DUK_OP_LDREG: {
2374 			duk_small_uint_fast_t a;
2375 			duk_uint_fast_t bc;
2376 			duk_tval *tv1, *tv2;
2377 
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 */
2381 			break;
2382 		}
2383 
2384 		case DUK_OP_STREG: {
2385 			duk_small_uint_fast_t a;
2386 			duk_uint_fast_t bc;
2387 			duk_tval *tv1, *tv2;
2388 
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 */
2392 			break;
2393 		}
2394 
2395 		case DUK_OP_LDCONST: {
2396 			duk_small_uint_fast_t a;
2397 			duk_uint_fast_t bc;
2398 			duk_tval *tv1, *tv2;
2399 
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 */
2403 			break;
2404 		}
2405 
2406 		case DUK_OP_LDINT: {
2407 			duk_small_uint_fast_t a;
2408 			duk_int_fast_t bc;
2409 			duk_tval *tv1;
2410 #if defined(DUK_USE_FASTINT)
2411 			duk_int32_t val;
2412 #else
2413 			duk_double_t val;
2414 #endif
2415 
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 */
2420 #else
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 */
2424 #endif
2425 			break;
2426 		}
2427 
2428 		case DUK_OP_LDINTX: {
2429 			duk_small_uint_fast_t a;
2430 			duk_tval *tv1;
2431 			duk_double_t val;
2432 
2433 			/* LDINTX is not necessarily in FASTINT range, so
2434 			 * no fast path for now.
2435 			 *
2436 			 * XXX: perhaps restrict LDINTX to fastint range, wider
2437 			 * range very rarely needed.
2438 			 */
2439 
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);
2446 #else
2447 			DUK_TVAL_SET_NUMBER(tv1, val);
2448 #endif
2449 			break;
2450 		}
2451 
2452 		case DUK_OP_MPUTOBJ:
2453 		case DUK_OP_MPUTOBJI: {
2454 			duk_context *ctx = (duk_context *) thr;
2455 			duk_small_uint_fast_t a;
2456 			duk_tval *tv1;
2457 			duk_hobject *obj;
2458 			duk_uint_fast_t idx;
2459 			duk_small_uint_fast_t count;
2460 
2461 			/* A -> register of target object
2462 			 * B -> first register of key/value pair list
2463 			 * C -> number of key/value pairs
2464 			 */
2465 
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);
2469 
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);
2475 			}
2476 
2477 			count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
2478 
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");
2484 			}
2485 #endif
2486 
2487 			duk_push_hobject(ctx, obj);
2488 
2489 			while (count > 0) {
2490 				/* XXX: faster initialization (direct access or better primitives) */
2491 
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] */
2496 
2497 				count--;
2498 				idx += 2;
2499 			}
2500 
2501 			duk_pop(ctx);  /* [... obj] -> [...] */
2502 			break;
2503 		}
2504 
2505 		case DUK_OP_MPUTARR:
2506 		case DUK_OP_MPUTARRI: {
2507 			duk_context *ctx = (duk_context *) thr;
2508 			duk_small_uint_fast_t a;
2509 			duk_tval *tv1;
2510 			duk_hobject *obj;
2511 			duk_uint_fast_t idx;
2512 			duk_small_uint_fast_t count;
2513 			duk_uint32_t arr_idx;
2514 
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)
2518 			 */
2519 
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);
2524 
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);
2530 			}
2531 
2532 			count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
2533 
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");
2539 			}
2540 #endif
2541 
2542 			tv1 = DUK__REGP(idx);
2543 			DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
2544 			arr_idx = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
2545 			idx++;
2546 
2547 			duk_push_hobject(ctx, obj);
2548 
2549 			while (count > 0) {
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.
2554 				 */
2555 
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).
2560 				 */
2561 
2562 				duk_push_tval(ctx, DUK__REGP(idx));          /* -> [... obj value] */
2563 				duk_xdef_prop_index_wec(ctx, -2, arr_idx);   /* -> [... obj] */
2564 
2565 				/* XXX: could use at least one fewer loop counters */
2566 				count--;
2567 				idx++;
2568 				arr_idx++;
2569 			}
2570 
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
2573 			 * 'arr_idx' type.
2574 			 */
2575 			duk_hobject_set_length(thr, obj, (duk_uint32_t) arr_idx);
2576 
2577 			duk_pop(ctx);  /* [... obj] -> [...] */
2578 			break;
2579 		}
2580 
2581 		case DUK_OP_NEW:
2582 		case DUK_OP_NEWI: {
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;
2587 
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)
2591 			 * C -> num args (N)
2592 			 */
2593 
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.
2597 			 */
2598 
2599 			/* Don't need to sync curr_pc here; duk_new() will do that
2600 			 * when it augments the created error.
2601 			 */
2602 
2603 			/* XXX: unnecessary copying of values?  Just set 'top' to
2604 			 * b + c, and let the return handling fix up the stack frame?
2605 			 */
2606 
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);
2612 			}
2613 
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");
2619 			}
2620 #endif
2621 
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));
2626 			}
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);
2630 
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.
2634 			 */
2635 			break;
2636 		}
2637 
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);
2644 
2645 			/* A -> target register
2646 			 * B -> bytecode (also contains flags)
2647 			 * C -> escaped source
2648 			 */
2649 
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);
2655 #else
2656 			/* The compiler should never emit DUK_OP_REGEXP if there is no
2657 			 * regexp support.
2658 			 */
2659 			DUK__INTERNAL_ERROR("no regexp support");
2660 #endif
2661 
2662 			break;
2663 		}
2664 
2665 		case DUK_OP_CSREG:
2666 		case DUK_OP_CSREGI: {
2667 			/*
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.
2671 			 */
2672 
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;
2676 
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)
2680 			 */
2681 
2682 			/* XXX: direct manipulation, or duk_replace_tval() */
2683 
2684 			/* Note: target registers a and a+1 may overlap with DUK__REGP(b).
2685 			 * Careful here.
2686 			 */
2687 
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);
2693 			}
2694 
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");
2700 			}
2701 #endif
2702 
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));
2707 			break;
2708 		}
2709 
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);
2715 			duk_tval *tv1;
2716 			duk_hstring *name;
2717 
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] */
2725 
2726 			duk_pop(ctx);  /* 'this' binding is not needed here */
2727 			duk_replace(ctx, (duk_idx_t) a);
2728 			break;
2729 		}
2730 
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);
2735 			duk_tval *tv1;
2736 			duk_hstring *name;
2737 
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);
2742 
2743 			/* XXX: putvar takes a duk_tval pointer, which is awkward and
2744 			 * should be reworked.
2745 			 */
2746 
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());
2750 			break;
2751 		}
2752 
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);
2759 			duk_tval *tv1;
2760 			duk_hstring *name;
2761 			duk_small_uint_t prop_flags;
2762 			duk_bool_t is_func_decl;
2763 			duk_bool_t is_undef_value;
2764 
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);
2769 
2770 			is_undef_value = ((a & DUK_BC_DECLVAR_FLAG_UNDEF_VALUE) != 0);
2771 			is_func_decl = ((a & DUK_BC_DECLVAR_FLAG_FUNC_DECL) != 0);
2772 
2773 			/* XXX: declvar takes an duk_tval pointer, which is awkward and
2774 			 * should be reworked.
2775 			 */
2776 
2777 			/* Compiler is responsible for selecting property flags (configurability,
2778 			 * writability, etc).
2779 			 */
2780 			prop_flags = a & DUK_PROPDESC_FLAGS_MASK;
2781 
2782 			if (is_undef_value) {
2783 				duk_push_undefined(ctx);
2784 			} else {
2785 				duk_push_tval(ctx, DUK__REGCONSTP(c));
2786 			}
2787 			tv1 = DUK_GET_TVAL_NEGIDX(ctx, -1);
2788 
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());
2794 			}
2795 
2796 			duk_pop(ctx);
2797 			break;
2798 		}
2799 
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);
2805 			duk_tval *tv1;
2806 			duk_hstring *name;
2807 			duk_bool_t rc;
2808 
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);
2816 
2817 			duk_push_boolean(ctx, rc);
2818 			duk_replace(ctx, (duk_idx_t) a);
2819 			break;
2820 		}
2821 
2822 		case DUK_OP_CSVAR:
2823 		case DUK_OP_CSVARI: {
2824 			/* 'this' value:
2825 			 * E5 Section 6.b.i
2826 			 *
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.
2830 			 *
2831 			 */
2832 
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;
2837 			duk_tval *tv1;
2838 			duk_hstring *name;
2839 
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] */
2846 
2847 			/* Note: target registers a and a+1 may overlap with DUK__REGCONSTP(b)
2848 			 * and DUK__REGCONSTP(c).  Careful here.
2849 			 */
2850 
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);
2856 			}
2857 
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");
2863 			}
2864 #endif
2865 
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) */
2868 			break;
2869 		}
2870 
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;
2878 
2879 			/* A -> target reg
2880 			 * BC -> inner function index
2881 			 */
2882 
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())));
2885 
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()));
2888 
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));
2894 
2895 			DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O",
2896 			                     (void *) fun_temp, (duk_heaphdr *) fun_temp));
2897 
2898 			if (act->lex_env == NULL) {
2899 				DUK_ASSERT(act->var_env == NULL);
2900 				duk_js_init_activation_environment_records_delayed(thr, act);
2901 			}
2902 			DUK_ASSERT(act->lex_env != NULL);
2903 			DUK_ASSERT(act->var_env != NULL);
2904 
2905 			/* functions always have a NEWENV flag, i.e. they get a
2906 			 * new variable declaration environment, so only lex_env
2907 			 * matters here.
2908 			 */
2909 			duk_js_push_closure(thr,
2910 			                    (duk_hcompiledfunction *) fun_temp,
2911 			                    act->var_env,
2912 			                    act->lex_env,
2913 			                    1 /*add_auto_proto*/);
2914 			duk_replace(ctx, (duk_idx_t) a);
2915 
2916 			break;
2917 		}
2918 
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);
2924 			duk_tval *tv_obj;
2925 			duk_tval *tv_key;
2926 			duk_bool_t rc;
2927 
2928 			/* A -> target reg
2929 			 * B -> object reg/const (may be const e.g. in "'foo'[1]")
2930 			 * C -> key reg/const
2931 			 */
2932 
2933 			tv_obj = DUK__REGCONSTP(b);
2934 			tv_key = DUK__REGCONSTP(c);
2935 			DUK_DDD(DUK_DDDPRINT("GETPROP: a=%ld obj=%!T, key=%!T",
2936 			                     (long) a,
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 */
2945 
2946 			duk_replace(ctx, (duk_idx_t) a);    /* val */
2947 			break;
2948 		}
2949 
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);
2954 			duk_tval *tv_obj;
2955 			duk_tval *tv_key;
2956 			duk_tval *tv_val;
2957 			duk_bool_t rc;
2958 
2959 			/* A -> object reg
2960 			 * B -> key reg/const
2961 			 * C -> value reg/const
2962 			 *
2963 			 * Note: intentional difference to register arrangement
2964 			 * of e.g. GETPROP; 'A' must contain a register-only value.
2965 			 */
2966 
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 */
2983 
2984 			break;
2985 		}
2986 
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);
2992 			duk_tval *tv_obj;
2993 			duk_tval *tv_key;
2994 			duk_bool_t rc;
2995 
2996 			/* A -> result reg
2997 			 * B -> object reg
2998 			 * C -> key reg/const
2999 			 */
3000 
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 */
3006 
3007 			duk_push_boolean(ctx, rc);
3008 			duk_replace(ctx, (duk_idx_t) a);    /* result */
3009 			break;
3010 		}
3011 
3012 		case DUK_OP_CSPROP:
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;
3018 			duk_tval *tv_obj;
3019 			duk_tval *tv_key;
3020 			duk_bool_t rc;
3021 
3022 			/* E5 Section 11.2.3, step 6.a.i */
3023 			/* E5 Section 10.4.3 */
3024 
3025 			/* XXX: allow object to be a const, e.g. in 'foo'.toString()?
3026 			 * On the other hand, DUK_REGCONSTP() is slower and generates
3027 			 * more code.
3028 			 */
3029 
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 */
3036 
3037 			/* Note: target registers a and a+1 may overlap with DUK__REGP(b)
3038 			 * and DUK__REGCONSTP(c).  Careful here.
3039 			 */
3040 
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);
3046 			}
3047 
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");
3053 			}
3054 #endif
3055 
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 */
3059 			break;
3060 		}
3061 
3062 		case DUK_OP_ADD:
3063 		case DUK_OP_SUB:
3064 		case DUK_OP_MUL:
3065 		case DUK_OP_DIV:
3066 		case DUK_OP_MOD: {
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);
3071 
3072 			if (op == DUK_OP_ADD) {
3073 				/*
3074 				 *  Handling DUK_OP_ADD this way is more compact (experimentally)
3075 				 *  than a separate case with separate argument decoding.
3076 				 */
3077 				duk__vm_arith_add(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a);
3078 			} else {
3079 				duk__vm_arith_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op);
3080 			}
3081 			break;
3082 		}
3083 
3084 		case DUK_OP_BAND:
3085 		case DUK_OP_BOR:
3086 		case DUK_OP_BXOR:
3087 		case DUK_OP_BASL:
3088 		case DUK_OP_BLSR:
3089 		case DUK_OP_BASR: {
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);
3094 
3095 			duk__vm_bitwise_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op);
3096 			break;
3097 		}
3098 
3099 		case DUK_OP_EQ:
3100 		case DUK_OP_NEQ: {
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);
3105 			duk_bool_t tmp;
3106 
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) {
3110 				tmp = !tmp;
3111 			}
3112 			duk_push_boolean(ctx, tmp);
3113 			duk_replace(ctx, (duk_idx_t) a);
3114 			break;
3115 		}
3116 
3117 		case DUK_OP_SEQ:
3118 		case DUK_OP_SNEQ: {
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);
3123 			duk_bool_t tmp;
3124 
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) {
3128 				tmp = !tmp;
3129 			}
3130 			duk_push_boolean(ctx, tmp);
3131 			duk_replace(ctx, (duk_idx_t) a);
3132 			break;
3133 		}
3134 
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).
3141 		 *
3142 		 * XXX: can be combined; check code size.
3143 		 */
3144 
3145 		case DUK_OP_GT: {
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);
3150 			duk_bool_t tmp;
3151 
3152 			/* x > y  -->  y < x */
3153 			tmp = duk_js_compare_helper(thr,
3154 			                            DUK__REGCONSTP(c),  /* y */
3155 			                            DUK__REGCONSTP(b),  /* x */
3156 			                            0);                 /* flags */
3157 
3158 			duk_push_boolean(ctx, tmp);
3159 			duk_replace(ctx, (duk_idx_t) a);
3160 			break;
3161 		}
3162 
3163 		case DUK_OP_GE: {
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);
3168 			duk_bool_t tmp;
3169 
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 */
3176 
3177 			duk_push_boolean(ctx, tmp);
3178 			duk_replace(ctx, (duk_idx_t) a);
3179 			break;
3180 		}
3181 
3182 		case DUK_OP_LT: {
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);
3187 			duk_bool_t tmp;
3188 
3189 			/* x < y */
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 */
3194 
3195 			duk_push_boolean(ctx, tmp);
3196 			duk_replace(ctx, (duk_idx_t) a);
3197 			break;
3198 		}
3199 
3200 		case DUK_OP_LE: {
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);
3205 			duk_bool_t tmp;
3206 
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 */
3212 
3213 			duk_push_boolean(ctx, tmp);
3214 			duk_replace(ctx, (duk_idx_t) a);
3215 			break;
3216 		}
3217 
3218 		case DUK_OP_IF: {
3219 			duk_small_uint_fast_t a = DUK_DEC_A(ins);
3220 			duk_small_uint_fast_t b = DUK_DEC_B(ins);
3221 			duk_bool_t tmp;
3222 
3223 			tmp = duk_js_toboolean(DUK__REGCONSTP(b));
3224 			if (tmp == (duk_bool_t) a) {
3225 				/* if boolean matches A, skip next inst */
3226 				curr_pc++;
3227 			} else {
3228 				;
3229 			}
3230 			break;
3231 		}
3232 
3233 		case DUK_OP_JUMP: {
3234 			duk_int_fast_t abc = DUK_DEC_ABC(ins);
3235 
3236 			curr_pc += abc - DUK_BC_JUMP_BIAS;
3237 			break;
3238 		}
3239 
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;
3246 
3247 			/* A -> flags
3248 			 * B -> return value reg/const
3249 			 * C -> currently unused
3250 			 */
3251 
3252 			DUK__SYNC_AND_NULL_CURR_PC();
3253 
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.
3257 			 */
3258 
3259 			if (a & DUK_BC_RETURN_FLAG_HAVE_RETVAL) {
3260 				duk_push_tval(ctx, DUK__REGCONSTP(b));
3261 			} else {
3262 				duk_push_undefined(ctx);
3263 			}
3264 			ret_result = duk__handle_return(thr,
3265 				                        entry_thread,
3266 				                        entry_callstack_top);
3267 			if (ret_result == DUK__RETHAND_RESTART) {
3268 				goto restart_execution;
3269 			}
3270 			DUK_ASSERT(ret_result == DUK__RETHAND_FINISHED);
3271 
3272 			DUK_DDD(DUK_DDDPRINT("exiting executor after RETURN handling"));
3273 			return;
3274 		}
3275 
3276 		case DUK_OP_CALL:
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;
3285 			duk_tval *tv_func;
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;
3291 #endif
3292 
3293 			/* A -> flags
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)
3296 			 * C -> nargs
3297 			 */
3298 
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);
3302 
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);
3308 			}
3309 
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");
3314 			}
3315 #endif
3316 
3317 			/*
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.
3321 			 *
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.
3328 			 *
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.
3336 			 */
3337 
3338 			duk_set_top(ctx, (duk_idx_t) (idx + c + 2));   /* [ ... func this arg1 ... argN ] */
3339 
3340 			call_flags = 0;
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.
3346 				 */
3347 				call_flags |= DUK_CALL_FLAG_IS_TAILCALL;
3348 			}
3349 
3350 			/* Compared to duk_handle_call():
3351 			 *   - protected call: never
3352 			 *   - ignore recursion limit: never
3353 			 */
3354 			num_stack_args = c;
3355 			setup_rc = duk_handle_ecma_call_setup(thr,
3356 			                                      num_stack_args,
3357 			                                      call_flags);
3358 
3359 			if (setup_rc) {
3360 				/* Ecma-to-ecma call possible, may or may not be a tail call.
3361 				 * Avoid C recursion by being clever.
3362 				 */
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;
3366 			}
3367 			DUK_ASSERT(thr->ptr_curr_pc != NULL);  /* restored if ecma-to-ecma setup fails */
3368 
3369 			DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call not possible, target is native (may be lightfunc)"));
3370 
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));
3374 
3375 			tv_func = DUK__REGP(idx);  /* Relookup if relocated */
3376 			if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
3377 
3378 				call_flags = 0;  /* not protected, respect reclimit, not constructor */
3379 
3380 				/* There is no eval() special handling here: eval() is never
3381 				 * automatically converted to a lightfunc.
3382 				 */
3383 				DUK_ASSERT(DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func) != duk_bi_global_object_eval);
3384 
3385 				duk_handle_call_unprotected(thr,
3386 				                            num_stack_args,
3387 				                            call_flags);
3388 
3389 				/* duk_js_call.c is required to restore the stack reserve
3390 				 * so we only need to reset the top.
3391 				 */
3392 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
3393 				fun = DUK__FUN();
3394 #endif
3395 				duk_set_top(ctx, (duk_idx_t) fun->nregs);
3396 
3397 				/* No need to reinit setjmp() catchpoint, as call handling
3398 				 * will store and restore our state.
3399 				 */
3400 			} else {
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));
3406 
3407 				/*
3408 				 *  Other cases, use C recursion.
3409 				 *
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.
3413 				 *
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'.
3417 				 */
3418 
3419 				call_flags = 0;  /* not protected, respect reclimit, not constructor */
3420 
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;
3426 					} else {
3427 						DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was not 'eval' -> indirect eval"));
3428 					}
3429 				}
3430 
3431 				duk_handle_call_unprotected(thr,
3432 				                            num_stack_args,
3433 				                            call_flags);
3434 
3435 				/* duk_js_call.c is required to restore the stack reserve
3436 				 * so we only need to reset the top.
3437 				 */
3438 #if !defined(DUK_USE_EXEC_FUN_LOCAL)
3439 				fun = DUK__FUN();
3440 #endif
3441 				duk_set_top(ctx, (duk_idx_t) fun->nregs);
3442 
3443 				/* No need to reinit setjmp() catchpoint, as call handling
3444 				 * will store and restore our state.
3445 				 */
3446 			}
3447 
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.
3451 			 */
3452 			break;
3453 		}
3454 
3455 		case DUK_OP_TRYCATCH: {
3456 			duk_context *ctx = (duk_context *) thr;
3457 			duk_activation *act;
3458 			duk_catcher *cat;
3459 			duk_tval *tv1;
3460 			duk_small_uint_fast_t a;
3461 			duk_uint_fast_t bc;
3462 
3463 			/* A -> flags
3464 			 * BC -> reg_catch; base register for two registers used both during
3465 			 *       trycatch setup and when catch is triggered
3466 			 *
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.
3471 			 *
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
3476 			 *          the 'try' clause.
3477 			 *
3478 			 * Note that a TRYCATCH generated for a 'with' statement has no
3479 			 * catch or finally parts.
3480 			 */
3481 
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.
3488 			 */
3489 
3490 			/* XXX: side effect handling is quite awkward here */
3491 
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)));
3500 
3501 			a = DUK_DEC_A(ins);
3502 			bc = DUK_DEC_BC(ins);
3503 
3504 			act = thr->callstack + thr->callstack_top - 1;
3505 			DUK_ASSERT(thr->callstack_top >= 1);
3506 
3507 			/* 'with' target must be created first, in case we run out of memory */
3508 			/* XXX: refactor out? */
3509 
3510 			if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) {
3511 				DUK_DDD(DUK_DDDPRINT("need to initialize a with binding object"));
3512 
3513 				if (act->lex_env == NULL) {
3514 					DUK_ASSERT(act->var_env == NULL);
3515 					DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
3516 
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 */
3521 				}
3522 				DUK_ASSERT(act->lex_env != NULL);
3523 				DUK_ASSERT(act->var_env != NULL);
3524 
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 */
3529 
3530 				duk_push_tval(ctx, DUK__REGP(bc));
3531 				duk_to_object(ctx, -1);
3532 				duk_dup(ctx, -1);
3533 
3534 				/* [ ... env target ] */
3535 				/* [ ... env target target ] */
3536 
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 */
3539 
3540 				/* [ ... env ] */
3541 
3542 				DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iT",
3543 				                     (duk_tval *) duk_get_tval(ctx, -1)));
3544 			}
3545 
3546 			/* allocate catcher and populate it (should be atomic) */
3547 
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++;
3552 
3553 			cat->flags = DUK_CAT_TYPE_TCF;
3554 			cat->h_varname = NULL;
3555 
3556 			if (a & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
3557 				cat->flags |= DUK_CAT_FLAG_CATCH_ENABLED;
3558 			}
3559 			if (a & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
3560 				cat->flags |= DUK_CAT_FLAG_FINALLY_ENABLED;
3561 			}
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));
3567 
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.
3571 				 */
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;
3576 
3577 				DUK_DDD(DUK_DDDPRINT("lexenv active flag set to catcher"));
3578 				cat->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE;
3579 
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);
3585 
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 */
3588 
3589 				act = thr->callstack + thr->callstack_top - 1;  /* relookup (side effects) */
3590 				act->lex_env = new_env;
3591 				DUK_HOBJECT_INCREF(thr, new_env);
3592 				duk_pop(ctx);
3593 			} else {
3594 				;
3595 			}
3596 
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.
3608 			 */
3609 			duk_to_undefined(ctx, bc);
3610 			duk_to_undefined(ctx, bc + 1);
3611 
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;
3616 
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));
3621 
3622 			curr_pc += 2;  /* skip jump slots */
3623 			break;
3624 		}
3625 
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;
3636 
3637 			/* Two lowest bits of opcode are used to distinguish
3638 			 * variants.  Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3639 			 */
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);
3644 
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) {
3652 						goto skip_fastint;
3653 					}
3654 					y_fi = x_fi - 1;
3655 				} else {
3656 					if (x_fi == DUK_FASTINT_MAX) {
3657 						goto skip_fastint;
3658 					}
3659 					y_fi = x_fi + 1;
3660 				}
3661 
3662 				DUK_TVAL_SET_FASTINT(tv1, y_fi);  /* no need for refcount update */
3663 
3664 				tv2 = DUK__REGP(a);
3665 				z_fi = (ins & DUK_ENC_OP(0x02)) ? x_fi : y_fi;
3666 				DUK_TVAL_SET_FASTINT_UPDREF(thr, tv2, z_fi);  /* side effects */
3667 				break;
3668 			}
3669 		 skip_fastint:
3670 #endif
3671 			if (DUK_TVAL_IS_NUMBER(tv1)) {
3672 				/* Fast path for the case where the register
3673 				 * is a number (e.g. loop counter).
3674 				 */
3675 
3676 				x = DUK_TVAL_GET_NUMBER(tv1);
3677 				if (ins & DUK_ENC_OP(0x01)) {
3678 					y = x - 1.0;
3679 				} else {
3680 					y = x + 1.0;
3681 				}
3682 
3683 				DUK_TVAL_SET_NUMBER(tv1, y);  /* no need for refcount update */
3684 			} else {
3685 				x = duk_to_number(ctx, bc);
3686 
3687 				if (ins & DUK_ENC_OP(0x01)) {
3688 					y = x - 1.0;
3689 				} else {
3690 					y = x + 1.0;
3691 				}
3692 
3693 				duk_push_number(ctx, y);
3694 				duk_replace(ctx, bc);
3695 			}
3696 
3697 			tv2 = DUK__REGP(a);
3698 			z = (ins & DUK_ENC_OP(0x02)) ? x : y;
3699 			DUK_TVAL_SET_NUMBER_UPDREF(thr, tv2, z);  /* side effects */
3700 			break;
3701 		}
3702 
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);
3712 			duk_double_t x, y;
3713 			duk_tval *tv1;
3714 			duk_hstring *name;
3715 
3716 			/* Two lowest bits of opcode are used to distinguish
3717 			 * variants.  Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3718 			 */
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);
3723 
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] */
3730 
3731 			/* XXX: fastint fast path would be very useful here */
3732 
3733 			x = duk_to_number(ctx, -2);
3734 			duk_pop_2(ctx);
3735 			if (ins & DUK_ENC_OP(0x01)) {
3736 				y = x - 1.0;
3737 			} else {
3738 				y = x + 1.0;
3739 			}
3740 
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());
3745 			duk_pop(ctx);
3746 
3747 			duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y);
3748 			duk_replace(ctx, (duk_idx_t) a);
3749 			break;
3750 		}
3751 
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);
3761 			duk_tval *tv_obj;
3762 			duk_tval *tv_key;
3763 			duk_tval *tv_val;
3764 			duk_bool_t rc;
3765 			duk_double_t x, y;
3766 
3767 			/* A -> target reg
3768 			 * B -> object reg/const (may be const e.g. in "'foo'[1]")
3769 			 * C -> key reg/const
3770 			 */
3771 
3772 			/* Two lowest bits of opcode are used to distinguish
3773 			 * variants.  Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
3774 			 */
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);
3779 
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 */
3786 
3787 			x = duk_to_number(ctx, -1);
3788 			duk_pop(ctx);
3789 			if (ins & DUK_ENC_OP(0x01)) {
3790 				y = x - 1.0;
3791 			} else {
3792 				y = x + 1.0;
3793 			}
3794 
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 */
3804 			duk_pop(ctx);
3805 
3806 			duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y);
3807 			duk_replace(ctx, (duk_idx_t) a);
3808 			break;
3809 		}
3810 
3811 		case DUK_OP_EXTRA: {
3812 			/* XXX: shared decoding of 'b' and 'c'? */
3813 
3814 			duk_small_uint_fast_t extraop = DUK_DEC_A(ins);
3815 			switch ((int) extraop) {
3816 			/* XXX: switch cast? */
3817 
3818 			case DUK_EXTRAOP_NOP: {
3819 				/* nop */
3820 				break;
3821 			}
3822 
3823 			case DUK_EXTRAOP_INVALID: {
3824 				DUK_ERROR_FMT1(thr, DUK_ERR_INTERNAL_ERROR, "INVALID opcode (%ld)", (long) DUK_DEC_BC(ins));
3825 				break;
3826 			}
3827 
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;
3832 
3833 				tv1 = DUK__REGP(bc);
3834 				tv2 = thr->valstack_bottom - 1;  /* 'this binding' is just under bottom */
3835 				DUK_ASSERT(tv2 >= thr->valstack);
3836 
3837 				DUK_DDD(DUK_DDDPRINT("LDTHIS: %!T to r%ld", (duk_tval *) tv2, (long) bc));
3838 
3839 				DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2);  /* side effects */
3840 				break;
3841 			}
3842 
3843 			case DUK_EXTRAOP_LDUNDEF: {
3844 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
3845 				duk_tval *tv1;
3846 
3847 				tv1 = DUK__REGP(bc);
3848 				DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1);  /* side effects */
3849 				break;
3850 			}
3851 
3852 			case DUK_EXTRAOP_LDNULL: {
3853 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
3854 				duk_tval *tv1;
3855 
3856 				tv1 = DUK__REGP(bc);
3857 				DUK_TVAL_SET_NULL_UPDREF(thr, tv1);  /* side effects */
3858 				break;
3859 			}
3860 
3861 			case DUK_EXTRAOP_LDTRUE:
3862 			case DUK_EXTRAOP_LDFALSE: {
3863 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
3864 				duk_tval *tv1;
3865 				duk_small_uint_fast_t bval = (extraop == DUK_EXTRAOP_LDTRUE ? 1 : 0);
3866 
3867 				tv1 = DUK__REGP(bc);
3868 				DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv1, bval);  /* side effects */
3869 				break;
3870 			}
3871 
3872 			case DUK_EXTRAOP_NEWOBJ: {
3873 				duk_context *ctx = (duk_context *) thr;
3874 				duk_small_uint_fast_t b = DUK_DEC_B(ins);
3875 
3876 				duk_push_object(ctx);
3877 				duk_replace(ctx, (duk_idx_t) b);
3878 				break;
3879 			}
3880 
3881 			case DUK_EXTRAOP_NEWARR: {
3882 				duk_context *ctx = (duk_context *) thr;
3883 				duk_small_uint_fast_t b = DUK_DEC_B(ins);
3884 
3885 				duk_push_array(ctx);
3886 				duk_replace(ctx, (duk_idx_t) b);
3887 				break;
3888 			}
3889 
3890 			case DUK_EXTRAOP_SETALEN: {
3891 				duk_small_uint_fast_t b;
3892 				duk_small_uint_fast_t c;
3893 				duk_tval *tv1;
3894 				duk_hobject *h;
3895 				duk_uint32_t len;
3896 
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);
3900 
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);
3904 
3905 				duk_hobject_set_length(thr, h, len);
3906 
3907 				break;
3908 			}
3909 
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);
3915 				break;
3916 			}
3917 
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);
3923 				duk_hstring *name;
3924 				duk_tval *tv;
3925 
3926 				/* B -> target register
3927 				 * C -> constant index of identifier name
3928 				 */
3929 
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);
3939 					duk_pop_2(ctx);
3940 				} else {
3941 					/* unresolvable, no stack changes */
3942 					duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED);
3943 					duk_replace(ctx, (duk_idx_t) b);
3944 				}
3945 
3946 				break;
3947 			}
3948 
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);
3953 
3954 				/*
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
3958 				 *  the compiler part
3959 				 */
3960 
3961 				/* B -> register for writing enumerator object
3962 				 * C -> value to be enumerated (register)
3963 				 */
3964 
3965 				if (duk_is_null_or_undefined(ctx, (duk_idx_t) c)) {
3966 					duk_push_null(ctx);
3967 					duk_replace(ctx, (duk_idx_t) b);
3968 				} else {
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);
3973 				}
3974 				break;
3975 			}
3976 
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);
3981 
3982 				/*
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.
3987 				 */
3988 
3989 				/* B -> target register for next key
3990 				 * C -> enum register
3991 				 */
3992 
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)));
3996 
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)));
4004 						curr_pc++;
4005 					} else {
4006 						/* [ ... enum ] -> [ ... ] */
4007 						DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot"));
4008 						duk_push_undefined(ctx);
4009 					}
4010 					duk_replace(ctx, (duk_idx_t) b);
4011 				} else {
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"));
4015 				}
4016 				break;
4017 			}
4018 
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;
4027 
4028 				/* B -> object register
4029 				 * C -> C+0 contains key, C+1 closure (value)
4030 				 */
4031 
4032 				/*
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).
4037 				 */
4038 
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);
4044 				}
4045 
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");
4051 				}
4052 #endif
4053 
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.
4057 				 */
4058 
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 ] */
4065 
4066 				duk_push_true(ctx);
4067 				duk_put_prop_stridx(ctx, -2, DUK_STRIDX_ENUMERABLE);
4068 				duk_push_true(ctx);
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));
4072 
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)));
4077 
4078 				duk_call_method(ctx, 3);  /* -> [ Object res ] */
4079 				duk_pop_2(ctx);
4080 
4081 				DUK_DDD(DUK_DDDPRINT("INITGET/INITSET AFTER: obj=%!T",
4082 				                     (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b)));
4083 				break;
4084 			}
4085 
4086 			case DUK_EXTRAOP_ENDTRY: {
4087 				duk_catcher *cat;
4088 				duk_tval *tv1;
4089 
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);
4093 
4094 				cat = thr->catchstack + thr->catchstack_top - 1;
4095 
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);
4098 
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'"));
4101 
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 */
4105 					tv1 = NULL;
4106 
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 */
4110 					tv1 = NULL;
4111 
4112 					DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
4113 				} else {
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 */
4117 				}
4118 
4119 				curr_pc = cat->pc_base + 1;
4120 				break;
4121 			}
4122 
4123 			case DUK_EXTRAOP_ENDCATCH: {
4124 				duk_activation *act;
4125 				duk_catcher *cat;
4126 				duk_tval *tv1;
4127 
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);
4131 
4132 				cat = thr->catchstack + thr->catchstack_top - 1;
4133 				DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat));  /* cleared before entering catch part */
4134 
4135 				act = thr->callstack + thr->callstack_top - 1;
4136 
4137 				if (DUK_CAT_HAS_LEXENV_ACTIVE(cat)) {
4138 					duk_hobject *prev_env;
4139 
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);
4143 
4144 					DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment"));
4145 
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 */
4151 				}
4152 
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'"));
4155 
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 */
4159 					tv1 = NULL;
4160 
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 */
4164 					tv1 = NULL;
4165 
4166 					DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
4167 				} else {
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 */
4171 				}
4172 
4173 				curr_pc = cat->pc_base + 1;
4174 				break;
4175 			}
4176 
4177 			case DUK_EXTRAOP_ENDFIN: {
4178 				duk_context *ctx = (duk_context *) thr;
4179 				duk_catcher *cat;
4180 				duk_tval *tv1;
4181 				duk_small_uint_t cont_type;
4182 				duk_small_uint_t ret_result;
4183 
4184 				/* Sync and NULL early. */
4185 				DUK__SYNC_AND_NULL_CURR_PC();
4186 
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);
4190 
4191 				cat = thr->catchstack + thr->catchstack_top - 1;
4192 
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
4195 				 * an error.
4196 				 */
4197 				DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat));  /* cleared before entering finally */
4198 				/* XXX: assert idx_base */
4199 
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)));
4203 
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);
4207 
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;
4215 				}
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));
4219 
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.
4223 					 */
4224 					DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat));  /* cleared before entering finally */
4225 #if 0
4226 					duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
4227 #endif
4228 
4229 					duk_push_tval(ctx, thr->valstack + cat->idx_base);
4230 					ret_result = duk__handle_return(thr,
4231 						                        entry_thread,
4232 						                        entry_callstack_top);
4233 					if (ret_result == DUK__RETHAND_RESTART) {
4234 						goto restart_execution;
4235 					}
4236 					DUK_ASSERT(ret_result == DUK__RETHAND_FINISHED);
4237 
4238 					DUK_DDD(DUK_DDDPRINT("exiting executor after ENDFIN and RETURN (pseudo) longjmp type"));
4239 					return;
4240 				}
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;
4245 
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.
4250 					 */
4251 #if 0
4252 					duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
4253 #endif
4254 
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);
4260 #else
4261 					label_id = (duk_small_uint_t) DUK_TVAL_GET_NUMBER(tv1);
4262 #endif
4263 					lj_type = cont_type;
4264 					duk__handle_break_or_continue(thr, label_id, lj_type);
4265 					goto restart_execution;
4266 				}
4267 				default: {
4268 					DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> "
4269 					                     "dismantle catcher, re-throw error",
4270 					                     (long) cont_type));
4271 
4272 					duk_push_tval(ctx, thr->valstack + cat->idx_base);
4273 
4274 					duk_err_setup_heap_ljstate(thr, (duk_small_int_t) cont_type);
4275 
4276 					DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL);  /* always in executor */
4277 					duk_err_longjmp(thr);
4278 					DUK_UNREACHABLE();
4279 				}
4280 				}
4281 
4282 				/* Must restart in all cases because we NULLed thr->ptr_curr_pc. */
4283 				DUK_UNREACHABLE();
4284 				break;
4285 			}
4286 
4287 			case DUK_EXTRAOP_THROW: {
4288 				duk_context *ctx = (duk_context *) thr;
4289 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4290 
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.
4294 				 */
4295 
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.
4299 				 */
4300 				DUK__SYNC_AND_NULL_CURR_PC();
4301 
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)));
4309 #endif
4310 
4311 				duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW);
4312 
4313 				DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL);  /* always in executor */
4314 				duk_err_longjmp(thr);
4315 				DUK_UNREACHABLE();
4316 				break;
4317 			}
4318 
4319 			case DUK_EXTRAOP_INVLHS: {
4320 				DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR, "invalid lvalue");
4321 
4322 				DUK_UNREACHABLE();
4323 				break;
4324 			}
4325 
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);
4330 				break;
4331 			}
4332 
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.
4338 				 */
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;
4346 				} else {
4347 					DUK_D(DUK_DPRINT("DEBUGGER statement ignored, debugger not attached"));
4348 				}
4349 #else
4350 				DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support"));
4351 #endif
4352 				break;
4353 			}
4354 
4355 			case DUK_EXTRAOP_BREAK: {
4356 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4357 
4358 				DUK_DDD(DUK_DDDPRINT("BREAK: %ld", (long) bc));
4359 
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;
4363 			}
4364 
4365 			case DUK_EXTRAOP_CONTINUE: {
4366 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4367 
4368 				DUK_DDD(DUK_DDDPRINT("CONTINUE: %ld", (long) bc));
4369 
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;
4373 			}
4374 
4375 			case DUK_EXTRAOP_BNOT: {
4376 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4377 
4378 				duk__vm_bitwise_not(thr, DUK__REGP(bc), bc);
4379 				break;
4380 			}
4381 
4382 			case DUK_EXTRAOP_LNOT: {
4383 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4384 				duk_tval *tv1;
4385 
4386 				tv1 = DUK__REGP(bc);
4387 				duk__vm_logical_not(thr, tv1, tv1);
4388 				break;
4389 			}
4390 
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);
4395 				duk_bool_t tmp;
4396 
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);
4400 				break;
4401 			}
4402 
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);
4407 				duk_bool_t tmp;
4408 
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);
4412 				break;
4413 			}
4414 
4415 			case DUK_EXTRAOP_LABEL: {
4416 				duk_catcher *cat;
4417 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4418 
4419 				/* allocate catcher and populate it (should be atomic) */
4420 
4421 				duk_hthread_catchstack_grow(thr);
4422 				cat = thr->catchstack + thr->catchstack_top;
4423 				thr->catchstack_top++;
4424 
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;
4430 
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)));
4435 
4436 				curr_pc += 2;  /* skip jump slots */
4437 				break;
4438 			}
4439 
4440 			case DUK_EXTRAOP_ENDLABEL: {
4441 				duk_catcher *cat;
4442 #if defined(DUK_USE_DDDPRINT) || defined(DUK_USE_ASSERTIONS)
4443 				duk_uint_fast_t bc = DUK_DEC_BC(ins);
4444 #endif
4445 #if defined(DUK_USE_DDDPRINT)
4446 				DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc));
4447 #endif
4448 
4449 				DUK_ASSERT(thr->catchstack_top >= 1);
4450 
4451 				cat = thr->catchstack + thr->catchstack_top - 1;
4452 				DUK_UNREF(cat);
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);
4455 
4456 				duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1);
4457 				/* no need to unwind callstack */
4458 				break;
4459 			}
4460 
4461 			default: {
4462 				DUK__INTERNAL_ERROR("invalid extra opcode");
4463 			}
4464 
4465 			}  /* end switch */
4466 
4467 			break;
4468 		}
4469 
4470 		default: {
4471 			/* this should never be possible, because the switch-case is
4472 			 * comprehensive
4473 			 */
4474 			DUK__INTERNAL_ERROR("invalid opcode");
4475 			break;
4476 		}
4477 
4478 		}  /* end switch */
4479 	}
4480 	DUK_UNREACHABLE();
4481 
4482 #ifndef DUK_USE_VERBOSE_EXECUTOR_ERRORS
4483  internal_error:
4484 	DUK_ERROR_INTERNAL(thr, "internal error in bytecode executor");
4485 #endif
4486 }
4487 
4488 #undef DUK__LONGJMP_RESTART
4489 #undef DUK__LONGJMP_FINISHED
4490 #undef DUK__LONGJMP_RETHROW
4491 
4492 #undef DUK__RETHAND_RESTART
4493 #undef DUK__RETHAND_FINISHED
4494 
4495 #undef DUK__FUN
4496 #undef DUK__STRICT
4497 #undef DUK__REG
4498 #undef DUK__REGP
4499 #undef DUK__CONST
4500 #undef DUK__CONSTP
4501 #undef DUK__RCISREG
4502 #undef DUK__REGCONST
4503 #undef DUK__REGCONSTP
4504 
4505 #undef DUK__INTERNAL_ERROR
4506 #undef DUK__SYNC_CURR_PC
4507 #undef DUK__SYNC_AND_NULL_CURR_PC
4508