]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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__reconfig_valstack(duk_hthread *thr, duk_size_t act_idx, duk_small_uint_t retval_count); | |
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. | |
24 | */ | |
25 | ||
26 | DUK_LOCAL duk_double_t duk__compute_mod(duk_double_t d1, duk_double_t d2) { | |
27 | /* | |
28 | * Ecmascript modulus ('%') does not match IEEE 754 "remainder" | |
29 | * operation (implemented by remainder() in C99) but does seem | |
30 | * to match ANSI C fmod(). | |
31 | * | |
32 | * Compare E5 Section 11.5.3 and "man fmod". | |
33 | */ | |
34 | ||
35 | return (duk_double_t) DUK_FMOD((double) d1, (double) d2); | |
36 | } | |
37 | ||
38 | DUK_LOCAL void duk__vm_arith_add(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_fast_t idx_z) { | |
39 | /* | |
40 | * Addition operator is different from other arithmetic | |
41 | * operations in that it also provides string concatenation. | |
42 | * Hence it is implemented separately. | |
43 | * | |
44 | * There is a fast path for number addition. Other cases go | |
45 | * through potentially multiple coercions as described in the | |
46 | * E5 specification. It may be possible to reduce the number | |
47 | * of coercions, but this must be done carefully to preserve | |
48 | * the exact semantics. | |
49 | * | |
50 | * E5 Section 11.6.1. | |
51 | * | |
52 | * Custom types also have special behavior implemented here. | |
53 | */ | |
54 | ||
55 | duk_context *ctx = (duk_context *) thr; | |
56 | duk_double_union du; | |
57 | ||
58 | DUK_ASSERT(thr != NULL); | |
59 | DUK_ASSERT(ctx != NULL); | |
60 | DUK_ASSERT(tv_x != NULL); /* may be reg or const */ | |
61 | DUK_ASSERT(tv_y != NULL); /* may be reg or const */ | |
62 | DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */ | |
63 | DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx)); | |
64 | ||
65 | /* | |
66 | * Fast paths | |
67 | */ | |
68 | ||
69 | #if defined(DUK_USE_FASTINT) | |
70 | if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) { | |
71 | duk_int64_t v1, v2, v3; | |
72 | duk_int32_t v3_hi; | |
73 | duk_tval tv_tmp; | |
74 | duk_tval *tv_z; | |
75 | ||
76 | /* Input values are signed 48-bit so we can detect overflow | |
77 | * reliably from high bits or just a comparison. | |
78 | */ | |
79 | ||
80 | v1 = DUK_TVAL_GET_FASTINT(tv_x); | |
81 | v2 = DUK_TVAL_GET_FASTINT(tv_y); | |
82 | v3 = v1 + v2; | |
83 | v3_hi = (duk_int32_t) (v3 >> 32); | |
84 | if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) { | |
85 | tv_z = thr->valstack_bottom + idx_z; | |
86 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
87 | DUK_TVAL_SET_FASTINT(tv_z, v3); | |
88 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
89 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
90 | return; | |
91 | } else { | |
92 | /* overflow, fall through */ | |
93 | ; | |
94 | } | |
95 | } | |
96 | #endif /* DUK_USE_FASTINT */ | |
97 | ||
98 | if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) { | |
99 | duk_tval tv_tmp; | |
100 | duk_tval *tv_z; | |
101 | ||
102 | du.d = DUK_TVAL_GET_NUMBER(tv_x) + DUK_TVAL_GET_NUMBER(tv_y); | |
103 | DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); | |
104 | DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du)); | |
105 | ||
106 | tv_z = thr->valstack_bottom + idx_z; | |
107 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
108 | DUK_TVAL_SET_NUMBER(tv_z, du.d); | |
109 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
110 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
111 | return; | |
112 | } | |
113 | ||
114 | /* | |
115 | * Slow path: potentially requires function calls for coercion | |
116 | */ | |
117 | ||
118 | duk_push_tval(ctx, tv_x); | |
119 | duk_push_tval(ctx, tv_y); | |
120 | duk_to_primitive(ctx, -2, DUK_HINT_NONE); /* side effects -> don't use tv_x, tv_y after */ | |
121 | duk_to_primitive(ctx, -1, DUK_HINT_NONE); | |
122 | ||
123 | /* As a first approximation, buffer values are coerced to strings | |
124 | * for addition. This means that adding two buffers currently | |
125 | * results in a string. | |
126 | */ | |
127 | if (duk_check_type_mask(ctx, -2, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BUFFER) || | |
128 | duk_check_type_mask(ctx, -1, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BUFFER)) { | |
129 | duk_to_string(ctx, -2); | |
130 | duk_to_string(ctx, -1); | |
131 | duk_concat(ctx, 2); /* [... s1 s2] -> [... s1+s2] */ | |
132 | duk_replace(ctx, (duk_idx_t) idx_z); /* side effects */ | |
133 | } else { | |
134 | duk_double_t d1, d2; | |
135 | ||
136 | d1 = duk_to_number(ctx, -2); | |
137 | d2 = duk_to_number(ctx, -1); | |
138 | DUK_ASSERT(duk_is_number(ctx, -2)); | |
139 | DUK_ASSERT(duk_is_number(ctx, -1)); | |
140 | DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1); | |
141 | DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2); | |
142 | ||
143 | du.d = d1 + d2; | |
144 | DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); | |
145 | DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du)); | |
146 | ||
147 | duk_pop_2(ctx); | |
148 | duk_push_number(ctx, du.d); | |
149 | duk_replace(ctx, (duk_idx_t) idx_z); /* side effects */ | |
150 | } | |
151 | } | |
152 | ||
153 | DUK_LOCAL void duk__vm_arith_binary_op(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_idx_t idx_z, duk_small_uint_fast_t opcode) { | |
154 | /* | |
155 | * Arithmetic operations other than '+' have number-only semantics | |
156 | * and are implemented here. The separate switch-case here means a | |
157 | * "double dispatch" of the arithmetic opcode, but saves code space. | |
158 | * | |
159 | * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3. | |
160 | */ | |
161 | ||
162 | duk_context *ctx = (duk_context *) thr; | |
163 | duk_tval tv_tmp; | |
164 | duk_tval *tv_z; | |
165 | duk_double_t d1, d2; | |
166 | duk_double_union du; | |
167 | ||
168 | DUK_ASSERT(thr != NULL); | |
169 | DUK_ASSERT(ctx != NULL); | |
170 | DUK_ASSERT(tv_x != NULL); /* may be reg or const */ | |
171 | DUK_ASSERT(tv_y != NULL); /* may be reg or const */ | |
172 | DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */ | |
173 | DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx)); | |
174 | ||
175 | #if defined(DUK_USE_FASTINT) | |
176 | if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) { | |
177 | duk_int64_t v1, v2, v3; | |
178 | duk_int32_t v3_hi; | |
179 | ||
180 | v1 = DUK_TVAL_GET_FASTINT(tv_x); | |
181 | v2 = DUK_TVAL_GET_FASTINT(tv_y); | |
182 | ||
183 | switch (opcode) { | |
184 | case DUK_OP_SUB: { | |
185 | v3 = v1 - v2; | |
186 | break; | |
187 | } | |
188 | case DUK_OP_MUL: { | |
189 | /* Must ensure result is 64-bit (no overflow); a | |
190 | * simple and sufficient fast path is to allow only | |
191 | * 32-bit inputs. Avoid zero inputs to avoid | |
192 | * negative zero issues (-1 * 0 = -0, for instance). | |
193 | */ | |
194 | if (v1 >= -0x80000000LL && v1 <= 0x7fffffffLL && v1 != 0 && | |
195 | v2 >= -0x80000000LL && v2 <= 0x7fffffffLL && v2 != 0) { | |
196 | v3 = v1 * v2; | |
197 | } else { | |
198 | goto skip_fastint; | |
199 | } | |
200 | break; | |
201 | } | |
202 | case DUK_OP_DIV: { | |
203 | /* Don't allow a zero divisor. Fast path check by | |
204 | * "verifying" with multiplication. Also avoid zero | |
205 | * dividend to avoid negative zero issues (0 / -1 = -0 | |
206 | * for instance). | |
207 | */ | |
208 | if (v1 == 0 || v2 == 0) { | |
209 | goto skip_fastint; | |
210 | } | |
211 | v3 = v1 / v2; | |
212 | if (v3 * v2 != v1) { | |
213 | goto skip_fastint; | |
214 | } | |
215 | break; | |
216 | } | |
217 | case DUK_OP_MOD: { | |
218 | /* Don't allow a zero divisor. Restrict both v1 and | |
219 | * v2 to positive values to avoid compiler specific | |
220 | * behavior. | |
221 | */ | |
222 | if (v1 < 1 || v2 < 1) { | |
223 | goto skip_fastint; | |
224 | } | |
225 | v3 = v1 % v2; | |
226 | DUK_ASSERT(v3 >= 0); | |
227 | DUK_ASSERT(v3 < v2); | |
228 | DUK_ASSERT(v1 - (v1 / v2) * v2 == v3); | |
229 | break; | |
230 | } | |
231 | default: { | |
232 | DUK_UNREACHABLE(); | |
233 | goto skip_fastint; | |
234 | } | |
235 | } | |
236 | ||
237 | v3_hi = (duk_int32_t) (v3 >> 32); | |
238 | if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) { | |
239 | tv_z = thr->valstack_bottom + idx_z; | |
240 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
241 | DUK_TVAL_SET_FASTINT(tv_z, v3); | |
242 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
243 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
244 | return; | |
245 | } | |
246 | /* fall through if overflow etc */ | |
247 | } | |
248 | skip_fastint: | |
249 | #endif /* DUK_USE_FASTINT */ | |
250 | ||
251 | if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) { | |
252 | /* fast path */ | |
253 | d1 = DUK_TVAL_GET_NUMBER(tv_x); | |
254 | d2 = DUK_TVAL_GET_NUMBER(tv_y); | |
255 | } else { | |
256 | duk_push_tval(ctx, tv_x); | |
257 | duk_push_tval(ctx, tv_y); | |
258 | d1 = duk_to_number(ctx, -2); /* side effects */ | |
259 | d2 = duk_to_number(ctx, -1); | |
260 | DUK_ASSERT(duk_is_number(ctx, -2)); | |
261 | DUK_ASSERT(duk_is_number(ctx, -1)); | |
262 | DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1); | |
263 | DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2); | |
264 | duk_pop_2(ctx); | |
265 | } | |
266 | ||
267 | switch (opcode) { | |
268 | case DUK_OP_SUB: { | |
269 | du.d = d1 - d2; | |
270 | break; | |
271 | } | |
272 | case DUK_OP_MUL: { | |
273 | du.d = d1 * d2; | |
274 | break; | |
275 | } | |
276 | case DUK_OP_DIV: { | |
277 | du.d = d1 / d2; | |
278 | break; | |
279 | } | |
280 | case DUK_OP_MOD: { | |
281 | du.d = duk__compute_mod(d1, d2); | |
282 | break; | |
283 | } | |
284 | default: { | |
285 | DUK_UNREACHABLE(); | |
286 | du.d = DUK_DOUBLE_NAN; /* should not happen */ | |
287 | break; | |
288 | } | |
289 | } | |
290 | ||
291 | /* important to use normalized NaN with 8-byte tagged types */ | |
292 | DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); | |
293 | DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du)); | |
294 | ||
295 | tv_z = thr->valstack_bottom + idx_z; | |
296 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
297 | DUK_TVAL_SET_NUMBER(tv_z, du.d); | |
298 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
299 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
300 | } | |
301 | ||
302 | DUK_LOCAL void duk__vm_bitwise_binary_op(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_fast_t idx_z, duk_small_uint_fast_t opcode) { | |
303 | /* | |
304 | * Binary bitwise operations use different coercions (ToInt32, ToUint32) | |
305 | * depending on the operation. We coerce the arguments first using | |
306 | * ToInt32(), and then cast to an 32-bit value if necessary. Note that | |
307 | * such casts must be correct even if there is no native 32-bit type | |
308 | * (e.g., duk_int32_t and duk_uint32_t are 64-bit). | |
309 | * | |
310 | * E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3 | |
311 | */ | |
312 | ||
313 | duk_context *ctx = (duk_context *) thr; | |
314 | duk_tval tv_tmp; | |
315 | duk_tval *tv_z; | |
316 | duk_int32_t i1, i2, i3; | |
317 | duk_uint32_t u1, u2, u3; | |
318 | #if defined(DUK_USE_FASTINT) | |
319 | duk_int64_t fi3; | |
320 | #else | |
321 | duk_double_t d3; | |
322 | #endif | |
323 | ||
324 | DUK_ASSERT(thr != NULL); | |
325 | DUK_ASSERT(ctx != NULL); | |
326 | DUK_ASSERT(tv_x != NULL); /* may be reg or const */ | |
327 | DUK_ASSERT(tv_y != NULL); /* may be reg or const */ | |
328 | DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */ | |
329 | DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx)); | |
330 | ||
331 | #if defined(DUK_USE_FASTINT) | |
332 | if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) { | |
333 | i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x); | |
334 | i2 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_y); | |
335 | } | |
336 | else | |
337 | #endif /* DUK_USE_FASTINT */ | |
338 | { | |
339 | duk_push_tval(ctx, tv_x); | |
340 | duk_push_tval(ctx, tv_y); | |
341 | i1 = duk_to_int32(ctx, -2); | |
342 | i2 = duk_to_int32(ctx, -1); | |
343 | duk_pop_2(ctx); | |
344 | } | |
345 | ||
346 | switch (opcode) { | |
347 | case DUK_OP_BAND: { | |
348 | i3 = i1 & i2; | |
349 | break; | |
350 | } | |
351 | case DUK_OP_BOR: { | |
352 | i3 = i1 | i2; | |
353 | break; | |
354 | } | |
355 | case DUK_OP_BXOR: { | |
356 | i3 = i1 ^ i2; | |
357 | break; | |
358 | } | |
359 | case DUK_OP_BASL: { | |
360 | /* Signed shift, named "arithmetic" (asl) because the result | |
361 | * is signed, e.g. 4294967295 << 1 -> -2. Note that result | |
362 | * must be masked. | |
363 | */ | |
364 | ||
365 | u2 = ((duk_uint32_t) i2) & 0xffffffffUL; | |
366 | i3 = i1 << (u2 & 0x1f); /* E5 Section 11.7.1, steps 7 and 8 */ | |
367 | i3 = i3 & ((duk_int32_t) 0xffffffffUL); /* Note: left shift, should mask */ | |
368 | break; | |
369 | } | |
370 | case DUK_OP_BASR: { | |
371 | /* signed shift */ | |
372 | ||
373 | u2 = ((duk_uint32_t) i2) & 0xffffffffUL; | |
374 | i3 = i1 >> (u2 & 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */ | |
375 | break; | |
376 | } | |
377 | case DUK_OP_BLSR: { | |
378 | /* unsigned shift */ | |
379 | ||
380 | u1 = ((duk_uint32_t) i1) & 0xffffffffUL; | |
381 | u2 = ((duk_uint32_t) i2) & 0xffffffffUL; | |
382 | ||
383 | /* special result value handling */ | |
384 | u3 = u1 >> (u2 & 0x1f); /* E5 Section 11.7.2, steps 7 and 8 */ | |
385 | #if defined(DUK_USE_FASTINT) | |
386 | fi3 = (duk_int64_t) u3; | |
387 | goto fastint_result_set; | |
388 | #else | |
389 | d3 = (duk_double_t) u3; | |
390 | goto result_set; | |
391 | #endif | |
392 | } | |
393 | default: { | |
394 | DUK_UNREACHABLE(); | |
395 | i3 = 0; /* should not happen */ | |
396 | break; | |
397 | } | |
398 | } | |
399 | ||
400 | #if defined(DUK_USE_FASTINT) | |
401 | /* Result is always fastint compatible. */ | |
402 | /* XXX: set 32-bit result (but must handle signed and unsigned) */ | |
403 | fi3 = (duk_int64_t) i3; | |
404 | ||
405 | fastint_result_set: | |
406 | tv_z = thr->valstack_bottom + idx_z; | |
407 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
408 | DUK_TVAL_SET_FASTINT(tv_z, fi3); | |
409 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
410 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
411 | #else | |
412 | d3 = (duk_double_t) i3; | |
413 | ||
414 | result_set: | |
415 | DUK_ASSERT(!DUK_ISNAN(d3)); /* 'd3' is never NaN, so no need to normalize */ | |
416 | DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3); /* always normalized */ | |
417 | ||
418 | tv_z = thr->valstack_bottom + idx_z; | |
419 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
420 | DUK_TVAL_SET_NUMBER(tv_z, d3); | |
421 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
422 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
423 | #endif | |
424 | } | |
425 | ||
426 | /* In-place unary operation. */ | |
427 | DUK_LOCAL void duk__vm_arith_unary_op(duk_hthread *thr, duk_tval *tv_x, duk_idx_t idx_x, duk_small_uint_fast_t opcode) { | |
428 | /* | |
429 | * Arithmetic operations other than '+' have number-only semantics | |
430 | * and are implemented here. The separate switch-case here means a | |
431 | * "double dispatch" of the arithmetic opcode, but saves code space. | |
432 | * | |
433 | * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3. | |
434 | */ | |
435 | ||
436 | duk_context *ctx = (duk_context *) thr; | |
437 | duk_double_t d1; | |
438 | duk_double_union du; | |
439 | ||
440 | DUK_ASSERT(thr != NULL); | |
441 | DUK_ASSERT(ctx != NULL); | |
442 | DUK_ASSERT(opcode == DUK_EXTRAOP_UNM || opcode == DUK_EXTRAOP_UNP); | |
443 | ||
444 | #if defined(DUK_USE_FASTINT) | |
445 | if (DUK_TVAL_IS_FASTINT(tv_x)) { | |
446 | duk_int64_t v1, v2; | |
447 | ||
448 | v1 = DUK_TVAL_GET_FASTINT(tv_x); | |
449 | if (opcode == DUK_EXTRAOP_UNM) { | |
450 | /* The smallest fastint is no longer 48-bit when | |
451 | * negated. Positive zero becames negative zero | |
452 | * (cannot be represented) when negated. | |
453 | */ | |
454 | if (DUK_LIKELY(v1 != DUK_FASTINT_MIN && v1 != 0)) { | |
455 | v2 = -v1; | |
456 | DUK_TVAL_SET_FASTINT(tv_x, v2); /* no refcount changes */ | |
457 | return; | |
458 | } | |
459 | } else { | |
460 | /* ToNumber() for a fastint is a no-op. */ | |
461 | DUK_ASSERT(opcode == DUK_EXTRAOP_UNP); | |
462 | return; | |
463 | } | |
464 | /* fall through if overflow etc */ | |
465 | } | |
466 | #endif /* DUK_USE_FASTINT */ | |
467 | ||
468 | if (!DUK_TVAL_IS_NUMBER(tv_x)) { | |
469 | duk_to_number(ctx, idx_x); /* side effects, perform in-place */ | |
470 | tv_x = duk_get_tval(ctx, idx_x); | |
471 | DUK_ASSERT(tv_x != NULL); | |
472 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x)); | |
473 | } | |
474 | ||
475 | d1 = DUK_TVAL_GET_NUMBER(tv_x); | |
476 | if (opcode == DUK_EXTRAOP_UNM) { | |
477 | du.d = -d1; | |
478 | } else { | |
479 | /* ToNumber() for a double is a no-op. */ | |
480 | DUK_ASSERT(opcode == DUK_EXTRAOP_UNP); | |
481 | du.d = d1; | |
482 | } | |
483 | DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); /* mandatory if du.d is a NaN */ | |
484 | ||
485 | DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du)); | |
486 | ||
487 | #if defined(DUK_USE_FASTINT) | |
488 | /* Unary plus is used to force a fastint check, so must include | |
489 | * downgrade check. | |
490 | */ | |
491 | DUK_TVAL_SET_NUMBER_CHKFAST(tv_x, du.d); /* no refcount changes */ | |
492 | #else | |
493 | DUK_TVAL_SET_NUMBER(tv_x, du.d); /* no refcount changes */ | |
494 | #endif | |
495 | } | |
496 | ||
497 | DUK_LOCAL void duk__vm_bitwise_not(duk_hthread *thr, duk_tval *tv_x, duk_small_uint_fast_t idx_z) { | |
498 | /* | |
499 | * E5 Section 11.4.8 | |
500 | */ | |
501 | ||
502 | duk_context *ctx = (duk_context *) thr; | |
503 | duk_tval tv_tmp; | |
504 | duk_tval *tv_z; | |
505 | duk_int32_t i1, i2; | |
506 | #if !defined(DUK_USE_FASTINT) | |
507 | duk_double_t d2; | |
508 | #endif | |
509 | ||
510 | DUK_ASSERT(thr != NULL); | |
511 | DUK_ASSERT(ctx != NULL); | |
512 | DUK_ASSERT(tv_x != NULL); /* may be reg or const */ | |
513 | DUK_ASSERT_DISABLE(idx_z >= 0); | |
514 | DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(ctx)); | |
515 | ||
516 | #if defined(DUK_USE_FASTINT) | |
517 | if (DUK_TVAL_IS_FASTINT(tv_x)) { | |
518 | i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x); | |
519 | } | |
520 | else | |
521 | #endif /* DUK_USE_FASTINT */ | |
522 | { | |
523 | duk_push_tval(ctx, tv_x); | |
524 | i1 = duk_to_int32(ctx, -1); | |
525 | duk_pop(ctx); | |
526 | } | |
527 | ||
528 | i2 = ~i1; | |
529 | ||
530 | #if defined(DUK_USE_FASTINT) | |
531 | /* Result is always fastint compatible. */ | |
532 | tv_z = thr->valstack_bottom + idx_z; | |
533 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
534 | DUK_TVAL_SET_FASTINT_I32(tv_z, i2); | |
535 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
536 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
537 | #else | |
538 | d2 = (duk_double_t) i2; | |
539 | ||
540 | DUK_ASSERT(!DUK_ISNAN(d2)); /* 'val' is never NaN, so no need to normalize */ | |
541 | DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2); /* always normalized */ | |
542 | ||
543 | tv_z = thr->valstack_bottom + idx_z; | |
544 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
545 | DUK_TVAL_SET_NUMBER(tv_z, d2); | |
546 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_z)); /* no need to incref */ | |
547 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
548 | #endif | |
549 | } | |
550 | ||
551 | DUK_LOCAL void duk__vm_logical_not(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_z) { | |
552 | /* | |
553 | * E5 Section 11.4.9 | |
554 | */ | |
555 | ||
556 | duk_tval tv_tmp; | |
557 | duk_bool_t res; | |
558 | ||
559 | DUK_ASSERT(thr != NULL); | |
560 | DUK_ASSERT(tv_x != NULL); /* may be reg or const */ | |
561 | DUK_ASSERT(tv_z != NULL); /* reg */ | |
562 | ||
563 | DUK_UNREF(thr); /* w/o refcounts */ | |
564 | ||
565 | /* ToBoolean() does not require any operations with side effects so | |
566 | * we can do it efficiently. For footprint it would be better to use | |
567 | * duk_js_toboolean() and then push+replace to the result slot. | |
568 | */ | |
569 | res = duk_js_toboolean(tv_x); /* does not modify tv_x */ | |
570 | DUK_ASSERT(res == 0 || res == 1); | |
571 | res ^= 1; | |
572 | DUK_TVAL_SET_TVAL(&tv_tmp, tv_z); | |
573 | DUK_TVAL_SET_BOOLEAN(tv_z, res); /* no need to incref */ | |
574 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
575 | } | |
576 | ||
577 | /* | |
578 | * Longjmp handler for the bytecode executor (and a bunch of static | |
579 | * helpers for it). | |
580 | * | |
581 | * Any type of longjmp() can be caught here, including intra-function | |
582 | * longjmp()s like 'break', 'continue', (slow) 'return', 'yield', etc. | |
583 | * | |
584 | * Error policy: should not ordinarily throw errors. Errors thrown | |
585 | * will bubble outwards. | |
586 | * | |
587 | * Returns: | |
588 | * 0 restart execution | |
589 | * 1 bytecode executor finished | |
590 | * 2 rethrow longjmp | |
591 | */ | |
592 | ||
593 | /* XXX: duk_api operations for cross-thread reg manipulation? */ | |
594 | /* XXX: post-condition: value stack must be correct; for ecmascript functions, clamped to 'nregs' */ | |
595 | ||
596 | #define DUK__LONGJMP_RESTART 0 /* state updated, restart bytecode execution */ | |
597 | #define DUK__LONGJMP_FINISHED 1 /* exit bytecode executor with return value */ | |
598 | #define DUK__LONGJMP_RETHROW 2 /* exit bytecode executor by rethrowing an error to caller */ | |
599 | ||
600 | /* only called when act_idx points to an Ecmascript function */ | |
601 | DUK_LOCAL void duk__reconfig_valstack(duk_hthread *thr, duk_size_t act_idx, duk_small_uint_t retval_count) { | |
602 | duk_hcompiledfunction *h_func; | |
603 | ||
604 | DUK_ASSERT(thr != NULL); | |
605 | DUK_ASSERT_DISABLE(act_idx >= 0); /* unsigned */ | |
606 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + act_idx) != NULL); | |
607 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + act_idx))); | |
608 | DUK_ASSERT_DISABLE(thr->callstack[act_idx].idx_retval >= 0); /* unsigned */ | |
609 | ||
610 | thr->valstack_bottom = thr->valstack + thr->callstack[act_idx].idx_bottom; | |
611 | ||
612 | /* clamp so that retval is at the top (retval_count == 1) or register just before | |
613 | * intended retval is at the top (retval_count == 0, happens e.g. with 'finally'). | |
614 | */ | |
615 | duk_set_top((duk_context *) thr, | |
616 | (duk_idx_t) (thr->callstack[act_idx].idx_retval - | |
617 | thr->callstack[act_idx].idx_bottom + | |
618 | retval_count)); | |
619 | ||
620 | /* | |
621 | * When returning to an Ecmascript function, extend the valstack | |
622 | * top to 'nregs' always. | |
623 | */ | |
624 | ||
625 | h_func = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(thr->callstack + act_idx); | |
626 | ||
627 | (void) duk_valstack_resize_raw((duk_context *) thr, | |
628 | (thr->valstack_bottom - thr->valstack) + /* bottom of current func */ | |
629 | h_func->nregs + /* reg count */ | |
630 | DUK_VALSTACK_INTERNAL_EXTRA, /* + spare */ | |
631 | DUK_VSRESIZE_FLAG_SHRINK | /* flags */ | |
632 | 0 /* no compact */ | | |
633 | DUK_VSRESIZE_FLAG_THROW); | |
634 | ||
635 | duk_set_top((duk_context *) thr, h_func->nregs); | |
636 | } | |
637 | ||
638 | DUK_LOCAL void duk__handle_catch_or_finally(duk_hthread *thr, duk_size_t cat_idx, duk_bool_t is_finally) { | |
639 | duk_context *ctx = (duk_context *) thr; | |
640 | duk_tval tv_tmp; | |
641 | duk_tval *tv1; | |
642 | ||
643 | DUK_DDD(DUK_DDDPRINT("handling catch/finally, cat_idx=%ld, is_finally=%ld", | |
644 | (long) cat_idx, (long) is_finally)); | |
645 | ||
646 | /* | |
647 | * Set caught value and longjmp type to catcher regs. | |
648 | */ | |
649 | ||
650 | DUK_DDD(DUK_DDDPRINT("writing catch registers: idx_base=%ld -> %!T, idx_base+1=%ld -> %!T", | |
651 | (long) thr->catchstack[cat_idx].idx_base, | |
652 | (duk_tval *) &thr->heap->lj.value1, | |
653 | (long) (thr->catchstack[cat_idx].idx_base + 1), | |
654 | (duk_tval *) &thr->heap->lj.value2)); | |
655 | ||
656 | tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base; | |
657 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
658 | DUK_TVAL_SET_TVAL(tv1, &thr->heap->lj.value1); | |
659 | DUK_TVAL_INCREF(thr, tv1); | |
660 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
661 | ||
662 | tv1 = thr->valstack + thr->catchstack[cat_idx].idx_base + 1; | |
663 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
664 | DUK_TVAL_SET_NUMBER(tv1, (duk_double_t) thr->heap->lj.type); /* XXX: set int */ | |
665 | DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv1)); /* no need to incref */ | |
666 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
667 | ||
668 | /* | |
669 | * Unwind catchstack and callstack. | |
670 | * | |
671 | * The 'cat_idx' catcher is always kept, even when executing finally. | |
672 | */ | |
673 | ||
674 | duk_hthread_catchstack_unwind(thr, cat_idx + 1); | |
675 | duk_hthread_callstack_unwind(thr, thr->catchstack[cat_idx].callstack_index + 1); | |
676 | ||
677 | /* | |
678 | * Reconfigure valstack to 'nregs' (this is always the case for | |
679 | * Ecmascript functions). | |
680 | */ | |
681 | ||
682 | DUK_ASSERT(thr->callstack_top >= 1); | |
683 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL); | |
684 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))); | |
685 | ||
686 | thr->valstack_bottom = thr->valstack + (thr->callstack + thr->callstack_top - 1)->idx_bottom; | |
687 | duk_set_top((duk_context *) thr, ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->nregs); | |
688 | ||
689 | /* | |
690 | * Reset PC: resume execution from catch or finally jump slot. | |
691 | */ | |
692 | ||
693 | (thr->callstack + thr->callstack_top - 1)->curr_pc = | |
694 | thr->catchstack[cat_idx].pc_base + (is_finally ? 1 : 0); | |
695 | ||
696 | /* | |
697 | * If entering a 'catch' block which requires an automatic | |
698 | * catch variable binding, create the lexical environment. | |
699 | * | |
700 | * The binding is mutable (= writable) but not deletable. | |
701 | * Step 4 for the catch production in E5 Section 12.14; | |
702 | * no value is given for CreateMutableBinding 'D' argument, | |
703 | * which implies the binding is not deletable. | |
704 | */ | |
705 | ||
706 | if (!is_finally && DUK_CAT_HAS_CATCH_BINDING_ENABLED(&thr->catchstack[cat_idx])) { | |
707 | duk_activation *act; | |
708 | duk_hobject *new_env; | |
709 | duk_hobject *act_lex_env; | |
710 | ||
711 | DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding")); | |
712 | ||
713 | /* Note: 'act' is dangerous here because it may get invalidate at many | |
714 | * points, so we re-lookup it multiple times. | |
715 | */ | |
716 | DUK_ASSERT(thr->callstack_top >= 1); | |
717 | act = thr->callstack + thr->callstack_top - 1; | |
718 | ||
719 | if (act->lex_env == NULL) { | |
720 | DUK_ASSERT(act->var_env == NULL); | |
721 | DUK_DDD(DUK_DDDPRINT("delayed environment initialization")); | |
722 | ||
723 | /* this may have side effects, so re-lookup act */ | |
724 | duk_js_init_activation_environment_records_delayed(thr, act); | |
725 | act = thr->callstack + thr->callstack_top - 1; | |
726 | } | |
727 | DUK_ASSERT(act->lex_env != NULL); | |
728 | DUK_ASSERT(act->var_env != NULL); | |
729 | DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL); | |
730 | DUK_UNREF(act); /* unreferenced without assertions */ | |
731 | ||
732 | act = thr->callstack + thr->callstack_top - 1; | |
733 | act_lex_env = act->lex_env; | |
734 | act = NULL; /* invalidated */ | |
735 | ||
736 | (void) duk_push_object_helper_proto(ctx, | |
737 | DUK_HOBJECT_FLAG_EXTENSIBLE | | |
738 | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV), | |
739 | act_lex_env); | |
740 | new_env = duk_require_hobject(ctx, -1); | |
741 | DUK_ASSERT(new_env != NULL); | |
742 | DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr *) new_env)); | |
743 | ||
744 | /* Note: currently the catch binding is handled without a register | |
745 | * binding because we don't support dynamic register bindings (they | |
746 | * must be fixed for an entire function). So, there is no need to | |
747 | * record regbases etc. | |
748 | */ | |
749 | ||
750 | DUK_ASSERT(thr->catchstack[cat_idx].h_varname != NULL); | |
751 | duk_push_hstring(ctx, thr->catchstack[cat_idx].h_varname); | |
752 | duk_push_tval(ctx, &thr->heap->lj.value1); | |
753 | duk_xdef_prop(ctx, -3, DUK_PROPDESC_FLAGS_W); /* writable, not configurable */ | |
754 | ||
755 | act = thr->callstack + thr->callstack_top - 1; | |
756 | act->lex_env = new_env; | |
757 | DUK_HOBJECT_INCREF(thr, new_env); /* reachable through activation */ | |
758 | ||
759 | DUK_CAT_SET_LEXENV_ACTIVE(&thr->catchstack[cat_idx]); | |
760 | ||
761 | duk_pop(ctx); | |
762 | ||
763 | DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr *) new_env)); | |
764 | } | |
765 | ||
766 | if (is_finally) { | |
767 | DUK_CAT_CLEAR_FINALLY_ENABLED(&thr->catchstack[cat_idx]); | |
768 | } else { | |
769 | DUK_CAT_CLEAR_CATCH_ENABLED(&thr->catchstack[cat_idx]); | |
770 | } | |
771 | } | |
772 | ||
773 | DUK_LOCAL void duk__handle_label(duk_hthread *thr, duk_size_t cat_idx) { | |
774 | duk_activation *act; | |
775 | ||
776 | /* no callstack changes, no value stack changes */ | |
777 | ||
778 | DUK_ASSERT(thr != NULL); | |
779 | DUK_ASSERT(thr->callstack_top >= 1); | |
780 | ||
781 | act = thr->callstack + thr->callstack_top - 1; | |
782 | ||
783 | DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL); | |
784 | DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(act))); | |
785 | ||
786 | /* +0 = break, +1 = continue */ | |
787 | act->curr_pc = thr->catchstack[cat_idx].pc_base + (thr->heap->lj.type == DUK_LJ_TYPE_CONTINUE ? 1 : 0); | |
788 | act = NULL; /* invalidated */ | |
789 | ||
790 | duk_hthread_catchstack_unwind(thr, cat_idx + 1); /* keep label catcher */ | |
791 | /* no need to unwind callstack */ | |
792 | ||
793 | /* valstack should not need changes */ | |
794 | #if defined(DUK_USE_ASSERTIONS) | |
795 | act = thr->callstack + thr->callstack_top - 1; | |
796 | DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) == | |
797 | (duk_size_t) ((duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act))->nregs); | |
798 | #endif | |
799 | } | |
800 | ||
801 | /* Note: called for DUK_LJ_TYPE_YIELD and for DUK_LJ_TYPE_RETURN, when a | |
802 | * return terminates a thread and yields to the resumer. | |
803 | */ | |
804 | DUK_LOCAL void duk__handle_yield(duk_hthread *thr, duk_hthread *resumer, duk_size_t act_idx) { | |
805 | duk_tval tv_tmp; | |
806 | duk_tval *tv1; | |
807 | ||
808 | /* this may also be called for DUK_LJ_TYPE_RETURN; this is OK as long as | |
809 | * lj.value1 is correct. | |
810 | */ | |
811 | ||
812 | DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + act_idx) != NULL); | |
813 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + act_idx))); /* resume caller must be an ecmascript func */ | |
814 | ||
815 | DUK_DDD(DUK_DDDPRINT("resume idx_retval is %ld", (long) resumer->callstack[act_idx].idx_retval)); | |
816 | ||
817 | tv1 = resumer->valstack + resumer->callstack[act_idx].idx_retval; /* return value from Duktape.Thread.resume() */ | |
818 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
819 | DUK_TVAL_SET_TVAL(tv1, &thr->heap->lj.value1); | |
820 | DUK_TVAL_INCREF(thr, tv1); | |
821 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
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(resumer, act_idx, 1); /* 1 = have retval */ | |
827 | ||
828 | /* caller must change active thread, and set thr->resumer to NULL */ | |
829 | } | |
830 | ||
831 | DUK_LOCAL | |
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_tval tv_tmp; | |
836 | duk_size_t entry_callstack_index; | |
837 | duk_small_uint_t retval = DUK__LONGJMP_RESTART; | |
838 | ||
839 | DUK_ASSERT(thr != NULL); | |
840 | DUK_ASSERT(entry_thread != NULL); | |
841 | DUK_ASSERT(entry_callstack_top > 0); /* guarantees entry_callstack_top - 1 >= 0 */ | |
842 | ||
843 | entry_callstack_index = entry_callstack_top - 1; | |
844 | ||
845 | /* 'thr' is the current thread, as no-one resumes except us and we | |
846 | * switch 'thr' in that case. | |
847 | */ | |
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(&tv_tmp, tv); | |
953 | DUK_TVAL_SET_TVAL(tv, tv2); | |
954 | DUK_TVAL_INCREF(thr, tv); | |
955 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
956 | ||
957 | duk_hthread_callstack_unwind(resumee, act_idx + 1); /* unwind to 'yield' caller */ | |
958 | ||
959 | /* no need to unwind catchstack */ | |
960 | ||
961 | duk__reconfig_valstack(resumee, act_idx, 1); /* 1 = have retval */ | |
962 | ||
963 | resumee->resumer = thr; | |
964 | resumee->state = DUK_HTHREAD_STATE_RUNNING; | |
965 | thr->state = DUK_HTHREAD_STATE_RESUMED; | |
966 | DUK_HEAP_SWITCH_THREAD(thr->heap, resumee); | |
967 | #if 0 | |
968 | thr = resumee; /* not needed, as we exit right away */ | |
969 | #endif | |
970 | DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee")); | |
971 | retval = DUK__LONGJMP_RESTART; | |
972 | goto wipe_and_return; | |
973 | } else { | |
974 | duk_small_uint_t call_flags; | |
975 | duk_bool_t setup_rc; | |
976 | ||
977 | /* resumee: [... initial_func] (currently actually: [initial_func]) */ | |
978 | ||
979 | duk_push_undefined((duk_context *) resumee); | |
980 | tv = &thr->heap->lj.value1; | |
981 | duk_push_tval((duk_context *) resumee, tv); | |
982 | ||
983 | /* resumee: [... initial_func undefined(= this) resume_value ] */ | |
984 | ||
985 | call_flags = DUK_CALL_FLAG_IS_RESUME; /* is resume, not a tail call */ | |
986 | ||
987 | setup_rc = duk_handle_ecma_call_setup(resumee, | |
988 | 1, /* num_stack_args */ | |
989 | call_flags); /* call_flags */ | |
990 | if (setup_rc == 0) { | |
991 | /* Shouldn't happen but check anyway. */ | |
992 | DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR); | |
993 | } | |
994 | ||
995 | resumee->resumer = thr; | |
996 | resumee->state = DUK_HTHREAD_STATE_RUNNING; | |
997 | thr->state = DUK_HTHREAD_STATE_RESUMED; | |
998 | DUK_HEAP_SWITCH_THREAD(thr->heap, resumee); | |
999 | #if 0 | |
1000 | thr = resumee; /* not needed, as we exit right away */ | |
1001 | #endif | |
1002 | DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee")); | |
1003 | retval = DUK__LONGJMP_RESTART; | |
1004 | goto wipe_and_return; | |
1005 | } | |
1006 | DUK_UNREACHABLE(); | |
1007 | break; /* never here */ | |
1008 | } | |
1009 | ||
1010 | case DUK_LJ_TYPE_YIELD: { | |
1011 | /* | |
1012 | * Currently only allowed only if yielding thread has only | |
1013 | * Ecmascript activations (except for the Duktape.Thread.yield() | |
1014 | * call at the callstack top) and none of them constructor | |
1015 | * calls. | |
1016 | * | |
1017 | * This excludes the 'entry' thread which will always have | |
1018 | * a preventcount > 0. | |
1019 | */ | |
1020 | ||
1021 | duk_hthread *resumer; | |
1022 | ||
1023 | /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */ | |
1024 | ||
1025 | DUK_ASSERT(thr != entry_thread); /* Duktape.Thread.yield() should prevent */ | |
1026 | DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged from Duktape.Thread.yield() */ | |
1027 | DUK_ASSERT(thr->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.yield() activation */ | |
1028 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL && | |
1029 | DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1)) && | |
1030 | ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))->func == duk_bi_thread_yield); | |
1031 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2) != NULL && | |
1032 | DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* an Ecmascript function */ | |
1033 | DUK_ASSERT_DISABLE((thr->callstack + thr->callstack_top - 2)->idx_retval >= 0); /* unsigned */ | |
1034 | ||
1035 | resumer = thr->resumer; | |
1036 | ||
1037 | DUK_ASSERT(resumer != NULL); | |
1038 | DUK_ASSERT(resumer->state == DUK_HTHREAD_STATE_RESUMED); /* written by a previous RESUME handling */ | |
1039 | DUK_ASSERT(resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */ | |
1040 | DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1) != NULL && | |
1041 | DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1)) && | |
1042 | ((duk_hnativefunction *) DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 1))->func == duk_bi_thread_resume); | |
1043 | DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 2) != NULL && | |
1044 | DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(resumer->callstack + resumer->callstack_top - 2))); /* an Ecmascript function */ | |
1045 | DUK_ASSERT_DISABLE((resumer->callstack + resumer->callstack_top - 2)->idx_retval >= 0); /* unsigned */ | |
1046 | ||
1047 | if (thr->heap->lj.iserror) { | |
1048 | thr->state = DUK_HTHREAD_STATE_YIELDED; | |
1049 | thr->resumer = NULL; | |
1050 | resumer->state = DUK_HTHREAD_STATE_RUNNING; | |
1051 | DUK_HEAP_SWITCH_THREAD(thr->heap, resumer); | |
1052 | thr = resumer; | |
1053 | ||
1054 | thr->heap->lj.type = DUK_LJ_TYPE_THROW; | |
1055 | /* lj.value1 is already set */ | |
1056 | DUK_ASSERT(thr->heap->lj.iserror); /* already set */ | |
1057 | ||
1058 | DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate")); | |
1059 | goto check_longjmp; | |
1060 | } else { | |
1061 | duk__handle_yield(thr, resumer, resumer->callstack_top - 2); | |
1062 | ||
1063 | thr->state = DUK_HTHREAD_STATE_YIELDED; | |
1064 | thr->resumer = NULL; | |
1065 | resumer->state = DUK_HTHREAD_STATE_RUNNING; | |
1066 | DUK_HEAP_SWITCH_THREAD(thr->heap, resumer); | |
1067 | #if 0 | |
1068 | thr = resumer; /* not needed, as we exit right away */ | |
1069 | #endif | |
1070 | ||
1071 | DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer")); | |
1072 | retval = DUK__LONGJMP_RESTART; | |
1073 | goto wipe_and_return; | |
1074 | } | |
1075 | DUK_UNREACHABLE(); | |
1076 | break; /* never here */ | |
1077 | } | |
1078 | ||
1079 | case DUK_LJ_TYPE_RETURN: { | |
1080 | /* | |
1081 | * Four possible outcomes: | |
1082 | * * A 'finally' in the same function catches the 'return'. | |
1083 | * (or) | |
1084 | * * The return happens at the entry level of the bytecode | |
1085 | * executor, so return from the executor (in C stack). | |
1086 | * (or) | |
1087 | * * There is a calling (Ecmascript) activation in the call | |
1088 | * stack => return to it. | |
1089 | * (or) | |
1090 | * * There is no calling activation, and the thread is | |
1091 | * terminated. There is always a resumer in this case, | |
1092 | * which gets the return value similarly to a 'yield' | |
1093 | * (except that the current thread can no longer be | |
1094 | * resumed). | |
1095 | */ | |
1096 | ||
1097 | duk_tval *tv1; | |
1098 | duk_hthread *resumer; | |
1099 | duk_catcher *cat; | |
1100 | duk_size_t orig_callstack_index; | |
1101 | ||
1102 | DUK_ASSERT(thr != NULL); | |
1103 | DUK_ASSERT(thr->callstack_top >= 1); | |
1104 | DUK_ASSERT(thr->catchstack != NULL); | |
1105 | ||
1106 | /* XXX: does not work if thr->catchstack is NULL */ | |
1107 | /* XXX: does not work if thr->catchstack is allocated but lowest pointer */ | |
1108 | ||
1109 | cat = thr->catchstack + thr->catchstack_top - 1; /* may be < thr->catchstack initially */ | |
1110 | DUK_ASSERT(thr->callstack_top > 0); /* ensures callstack_top - 1 >= 0 */ | |
1111 | orig_callstack_index = thr->callstack_top - 1; | |
1112 | ||
1113 | while (cat >= thr->catchstack) { | |
1114 | if (cat->callstack_index != orig_callstack_index) { | |
1115 | break; | |
1116 | } | |
1117 | if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF && | |
1118 | DUK_CAT_HAS_FINALLY_ENABLED(cat)) { | |
1119 | /* 'finally' catches */ | |
1120 | duk__handle_catch_or_finally(thr, | |
1121 | cat - thr->catchstack, | |
1122 | 1); /* is_finally */ | |
1123 | ||
1124 | DUK_DD(DUK_DDPRINT("-> return caught by a finally (in the same function), restart execution")); | |
1125 | retval = DUK__LONGJMP_RESTART; | |
1126 | goto wipe_and_return; | |
1127 | } | |
1128 | cat--; | |
1129 | } | |
1130 | /* if out of catchstack, cat = thr->catchstack - 1 */ | |
1131 | ||
1132 | DUK_DD(DUK_DDPRINT("no catcher in catch stack, return to calling activation / yield")); | |
1133 | ||
1134 | /* return to calling activation (if any) */ | |
1135 | ||
1136 | if (thr == entry_thread && | |
1137 | thr->callstack_top == entry_callstack_top) { | |
1138 | /* return to the bytecode executor caller */ | |
1139 | ||
1140 | duk_push_tval((duk_context *) thr, &thr->heap->lj.value1); | |
1141 | ||
1142 | /* [ ... retval ] */ | |
1143 | ||
1144 | DUK_DD(DUK_DDPRINT("-> return propagated up to entry level, exit bytecode executor")); | |
1145 | retval = DUK__LONGJMP_FINISHED; | |
1146 | goto wipe_and_return; | |
1147 | } | |
1148 | ||
1149 | if (thr->callstack_top >= 2) { | |
1150 | /* there is a caller; it MUST be an Ecmascript caller (otherwise it would | |
1151 | * match entry level check) | |
1152 | */ | |
1153 | ||
1154 | DUK_DDD(DUK_DDDPRINT("slow return to Ecmascript caller, idx_retval=%ld, lj_value1=%!T", | |
1155 | (long) (thr->callstack + thr->callstack_top - 2)->idx_retval, | |
1156 | (duk_tval *) &thr->heap->lj.value1)); | |
1157 | ||
1158 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* must be ecmascript */ | |
1159 | ||
1160 | tv1 = thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval; | |
1161 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
1162 | DUK_TVAL_SET_TVAL(tv1, &thr->heap->lj.value1); | |
1163 | DUK_TVAL_INCREF(thr, tv1); | |
1164 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
1165 | ||
1166 | DUK_DDD(DUK_DDDPRINT("return value at idx_retval=%ld is %!T", | |
1167 | (long) (thr->callstack + thr->callstack_top - 2)->idx_retval, | |
1168 | (duk_tval *) (thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval))); | |
1169 | ||
1170 | duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */ | |
1171 | duk_hthread_callstack_unwind(thr, thr->callstack_top - 1); | |
1172 | duk__reconfig_valstack(thr, thr->callstack_top - 1, 1); /* new top, i.e. callee */ | |
1173 | ||
1174 | DUK_DD(DUK_DDPRINT("-> return not caught, restart execution in caller")); | |
1175 | retval = DUK__LONGJMP_RESTART; | |
1176 | goto wipe_and_return; | |
1177 | } | |
1178 | ||
1179 | DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)")); | |
1180 | ||
1181 | DUK_ASSERT(thr->resumer != NULL); | |
1182 | DUK_ASSERT(thr->resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */ | |
1183 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1) != NULL && | |
1184 | DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1)) && | |
1185 | ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */ | |
1186 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2) != NULL && | |
1187 | DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2))); /* an Ecmascript function */ | |
1188 | DUK_ASSERT_DISABLE((thr->resumer->callstack + thr->resumer->callstack_top - 2)->idx_retval >= 0); /* unsigned */ | |
1189 | DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); | |
1190 | DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED); | |
1191 | ||
1192 | resumer = thr->resumer; | |
1193 | ||
1194 | duk__handle_yield(thr, resumer, resumer->callstack_top - 2); | |
1195 | ||
1196 | duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */ | |
1197 | DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED); | |
1198 | ||
1199 | thr->resumer = NULL; | |
1200 | resumer->state = DUK_HTHREAD_STATE_RUNNING; | |
1201 | DUK_HEAP_SWITCH_THREAD(thr->heap, resumer); | |
1202 | #if 0 | |
1203 | thr = resumer; /* not needed */ | |
1204 | #endif | |
1205 | ||
1206 | DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer")); | |
1207 | retval = DUK__LONGJMP_RESTART; | |
1208 | goto wipe_and_return; | |
1209 | } | |
1210 | ||
1211 | case DUK_LJ_TYPE_BREAK: | |
1212 | case DUK_LJ_TYPE_CONTINUE: { | |
1213 | /* | |
1214 | * Find a matching label catcher or 'finally' catcher in | |
1215 | * the same function. | |
1216 | * | |
1217 | * A label catcher must always exist and will match unless | |
1218 | * a 'finally' captures the break/continue first. It is the | |
1219 | * compiler's responsibility to ensure that labels are used | |
1220 | * correctly. | |
1221 | */ | |
1222 | ||
1223 | duk_catcher *cat; | |
1224 | duk_size_t orig_callstack_index; | |
1225 | duk_uint_t lj_label; | |
1226 | ||
1227 | cat = thr->catchstack + thr->catchstack_top - 1; | |
1228 | orig_callstack_index = cat->callstack_index; | |
1229 | ||
1230 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(&thr->heap->lj.value1)); | |
1231 | lj_label = (duk_uint_t) DUK_TVAL_GET_NUMBER(&thr->heap->lj.value1); | |
1232 | ||
1233 | DUK_DDD(DUK_DDDPRINT("handling break/continue with label=%ld, callstack index=%ld", | |
1234 | (long) lj_label, (long) cat->callstack_index)); | |
1235 | ||
1236 | while (cat >= thr->catchstack) { | |
1237 | if (cat->callstack_index != orig_callstack_index) { | |
1238 | break; | |
1239 | } | |
1240 | DUK_DDD(DUK_DDDPRINT("considering catcher %ld: type=%ld label=%ld", | |
1241 | (long) (cat - thr->catchstack), | |
1242 | (long) DUK_CAT_GET_TYPE(cat), | |
1243 | (long) DUK_CAT_GET_LABEL(cat))); | |
1244 | ||
1245 | if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF && | |
1246 | DUK_CAT_HAS_FINALLY_ENABLED(cat)) { | |
1247 | /* finally catches */ | |
1248 | duk__handle_catch_or_finally(thr, | |
1249 | cat - thr->catchstack, | |
1250 | 1); /* is_finally */ | |
1251 | ||
1252 | DUK_DD(DUK_DDPRINT("-> break/continue caught by a finally (in the same function), restart execution")); | |
1253 | retval = DUK__LONGJMP_RESTART; | |
1254 | goto wipe_and_return; | |
1255 | } | |
1256 | if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL && | |
1257 | (duk_uint_t) DUK_CAT_GET_LABEL(cat) == lj_label) { | |
1258 | /* found label */ | |
1259 | duk__handle_label(thr, | |
1260 | cat - thr->catchstack); | |
1261 | ||
1262 | DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution")); | |
1263 | retval = DUK__LONGJMP_RESTART; | |
1264 | goto wipe_and_return; | |
1265 | } | |
1266 | cat--; | |
1267 | } | |
1268 | ||
1269 | /* should never happen, but be robust */ | |
1270 | DUK_D(DUK_DPRINT("break/continue not caught by anything in the current function (should never happen)")); | |
1271 | goto convert_to_internal_error; | |
1272 | } | |
1273 | ||
1274 | case DUK_LJ_TYPE_THROW: { | |
1275 | /* | |
1276 | * Three possible outcomes: | |
1277 | * * A try or finally catcher is found => resume there. | |
1278 | * (or) | |
1279 | * * The error propagates to the bytecode executor entry | |
1280 | * level (and we're in the entry thread) => rethrow | |
1281 | * with a new longjmp(), after restoring the previous | |
1282 | * catchpoint. | |
1283 | * * The error is not caught in the current thread, so | |
1284 | * the thread finishes with an error. This works like | |
1285 | * a yielded error, except that the thread is finished | |
1286 | * and can no longer be resumed. (There is always a | |
1287 | * resumer in this case.) | |
1288 | * | |
1289 | * Note: until we hit the entry level, there can only be | |
1290 | * Ecmascript activations. | |
1291 | */ | |
1292 | ||
1293 | duk_catcher *cat; | |
1294 | duk_hthread *resumer; | |
1295 | ||
1296 | cat = thr->catchstack + thr->catchstack_top - 1; | |
1297 | while (cat >= thr->catchstack) { | |
1298 | if (thr == entry_thread && | |
1299 | cat->callstack_index < entry_callstack_index) { | |
1300 | /* entry level reached */ | |
1301 | break; | |
1302 | } | |
1303 | ||
1304 | if (DUK_CAT_HAS_CATCH_ENABLED(cat)) { | |
1305 | /* try catches */ | |
1306 | DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF); | |
1307 | ||
1308 | duk__handle_catch_or_finally(thr, | |
1309 | cat - thr->catchstack, | |
1310 | 0); /* is_finally */ | |
1311 | ||
1312 | DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution")); | |
1313 | retval = DUK__LONGJMP_RESTART; | |
1314 | goto wipe_and_return; | |
1315 | } | |
1316 | ||
1317 | if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) { | |
1318 | DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF); | |
1319 | DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat)); | |
1320 | ||
1321 | duk__handle_catch_or_finally(thr, | |
1322 | cat - thr->catchstack, | |
1323 | 1); /* is_finally */ | |
1324 | ||
1325 | DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution")); | |
1326 | retval = DUK__LONGJMP_RESTART; | |
1327 | goto wipe_and_return; | |
1328 | } | |
1329 | ||
1330 | cat--; | |
1331 | } | |
1332 | ||
1333 | if (thr == entry_thread) { | |
1334 | /* not caught by anything before entry level; rethrow and let the | |
1335 | * final catcher unwind everything | |
1336 | */ | |
1337 | #if 0 | |
1338 | duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */ | |
1339 | duk_hthread_callstack_unwind(thr, entry_callstack_index + 1); | |
1340 | ||
1341 | #endif | |
1342 | DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor")); | |
1343 | retval = DUK__LONGJMP_RETHROW; | |
1344 | goto just_return; | |
1345 | /* Note: MUST NOT wipe_and_return here, as heap->lj must remain intact */ | |
1346 | } | |
1347 | ||
1348 | DUK_DD(DUK_DDPRINT("not caught by current thread, yield error to resumer")); | |
1349 | ||
1350 | /* not caught by current thread, thread terminates (yield error to resumer); | |
1351 | * note that this may cause a cascade if the resumer terminates with an uncaught | |
1352 | * exception etc (this is OK, but needs careful testing) | |
1353 | */ | |
1354 | ||
1355 | DUK_ASSERT(thr->resumer != NULL); | |
1356 | DUK_ASSERT(thr->resumer->callstack_top >= 2); /* Ecmascript activation + Duktape.Thread.resume() activation */ | |
1357 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1) != NULL && | |
1358 | DUK_HOBJECT_IS_NATIVEFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1)) && | |
1359 | ((duk_hnativefunction *) DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 1))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */ | |
1360 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2) != NULL && | |
1361 | DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->resumer->callstack + thr->resumer->callstack_top - 2))); /* an Ecmascript function */ | |
1362 | ||
1363 | resumer = thr->resumer; | |
1364 | ||
1365 | /* reset longjmp */ | |
1366 | ||
1367 | DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW); /* already set */ | |
1368 | /* lj.value1 already set */ | |
1369 | ||
1370 | duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */ | |
1371 | DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED); | |
1372 | ||
1373 | thr->resumer = NULL; | |
1374 | resumer->state = DUK_HTHREAD_STATE_RUNNING; | |
1375 | DUK_HEAP_SWITCH_THREAD(thr->heap, resumer); | |
1376 | thr = resumer; | |
1377 | goto check_longjmp; | |
1378 | } | |
1379 | ||
1380 | case DUK_LJ_TYPE_NORMAL: { | |
1381 | DUK_D(DUK_DPRINT("caught DUK_LJ_TYPE_NORMAL, should never happen, treat as internal error")); | |
1382 | goto convert_to_internal_error; | |
1383 | } | |
1384 | ||
1385 | default: { | |
1386 | /* should never happen, but be robust */ | |
1387 | DUK_D(DUK_DPRINT("caught unknown longjmp type %ld, treat as internal error", (long) thr->heap->lj.type)); | |
1388 | goto convert_to_internal_error; | |
1389 | } | |
1390 | ||
1391 | } /* end switch */ | |
1392 | ||
1393 | DUK_UNREACHABLE(); | |
1394 | ||
1395 | wipe_and_return: | |
1396 | /* this is not strictly necessary, but helps debugging */ | |
1397 | thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN; | |
1398 | thr->heap->lj.iserror = 0; | |
1399 | ||
1400 | DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value1); | |
1401 | DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value1); | |
1402 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
1403 | ||
1404 | DUK_TVAL_SET_TVAL(&tv_tmp, &thr->heap->lj.value2); | |
1405 | DUK_TVAL_SET_UNDEFINED_UNUSED(&thr->heap->lj.value2); | |
1406 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
1407 | ||
1408 | just_return: | |
1409 | return retval; | |
1410 | ||
1411 | convert_to_internal_error: | |
1412 | /* This could also be thrown internally (set the error, goto check_longjmp), | |
1413 | * but it's better for internal errors to bubble outwards. | |
1414 | */ | |
1415 | DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR_EXEC_LONGJMP); | |
1416 | DUK_UNREACHABLE(); | |
1417 | return retval; | |
1418 | } | |
1419 | ||
1420 | /* XXX: Disabled for 1.0 release. This needs to handle unwinding for label | |
1421 | * sites (which are created for explicit labels but also for control statements | |
1422 | * like for-loops). At that point it's quite close to the "slow return" handler | |
1423 | * except for longjmp(). Perhaps all returns could initially be handled as fast | |
1424 | * returns and only converted to longjmp()s when basic handling won't do? | |
1425 | */ | |
1426 | #if 0 | |
1427 | /* Try a fast return. Return false if fails, so that a slow return can be done | |
1428 | * instead. | |
1429 | */ | |
1430 | DUK_LOCAL | |
1431 | duk_bool_t duk__handle_fast_return(duk_hthread *thr, | |
1432 | duk_tval *tv_retval, | |
1433 | duk_hthread *entry_thread, | |
1434 | duk_size_t entry_callstack_top) { | |
1435 | duk_tval tv_tmp; | |
1436 | duk_tval *tv1; | |
1437 | ||
1438 | /* retval == NULL indicates 'undefined' return value */ | |
1439 | ||
1440 | if (thr == entry_thread && thr->callstack_top == entry_callstack_top) { | |
1441 | DUK_DDD(DUK_DDDPRINT("reject fast return: return would exit bytecode executor to caller")); | |
1442 | return 0; | |
1443 | } | |
1444 | if (thr->callstack_top <= 1) { | |
1445 | DUK_DDD(DUK_DDDPRINT("reject fast return: there is no caller in this callstack (thread yield)")); | |
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | /* There is a caller, and it must be an Ecmascript caller (otherwise | |
1450 | * it would have matched the entry level check). | |
1451 | */ | |
1452 | DUK_ASSERT(thr->callstack_top >= 2); | |
1453 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 2))); /* must be ecmascript */ | |
1454 | ||
1455 | tv1 = thr->valstack + (thr->callstack + thr->callstack_top - 2)->idx_retval; | |
1456 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
1457 | if (tv_retval) { | |
1458 | DUK_TVAL_SET_TVAL(tv1, tv_retval); | |
1459 | DUK_TVAL_INCREF(thr, tv1); | |
1460 | } else { | |
1461 | DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1); | |
1462 | /* no need to incref */ | |
1463 | } | |
1464 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
1465 | ||
1466 | /* No catchstack to unwind. */ | |
1467 | #if 0 | |
1468 | duk_hthread_catchstack_unwind(thr, (cat - thr->catchstack) + 1); /* leave 'cat' as top catcher (also works if catchstack exhausted) */ | |
1469 | #endif | |
1470 | duk_hthread_callstack_unwind(thr, thr->callstack_top - 1); | |
1471 | duk__reconfig_valstack(thr, thr->callstack_top - 1, 1); /* new top, i.e. callee */ | |
1472 | ||
1473 | DUK_DDD(DUK_DDDPRINT("fast return accepted")); | |
1474 | return 1; | |
1475 | } | |
1476 | #endif | |
1477 | ||
1478 | /* | |
1479 | * Executor interrupt handling | |
1480 | * | |
1481 | * The handler is called whenever the interrupt countdown reaches zero | |
1482 | * (or below). The handler must perform whatever checks are activated, | |
1483 | * e.g. check for cumulative step count to impose an execution step | |
1484 | * limit or check for breakpoints or other debugger interaction. | |
1485 | * | |
1486 | * When the actions are done, the handler must reinit the interrupt | |
1487 | * init and counter values. The 'init' value must indicate how many | |
1488 | * bytecode instructions are executed before the next interrupt. The | |
1489 | * counter must interface with the bytecode executor loop. Concretely, | |
1490 | * the new init value is normally one higher than the new counter value. | |
1491 | * For instance, to execute exactly one bytecode instruction the init | |
1492 | * value is set to 1 and the counter to 0. If an error is thrown by the | |
1493 | * interrupt handler, the counters are set to the same value (e.g. both | |
1494 | * to 0 to cause an interrupt when the next bytecode instruction is about | |
1495 | * to be executed after error handling). | |
1496 | * | |
1497 | * Maintaining the init/counter value properly is important for accurate | |
1498 | * behavior. For instance, executor step limit needs a cumulative step | |
1499 | * count which is simply computed as a sum of 'init' values. This must | |
1500 | * work accurately even when single stepping. | |
1501 | */ | |
1502 | ||
1503 | #if defined(DUK_USE_INTERRUPT_COUNTER) | |
1504 | ||
1505 | #define DUK__INT_NOACTION 0 /* no specific action, resume normal execution */ | |
1506 | #define DUK__INT_RESTART 1 /* must "goto restart_execution", e.g. breakpoints changed */ | |
1507 | ||
1508 | #if defined(DUK_USE_DEBUGGER_SUPPORT) | |
1509 | DUK_LOCAL void duk__interrupt_handle_debugger(duk_hthread *thr, duk_bool_t *out_immediate, duk_small_uint_t *out_interrupt_retval) { | |
1510 | duk_context *ctx; | |
1511 | duk_activation *act; | |
1512 | duk_breakpoint *bp; | |
1513 | duk_breakpoint **bp_active; | |
1514 | duk_uint_fast32_t line = 0; | |
1515 | duk_bool_t send_status; | |
1516 | duk_bool_t process_messages; | |
1517 | duk_bool_t processed_messages = 0; | |
1518 | ||
1519 | ctx = (duk_context *) thr; | |
1520 | act = thr->callstack + thr->callstack_top - 1; | |
1521 | ||
1522 | /* It might seem that replacing 'thr->heap' with just 'heap' below | |
1523 | * might be a good idea, but it increases code size slightly | |
1524 | * (probably due to unnecessary spilling) at least on x64. | |
1525 | */ | |
1526 | ||
1527 | /* | |
1528 | * Breakpoint and step state checks | |
1529 | */ | |
1530 | ||
1531 | if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE || | |
1532 | (thr->heap->dbg_step_thread == thr && | |
1533 | thr->heap->dbg_step_csindex == thr->callstack_top - 1)) { | |
1534 | line = duk_debug_curr_line(thr); | |
1535 | ||
1536 | if (act->prev_line != line) { | |
1537 | /* Stepped? Step out is handled by callstack unwind. */ | |
1538 | if ((thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO || | |
1539 | thr->heap->dbg_step_type == DUK_STEP_TYPE_OVER) && | |
1540 | (thr->heap->dbg_step_thread == thr) && | |
1541 | (thr->heap->dbg_step_csindex == thr->callstack_top - 1) && | |
1542 | (line != thr->heap->dbg_step_startline)) { | |
1543 | DUK_D(DUK_DPRINT("STEP STATE TRIGGERED PAUSE at line %ld", | |
1544 | (long) line)); | |
1545 | ||
1546 | DUK_HEAP_SET_PAUSED(thr->heap); | |
1547 | } | |
1548 | ||
1549 | /* Check for breakpoints only on line transition. | |
1550 | * Breakpoint is triggered when we enter the target | |
1551 | * line from a different line, and the previous line | |
1552 | * was within the same function. | |
1553 | * | |
1554 | * This condition is tricky: the condition used to be | |
1555 | * that transition to -or across- the breakpoint line | |
1556 | * triggered the breakpoint. This seems intuitively | |
1557 | * better because it handles breakpoints on lines with | |
1558 | * no emitted opcodes; but this leads to the issue | |
1559 | * described in: https://github.com/svaarala/duktape/issues/263. | |
1560 | */ | |
1561 | bp_active = thr->heap->dbg_breakpoints_active; | |
1562 | for (;;) { | |
1563 | bp = *bp_active++; | |
1564 | if (bp == NULL) { | |
1565 | break; | |
1566 | } | |
1567 | ||
1568 | DUK_ASSERT(bp->filename != NULL); | |
1569 | if (act->prev_line != bp->line && line == bp->line) { | |
1570 | DUK_D(DUK_DPRINT("BREAKPOINT TRIGGERED at %!O:%ld", | |
1571 | (duk_heaphdr *) bp->filename, (long) bp->line)); | |
1572 | ||
1573 | DUK_HEAP_SET_PAUSED(thr->heap); | |
1574 | } | |
1575 | } | |
1576 | } else { | |
1577 | ; | |
1578 | } | |
1579 | ||
1580 | act->prev_line = line; | |
1581 | } | |
1582 | ||
1583 | /* | |
1584 | * Rate limit check for sending status update or peeking into | |
1585 | * the debug transport. Both can be expensive operations that | |
1586 | * we don't want to do on every opcode. | |
1587 | * | |
1588 | * Making sure the interval remains reasonable on a wide variety | |
1589 | * of targets and bytecode is difficult without a timestamp, so | |
1590 | * we use a Date-provided timestamp for the rate limit check. | |
1591 | * But since it's also expensive to get a timestamp, a bytecode | |
1592 | * counter is used to rate limit getting timestamps. | |
1593 | */ | |
1594 | ||
1595 | if (thr->heap->dbg_state_dirty || thr->heap->dbg_paused) { | |
1596 | send_status = 1; | |
1597 | } else { | |
1598 | send_status = 0; | |
1599 | } | |
1600 | ||
1601 | if (thr->heap->dbg_paused) { | |
1602 | process_messages = 1; | |
1603 | } else { | |
1604 | process_messages = 0; | |
1605 | } | |
1606 | ||
1607 | /* XXX: remove heap->dbg_exec_counter, use heap->inst_count_interrupt instead? */ | |
1608 | thr->heap->dbg_exec_counter += thr->interrupt_init; | |
1609 | if (thr->heap->dbg_exec_counter - thr->heap->dbg_last_counter >= DUK_HEAP_DBG_RATELIMIT_OPCODES) { | |
1610 | /* Overflow of the execution counter is fine and doesn't break | |
1611 | * anything here. | |
1612 | */ | |
1613 | ||
1614 | duk_double_t now, diff_last; | |
1615 | ||
1616 | thr->heap->dbg_last_counter = thr->heap->dbg_exec_counter; | |
1617 | now = DUK_USE_DATE_GET_NOW(ctx); | |
1618 | ||
1619 | diff_last = now - thr->heap->dbg_last_time; | |
1620 | if (diff_last < 0.0 || diff_last >= (duk_double_t) DUK_HEAP_DBG_RATELIMIT_MILLISECS) { | |
1621 | /* Negative value checked so that a "time jump" works | |
1622 | * reasonably. | |
1623 | * | |
1624 | * Same interval is now used for status sending and | |
1625 | * peeking. | |
1626 | */ | |
1627 | ||
1628 | thr->heap->dbg_last_time = now; | |
1629 | send_status = 1; | |
1630 | process_messages = 1; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | /* | |
1635 | * Send status | |
1636 | */ | |
1637 | ||
1638 | act = NULL; /* may be changed */ | |
1639 | if (send_status) { | |
1640 | duk_debug_send_status(thr); | |
1641 | thr->heap->dbg_state_dirty = 0; | |
1642 | } | |
1643 | ||
1644 | /* | |
1645 | * Process messages. If we're paused, we'll block for new messages. | |
1646 | * if we're not paused, we'll process anything we can peek but won't | |
1647 | * block for more. | |
1648 | */ | |
1649 | ||
1650 | if (process_messages) { | |
1651 | processed_messages = duk_debug_process_messages(thr, 0 /*no_block*/); | |
1652 | } | |
1653 | ||
1654 | /* XXX: any case here where we need to re-send status? */ | |
1655 | ||
1656 | /* Continue checked execution if there are breakpoints or we're stepping. | |
1657 | * Also use checked execution if paused flag is active - it shouldn't be | |
1658 | * because the debug message loop shouldn't terminate if it was. Step out | |
1659 | * is handled by callstack unwind and doesn't need checked execution. | |
1660 | * Note that debugger may have detached due to error or explicit request | |
1661 | * above, so we must recheck attach status. | |
1662 | */ | |
1663 | ||
1664 | if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { | |
1665 | act = thr->callstack + thr->callstack_top - 1; /* relookup, may have changed */ | |
1666 | if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE || | |
1667 | ((thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO || | |
1668 | thr->heap->dbg_step_type == DUK_STEP_TYPE_OVER) && | |
1669 | thr->heap->dbg_step_thread == thr && | |
1670 | thr->heap->dbg_step_csindex == thr->callstack_top - 1) || | |
1671 | thr->heap->dbg_paused) { | |
1672 | *out_immediate = 1; | |
1673 | } | |
1674 | ||
1675 | /* If we processed any debug messages breakpoints may have | |
1676 | * changed; restart execution to re-check active breakpoints. | |
1677 | */ | |
1678 | if (processed_messages) { | |
1679 | DUK_D(DUK_DPRINT("processed debug messages, restart execution to recheck possibly changed breakpoints")); | |
1680 | *out_interrupt_retval = DUK__INT_RESTART; | |
1681 | } | |
1682 | } else { | |
1683 | DUK_D(DUK_DPRINT("debugger became detached, resume normal execution")); | |
1684 | } | |
1685 | } | |
1686 | #endif /* DUK_USE_DEBUGGER_SUPPORT */ | |
1687 | ||
1688 | DUK_LOCAL duk_small_uint_t duk__executor_interrupt(duk_hthread *thr) { | |
1689 | duk_int_t ctr; | |
1690 | duk_activation *act; | |
1691 | duk_hcompiledfunction *fun; | |
1692 | duk_bool_t immediate = 0; | |
1693 | duk_small_uint_t retval; | |
1694 | ||
1695 | DUK_ASSERT(thr != NULL); | |
1696 | DUK_ASSERT(thr->heap != NULL); | |
1697 | DUK_ASSERT(thr->callstack != NULL); | |
1698 | DUK_ASSERT(thr->callstack_top > 0); | |
1699 | ||
1700 | #if defined(DUK_USE_DEBUG) | |
1701 | thr->heap->inst_count_interrupt += thr->interrupt_init; | |
1702 | DUK_DD(DUK_DDPRINT("execution interrupt, counter=%ld, init=%ld, " | |
1703 | "instruction counts: executor=%ld, interrupt=%ld", | |
1704 | (long) thr->interrupt_counter, (long) thr->interrupt_init, | |
1705 | (long) thr->heap->inst_count_exec, (long) thr->heap->inst_count_interrupt)); | |
1706 | #endif | |
1707 | ||
1708 | retval = DUK__INT_NOACTION; | |
1709 | ctr = DUK_HTHREAD_INTCTR_DEFAULT; | |
1710 | ||
1711 | /* | |
1712 | * Avoid nested calls. Concretely this happens during debugging, e.g. | |
1713 | * when we eval() an expression. | |
1714 | */ | |
1715 | ||
1716 | if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap)) { | |
1717 | DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring")); | |
1718 | ||
1719 | /* Set a high interrupt counter; the original executor | |
1720 | * interrupt invocation will rewrite before exiting. | |
1721 | */ | |
1722 | thr->interrupt_init = ctr; | |
1723 | thr->interrupt_counter = ctr - 1; | |
1724 | return DUK__INT_NOACTION; | |
1725 | } | |
1726 | DUK_HEAP_SET_INTERRUPT_RUNNING(thr->heap); | |
1727 | ||
1728 | act = thr->callstack + thr->callstack_top - 1; | |
1729 | fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act); | |
1730 | DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION((duk_hobject *) fun)); | |
1731 | DUK_UNREF(fun); | |
1732 | ||
1733 | #if defined(DUK_USE_EXEC_TIMEOUT_CHECK) | |
1734 | /* | |
1735 | * Execution timeout check | |
1736 | */ | |
1737 | ||
1738 | if (DUK_USE_EXEC_TIMEOUT_CHECK(thr->heap->heap_udata)) { | |
1739 | /* Keep throwing an error whenever we get here. The unusual values | |
1740 | * are set this way because no instruction is ever executed, we just | |
1741 | * throw an error until all try/catch/finally and other catchpoints | |
1742 | * have been exhausted. Duktape/C code gets control at each protected | |
1743 | * call but whenever it enters back into Duktape the RangeError gets | |
1744 | * raised. User exec timeout check must consistently indicate a timeout | |
1745 | * until we've fully bubbled out of Duktape. | |
1746 | */ | |
1747 | DUK_D(DUK_DPRINT("execution timeout, throwing a RangeError")); | |
1748 | thr->interrupt_init = 0; | |
1749 | thr->interrupt_counter = 0; | |
1750 | DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap); | |
1751 | DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, "execution timeout"); | |
1752 | } | |
1753 | #endif /* DUK_USE_EXEC_TIMEOUT_CHECK */ | |
1754 | ||
1755 | #if defined(DUK_USE_DEBUGGER_SUPPORT) | |
1756 | if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { | |
1757 | duk__interrupt_handle_debugger(thr, &immediate, &retval); | |
1758 | act = thr->callstack + thr->callstack_top - 1; /* relookup if changed */ | |
1759 | } | |
1760 | #endif /* DUK_USE_DEBUGGER_SUPPORT */ | |
1761 | ||
1762 | /* | |
1763 | * Update the interrupt counter | |
1764 | */ | |
1765 | ||
1766 | if (immediate) { | |
1767 | /* Cause an interrupt after executing one instruction. */ | |
1768 | ctr = 1; | |
1769 | } | |
1770 | ||
1771 | /* The counter value is one less than the init value: init value should | |
1772 | * indicate how many instructions are executed before interrupt. To | |
1773 | * execute 1 instruction (after interrupt handler return), counter must | |
1774 | * be 0. | |
1775 | */ | |
1776 | thr->interrupt_init = ctr; | |
1777 | thr->interrupt_counter = ctr - 1; | |
1778 | DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap); | |
1779 | ||
1780 | return retval; | |
1781 | } | |
1782 | #endif /* DUK_USE_INTERRUPT_COUNTER */ | |
1783 | ||
1784 | /* | |
1785 | * Debugger handling for executor restart | |
1786 | * | |
1787 | * Check for breakpoints, stepping, etc, and figure out if we should execute | |
1788 | * in checked or normal mode. Note that we can't do this when an activation | |
1789 | * is created, because breakpoint status (and stepping status) may change | |
1790 | * later, so we must recheck every time we're executing an activation. | |
1791 | */ | |
1792 | ||
1793 | #if defined(DUK_USE_DEBUGGER_SUPPORT) | |
1794 | DUK_LOCAL void duk__executor_handle_debugger(duk_hthread *thr, duk_activation *act, duk_hcompiledfunction *fun) { | |
1795 | duk_heap *heap; | |
1796 | duk_tval *tv_tmp; | |
1797 | duk_hstring *filename; | |
1798 | duk_small_uint_t bp_idx; | |
1799 | duk_breakpoint **bp_active; | |
1800 | ||
1801 | DUK_ASSERT(thr != NULL); | |
1802 | DUK_ASSERT(act != NULL); | |
1803 | DUK_ASSERT(fun != NULL); | |
1804 | ||
1805 | heap = thr->heap; | |
1806 | bp_active = heap->dbg_breakpoints_active; | |
1807 | act->flags &= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE; | |
1808 | ||
1809 | tv_tmp = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) fun, DUK_HTHREAD_STRING_FILE_NAME(thr)); | |
1810 | if (tv_tmp && DUK_TVAL_IS_STRING(tv_tmp)) { | |
1811 | filename = DUK_TVAL_GET_STRING(tv_tmp); | |
1812 | ||
1813 | /* Figure out all active breakpoints. A breakpoint is | |
1814 | * considered active if the current function's fileName | |
1815 | * matches the breakpoint's fileName, AND there is no | |
1816 | * inner function that has matching line numbers | |
1817 | * (otherwise a breakpoint would be triggered both | |
1818 | * inside and outside of the inner function which would | |
1819 | * be confusing). Example: | |
1820 | * | |
1821 | * function foo() { | |
1822 | * print('foo'); | |
1823 | * function bar() { <-. breakpoints in these | |
1824 | * print('bar'); | lines should not affect | |
1825 | * } <-' foo() execution | |
1826 | * bar(); | |
1827 | * } | |
1828 | * | |
1829 | * We need a few things that are only available when | |
1830 | * debugger support is enabled: (1) a line range for | |
1831 | * each function, and (2) access to the function | |
1832 | * template to access the inner functions (and their | |
1833 | * line ranges). | |
1834 | * | |
1835 | * It's important to have a narrow match for active | |
1836 | * breakpoints so that we don't enter checked execution | |
1837 | * when that's not necessary. For instance, if we're | |
1838 | * running inside a certain function and there's | |
1839 | * breakpoint outside in (after the call site), we | |
1840 | * don't want to slow down execution of the function. | |
1841 | */ | |
1842 | ||
1843 | for (bp_idx = 0; bp_idx < heap->dbg_breakpoint_count; bp_idx++) { | |
1844 | duk_breakpoint *bp = heap->dbg_breakpoints + bp_idx; | |
1845 | duk_hobject **funcs, **funcs_end; | |
1846 | duk_hcompiledfunction *inner_fun; | |
1847 | duk_bool_t bp_match; | |
1848 | ||
1849 | if (bp->filename == filename && | |
1850 | bp->line >= fun->start_line && bp->line <= fun->end_line) { | |
1851 | bp_match = 1; | |
1852 | DUK_DD(DUK_DDPRINT("breakpoint filename and line match: " | |
1853 | "%s:%ld vs. %s (line %ld vs. %ld-%ld)", | |
1854 | DUK_HSTRING_GET_DATA(bp->filename), | |
1855 | (long) bp->line, | |
1856 | DUK_HSTRING_GET_DATA(filename), | |
1857 | (long) bp->line, | |
1858 | (long) fun->start_line, | |
1859 | (long) fun->end_line)); | |
1860 | ||
1861 | funcs = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun); | |
1862 | funcs_end = DUK_HCOMPILEDFUNCTION_GET_FUNCS_END(thr->heap, fun); | |
1863 | while (funcs != funcs_end) { | |
1864 | inner_fun = (duk_hcompiledfunction *) *funcs; | |
1865 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) inner_fun)); | |
1866 | if (bp->line >= inner_fun->start_line && bp->line <= inner_fun->end_line) { | |
1867 | DUK_DD(DUK_DDPRINT("inner function masks ('captures') breakpoint")); | |
1868 | bp_match = 0; | |
1869 | break; | |
1870 | } | |
1871 | funcs++; | |
1872 | } | |
1873 | ||
1874 | if (bp_match) { | |
1875 | /* No need to check for size of bp_active list, | |
1876 | * it's always larger than maximum number of | |
1877 | * breakpoints. | |
1878 | */ | |
1879 | act->flags |= DUK_ACT_FLAG_BREAKPOINT_ACTIVE; | |
1880 | *bp_active = heap->dbg_breakpoints + bp_idx; | |
1881 | bp_active++; | |
1882 | } | |
1883 | } | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | *bp_active = NULL; /* terminate */ | |
1888 | ||
1889 | DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active - thr->heap->dbg_breakpoints_active))); | |
1890 | ||
1891 | /* Force pause if we were doing "step into" in another activation. */ | |
1892 | if (thr->heap->dbg_step_thread != NULL && | |
1893 | thr->heap->dbg_step_type == DUK_STEP_TYPE_INTO && | |
1894 | (thr->heap->dbg_step_thread != thr || | |
1895 | thr->heap->dbg_step_csindex != thr->callstack_top - 1)) { | |
1896 | DUK_D(DUK_DPRINT("STEP INTO ACTIVE, FORCE PAUSED")); | |
1897 | DUK_HEAP_SET_PAUSED(thr->heap); | |
1898 | } | |
1899 | ||
1900 | /* Force interrupt right away if we're paused or in "checked mode". | |
1901 | * Step out is handled by callstack unwind. | |
1902 | */ | |
1903 | if (act->flags & (DUK_ACT_FLAG_BREAKPOINT_ACTIVE) || | |
1904 | thr->heap->dbg_paused || | |
1905 | (thr->heap->dbg_step_type != DUK_STEP_TYPE_OUT && | |
1906 | thr->heap->dbg_step_csindex == thr->callstack_top - 1)) { | |
1907 | /* We'll need to interrupt early so recompute the init | |
1908 | * counter to reflect the number of bytecode instructions | |
1909 | * executed so that step counts for e.g. debugger rate | |
1910 | * limiting are accurate. | |
1911 | */ | |
1912 | DUK_ASSERT(thr->interrupt_counter <= thr->interrupt_init); | |
1913 | thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter; | |
1914 | thr->interrupt_counter = 0; | |
1915 | } | |
1916 | } | |
1917 | #endif /* DUK_USE_DEBUGGER_SUPPORT */ | |
1918 | ||
1919 | /* | |
1920 | * Ecmascript bytecode executor. | |
1921 | * | |
1922 | * Resume execution for the current thread from its current activation. | |
1923 | * Returns when execution would return from the entry level activation, | |
1924 | * leaving a single return value on top of the stack. Function calls | |
1925 | * and thread resumptions are handled internally. If an error occurs, | |
1926 | * a longjmp() with type DUK_LJ_TYPE_THROW is called on the entry level | |
1927 | * setjmp() jmpbuf. | |
1928 | * | |
1929 | * Ecmascript function calls and coroutine resumptions are handled | |
1930 | * internally without recursive C calls. Other function calls are | |
1931 | * handled using duk_handle_call(), increasing C recursion depth. | |
1932 | * | |
1933 | * There are many other tricky control flow situations, such as: | |
1934 | * | |
1935 | * - Break and continue (fast and slow) | |
1936 | * - Return (fast and slow) | |
1937 | * - Error throwing | |
1938 | * - Thread resume and yield | |
1939 | * | |
1940 | * For more detailed notes, see doc/execution.rst. | |
1941 | * | |
1942 | * Also see doc/code-issues.rst for discussion of setjmp(), longjmp(), | |
1943 | * and volatile. | |
1944 | */ | |
1945 | ||
1946 | #define DUK__STRICT() (DUK_HOBJECT_HAS_STRICT(&(fun)->obj)) | |
1947 | #define DUK__REG(x) (*(thr->valstack_bottom + (x))) | |
1948 | #define DUK__REGP(x) (thr->valstack_bottom + (x)) | |
1949 | #define DUK__CONST(x) (*(consts + (x))) | |
1950 | #define DUK__CONSTP(x) (consts + (x)) | |
1951 | #define DUK__REGCONST(x) ((x) < DUK_BC_REGLIMIT ? DUK__REG((x)) : DUK__CONST((x) - DUK_BC_REGLIMIT)) | |
1952 | #define DUK__REGCONSTP(x) ((x) < DUK_BC_REGLIMIT ? DUK__REGP((x)) : DUK__CONSTP((x) - DUK_BC_REGLIMIT)) | |
1953 | ||
1954 | #ifdef DUK_USE_VERBOSE_EXECUTOR_ERRORS | |
1955 | #define DUK__INTERNAL_ERROR(msg) do { \ | |
1956 | DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, (msg)); \ | |
1957 | } while (0) | |
1958 | #else | |
1959 | #define DUK__INTERNAL_ERROR(msg) do { \ | |
1960 | goto internal_error; \ | |
1961 | } while (0) | |
1962 | #endif | |
1963 | ||
1964 | #define DUK__SYNC_CURR_PC() do { \ | |
1965 | duk_activation *act; \ | |
1966 | act = thr->callstack + thr->callstack_top - 1; \ | |
1967 | act->curr_pc = curr_pc; \ | |
1968 | } while (0) | |
1969 | #define DUK__SYNC_AND_NULL_CURR_PC() do { \ | |
1970 | duk_activation *act; \ | |
1971 | act = thr->callstack + thr->callstack_top - 1; \ | |
1972 | act->curr_pc = curr_pc; \ | |
1973 | thr->ptr_curr_pc = NULL; \ | |
1974 | } while (0) | |
1975 | ||
1976 | DUK_INTERNAL void duk_js_execute_bytecode(duk_hthread *exec_thr) { | |
1977 | /* Entry level info. Although these are assigned to before setjmp() | |
1978 | * a 'volatile' seems to be needed. Note placement of "volatile" for | |
1979 | * pointers. See doc/code-issues.rst for more discussion. | |
1980 | */ | |
1981 | duk_hthread * volatile entry_thread; /* volatile copy of exec_thr */ | |
1982 | volatile duk_size_t entry_callstack_top; | |
1983 | volatile duk_int_t entry_call_recursion_depth; | |
1984 | duk_jmpbuf * volatile entry_jmpbuf_ptr; | |
1985 | ||
1986 | /* current PC, volatile because it is accessed by other functions | |
1987 | * through thr->ptr_to_curr_pc. Critical for performance. It would | |
1988 | * be safest to make this volatile, but that eliminates performance | |
1989 | * benefits. Aliasing guarantees should be enough though. | |
1990 | */ | |
1991 | duk_instr_t *curr_pc; /* stable */ | |
1992 | ||
1993 | /* "hot" variables for interpretation -- not volatile, value not guaranteed in setjmp error handling */ | |
1994 | duk_hthread *thr; /* stable */ | |
1995 | duk_hcompiledfunction *fun; /* stable */ | |
1996 | duk_tval *consts; /* stable */ | |
1997 | /* 'funcs' is quite rarely used, so no local for it */ | |
1998 | ||
1999 | /* "hot" temps for interpretation -- not volatile, value not guaranteed in setjmp error handling */ | |
2000 | duk_uint_fast32_t ins; /* XXX: check performance impact on x64 between fast/non-fast variant */ | |
2001 | ||
2002 | /* jmpbuf */ | |
2003 | duk_jmpbuf jmpbuf; | |
2004 | ||
2005 | #ifdef DUK_USE_INTERRUPT_COUNTER | |
2006 | duk_int_t int_ctr; | |
2007 | #endif | |
2008 | ||
2009 | #ifdef DUK_USE_ASSERTIONS | |
2010 | duk_size_t valstack_top_base; /* valstack top, should match before interpreting each op (no leftovers) */ | |
2011 | #endif | |
2012 | ||
2013 | /* XXX: document assumptions on setjmp and volatile variables | |
2014 | * (see duk_handle_call()). | |
2015 | */ | |
2016 | ||
2017 | /* | |
2018 | * Preliminaries | |
2019 | */ | |
2020 | ||
2021 | DUK_ASSERT(exec_thr != NULL); | |
2022 | DUK_ASSERT(exec_thr->heap != NULL); | |
2023 | DUK_ASSERT(exec_thr->heap->curr_thread != NULL); | |
2024 | DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR((duk_heaphdr *) exec_thr); | |
2025 | DUK_ASSERT(exec_thr->callstack_top >= 1); /* at least one activation, ours */ | |
2026 | DUK_ASSERT(DUK_ACT_GET_FUNC(exec_thr->callstack + exec_thr->callstack_top - 1) != NULL); | |
2027 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(exec_thr->callstack + exec_thr->callstack_top - 1))); | |
2028 | ||
2029 | entry_thread = exec_thr; /* volatile copy */ | |
2030 | thr = (duk_hthread *) entry_thread; | |
2031 | entry_callstack_top = thr->callstack_top; | |
2032 | entry_call_recursion_depth = thr->heap->call_recursion_depth; | |
2033 | entry_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr; | |
2034 | ||
2035 | /* | |
2036 | * Setjmp catchpoint setup. | |
2037 | * | |
2038 | * Note: we currently assume that the setjmp() catchpoint is | |
2039 | * not re-entrant (longjmp() cannot be called more than once | |
2040 | * for a single setjmp()). | |
2041 | */ | |
2042 | ||
2043 | reset_setjmp_catchpoint: | |
2044 | ||
2045 | DUK_ASSERT(thr != NULL); | |
2046 | thr->heap->lj.jmpbuf_ptr = &jmpbuf; | |
2047 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); | |
2048 | ||
2049 | if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb)) { | |
2050 | /* | |
2051 | * Note: any local variables accessed here must have their value | |
2052 | * assigned *before* the setjmp() call, OR they must be declared | |
2053 | * volatile. Otherwise their value is not guaranteed to be correct. | |
2054 | * | |
2055 | * 'thr' might seem to be a risky variable because it is changed | |
2056 | * for yield and resume. However, yield and resume are handled | |
2057 | * using longjmp()s. | |
2058 | */ | |
2059 | ||
2060 | duk_small_uint_t lj_ret; | |
2061 | duk_hthread *tmp_entry_thread; | |
2062 | duk_size_t tmp_entry_callstack_top; | |
2063 | ||
2064 | DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor")); | |
2065 | ||
2066 | /* Relookup 'thr': it's not volatile so its value is not | |
2067 | * guaranteed. The heap->curr_thread value should always be | |
2068 | * valid here because longjmp callers don't switch threads, | |
2069 | * only the longjmp handler does that (even for RESUME and | |
2070 | * YIELD). | |
2071 | */ | |
2072 | DUK_ASSERT(entry_thread != NULL); | |
2073 | thr = entry_thread->heap->curr_thread; | |
2074 | ||
2075 | /* Don't sync curr_pc when unwinding: with recursive executor | |
2076 | * calls thr->ptr_curr_pc may be dangling. | |
2077 | */ | |
2078 | ||
2079 | /* XXX: signalling the need to shrink check (only if unwound) */ | |
2080 | ||
2081 | /* Must be restored here to handle e.g. yields properly. */ | |
2082 | thr->heap->call_recursion_depth = entry_call_recursion_depth; | |
2083 | ||
2084 | /* Switch to caller's setjmp() catcher so that if an error occurs | |
2085 | * during error handling, it is always propagated outwards instead | |
2086 | * of causing an infinite loop in our own handler. | |
2087 | */ | |
2088 | ||
2089 | DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p", | |
2090 | (void *) ((thr && thr->heap) ? thr->heap->lj.jmpbuf_ptr : NULL), | |
2091 | (void *) entry_jmpbuf_ptr)); | |
2092 | thr->heap->lj.jmpbuf_ptr = (duk_jmpbuf *) entry_jmpbuf_ptr; | |
2093 | ||
2094 | tmp_entry_thread = (duk_hthread *) entry_thread; /* use temps to avoid GH-318 */ | |
2095 | tmp_entry_callstack_top = (duk_size_t) entry_callstack_top; | |
2096 | lj_ret = duk__handle_longjmp(thr, tmp_entry_thread, tmp_entry_callstack_top); | |
2097 | ||
2098 | if (lj_ret == DUK__LONGJMP_RESTART) { | |
2099 | /* | |
2100 | * Restart bytecode execution, possibly with a changed thread. | |
2101 | */ | |
2102 | thr = thr->heap->curr_thread; | |
2103 | goto reset_setjmp_catchpoint; | |
2104 | } else if (lj_ret == DUK__LONGJMP_RETHROW) { | |
2105 | /* | |
2106 | * Rethrow error to calling state. | |
2107 | */ | |
2108 | ||
2109 | /* thread may have changed (e.g. YIELD converted to THROW) */ | |
2110 | thr = thr->heap->curr_thread; | |
2111 | ||
2112 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr); | |
2113 | ||
2114 | duk_err_longjmp(thr); | |
2115 | DUK_UNREACHABLE(); | |
2116 | } else { | |
2117 | /* | |
2118 | * Return from bytecode executor with a return value. | |
2119 | */ | |
2120 | DUK_ASSERT(lj_ret == DUK__LONGJMP_FINISHED); | |
2121 | /* XXX: return assertions for valstack, callstack, catchstack */ | |
2122 | ||
2123 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr); | |
2124 | return; | |
2125 | } | |
2126 | DUK_UNREACHABLE(); | |
2127 | } | |
2128 | ||
2129 | /* | |
2130 | * Restart execution by reloading thread state. | |
2131 | * | |
2132 | * Note that 'thr' and any thread configuration may have changed, | |
2133 | * so all local variables are suspect. | |
2134 | * | |
2135 | * The number of local variables should be kept to a minimum: if | |
2136 | * the variables are spilled, they will need to be loaded from | |
2137 | * memory anyway. | |
2138 | * | |
2139 | * Any 'goto restart_execution;' code path in opcode dispatch must | |
2140 | * ensure 'curr_pc' is synced back to act->curr_pc before the goto | |
2141 | * takes place. | |
2142 | */ | |
2143 | ||
2144 | restart_execution: | |
2145 | ||
2146 | /* Lookup current thread; use the volatile 'entry_thread' for this to | |
2147 | * avoid clobber warnings. (Any valid, reachable 'thr' value would be | |
2148 | * fine for this, so using 'entry_thread' is just to silence warnings.) | |
2149 | */ | |
2150 | thr = entry_thread->heap->curr_thread; | |
2151 | DUK_ASSERT(thr != NULL); | |
2152 | DUK_ASSERT(thr->callstack_top >= 1); | |
2153 | DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1) != NULL); | |
2154 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(DUK_ACT_GET_FUNC(thr->callstack + thr->callstack_top - 1))); | |
2155 | ||
2156 | thr->ptr_curr_pc = &curr_pc; | |
2157 | ||
2158 | /* Assume interrupt init/counter are properly initialized here. */ | |
2159 | ||
2160 | /* assume that thr->valstack_bottom has been set-up before getting here */ | |
2161 | { | |
2162 | duk_activation *act; | |
2163 | ||
2164 | act = thr->callstack + thr->callstack_top - 1; | |
2165 | fun = (duk_hcompiledfunction *) DUK_ACT_GET_FUNC(act); | |
2166 | DUK_ASSERT(fun != NULL); | |
2167 | DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == fun->nregs); | |
2168 | consts = DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, fun); | |
2169 | DUK_ASSERT(consts != NULL); | |
2170 | } | |
2171 | ||
2172 | #if defined(DUK_USE_DEBUGGER_SUPPORT) | |
2173 | if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap) && !thr->heap->dbg_processing) { | |
2174 | duk_activation *act; | |
2175 | ||
2176 | thr->heap->dbg_processing = 1; | |
2177 | act = thr->callstack + thr->callstack_top - 1; | |
2178 | duk__executor_handle_debugger(thr, act, fun); | |
2179 | thr->heap->dbg_processing = 0; | |
2180 | } | |
2181 | #endif /* DUK_USE_DEBUGGER_SUPPORT */ | |
2182 | ||
2183 | /* XXX: shrink check flag? */ | |
2184 | ||
2185 | /* | |
2186 | * Bytecode interpreter. | |
2187 | * | |
2188 | * The interpreter must be very careful with memory pointers, as | |
2189 | * many pointers are not guaranteed to be 'stable' and may be | |
2190 | * reallocated and relocated on-the-fly quite easily (e.g. by a | |
2191 | * memory allocation or a property access). | |
2192 | * | |
2193 | * The following are assumed to have stable pointers: | |
2194 | * - the current thread | |
2195 | * - the current function | |
2196 | * - the bytecode, constant table, inner function table of the | |
2197 | * current function (as they are a part of the function allocation) | |
2198 | * | |
2199 | * The following are assumed to have semi-stable pointers: | |
2200 | * - the current activation entry: stable as long as callstack | |
2201 | * is not changed (reallocated by growing or shrinking), or | |
2202 | * by any garbage collection invocation (through finalizers) | |
2203 | * - Note in particular that ANY DECREF can invalidate the | |
2204 | * activation pointer | |
2205 | * | |
2206 | * The following are not assumed to have stable pointers at all: | |
2207 | * - the value stack (registers) of the current thread | |
2208 | * - the catch stack of the current thread | |
2209 | * | |
2210 | * See execution.rst for discussion. | |
2211 | */ | |
2212 | ||
2213 | DUK_ASSERT(thr != NULL); | |
2214 | DUK_ASSERT(fun != NULL); | |
2215 | ||
2216 | DUK_DD(DUK_DDPRINT("restarting execution, thr %p, act idx %ld, fun %p," | |
2217 | "consts %p, funcs %p, lev %ld, regbot %ld, regtop %ld, catchstack_top=%ld, " | |
2218 | "preventcount=%ld", | |
2219 | (void *) thr, | |
2220 | (long) (thr->callstack_top - 1), | |
2221 | (void *) fun, | |
2222 | (void *) DUK_HCOMPILEDFUNCTION_GET_CONSTS_BASE(thr->heap, fun), | |
2223 | (void *) DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun), | |
2224 | (long) (thr->callstack_top - 1), | |
2225 | (long) (thr->valstack_bottom - thr->valstack), | |
2226 | (long) (thr->valstack_top - thr->valstack), | |
2227 | (long) thr->catchstack_top, | |
2228 | (long) thr->callstack_preventcount)); | |
2229 | ||
2230 | #ifdef DUK_USE_ASSERTIONS | |
2231 | valstack_top_base = (duk_size_t) (thr->valstack_top - thr->valstack); | |
2232 | #endif | |
2233 | ||
2234 | /* Set up curr_pc for opcode dispatch. */ | |
2235 | { | |
2236 | duk_activation *act; | |
2237 | act = thr->callstack + thr->callstack_top - 1; | |
2238 | curr_pc = act->curr_pc; | |
2239 | } | |
2240 | ||
2241 | for (;;) { | |
2242 | DUK_ASSERT(thr->callstack_top >= 1); | |
2243 | DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == fun->nregs); | |
2244 | DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) == valstack_top_base); | |
2245 | ||
2246 | /* Executor interrupt counter check, used to implement breakpoints, | |
2247 | * debugging interface, execution timeouts, etc. The counter is heap | |
2248 | * specific but is maintained in the current thread to make the check | |
2249 | * as fast as possible. The counter is copied back to the heap struct | |
2250 | * whenever a thread switch occurs by the DUK_HEAP_SWITCH_THREAD() macro. | |
2251 | */ | |
2252 | #ifdef DUK_USE_INTERRUPT_COUNTER | |
2253 | int_ctr = thr->interrupt_counter; | |
2254 | if (DUK_LIKELY(int_ctr > 0)) { | |
2255 | thr->interrupt_counter = int_ctr - 1; | |
2256 | } else { | |
2257 | /* Trigger at zero or below */ | |
2258 | duk_small_uint_t exec_int_ret; | |
2259 | ||
2260 | /* Write curr_pc back for the debugger. */ | |
2261 | DUK_ASSERT(thr->callstack_top > 0); | |
2262 | { | |
2263 | duk_activation *act; | |
2264 | act = thr->callstack + thr->callstack_top - 1; | |
2265 | act->curr_pc = (duk_instr_t *) curr_pc; | |
2266 | } | |
2267 | ||
2268 | /* Force restart caused by a function return; must recheck | |
2269 | * debugger breakpoints before checking line transitions, | |
2270 | * see GH-303. Restart and then handle interrupt_counter | |
2271 | * zero again. | |
2272 | */ | |
2273 | #if defined(DUK_USE_DEBUGGER_SUPPORT) | |
2274 | if (thr->heap->dbg_force_restart) { | |
2275 | DUK_DD(DUK_DDPRINT("dbg_force_restart flag forced restart execution")); /* GH-303 */ | |
2276 | thr->heap->dbg_force_restart = 0; | |
2277 | goto restart_execution; | |
2278 | } | |
2279 | #endif | |
2280 | ||
2281 | exec_int_ret = duk__executor_interrupt(thr); | |
2282 | if (exec_int_ret == DUK__INT_RESTART) { | |
2283 | /* curr_pc synced back above */ | |
2284 | goto restart_execution; | |
2285 | } | |
2286 | } | |
2287 | #endif | |
2288 | #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG) | |
2289 | thr->heap->inst_count_exec++; | |
2290 | #endif | |
2291 | ||
2292 | #if defined(DUK_USE_ASSERTIONS) || defined(DUK_USE_DEBUG) | |
2293 | { | |
2294 | duk_activation *act; | |
2295 | act = thr->callstack + thr->callstack_top - 1; | |
2296 | DUK_ASSERT(curr_pc >= DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, fun)); | |
2297 | DUK_ASSERT(curr_pc < DUK_HCOMPILEDFUNCTION_GET_CODE_END(thr->heap, fun)); | |
2298 | DUK_UNREF(act); /* if debugging disabled */ | |
2299 | ||
2300 | DUK_DDD(DUK_DDDPRINT("executing bytecode: pc=%ld, ins=0x%08lx, op=%ld, valstack_top=%ld/%ld, nregs=%ld --> %!I", | |
2301 | (long) (curr_pc - DUK_HCOMPILEDFUNCTION_GET_CODE_BASE(thr->heap, fun)), | |
2302 | (unsigned long) *curr_pc, | |
2303 | (long) DUK_DEC_OP(*curr_pc), | |
2304 | (long) (thr->valstack_top - thr->valstack), | |
2305 | (long) (thr->valstack_end - thr->valstack), | |
2306 | (long) (fun ? fun->nregs : -1), | |
2307 | (duk_instr_t) *curr_pc)); | |
2308 | } | |
2309 | #endif | |
2310 | ||
2311 | #if defined(DUK_USE_ASSERTIONS) | |
2312 | /* Quite heavy assert: check that valstack is in correctly | |
2313 | * initialized state. Improper shuffle instructions can | |
2314 | * write beyond valstack_end so this check catches them in | |
2315 | * the act. | |
2316 | */ | |
2317 | { | |
2318 | duk_tval *tv; | |
2319 | tv = thr->valstack_top; | |
2320 | while (tv != thr->valstack_end) { | |
2321 | DUK_ASSERT(DUK_TVAL_IS_UNDEFINED_UNUSED(tv)); | |
2322 | tv++; | |
2323 | } | |
2324 | } | |
2325 | #endif | |
2326 | ||
2327 | ins = *curr_pc++; | |
2328 | ||
2329 | /* Typing: use duk_small_(u)int_fast_t when decoding small | |
2330 | * opcode fields (op, A, B, C) and duk_(u)int_fast_t when | |
2331 | * decoding larger fields (e.g. BC which is 18 bits). Use | |
2332 | * unsigned variant by default, signed when the value is used | |
2333 | * in signed arithmetic. Using variable names such as 'a', 'b', | |
2334 | * 'c', 'bc', etc makes it easier to spot typing mismatches. | |
2335 | */ | |
2336 | ||
2337 | /* XXX: the best typing needs to be validated by perf measurement: | |
2338 | * e.g. using a small type which is the cast to a larger duk_idx_t | |
2339 | * may be slower than declaring the variable as a duk_idx_t in the | |
2340 | * first place. | |
2341 | */ | |
2342 | ||
2343 | /* XXX: use macros for the repetitive tval/refcount handling. */ | |
2344 | ||
2345 | switch ((int) DUK_DEC_OP(ins)) { | |
2346 | /* XXX: switch cast? */ | |
2347 | ||
2348 | case DUK_OP_LDREG: { | |
2349 | duk_small_uint_fast_t a; | |
2350 | duk_uint_fast_t bc; | |
2351 | duk_tval tv_tmp; | |
2352 | duk_tval *tv1, *tv2; | |
2353 | ||
2354 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2355 | bc = DUK_DEC_BC(ins); tv2 = DUK__REGP(bc); | |
2356 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
2357 | DUK_TVAL_SET_TVAL(tv1, tv2); | |
2358 | DUK_TVAL_INCREF(thr, tv1); | |
2359 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
2360 | break; | |
2361 | } | |
2362 | ||
2363 | case DUK_OP_STREG: { | |
2364 | duk_small_uint_fast_t a; | |
2365 | duk_uint_fast_t bc; | |
2366 | duk_tval tv_tmp; | |
2367 | duk_tval *tv1, *tv2; | |
2368 | ||
2369 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2370 | bc = DUK_DEC_BC(ins); tv2 = DUK__REGP(bc); | |
2371 | DUK_TVAL_SET_TVAL(&tv_tmp, tv2); | |
2372 | DUK_TVAL_SET_TVAL(tv2, tv1); | |
2373 | DUK_TVAL_INCREF(thr, tv2); | |
2374 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
2375 | break; | |
2376 | } | |
2377 | ||
2378 | case DUK_OP_LDCONST: { | |
2379 | duk_small_uint_fast_t a; | |
2380 | duk_uint_fast_t bc; | |
2381 | duk_tval tv_tmp; | |
2382 | duk_tval *tv1, *tv2; | |
2383 | ||
2384 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2385 | bc = DUK_DEC_BC(ins); tv2 = DUK__CONSTP(bc); | |
2386 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
2387 | DUK_TVAL_SET_TVAL(tv1, tv2); | |
2388 | DUK_TVAL_INCREF(thr, tv2); /* may be e.g. string */ | |
2389 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
2390 | break; | |
2391 | } | |
2392 | ||
2393 | case DUK_OP_LDINT: { | |
2394 | duk_small_uint_fast_t a; | |
2395 | duk_int_fast_t bc; | |
2396 | duk_tval tv_tmp; | |
2397 | duk_tval *tv1; | |
2398 | #if defined(DUK_USE_FASTINT) | |
2399 | duk_int32_t val; | |
2400 | #else | |
2401 | duk_double_t val; | |
2402 | #endif | |
2403 | ||
2404 | #if defined(DUK_USE_FASTINT) | |
2405 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2406 | bc = DUK_DEC_BC(ins); val = (duk_int32_t) (bc - DUK_BC_LDINT_BIAS); | |
2407 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
2408 | DUK_TVAL_SET_FASTINT_I32(tv1, val); | |
2409 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
2410 | #else | |
2411 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2412 | bc = DUK_DEC_BC(ins); val = (duk_double_t) (bc - DUK_BC_LDINT_BIAS); | |
2413 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
2414 | DUK_TVAL_SET_NUMBER(tv1, val); | |
2415 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
2416 | #endif | |
2417 | break; | |
2418 | } | |
2419 | ||
2420 | case DUK_OP_LDINTX: { | |
2421 | duk_small_uint_fast_t a; | |
2422 | duk_tval *tv1; | |
2423 | duk_double_t val; | |
2424 | ||
2425 | /* LDINTX is not necessarily in FASTINT range, so | |
2426 | * no fast path for now. | |
2427 | * | |
2428 | * XXX: perhaps restrict LDINTX to fastint range, wider | |
2429 | * range very rarely needed. | |
2430 | */ | |
2431 | ||
2432 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2433 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1)); | |
2434 | val = DUK_TVAL_GET_NUMBER(tv1) * ((duk_double_t) (1L << DUK_BC_LDINTX_SHIFT)) + | |
2435 | (duk_double_t) DUK_DEC_BC(ins); | |
2436 | #if defined(DUK_USE_FASTINT) | |
2437 | DUK_TVAL_SET_NUMBER_CHKFAST(tv1, val); | |
2438 | #else | |
2439 | DUK_TVAL_SET_NUMBER(tv1, val); | |
2440 | #endif | |
2441 | break; | |
2442 | } | |
2443 | ||
2444 | case DUK_OP_MPUTOBJ: | |
2445 | case DUK_OP_MPUTOBJI: { | |
2446 | duk_context *ctx = (duk_context *) thr; | |
2447 | duk_small_uint_fast_t a; | |
2448 | duk_tval *tv1; | |
2449 | duk_hobject *obj; | |
2450 | duk_uint_fast_t idx; | |
2451 | duk_small_uint_fast_t count; | |
2452 | ||
2453 | /* A -> register of target object | |
2454 | * B -> first register of key/value pair list | |
2455 | * C -> number of key/value pairs | |
2456 | */ | |
2457 | ||
2458 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2459 | DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1)); | |
2460 | obj = DUK_TVAL_GET_OBJECT(tv1); | |
2461 | ||
2462 | idx = (duk_uint_fast_t) DUK_DEC_B(ins); | |
2463 | if (DUK_DEC_OP(ins) == DUK_OP_MPUTOBJI) { | |
2464 | duk_tval *tv_ind = DUK__REGP(idx); | |
2465 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
2466 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
2467 | } | |
2468 | ||
2469 | count = (duk_small_uint_fast_t) DUK_DEC_C(ins); | |
2470 | ||
2471 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
2472 | if (DUK_UNLIKELY(idx + count * 2 > (duk_uint_fast_t) duk_get_top(ctx))) { | |
2473 | /* XXX: use duk_is_valid_index() instead? */ | |
2474 | /* XXX: improve check; check against nregs, not against top */ | |
2475 | DUK__INTERNAL_ERROR("MPUTOBJ out of bounds"); | |
2476 | } | |
2477 | #endif | |
2478 | ||
2479 | duk_push_hobject(ctx, obj); | |
2480 | ||
2481 | while (count > 0) { | |
2482 | /* XXX: faster initialization (direct access or better primitives) */ | |
2483 | ||
2484 | duk_push_tval(ctx, DUK__REGP(idx)); | |
2485 | DUK_ASSERT(duk_is_string(ctx, -1)); | |
2486 | duk_push_tval(ctx, DUK__REGP(idx + 1)); /* -> [... obj key value] */ | |
2487 | duk_xdef_prop_wec(ctx, -3); /* -> [... obj] */ | |
2488 | ||
2489 | count--; | |
2490 | idx += 2; | |
2491 | } | |
2492 | ||
2493 | duk_pop(ctx); /* [... obj] -> [...] */ | |
2494 | break; | |
2495 | } | |
2496 | ||
2497 | case DUK_OP_MPUTARR: | |
2498 | case DUK_OP_MPUTARRI: { | |
2499 | duk_context *ctx = (duk_context *) thr; | |
2500 | duk_small_uint_fast_t a; | |
2501 | duk_tval *tv1; | |
2502 | duk_hobject *obj; | |
2503 | duk_uint_fast_t idx; | |
2504 | duk_small_uint_fast_t count; | |
2505 | duk_uint32_t arr_idx; | |
2506 | ||
2507 | /* A -> register of target object | |
2508 | * B -> first register of value data (start_index, value1, value2, ..., valueN) | |
2509 | * C -> number of key/value pairs (N) | |
2510 | */ | |
2511 | ||
2512 | a = DUK_DEC_A(ins); tv1 = DUK__REGP(a); | |
2513 | DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1)); | |
2514 | obj = DUK_TVAL_GET_OBJECT(tv1); | |
2515 | DUK_ASSERT(obj != NULL); | |
2516 | ||
2517 | idx = (duk_uint_fast_t) DUK_DEC_B(ins); | |
2518 | if (DUK_DEC_OP(ins) == DUK_OP_MPUTARRI) { | |
2519 | duk_tval *tv_ind = DUK__REGP(idx); | |
2520 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
2521 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
2522 | } | |
2523 | ||
2524 | count = (duk_small_uint_fast_t) DUK_DEC_C(ins); | |
2525 | ||
2526 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
2527 | if (idx + count + 1 > (duk_uint_fast_t) duk_get_top(ctx)) { | |
2528 | /* XXX: use duk_is_valid_index() instead? */ | |
2529 | /* XXX: improve check; check against nregs, not against top */ | |
2530 | DUK__INTERNAL_ERROR("MPUTARR out of bounds"); | |
2531 | } | |
2532 | #endif | |
2533 | ||
2534 | tv1 = DUK__REGP(idx); | |
2535 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1)); | |
2536 | arr_idx = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1); | |
2537 | idx++; | |
2538 | ||
2539 | duk_push_hobject(ctx, obj); | |
2540 | ||
2541 | while (count > 0) { | |
2542 | /* duk_xdef_prop() will define an own property without any array | |
2543 | * special behaviors. We'll need to set the array length explicitly | |
2544 | * in the end. For arrays with elisions, the compiler will emit an | |
2545 | * explicit SETALEN which will update the length. | |
2546 | */ | |
2547 | ||
2548 | /* XXX: because we're dealing with 'own' properties of a fresh array, | |
2549 | * the array initializer should just ensure that the array has a large | |
2550 | * enough array part and write the values directly into array part, | |
2551 | * and finally set 'length' manually in the end (as already happens now). | |
2552 | */ | |
2553 | ||
2554 | duk_push_tval(ctx, DUK__REGP(idx)); /* -> [... obj value] */ | |
2555 | duk_xdef_prop_index_wec(ctx, -2, arr_idx); /* -> [... obj] */ | |
2556 | ||
2557 | /* XXX: could use at least one fewer loop counters */ | |
2558 | count--; | |
2559 | idx++; | |
2560 | arr_idx++; | |
2561 | } | |
2562 | ||
2563 | /* XXX: E5.1 Section 11.1.4 coerces the final length through | |
2564 | * ToUint32() which is odd but happens now as a side effect of | |
2565 | * 'arr_idx' type. | |
2566 | */ | |
2567 | duk_hobject_set_length(thr, obj, (duk_uint32_t) arr_idx); | |
2568 | ||
2569 | duk_pop(ctx); /* [... obj] -> [...] */ | |
2570 | break; | |
2571 | } | |
2572 | ||
2573 | case DUK_OP_NEW: | |
2574 | case DUK_OP_NEWI: { | |
2575 | duk_context *ctx = (duk_context *) thr; | |
2576 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
2577 | duk_uint_fast_t idx; | |
2578 | duk_small_uint_fast_t i; | |
2579 | ||
2580 | /* A -> unused (reserved for flags, for consistency with DUK_OP_CALL) | |
2581 | * B -> target register and start reg: constructor, arg1, ..., argN | |
2582 | * (for DUK_OP_NEWI, 'b' is indirect) | |
2583 | * C -> num args (N) | |
2584 | */ | |
2585 | ||
2586 | /* duk_new() will call the constuctor using duk_handle_call(). | |
2587 | * A constructor call prevents a yield from inside the constructor, | |
2588 | * even if the constructor is an Ecmascript function. | |
2589 | */ | |
2590 | ||
2591 | /* Don't need to sync curr_pc here; duk_new() will do that | |
2592 | * when it augments the created error. | |
2593 | */ | |
2594 | ||
2595 | /* XXX: unnecessary copying of values? Just set 'top' to | |
2596 | * b + c, and let the return handling fix up the stack frame? | |
2597 | */ | |
2598 | ||
2599 | idx = (duk_uint_fast_t) DUK_DEC_B(ins); | |
2600 | if (DUK_DEC_OP(ins) == DUK_OP_NEWI) { | |
2601 | duk_tval *tv_ind = DUK__REGP(idx); | |
2602 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
2603 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
2604 | } | |
2605 | ||
2606 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
2607 | if (idx + c + 1 > (duk_uint_fast_t) duk_get_top(ctx)) { | |
2608 | /* XXX: use duk_is_valid_index() instead? */ | |
2609 | /* XXX: improve check; check against nregs, not against top */ | |
2610 | DUK__INTERNAL_ERROR("NEW out of bounds"); | |
2611 | } | |
2612 | #endif | |
2613 | ||
2614 | duk_require_stack(ctx, (duk_idx_t) c); | |
2615 | duk_push_tval(ctx, DUK__REGP(idx)); | |
2616 | for (i = 0; i < c; i++) { | |
2617 | duk_push_tval(ctx, DUK__REGP(idx + i + 1)); | |
2618 | } | |
2619 | duk_new(ctx, (duk_idx_t) c); /* [... constructor arg1 ... argN] -> [retval] */ | |
2620 | DUK_DDD(DUK_DDDPRINT("NEW -> %!iT", (duk_tval *) duk_get_tval(ctx, -1))); | |
2621 | duk_replace(ctx, (duk_idx_t) idx); | |
2622 | ||
2623 | /* When debugger is enabled, we need to recheck the activation | |
2624 | * status after returning. This is now handled by call handling | |
2625 | * and heap->dbg_force_restart. | |
2626 | */ | |
2627 | break; | |
2628 | } | |
2629 | ||
2630 | case DUK_OP_REGEXP: { | |
2631 | #ifdef DUK_USE_REGEXP_SUPPORT | |
2632 | duk_context *ctx = (duk_context *) thr; | |
2633 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2634 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2635 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
2636 | ||
2637 | /* A -> target register | |
2638 | * B -> bytecode (also contains flags) | |
2639 | * C -> escaped source | |
2640 | */ | |
2641 | ||
2642 | duk_push_tval(ctx, DUK__REGCONSTP(c)); | |
2643 | duk_push_tval(ctx, DUK__REGCONSTP(b)); /* -> [ ... escaped_source bytecode ] */ | |
2644 | duk_regexp_create_instance(thr); /* -> [ ... regexp_instance ] */ | |
2645 | DUK_DDD(DUK_DDDPRINT("regexp instance: %!iT", (duk_tval *) duk_get_tval(ctx, -1))); | |
2646 | duk_replace(ctx, (duk_idx_t) a); | |
2647 | #else | |
2648 | /* The compiler should never emit DUK_OP_REGEXP if there is no | |
2649 | * regexp support. | |
2650 | */ | |
2651 | DUK__INTERNAL_ERROR("no regexp support"); | |
2652 | #endif | |
2653 | ||
2654 | break; | |
2655 | } | |
2656 | ||
2657 | case DUK_OP_CSREG: | |
2658 | case DUK_OP_CSREGI: { | |
2659 | /* | |
2660 | * Assuming a register binds to a variable declared within this | |
2661 | * function (a declarative binding), the 'this' for the call | |
2662 | * setup is always 'undefined'. E5 Section 10.2.1.1.6. | |
2663 | */ | |
2664 | ||
2665 | duk_context *ctx = (duk_context *) thr; | |
2666 | duk_small_uint_fast_t b = DUK_DEC_B(ins); /* restricted to regs */ | |
2667 | duk_uint_fast_t idx; | |
2668 | ||
2669 | /* A -> target register (A, A+1) for call setup | |
2670 | * (for DUK_OP_CSREGI, 'a' is indirect) | |
2671 | * B -> register containing target function (not type checked here) | |
2672 | */ | |
2673 | ||
2674 | /* XXX: direct manipulation, or duk_replace_tval() */ | |
2675 | ||
2676 | /* Note: target registers a and a+1 may overlap with DUK__REGP(b). | |
2677 | * Careful here. | |
2678 | */ | |
2679 | ||
2680 | idx = (duk_uint_fast_t) DUK_DEC_A(ins); | |
2681 | if (DUK_DEC_OP(ins) == DUK_OP_CSREGI) { | |
2682 | duk_tval *tv_ind = DUK__REGP(idx); | |
2683 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
2684 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
2685 | } | |
2686 | ||
2687 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
2688 | if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) { | |
2689 | /* XXX: use duk_is_valid_index() instead? */ | |
2690 | /* XXX: improve check; check against nregs, not against top */ | |
2691 | DUK__INTERNAL_ERROR("CSREG out of bounds"); | |
2692 | } | |
2693 | #endif | |
2694 | ||
2695 | duk_push_tval(ctx, DUK__REGP(b)); | |
2696 | duk_replace(ctx, (duk_idx_t) idx); | |
2697 | duk_push_undefined(ctx); | |
2698 | duk_replace(ctx, (duk_idx_t) (idx + 1)); | |
2699 | break; | |
2700 | } | |
2701 | ||
2702 | case DUK_OP_GETVAR: { | |
2703 | duk_context *ctx = (duk_context *) thr; | |
2704 | duk_activation *act; | |
2705 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2706 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
2707 | duk_tval *tv1; | |
2708 | duk_hstring *name; | |
2709 | ||
2710 | tv1 = DUK__CONSTP(bc); | |
2711 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
2712 | name = DUK_TVAL_GET_STRING(tv1); | |
2713 | DUK_ASSERT(name != NULL); | |
2714 | DUK_DDD(DUK_DDDPRINT("GETVAR: '%!O'", (duk_heaphdr *) name)); | |
2715 | act = thr->callstack + thr->callstack_top - 1; | |
2716 | (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */ | |
2717 | ||
2718 | duk_pop(ctx); /* 'this' binding is not needed here */ | |
2719 | duk_replace(ctx, (duk_idx_t) a); | |
2720 | break; | |
2721 | } | |
2722 | ||
2723 | case DUK_OP_PUTVAR: { | |
2724 | duk_activation *act; | |
2725 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2726 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
2727 | duk_tval *tv1; | |
2728 | duk_hstring *name; | |
2729 | ||
2730 | tv1 = DUK__CONSTP(bc); | |
2731 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
2732 | name = DUK_TVAL_GET_STRING(tv1); | |
2733 | DUK_ASSERT(name != NULL); | |
2734 | ||
2735 | /* XXX: putvar takes a duk_tval pointer, which is awkward and | |
2736 | * should be reworked. | |
2737 | */ | |
2738 | ||
2739 | tv1 = DUK__REGP(a); /* val */ | |
2740 | act = thr->callstack + thr->callstack_top - 1; | |
2741 | duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT()); | |
2742 | break; | |
2743 | } | |
2744 | ||
2745 | case DUK_OP_DECLVAR: { | |
2746 | duk_activation *act; | |
2747 | duk_context *ctx = (duk_context *) thr; | |
2748 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2749 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2750 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
2751 | duk_tval *tv1; | |
2752 | duk_hstring *name; | |
2753 | duk_small_uint_t prop_flags; | |
2754 | duk_bool_t is_func_decl; | |
2755 | duk_bool_t is_undef_value; | |
2756 | ||
2757 | tv1 = DUK__REGCONSTP(b); | |
2758 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
2759 | name = DUK_TVAL_GET_STRING(tv1); | |
2760 | DUK_ASSERT(name != NULL); | |
2761 | ||
2762 | is_undef_value = ((a & DUK_BC_DECLVAR_FLAG_UNDEF_VALUE) != 0); | |
2763 | is_func_decl = ((a & DUK_BC_DECLVAR_FLAG_FUNC_DECL) != 0); | |
2764 | ||
2765 | /* XXX: declvar takes an duk_tval pointer, which is awkward and | |
2766 | * should be reworked. | |
2767 | */ | |
2768 | ||
2769 | /* Compiler is responsible for selecting property flags (configurability, | |
2770 | * writability, etc). | |
2771 | */ | |
2772 | prop_flags = a & DUK_PROPDESC_FLAGS_MASK; | |
2773 | ||
2774 | if (is_undef_value) { | |
2775 | duk_push_undefined(ctx); | |
2776 | } else { | |
2777 | duk_push_tval(ctx, DUK__REGCONSTP(c)); | |
2778 | } | |
2779 | tv1 = duk_get_tval(ctx, -1); | |
2780 | ||
2781 | act = thr->callstack + thr->callstack_top - 1; | |
2782 | if (duk_js_declvar_activation(thr, act, name, tv1, prop_flags, is_func_decl)) { | |
2783 | /* already declared, must update binding value */ | |
2784 | tv1 = duk_get_tval(ctx, -1); | |
2785 | duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT()); | |
2786 | } | |
2787 | ||
2788 | duk_pop(ctx); | |
2789 | break; | |
2790 | } | |
2791 | ||
2792 | case DUK_OP_DELVAR: { | |
2793 | duk_activation *act; | |
2794 | duk_context *ctx = (duk_context *) thr; | |
2795 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2796 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2797 | duk_tval *tv1; | |
2798 | duk_hstring *name; | |
2799 | duk_bool_t rc; | |
2800 | ||
2801 | tv1 = DUK__REGCONSTP(b); | |
2802 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
2803 | name = DUK_TVAL_GET_STRING(tv1); | |
2804 | DUK_ASSERT(name != NULL); | |
2805 | DUK_DDD(DUK_DDDPRINT("DELVAR '%!O'", (duk_heaphdr *) name)); | |
2806 | act = thr->callstack + thr->callstack_top - 1; | |
2807 | rc = duk_js_delvar_activation(thr, act, name); | |
2808 | ||
2809 | duk_push_boolean(ctx, rc); | |
2810 | duk_replace(ctx, (duk_idx_t) a); | |
2811 | break; | |
2812 | } | |
2813 | ||
2814 | case DUK_OP_CSVAR: | |
2815 | case DUK_OP_CSVARI: { | |
2816 | /* 'this' value: | |
2817 | * E5 Section 6.b.i | |
2818 | * | |
2819 | * The only (standard) case where the 'this' binding is non-null is when | |
2820 | * (1) the variable is found in an object environment record, and | |
2821 | * (2) that object environment record is a 'with' block. | |
2822 | * | |
2823 | */ | |
2824 | ||
2825 | duk_context *ctx = (duk_context *) thr; | |
2826 | duk_activation *act; | |
2827 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2828 | duk_uint_fast_t idx; | |
2829 | duk_tval *tv1; | |
2830 | duk_hstring *name; | |
2831 | ||
2832 | tv1 = DUK__REGCONSTP(b); | |
2833 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
2834 | name = DUK_TVAL_GET_STRING(tv1); | |
2835 | DUK_ASSERT(name != NULL); | |
2836 | act = thr->callstack + thr->callstack_top - 1; | |
2837 | (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */ | |
2838 | ||
2839 | /* Note: target registers a and a+1 may overlap with DUK__REGCONSTP(b) | |
2840 | * and DUK__REGCONSTP(c). Careful here. | |
2841 | */ | |
2842 | ||
2843 | idx = (duk_uint_fast_t) DUK_DEC_A(ins); | |
2844 | if (DUK_DEC_OP(ins) == DUK_OP_CSVARI) { | |
2845 | duk_tval *tv_ind = DUK__REGP(idx); | |
2846 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
2847 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
2848 | } | |
2849 | ||
2850 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
2851 | if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) { | |
2852 | /* XXX: use duk_is_valid_index() instead? */ | |
2853 | /* XXX: improve check; check against nregs, not against top */ | |
2854 | DUK__INTERNAL_ERROR("CSVAR out of bounds"); | |
2855 | } | |
2856 | #endif | |
2857 | ||
2858 | duk_replace(ctx, (duk_idx_t) (idx + 1)); /* 'this' binding */ | |
2859 | duk_replace(ctx, (duk_idx_t) idx); /* variable value (function, we hope, not checked here) */ | |
2860 | break; | |
2861 | } | |
2862 | ||
2863 | case DUK_OP_CLOSURE: { | |
2864 | duk_context *ctx = (duk_context *) thr; | |
2865 | duk_activation *act; | |
2866 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2867 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
2868 | duk_hobject *fun_temp; | |
2869 | ||
2870 | /* A -> target reg | |
2871 | * BC -> inner function index | |
2872 | */ | |
2873 | ||
2874 | DUK_DDD(DUK_DDDPRINT("CLOSURE to target register %ld, fnum %ld (count %ld)", | |
2875 | (long) a, (long) bc, (long) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, fun))); | |
2876 | ||
2877 | DUK_ASSERT_DISABLE(bc >= 0); /* unsigned */ | |
2878 | DUK_ASSERT((duk_uint_t) bc < (duk_uint_t) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(thr->heap, fun)); | |
2879 | fun_temp = DUK_HCOMPILEDFUNCTION_GET_FUNCS_BASE(thr->heap, fun)[bc]; | |
2880 | DUK_ASSERT(fun_temp != NULL); | |
2881 | DUK_ASSERT(DUK_HOBJECT_IS_COMPILEDFUNCTION(fun_temp)); | |
2882 | ||
2883 | DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O", | |
2884 | (void *) fun_temp, (duk_heaphdr *) fun_temp)); | |
2885 | ||
2886 | act = thr->callstack + thr->callstack_top - 1; | |
2887 | if (act->lex_env == NULL) { | |
2888 | DUK_ASSERT(act->var_env == NULL); | |
2889 | duk_js_init_activation_environment_records_delayed(thr, act); | |
2890 | } | |
2891 | DUK_ASSERT(act->lex_env != NULL); | |
2892 | DUK_ASSERT(act->var_env != NULL); | |
2893 | ||
2894 | /* functions always have a NEWENV flag, i.e. they get a | |
2895 | * new variable declaration environment, so only lex_env | |
2896 | * matters here. | |
2897 | */ | |
2898 | duk_js_push_closure(thr, | |
2899 | (duk_hcompiledfunction *) fun_temp, | |
2900 | act->var_env, | |
2901 | act->lex_env); | |
2902 | duk_replace(ctx, (duk_idx_t) a); | |
2903 | ||
2904 | break; | |
2905 | } | |
2906 | ||
2907 | case DUK_OP_GETPROP: { | |
2908 | duk_context *ctx = (duk_context *) thr; | |
2909 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2910 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2911 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
2912 | duk_tval *tv_obj; | |
2913 | duk_tval *tv_key; | |
2914 | duk_bool_t rc; | |
2915 | ||
2916 | /* A -> target reg | |
2917 | * B -> object reg/const (may be const e.g. in "'foo'[1]") | |
2918 | * C -> key reg/const | |
2919 | */ | |
2920 | ||
2921 | tv_obj = DUK__REGCONSTP(b); | |
2922 | tv_key = DUK__REGCONSTP(c); | |
2923 | DUK_DDD(DUK_DDDPRINT("GETPROP: a=%ld obj=%!T, key=%!T", | |
2924 | (long) a, | |
2925 | (duk_tval *) DUK__REGCONSTP(b), | |
2926 | (duk_tval *) DUK__REGCONSTP(c))); | |
2927 | rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */ | |
2928 | DUK_UNREF(rc); /* ignore */ | |
2929 | DUK_DDD(DUK_DDDPRINT("GETPROP --> %!T", | |
2930 | (duk_tval *) duk_get_tval(ctx, -1))); | |
2931 | tv_obj = NULL; /* invalidated */ | |
2932 | tv_key = NULL; /* invalidated */ | |
2933 | ||
2934 | duk_replace(ctx, (duk_idx_t) a); /* val */ | |
2935 | break; | |
2936 | } | |
2937 | ||
2938 | case DUK_OP_PUTPROP: { | |
2939 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2940 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2941 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
2942 | duk_tval *tv_obj; | |
2943 | duk_tval *tv_key; | |
2944 | duk_tval *tv_val; | |
2945 | duk_bool_t rc; | |
2946 | ||
2947 | /* A -> object reg | |
2948 | * B -> key reg/const | |
2949 | * C -> value reg/const | |
2950 | * | |
2951 | * Note: intentional difference to register arrangement | |
2952 | * of e.g. GETPROP; 'A' must contain a register-only value. | |
2953 | */ | |
2954 | ||
2955 | tv_obj = DUK__REGP(a); | |
2956 | tv_key = DUK__REGCONSTP(b); | |
2957 | tv_val = DUK__REGCONSTP(c); | |
2958 | DUK_DDD(DUK_DDDPRINT("PUTPROP: obj=%!T, key=%!T, val=%!T", | |
2959 | (duk_tval *) DUK__REGP(a), | |
2960 | (duk_tval *) DUK__REGCONSTP(b), | |
2961 | (duk_tval *) DUK__REGCONSTP(c))); | |
2962 | rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT()); | |
2963 | DUK_UNREF(rc); /* ignore */ | |
2964 | DUK_DDD(DUK_DDDPRINT("PUTPROP --> obj=%!T, key=%!T, val=%!T", | |
2965 | (duk_tval *) DUK__REGP(a), | |
2966 | (duk_tval *) DUK__REGCONSTP(b), | |
2967 | (duk_tval *) DUK__REGCONSTP(c))); | |
2968 | tv_obj = NULL; /* invalidated */ | |
2969 | tv_key = NULL; /* invalidated */ | |
2970 | tv_val = NULL; /* invalidated */ | |
2971 | ||
2972 | break; | |
2973 | } | |
2974 | ||
2975 | case DUK_OP_DELPROP: { | |
2976 | duk_context *ctx = (duk_context *) thr; | |
2977 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
2978 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
2979 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
2980 | duk_tval *tv_obj; | |
2981 | duk_tval *tv_key; | |
2982 | duk_bool_t rc; | |
2983 | ||
2984 | /* A -> result reg | |
2985 | * B -> object reg | |
2986 | * C -> key reg/const | |
2987 | */ | |
2988 | ||
2989 | tv_obj = DUK__REGP(b); | |
2990 | tv_key = DUK__REGCONSTP(c); | |
2991 | rc = duk_hobject_delprop(thr, tv_obj, tv_key, DUK__STRICT()); | |
2992 | tv_obj = NULL; /* invalidated */ | |
2993 | tv_key = NULL; /* invalidated */ | |
2994 | ||
2995 | duk_push_boolean(ctx, rc); | |
2996 | duk_replace(ctx, (duk_idx_t) a); /* result */ | |
2997 | break; | |
2998 | } | |
2999 | ||
3000 | case DUK_OP_CSPROP: | |
3001 | case DUK_OP_CSPROPI: { | |
3002 | duk_context *ctx = (duk_context *) thr; | |
3003 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3004 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3005 | duk_uint_fast_t idx; | |
3006 | duk_tval *tv_obj; | |
3007 | duk_tval *tv_key; | |
3008 | duk_bool_t rc; | |
3009 | ||
3010 | /* E5 Section 11.2.3, step 6.a.i */ | |
3011 | /* E5 Section 10.4.3 */ | |
3012 | ||
3013 | /* XXX: allow object to be a const, e.g. in 'foo'.toString()? | |
3014 | * On the other hand, DUK_REGCONSTP() is slower and generates | |
3015 | * more code. | |
3016 | */ | |
3017 | ||
3018 | tv_obj = DUK__REGP(b); | |
3019 | tv_key = DUK__REGCONSTP(c); | |
3020 | rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */ | |
3021 | DUK_UNREF(rc); /* unused */ | |
3022 | tv_obj = NULL; /* invalidated */ | |
3023 | tv_key = NULL; /* invalidated */ | |
3024 | ||
3025 | /* Note: target registers a and a+1 may overlap with DUK__REGP(b) | |
3026 | * and DUK__REGCONSTP(c). Careful here. | |
3027 | */ | |
3028 | ||
3029 | idx = (duk_uint_fast_t) DUK_DEC_A(ins); | |
3030 | if (DUK_DEC_OP(ins) == DUK_OP_CSPROPI) { | |
3031 | duk_tval *tv_ind = DUK__REGP(idx); | |
3032 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
3033 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
3034 | } | |
3035 | ||
3036 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
3037 | if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) { | |
3038 | /* XXX: use duk_is_valid_index() instead? */ | |
3039 | /* XXX: improve check; check against nregs, not against top */ | |
3040 | DUK__INTERNAL_ERROR("CSPROP out of bounds"); | |
3041 | } | |
3042 | #endif | |
3043 | ||
3044 | duk_push_tval(ctx, DUK__REGP(b)); /* [ ... val obj ] */ | |
3045 | duk_replace(ctx, (duk_idx_t) (idx + 1)); /* 'this' binding */ | |
3046 | duk_replace(ctx, (duk_idx_t) idx); /* val */ | |
3047 | break; | |
3048 | } | |
3049 | ||
3050 | case DUK_OP_ADD: | |
3051 | case DUK_OP_SUB: | |
3052 | case DUK_OP_MUL: | |
3053 | case DUK_OP_DIV: | |
3054 | case DUK_OP_MOD: { | |
3055 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3056 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3057 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3058 | duk_small_uint_fast_t op = DUK_DEC_OP(ins); | |
3059 | ||
3060 | if (op == DUK_OP_ADD) { | |
3061 | /* | |
3062 | * Handling DUK_OP_ADD this way is more compact (experimentally) | |
3063 | * than a separate case with separate argument decoding. | |
3064 | */ | |
3065 | duk__vm_arith_add(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a); | |
3066 | } else { | |
3067 | duk__vm_arith_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op); | |
3068 | } | |
3069 | break; | |
3070 | } | |
3071 | ||
3072 | case DUK_OP_BAND: | |
3073 | case DUK_OP_BOR: | |
3074 | case DUK_OP_BXOR: | |
3075 | case DUK_OP_BASL: | |
3076 | case DUK_OP_BLSR: | |
3077 | case DUK_OP_BASR: { | |
3078 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3079 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3080 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3081 | duk_small_uint_fast_t op = DUK_DEC_OP(ins); | |
3082 | ||
3083 | duk__vm_bitwise_binary_op(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c), a, op); | |
3084 | break; | |
3085 | } | |
3086 | ||
3087 | case DUK_OP_EQ: | |
3088 | case DUK_OP_NEQ: { | |
3089 | duk_context *ctx = (duk_context *) thr; | |
3090 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3091 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3092 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3093 | duk_bool_t tmp; | |
3094 | ||
3095 | /* E5 Sections 11.9.1, 11.9.3 */ | |
3096 | tmp = duk_js_equals(thr, DUK__REGCONSTP(b), DUK__REGCONSTP(c)); | |
3097 | if (DUK_DEC_OP(ins) == DUK_OP_NEQ) { | |
3098 | tmp = !tmp; | |
3099 | } | |
3100 | duk_push_boolean(ctx, tmp); | |
3101 | duk_replace(ctx, (duk_idx_t) a); | |
3102 | break; | |
3103 | } | |
3104 | ||
3105 | case DUK_OP_SEQ: | |
3106 | case DUK_OP_SNEQ: { | |
3107 | duk_context *ctx = (duk_context *) thr; | |
3108 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3109 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3110 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3111 | duk_bool_t tmp; | |
3112 | ||
3113 | /* E5 Sections 11.9.1, 11.9.3 */ | |
3114 | tmp = duk_js_strict_equals(DUK__REGCONSTP(b), DUK__REGCONSTP(c)); | |
3115 | if (DUK_DEC_OP(ins) == DUK_OP_SNEQ) { | |
3116 | tmp = !tmp; | |
3117 | } | |
3118 | duk_push_boolean(ctx, tmp); | |
3119 | duk_replace(ctx, (duk_idx_t) a); | |
3120 | break; | |
3121 | } | |
3122 | ||
3123 | /* Note: combining comparison ops must be done carefully because | |
3124 | * of uncomparable values (NaN): it's not necessarily true that | |
3125 | * (x >= y) === !(x < y). Also, evaluation order matters, and | |
3126 | * although it would only seem to affect the compiler this is | |
3127 | * actually not the case, because there are also run-time coercions | |
3128 | * of the arguments (with potential side effects). | |
3129 | * | |
3130 | * XXX: can be combined; check code size. | |
3131 | */ | |
3132 | ||
3133 | case DUK_OP_GT: { | |
3134 | duk_context *ctx = (duk_context *) thr; | |
3135 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3136 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3137 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3138 | duk_bool_t tmp; | |
3139 | ||
3140 | /* x > y --> y < x */ | |
3141 | tmp = duk_js_compare_helper(thr, | |
3142 | DUK__REGCONSTP(c), /* y */ | |
3143 | DUK__REGCONSTP(b), /* x */ | |
3144 | 0); /* flags */ | |
3145 | ||
3146 | duk_push_boolean(ctx, tmp); | |
3147 | duk_replace(ctx, (duk_idx_t) a); | |
3148 | break; | |
3149 | } | |
3150 | ||
3151 | case DUK_OP_GE: { | |
3152 | duk_context *ctx = (duk_context *) thr; | |
3153 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3154 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3155 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3156 | duk_bool_t tmp; | |
3157 | ||
3158 | /* x >= y --> not (x < y) */ | |
3159 | tmp = duk_js_compare_helper(thr, | |
3160 | DUK__REGCONSTP(b), /* x */ | |
3161 | DUK__REGCONSTP(c), /* y */ | |
3162 | DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | | |
3163 | DUK_COMPARE_FLAG_NEGATE); /* flags */ | |
3164 | ||
3165 | duk_push_boolean(ctx, tmp); | |
3166 | duk_replace(ctx, (duk_idx_t) a); | |
3167 | break; | |
3168 | } | |
3169 | ||
3170 | case DUK_OP_LT: { | |
3171 | duk_context *ctx = (duk_context *) thr; | |
3172 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3173 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3174 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3175 | duk_bool_t tmp; | |
3176 | ||
3177 | /* x < y */ | |
3178 | tmp = duk_js_compare_helper(thr, | |
3179 | DUK__REGCONSTP(b), /* x */ | |
3180 | DUK__REGCONSTP(c), /* y */ | |
3181 | DUK_COMPARE_FLAG_EVAL_LEFT_FIRST); /* flags */ | |
3182 | ||
3183 | duk_push_boolean(ctx, tmp); | |
3184 | duk_replace(ctx, (duk_idx_t) a); | |
3185 | break; | |
3186 | } | |
3187 | ||
3188 | case DUK_OP_LE: { | |
3189 | duk_context *ctx = (duk_context *) thr; | |
3190 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3191 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3192 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3193 | duk_bool_t tmp; | |
3194 | ||
3195 | /* x <= y --> not (x > y) --> not (y < x) */ | |
3196 | tmp = duk_js_compare_helper(thr, | |
3197 | DUK__REGCONSTP(c), /* y */ | |
3198 | DUK__REGCONSTP(b), /* x */ | |
3199 | DUK_COMPARE_FLAG_NEGATE); /* flags */ | |
3200 | ||
3201 | duk_push_boolean(ctx, tmp); | |
3202 | duk_replace(ctx, (duk_idx_t) a); | |
3203 | break; | |
3204 | } | |
3205 | ||
3206 | case DUK_OP_IF: { | |
3207 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3208 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3209 | duk_bool_t tmp; | |
3210 | ||
3211 | tmp = duk_js_toboolean(DUK__REGCONSTP(b)); | |
3212 | if (tmp == (duk_bool_t) a) { | |
3213 | /* if boolean matches A, skip next inst */ | |
3214 | curr_pc++; | |
3215 | } else { | |
3216 | ; | |
3217 | } | |
3218 | break; | |
3219 | } | |
3220 | ||
3221 | case DUK_OP_JUMP: { | |
3222 | duk_int_fast_t abc = DUK_DEC_ABC(ins); | |
3223 | ||
3224 | curr_pc += abc - DUK_BC_JUMP_BIAS; | |
3225 | break; | |
3226 | } | |
3227 | ||
3228 | case DUK_OP_RETURN: { | |
3229 | duk_context *ctx = (duk_context *) thr; | |
3230 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3231 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3232 | /* duk_small_uint_fast_t c = DUK_DEC_C(ins); */ | |
3233 | duk_tval *tv_val; | |
3234 | ||
3235 | /* A -> flags | |
3236 | * B -> return value reg/const | |
3237 | * C -> currently unused | |
3238 | */ | |
3239 | ||
3240 | /* A fast return avoids full longjmp handling for a set of | |
3241 | * scenarios which hopefully represents the common cases. | |
3242 | * The compiler is responsible for emitting fast returns | |
3243 | * only when they are safe. Currently this means that there | |
3244 | * is nothing on the catch stack (not even label catchers). | |
3245 | * The speed advantage of fast returns (avoiding longjmp) is | |
3246 | * not very high, around 10-15%. | |
3247 | */ | |
3248 | #if 0 /* XXX: Disabled for 1.0 release */ | |
3249 | if (a & DUK_BC_RETURN_FLAG_FAST) { | |
3250 | DUK_DDD(DUK_DDDPRINT("FASTRETURN attempt a=%ld b=%ld", (long) a, (long) b)); | |
3251 | ||
3252 | if (duk__handle_fast_return(thr, | |
3253 | (a & DUK_BC_RETURN_FLAG_HAVE_RETVAL) ? DUK__REGCONSTP(b) : NULL, | |
3254 | entry_thread, | |
3255 | entry_callstack_top)) { | |
3256 | DUK_DDD(DUK_DDDPRINT("FASTRETURN success a=%ld b=%ld", (long) a, (long) b)); | |
3257 | DUK__SYNC_CURR_PC(); | |
3258 | goto restart_execution; | |
3259 | } | |
3260 | } | |
3261 | #endif | |
3262 | ||
3263 | /* No fast return, slow path. */ | |
3264 | DUK_DDD(DUK_DDDPRINT("SLOWRETURN a=%ld b=%ld", (long) a, (long) b)); | |
3265 | ||
3266 | if (a & DUK_BC_RETURN_FLAG_HAVE_RETVAL) { | |
3267 | tv_val = DUK__REGCONSTP(b); | |
3268 | #if defined(DUK_USE_FASTINT) | |
3269 | /* Explicit check for fastint downgrade. Do | |
3270 | * it also for consts for now, which is odd | |
3271 | * but harmless. | |
3272 | */ | |
3273 | /* XXX: restrict to reg values only? */ | |
3274 | ||
3275 | DUK_TVAL_CHKFAST_INPLACE(tv_val); | |
3276 | #endif | |
3277 | duk_push_tval(ctx, tv_val); | |
3278 | } else { | |
3279 | duk_push_undefined(ctx); | |
3280 | } | |
3281 | ||
3282 | duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_RETURN); | |
3283 | ||
3284 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* in bytecode executor, should always be set */ | |
3285 | DUK__SYNC_AND_NULL_CURR_PC(); | |
3286 | duk_err_longjmp(thr); | |
3287 | DUK_UNREACHABLE(); | |
3288 | break; | |
3289 | } | |
3290 | ||
3291 | case DUK_OP_CALL: | |
3292 | case DUK_OP_CALLI: { | |
3293 | duk_context *ctx = (duk_context *) thr; | |
3294 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3295 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3296 | duk_uint_fast_t idx; | |
3297 | duk_small_uint_t call_flags; | |
3298 | duk_small_uint_t flag_tailcall; | |
3299 | duk_small_uint_t flag_evalcall; | |
3300 | duk_tval *tv_func; | |
3301 | duk_hobject *obj_func; | |
3302 | duk_bool_t setup_rc; | |
3303 | duk_idx_t num_stack_args; | |
3304 | ||
3305 | /* A -> flags | |
3306 | * B -> base register for call (base -> func, base+1 -> this, base+2 -> arg1 ... base+2+N-1 -> argN) | |
3307 | * (for DUK_OP_CALLI, 'b' is indirect) | |
3308 | * C -> nargs | |
3309 | */ | |
3310 | ||
3311 | /* these are not necessarily 0 or 1 (may be other non-zero), that's ok */ | |
3312 | flag_tailcall = (a & DUK_BC_CALL_FLAG_TAILCALL); | |
3313 | flag_evalcall = (a & DUK_BC_CALL_FLAG_EVALCALL); | |
3314 | ||
3315 | idx = (duk_uint_fast_t) DUK_DEC_B(ins); | |
3316 | if (DUK_DEC_OP(ins) == DUK_OP_CALLI) { | |
3317 | duk_tval *tv_ind = DUK__REGP(idx); | |
3318 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
3319 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
3320 | } | |
3321 | ||
3322 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
3323 | if (!duk_is_valid_index(ctx, (duk_idx_t) idx)) { | |
3324 | /* XXX: improve check; check against nregs, not against top */ | |
3325 | DUK__INTERNAL_ERROR("CALL out of bounds"); | |
3326 | } | |
3327 | #endif | |
3328 | ||
3329 | /* | |
3330 | * To determine whether to use an optimized Ecmascript-to-Ecmascript | |
3331 | * call, we need to know whether the final, non-bound function is an | |
3332 | * Ecmascript function. | |
3333 | * | |
3334 | * This is now implemented so that we start to do an ecma-to-ecma call | |
3335 | * setup which will resolve the bound chain as the first thing. If the | |
3336 | * final function is not eligible, the return value indicates that the | |
3337 | * ecma-to-ecma call is not possible. The setup will overwrite the call | |
3338 | * target at DUK__REGP(idx) with the final, non-bound function (which | |
3339 | * may be a lightfunc), and fudge arguments if necessary. | |
3340 | * | |
3341 | * XXX: If an ecma-to-ecma call is not possible, this initial call | |
3342 | * setup will do bound function chain resolution but won't do the | |
3343 | * "effective this binding" resolution which is quite confusing. | |
3344 | * Perhaps add a helper for doing bound function and effective this | |
3345 | * binding resolution - and call that explicitly? Ecma-to-ecma call | |
3346 | * setup and normal function handling can then assume this prestep has | |
3347 | * been done by the caller. | |
3348 | */ | |
3349 | ||
3350 | duk_set_top(ctx, (duk_idx_t) (idx + c + 2)); /* [ ... func this arg1 ... argN ] */ | |
3351 | ||
3352 | call_flags = 0; | |
3353 | if (flag_tailcall) { | |
3354 | /* We request a tail call, but in some corner cases | |
3355 | * call handling can decide that a tail call is | |
3356 | * actually not possible. | |
3357 | * See: test-bug-tailcall-preventyield-assert.c. | |
3358 | */ | |
3359 | call_flags |= DUK_CALL_FLAG_IS_TAILCALL; | |
3360 | } | |
3361 | ||
3362 | /* Compared to duk_handle_call(): | |
3363 | * - protected call: never | |
3364 | * - ignore recursion limit: never | |
3365 | */ | |
3366 | num_stack_args = c; | |
3367 | setup_rc = duk_handle_ecma_call_setup(thr, | |
3368 | num_stack_args, | |
3369 | call_flags); | |
3370 | ||
3371 | if (setup_rc) { | |
3372 | /* Ecma-to-ecma call possible, may or may not be a tail call. | |
3373 | * Avoid C recursion by being clever. | |
3374 | */ | |
3375 | DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call setup possible, restart execution")); | |
3376 | /* curr_pc synced by duk_handle_ecma_call_setup() */ | |
3377 | goto restart_execution; | |
3378 | } | |
3379 | DUK_ASSERT(thr->ptr_curr_pc != NULL); /* restored if ecma-to-ecma setup fails */ | |
3380 | ||
3381 | DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call not possible, target is native (may be lightfunc)")); | |
3382 | ||
3383 | /* Recompute argument count: bound function handling may have shifted. */ | |
3384 | num_stack_args = duk_get_top(ctx) - (idx + 2); | |
3385 | DUK_DDD(DUK_DDDPRINT("recomputed arg count: %ld\n", (long) num_stack_args)); | |
3386 | ||
3387 | tv_func = DUK__REGP(idx); /* Relookup if relocated */ | |
3388 | if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) { | |
3389 | call_flags = 0; /* not protected, respect reclimit, not constructor */ | |
3390 | ||
3391 | /* There is no eval() special handling here: eval() is never | |
3392 | * automatically converted to a lightfunc. | |
3393 | */ | |
3394 | DUK_ASSERT(DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func) != duk_bi_global_object_eval); | |
3395 | ||
3396 | duk_handle_call(thr, | |
3397 | num_stack_args, | |
3398 | call_flags); | |
3399 | ||
3400 | /* duk_js_call.c is required to restore the stack reserve | |
3401 | * so we only need to reset the top. | |
3402 | */ | |
3403 | duk_set_top(ctx, (duk_idx_t) fun->nregs); | |
3404 | ||
3405 | /* No need to reinit setjmp() catchpoint, as call handling | |
3406 | * will store and restore our state. | |
3407 | */ | |
3408 | } else { | |
3409 | /* Call setup checks callability. */ | |
3410 | DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_func)); | |
3411 | obj_func = DUK_TVAL_GET_OBJECT(tv_func); | |
3412 | DUK_ASSERT(obj_func != NULL); | |
3413 | DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(obj_func)); | |
3414 | ||
3415 | /* | |
3416 | * Other cases, use C recursion. | |
3417 | * | |
3418 | * If a tail call was requested we ignore it and execute a normal call. | |
3419 | * Since Duktape 0.11.0 the compiler emits a RETURN opcode even after | |
3420 | * a tail call to avoid test-bug-tailcall-thread-yield-resume.js. | |
3421 | * | |
3422 | * Direct eval call: (1) call target (before following bound function | |
3423 | * chain) is the built-in eval() function, and (2) call was made with | |
3424 | * the identifier 'eval'. | |
3425 | */ | |
3426 | ||
3427 | call_flags = 0; /* not protected, respect reclimit, not constructor */ | |
3428 | ||
3429 | if (DUK_HOBJECT_IS_NATIVEFUNCTION(obj_func) && | |
3430 | ((duk_hnativefunction *) obj_func)->func == duk_bi_global_object_eval) { | |
3431 | if (flag_evalcall) { | |
3432 | DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was 'eval' -> direct eval")); | |
3433 | call_flags |= DUK_CALL_FLAG_DIRECT_EVAL; | |
3434 | } else { | |
3435 | DUK_DDD(DUK_DDDPRINT("call target is eval, call identifier was not 'eval' -> indirect eval")); | |
3436 | } | |
3437 | } | |
3438 | ||
3439 | duk_handle_call(thr, | |
3440 | num_stack_args, | |
3441 | call_flags); | |
3442 | ||
3443 | /* duk_js_call.c is required to restore the stack reserve | |
3444 | * so we only need to reset the top. | |
3445 | */ | |
3446 | duk_set_top(ctx, (duk_idx_t) fun->nregs); | |
3447 | ||
3448 | /* No need to reinit setjmp() catchpoint, as call handling | |
3449 | * will store and restore our state. | |
3450 | */ | |
3451 | } | |
3452 | ||
3453 | /* When debugger is enabled, we need to recheck the activation | |
3454 | * status after returning. This is now handled by call handling | |
3455 | * and heap->dbg_force_restart. | |
3456 | */ | |
3457 | break; | |
3458 | } | |
3459 | ||
3460 | case DUK_OP_TRYCATCH: { | |
3461 | duk_context *ctx = (duk_context *) thr; | |
3462 | duk_activation *act; | |
3463 | duk_catcher *cat; | |
3464 | duk_tval *tv1; | |
3465 | duk_small_uint_fast_t a; | |
3466 | duk_uint_fast_t bc; | |
3467 | ||
3468 | /* A -> flags | |
3469 | * BC -> reg_catch; base register for two registers used both during | |
3470 | * trycatch setup and when catch is triggered | |
3471 | * | |
3472 | * If DUK_BC_TRYCATCH_FLAG_CATCH_BINDING set: | |
3473 | * reg_catch + 0: catch binding variable name (string). | |
3474 | * Automatic declarative environment is established for | |
3475 | * the duration of the 'catch' clause. | |
3476 | * | |
3477 | * If DUK_BC_TRYCATCH_FLAG_WITH_BINDING set: | |
3478 | * reg_catch + 0: with 'target value', which is coerced to | |
3479 | * an object and then used as a bindind object for an | |
3480 | * environment record. The binding is initialized here, for | |
3481 | * the 'try' clause. | |
3482 | * | |
3483 | * Note that a TRYCATCH generated for a 'with' statement has no | |
3484 | * catch or finally parts. | |
3485 | */ | |
3486 | ||
3487 | /* XXX: TRYCATCH handling should be reworked to avoid creating | |
3488 | * an explicit scope unless it is actually needed (e.g. function | |
3489 | * instances or eval is executed inside the catch block). This | |
3490 | * rework is not trivial because the compiler doesn't have an | |
3491 | * intermediate representation. When the rework is done, the | |
3492 | * opcode format can also be made more straightforward. | |
3493 | */ | |
3494 | ||
3495 | /* XXX: side effect handling is quite awkward here */ | |
3496 | ||
3497 | DUK_DDD(DUK_DDDPRINT("TRYCATCH: reg_catch=%ld, have_catch=%ld, " | |
3498 | "have_finally=%ld, catch_binding=%ld, with_binding=%ld (flags=0x%02lx)", | |
3499 | (long) DUK_DEC_BC(ins), | |
3500 | (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH ? 1 : 0), | |
3501 | (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY ? 1 : 0), | |
3502 | (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING ? 1 : 0), | |
3503 | (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_WITH_BINDING ? 1 : 0), | |
3504 | (unsigned long) DUK_DEC_A(ins))); | |
3505 | ||
3506 | a = DUK_DEC_A(ins); | |
3507 | bc = DUK_DEC_BC(ins); | |
3508 | ||
3509 | act = thr->callstack + thr->callstack_top - 1; | |
3510 | DUK_ASSERT(thr->callstack_top >= 1); | |
3511 | ||
3512 | /* 'with' target must be created first, in case we run out of memory */ | |
3513 | /* XXX: refactor out? */ | |
3514 | ||
3515 | if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) { | |
3516 | DUK_DDD(DUK_DDDPRINT("need to initialize a with binding object")); | |
3517 | ||
3518 | if (act->lex_env == NULL) { | |
3519 | DUK_ASSERT(act->var_env == NULL); | |
3520 | DUK_DDD(DUK_DDDPRINT("delayed environment initialization")); | |
3521 | ||
3522 | /* must relookup act in case of side effects */ | |
3523 | duk_js_init_activation_environment_records_delayed(thr, act); | |
3524 | act = thr->callstack + thr->callstack_top - 1; | |
3525 | } | |
3526 | DUK_ASSERT(act->lex_env != NULL); | |
3527 | DUK_ASSERT(act->var_env != NULL); | |
3528 | ||
3529 | (void) duk_push_object_helper(ctx, | |
3530 | DUK_HOBJECT_FLAG_EXTENSIBLE | | |
3531 | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV), | |
3532 | -1); /* no prototype, updated below */ | |
3533 | ||
3534 | duk_push_tval(ctx, DUK__REGP(bc)); | |
3535 | duk_to_object(ctx, -1); | |
3536 | duk_dup(ctx, -1); | |
3537 | ||
3538 | /* [ ... env target ] */ | |
3539 | /* [ ... env target target ] */ | |
3540 | ||
3541 | duk_xdef_prop_stridx(thr, -3, DUK_STRIDX_INT_TARGET, DUK_PROPDESC_FLAGS_NONE); | |
3542 | duk_xdef_prop_stridx(thr, -2, DUK_STRIDX_INT_THIS, DUK_PROPDESC_FLAGS_NONE); /* always provideThis=true */ | |
3543 | ||
3544 | /* [ ... env ] */ | |
3545 | ||
3546 | DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iT", | |
3547 | (duk_tval *) duk_get_tval(ctx, -1))); | |
3548 | } | |
3549 | ||
3550 | /* allocate catcher and populate it (should be atomic) */ | |
3551 | ||
3552 | duk_hthread_catchstack_grow(thr); | |
3553 | cat = thr->catchstack + thr->catchstack_top; | |
3554 | DUK_ASSERT(thr->catchstack_top + 1 <= thr->catchstack_size); | |
3555 | thr->catchstack_top++; | |
3556 | ||
3557 | cat->flags = DUK_CAT_TYPE_TCF; | |
3558 | cat->h_varname = NULL; | |
3559 | ||
3560 | if (a & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) { | |
3561 | cat->flags |= DUK_CAT_FLAG_CATCH_ENABLED; | |
3562 | } | |
3563 | if (a & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) { | |
3564 | cat->flags |= DUK_CAT_FLAG_FINALLY_ENABLED; | |
3565 | } | |
3566 | if (a & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING) { | |
3567 | DUK_DDD(DUK_DDDPRINT("catch binding flag set to catcher")); | |
3568 | cat->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED; | |
3569 | tv1 = DUK__REGP(bc); | |
3570 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
3571 | ||
3572 | /* borrowed reference; although 'tv1' comes from a register, | |
3573 | * its value was loaded using LDCONST so the constant will | |
3574 | * also exist and be reachable. | |
3575 | */ | |
3576 | cat->h_varname = DUK_TVAL_GET_STRING(tv1); | |
3577 | } else if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) { | |
3578 | /* env created above to stack top */ | |
3579 | duk_hobject *new_env; | |
3580 | ||
3581 | DUK_DDD(DUK_DDDPRINT("lexenv active flag set to catcher")); | |
3582 | cat->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE; | |
3583 | ||
3584 | DUK_DDD(DUK_DDDPRINT("activating object env: %!iT", | |
3585 | (duk_tval *) duk_get_tval(ctx, -1))); | |
3586 | DUK_ASSERT(act->lex_env != NULL); | |
3587 | new_env = duk_get_hobject(ctx, -1); | |
3588 | DUK_ASSERT(new_env != NULL); | |
3589 | ||
3590 | act = thr->callstack + thr->callstack_top - 1; /* relookup (side effects) */ | |
3591 | DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, new_env, act->lex_env); | |
3592 | ||
3593 | act = thr->callstack + thr->callstack_top - 1; /* relookup (side effects) */ | |
3594 | act->lex_env = new_env; | |
3595 | DUK_HOBJECT_INCREF(thr, new_env); | |
3596 | duk_pop(ctx); | |
3597 | } else { | |
3598 | ; | |
3599 | } | |
3600 | ||
3601 | /* Registers 'bc' and 'bc + 1' are written in longjmp handling | |
3602 | * and if their previous values (which are temporaries) become | |
3603 | * unreachable -and- have a finalizer, there'll be a function | |
3604 | * call during error handling which is not supported now (GH-287). | |
3605 | * Ensure that both 'bc' and 'bc + 1' have primitive values to | |
3606 | * guarantee no finalizer calls in error handling. Scrubbing also | |
3607 | * ensures finalizers for the previous values run here rather than | |
3608 | * later. Error handling related values are also written to 'bc' | |
3609 | * and 'bc + 1' but those values never become unreachable during | |
3610 | * error handling, so there's no side effect problem even if the | |
3611 | * error value has a finalizer. | |
3612 | */ | |
3613 | duk_to_undefined(ctx, bc); | |
3614 | duk_to_undefined(ctx, bc + 1); | |
3615 | ||
3616 | cat = thr->catchstack + thr->catchstack_top - 1; /* relookup (side effects) */ | |
3617 | cat->callstack_index = thr->callstack_top - 1; | |
3618 | cat->pc_base = (duk_instr_t *) curr_pc; /* pre-incremented, points to first jump slot */ | |
3619 | cat->idx_base = (duk_size_t) (thr->valstack_bottom - thr->valstack) + bc; | |
3620 | ||
3621 | DUK_DDD(DUK_DDDPRINT("TRYCATCH catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, " | |
3622 | "idx_base=%ld, h_varname=%!O", | |
3623 | (unsigned long) cat->flags, (long) cat->callstack_index, | |
3624 | (long) cat->pc_base, (long) cat->idx_base, (duk_heaphdr *) cat->h_varname)); | |
3625 | ||
3626 | curr_pc += 2; /* skip jump slots */ | |
3627 | break; | |
3628 | } | |
3629 | ||
3630 | /* Pre/post inc/dec for register variables, important for loops. */ | |
3631 | case DUK_OP_PREINCR: | |
3632 | case DUK_OP_PREDECR: | |
3633 | case DUK_OP_POSTINCR: | |
3634 | case DUK_OP_POSTDECR: { | |
3635 | duk_context *ctx = (duk_context *) thr; | |
3636 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3637 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3638 | duk_tval *tv1, *tv2; | |
3639 | duk_tval tv_tmp; | |
3640 | duk_double_t x, y, z; | |
3641 | ||
3642 | /* Two lowest bits of opcode are used to distinguish | |
3643 | * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1). | |
3644 | */ | |
3645 | DUK_ASSERT((DUK_OP_PREINCR & 0x03) == 0x00); | |
3646 | DUK_ASSERT((DUK_OP_PREDECR & 0x03) == 0x01); | |
3647 | DUK_ASSERT((DUK_OP_POSTINCR & 0x03) == 0x02); | |
3648 | DUK_ASSERT((DUK_OP_POSTDECR & 0x03) == 0x03); | |
3649 | ||
3650 | tv1 = DUK__REGP(bc); | |
3651 | #if defined(DUK_USE_FASTINT) | |
3652 | if (DUK_TVAL_IS_FASTINT(tv1)) { | |
3653 | duk_int64_t x_fi, y_fi, z_fi; | |
3654 | x_fi = DUK_TVAL_GET_FASTINT(tv1); | |
3655 | if (ins & DUK_ENC_OP(0x01)) { | |
3656 | if (x_fi == DUK_FASTINT_MIN) { | |
3657 | goto skip_fastint; | |
3658 | } | |
3659 | y_fi = x_fi - 1; | |
3660 | } else { | |
3661 | if (x_fi == DUK_FASTINT_MAX) { | |
3662 | goto skip_fastint; | |
3663 | } | |
3664 | y_fi = x_fi + 1; | |
3665 | } | |
3666 | ||
3667 | DUK_TVAL_SET_FASTINT(tv1, y_fi); /* no need for refcount update */ | |
3668 | ||
3669 | tv2 = DUK__REGP(a); | |
3670 | DUK_TVAL_SET_TVAL(&tv_tmp, tv2); | |
3671 | z_fi = (ins & DUK_ENC_OP(0x02)) ? x_fi : y_fi; | |
3672 | DUK_TVAL_SET_FASTINT(tv2, z_fi); /* no need for incref */ | |
3673 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
3674 | break; | |
3675 | } | |
3676 | skip_fastint: | |
3677 | #endif | |
3678 | if (DUK_TVAL_IS_NUMBER(tv1)) { | |
3679 | /* Fast path for the case where the register | |
3680 | * is a number (e.g. loop counter). | |
3681 | */ | |
3682 | ||
3683 | x = DUK_TVAL_GET_NUMBER(tv1); | |
3684 | if (ins & DUK_ENC_OP(0x01)) { | |
3685 | y = x - 1.0; | |
3686 | } else { | |
3687 | y = x + 1.0; | |
3688 | } | |
3689 | ||
3690 | DUK_TVAL_SET_NUMBER(tv1, y); /* no need for refcount update */ | |
3691 | } else { | |
3692 | x = duk_to_number(ctx, bc); | |
3693 | ||
3694 | if (ins & DUK_ENC_OP(0x01)) { | |
3695 | y = x - 1.0; | |
3696 | } else { | |
3697 | y = x + 1.0; | |
3698 | } | |
3699 | ||
3700 | duk_push_number(ctx, y); | |
3701 | duk_replace(ctx, bc); | |
3702 | } | |
3703 | ||
3704 | tv2 = DUK__REGP(a); | |
3705 | DUK_TVAL_SET_TVAL(&tv_tmp, tv2); | |
3706 | z = (ins & DUK_ENC_OP(0x02)) ? x : y; | |
3707 | DUK_TVAL_SET_NUMBER(tv2, z); /* no need for incref */ | |
3708 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
3709 | break; | |
3710 | } | |
3711 | ||
3712 | /* Preinc/predec for var-by-name, slow path. */ | |
3713 | case DUK_OP_PREINCV: | |
3714 | case DUK_OP_PREDECV: | |
3715 | case DUK_OP_POSTINCV: | |
3716 | case DUK_OP_POSTDECV: { | |
3717 | duk_context *ctx = (duk_context *) thr; | |
3718 | duk_activation *act; | |
3719 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3720 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3721 | duk_double_t x, y; | |
3722 | duk_tval *tv1; | |
3723 | duk_hstring *name; | |
3724 | ||
3725 | /* Two lowest bits of opcode are used to distinguish | |
3726 | * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1). | |
3727 | */ | |
3728 | DUK_ASSERT((DUK_OP_PREINCV & 0x03) == 0x00); | |
3729 | DUK_ASSERT((DUK_OP_PREDECV & 0x03) == 0x01); | |
3730 | DUK_ASSERT((DUK_OP_POSTINCV & 0x03) == 0x02); | |
3731 | DUK_ASSERT((DUK_OP_POSTDECV & 0x03) == 0x03); | |
3732 | ||
3733 | tv1 = DUK__CONSTP(bc); | |
3734 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv1)); | |
3735 | name = DUK_TVAL_GET_STRING(tv1); | |
3736 | DUK_ASSERT(name != NULL); | |
3737 | act = thr->callstack + thr->callstack_top - 1; | |
3738 | (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */ | |
3739 | ||
3740 | /* XXX: fastint fast path would be very useful here */ | |
3741 | ||
3742 | x = duk_to_number(ctx, -2); | |
3743 | duk_pop_2(ctx); | |
3744 | if (ins & DUK_ENC_OP(0x01)) { | |
3745 | y = x - 1.0; | |
3746 | } else { | |
3747 | y = x + 1.0; | |
3748 | } | |
3749 | ||
3750 | duk_push_number(ctx, y); | |
3751 | tv1 = duk_get_tval(ctx, -1); | |
3752 | DUK_ASSERT(tv1 != NULL); | |
3753 | duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT()); | |
3754 | duk_pop(ctx); | |
3755 | ||
3756 | duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y); | |
3757 | duk_replace(ctx, (duk_idx_t) a); | |
3758 | break; | |
3759 | } | |
3760 | ||
3761 | /* Preinc/predec for object properties. */ | |
3762 | case DUK_OP_PREINCP: | |
3763 | case DUK_OP_PREDECP: | |
3764 | case DUK_OP_POSTINCP: | |
3765 | case DUK_OP_POSTDECP: { | |
3766 | duk_context *ctx = (duk_context *) thr; | |
3767 | duk_small_uint_fast_t a = DUK_DEC_A(ins); | |
3768 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3769 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3770 | duk_tval *tv_obj; | |
3771 | duk_tval *tv_key; | |
3772 | duk_tval *tv_val; | |
3773 | duk_bool_t rc; | |
3774 | duk_double_t x, y; | |
3775 | ||
3776 | /* A -> target reg | |
3777 | * B -> object reg/const (may be const e.g. in "'foo'[1]") | |
3778 | * C -> key reg/const | |
3779 | */ | |
3780 | ||
3781 | /* Two lowest bits of opcode are used to distinguish | |
3782 | * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1). | |
3783 | */ | |
3784 | DUK_ASSERT((DUK_OP_PREINCP & 0x03) == 0x00); | |
3785 | DUK_ASSERT((DUK_OP_PREDECP & 0x03) == 0x01); | |
3786 | DUK_ASSERT((DUK_OP_POSTINCP & 0x03) == 0x02); | |
3787 | DUK_ASSERT((DUK_OP_POSTDECP & 0x03) == 0x03); | |
3788 | ||
3789 | tv_obj = DUK__REGCONSTP(b); | |
3790 | tv_key = DUK__REGCONSTP(c); | |
3791 | rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */ | |
3792 | DUK_UNREF(rc); /* ignore */ | |
3793 | tv_obj = NULL; /* invalidated */ | |
3794 | tv_key = NULL; /* invalidated */ | |
3795 | ||
3796 | x = duk_to_number(ctx, -1); | |
3797 | duk_pop(ctx); | |
3798 | if (ins & DUK_ENC_OP(0x01)) { | |
3799 | y = x - 1.0; | |
3800 | } else { | |
3801 | y = x + 1.0; | |
3802 | } | |
3803 | ||
3804 | duk_push_number(ctx, y); | |
3805 | tv_val = duk_get_tval(ctx, -1); | |
3806 | DUK_ASSERT(tv_val != NULL); | |
3807 | tv_obj = DUK__REGCONSTP(b); | |
3808 | tv_key = DUK__REGCONSTP(c); | |
3809 | rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT()); | |
3810 | DUK_UNREF(rc); /* ignore */ | |
3811 | tv_obj = NULL; /* invalidated */ | |
3812 | tv_key = NULL; /* invalidated */ | |
3813 | duk_pop(ctx); | |
3814 | ||
3815 | duk_push_number(ctx, (ins & DUK_ENC_OP(0x02)) ? x : y); | |
3816 | duk_replace(ctx, (duk_idx_t) a); | |
3817 | break; | |
3818 | } | |
3819 | ||
3820 | case DUK_OP_EXTRA: { | |
3821 | /* XXX: shared decoding of 'b' and 'c'? */ | |
3822 | ||
3823 | duk_small_uint_fast_t extraop = DUK_DEC_A(ins); | |
3824 | switch ((int) extraop) { | |
3825 | /* XXX: switch cast? */ | |
3826 | ||
3827 | case DUK_EXTRAOP_NOP: { | |
3828 | /* nop */ | |
3829 | break; | |
3830 | } | |
3831 | ||
3832 | case DUK_EXTRAOP_INVALID: { | |
3833 | DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "INVALID opcode (%ld)", (long) DUK_DEC_BC(ins)); | |
3834 | break; | |
3835 | } | |
3836 | ||
3837 | case DUK_EXTRAOP_LDTHIS: { | |
3838 | /* Note: 'this' may be bound to any value, not just an object */ | |
3839 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3840 | duk_tval tv_tmp; | |
3841 | duk_tval *tv1, *tv2; | |
3842 | ||
3843 | tv1 = DUK__REGP(bc); | |
3844 | tv2 = thr->valstack_bottom - 1; /* 'this binding' is just under bottom */ | |
3845 | DUK_ASSERT(tv2 >= thr->valstack); | |
3846 | ||
3847 | DUK_DDD(DUK_DDDPRINT("LDTHIS: %!T to r%ld", (duk_tval *) tv2, (long) bc)); | |
3848 | ||
3849 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
3850 | DUK_TVAL_SET_TVAL(tv1, tv2); | |
3851 | DUK_TVAL_INCREF(thr, tv1); | |
3852 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
3853 | break; | |
3854 | } | |
3855 | ||
3856 | case DUK_EXTRAOP_LDUNDEF: { | |
3857 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3858 | duk_tval tv_tmp; | |
3859 | duk_tval *tv1; | |
3860 | ||
3861 | tv1 = DUK__REGP(bc); | |
3862 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
3863 | DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1); | |
3864 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
3865 | break; | |
3866 | } | |
3867 | ||
3868 | case DUK_EXTRAOP_LDNULL: { | |
3869 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3870 | duk_tval tv_tmp; | |
3871 | duk_tval *tv1; | |
3872 | ||
3873 | tv1 = DUK__REGP(bc); | |
3874 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
3875 | DUK_TVAL_SET_NULL(tv1); | |
3876 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
3877 | break; | |
3878 | } | |
3879 | ||
3880 | case DUK_EXTRAOP_LDTRUE: | |
3881 | case DUK_EXTRAOP_LDFALSE: { | |
3882 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3883 | duk_tval tv_tmp; | |
3884 | duk_tval *tv1; | |
3885 | duk_small_uint_fast_t bval = (extraop == DUK_EXTRAOP_LDTRUE ? 1 : 0); | |
3886 | ||
3887 | tv1 = DUK__REGP(bc); | |
3888 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
3889 | DUK_TVAL_SET_BOOLEAN(tv1, bval); | |
3890 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
3891 | break; | |
3892 | } | |
3893 | ||
3894 | case DUK_EXTRAOP_NEWOBJ: { | |
3895 | duk_context *ctx = (duk_context *) thr; | |
3896 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3897 | ||
3898 | duk_push_object(ctx); | |
3899 | duk_replace(ctx, (duk_idx_t) b); | |
3900 | break; | |
3901 | } | |
3902 | ||
3903 | case DUK_EXTRAOP_NEWARR: { | |
3904 | duk_context *ctx = (duk_context *) thr; | |
3905 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3906 | ||
3907 | duk_push_array(ctx); | |
3908 | duk_replace(ctx, (duk_idx_t) b); | |
3909 | break; | |
3910 | } | |
3911 | ||
3912 | case DUK_EXTRAOP_SETALEN: { | |
3913 | duk_small_uint_fast_t b; | |
3914 | duk_small_uint_fast_t c; | |
3915 | duk_tval *tv1; | |
3916 | duk_hobject *h; | |
3917 | duk_uint32_t len; | |
3918 | ||
3919 | b = DUK_DEC_B(ins); tv1 = DUK__REGP(b); | |
3920 | DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1)); | |
3921 | h = DUK_TVAL_GET_OBJECT(tv1); | |
3922 | ||
3923 | c = DUK_DEC_C(ins); tv1 = DUK__REGP(c); | |
3924 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1)); | |
3925 | len = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1); | |
3926 | ||
3927 | duk_hobject_set_length(thr, h, len); | |
3928 | ||
3929 | break; | |
3930 | } | |
3931 | ||
3932 | case DUK_EXTRAOP_TYPEOF: { | |
3933 | duk_context *ctx = (duk_context *) thr; | |
3934 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
3935 | duk_push_hstring(ctx, duk_js_typeof(thr, DUK__REGP(bc))); | |
3936 | duk_replace(ctx, (duk_idx_t) bc); | |
3937 | break; | |
3938 | } | |
3939 | ||
3940 | case DUK_EXTRAOP_TYPEOFID: { | |
3941 | duk_context *ctx = (duk_context *) thr; | |
3942 | duk_activation *act; | |
3943 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3944 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3945 | duk_hstring *name; | |
3946 | duk_tval *tv; | |
3947 | ||
3948 | /* B -> target register | |
3949 | * C -> constant index of identifier name | |
3950 | */ | |
3951 | ||
3952 | tv = DUK__REGCONSTP(c); /* XXX: this could be a DUK__CONSTP instead */ | |
3953 | DUK_ASSERT(DUK_TVAL_IS_STRING(tv)); | |
3954 | name = DUK_TVAL_GET_STRING(tv); | |
3955 | act = thr->callstack + thr->callstack_top - 1; | |
3956 | if (duk_js_getvar_activation(thr, act, name, 0 /*throw*/)) { | |
3957 | /* -> [... val this] */ | |
3958 | tv = duk_get_tval(ctx, -2); | |
3959 | duk_push_hstring(ctx, duk_js_typeof(thr, tv)); | |
3960 | duk_replace(ctx, (duk_idx_t) b); | |
3961 | duk_pop_2(ctx); | |
3962 | } else { | |
3963 | /* unresolvable, no stack changes */ | |
3964 | duk_push_hstring_stridx(ctx, DUK_STRIDX_LC_UNDEFINED); | |
3965 | duk_replace(ctx, (duk_idx_t) b); | |
3966 | } | |
3967 | ||
3968 | break; | |
3969 | } | |
3970 | ||
3971 | case DUK_EXTRAOP_INITENUM: { | |
3972 | duk_context *ctx = (duk_context *) thr; | |
3973 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
3974 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
3975 | ||
3976 | /* | |
3977 | * Enumeration semantics come from for-in statement, E5 Section 12.6.4. | |
3978 | * If called with 'null' or 'undefined', this opcode returns 'null' as | |
3979 | * the enumerator, which is special cased in NEXTENUM. This simplifies | |
3980 | * the compiler part | |
3981 | */ | |
3982 | ||
3983 | /* B -> register for writing enumerator object | |
3984 | * C -> value to be enumerated (register) | |
3985 | */ | |
3986 | ||
3987 | if (duk_is_null_or_undefined(ctx, (duk_idx_t) c)) { | |
3988 | duk_push_null(ctx); | |
3989 | duk_replace(ctx, (duk_idx_t) b); | |
3990 | } else { | |
3991 | duk_dup(ctx, (duk_idx_t) c); | |
3992 | duk_to_object(ctx, -1); | |
3993 | duk_hobject_enumerator_create(ctx, 0 /*enum_flags*/); /* [ ... val ] --> [ ... enum ] */ | |
3994 | duk_replace(ctx, (duk_idx_t) b); | |
3995 | } | |
3996 | break; | |
3997 | } | |
3998 | ||
3999 | case DUK_EXTRAOP_NEXTENUM: { | |
4000 | duk_context *ctx = (duk_context *) thr; | |
4001 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
4002 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
4003 | ||
4004 | /* | |
4005 | * NEXTENUM checks whether the enumerator still has unenumerated | |
4006 | * keys. If so, the next key is loaded to the target register | |
4007 | * and the next instruction is skipped. Otherwise the next instruction | |
4008 | * will be executed, jumping out of the enumeration loop. | |
4009 | */ | |
4010 | ||
4011 | /* B -> target register for next key | |
4012 | * C -> enum register | |
4013 | */ | |
4014 | ||
4015 | DUK_DDD(DUK_DDDPRINT("NEXTENUM: b->%!T, c->%!T", | |
4016 | (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b), | |
4017 | (duk_tval *) duk_get_tval(ctx, (duk_idx_t) c))); | |
4018 | ||
4019 | if (duk_is_object(ctx, (duk_idx_t) c)) { | |
4020 | /* XXX: assert 'c' is an enumerator */ | |
4021 | duk_dup(ctx, (duk_idx_t) c); | |
4022 | if (duk_hobject_enumerator_next(ctx, 0 /*get_value*/)) { | |
4023 | /* [ ... enum ] -> [ ... next_key ] */ | |
4024 | DUK_DDD(DUK_DDDPRINT("enum active, next key is %!T, skip jump slot ", | |
4025 | (duk_tval *) duk_get_tval(ctx, -1))); | |
4026 | curr_pc++; | |
4027 | } else { | |
4028 | /* [ ... enum ] -> [ ... ] */ | |
4029 | DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot")); | |
4030 | duk_push_undefined(ctx); | |
4031 | } | |
4032 | duk_replace(ctx, (duk_idx_t) b); | |
4033 | } else { | |
4034 | /* 'null' enumerator case -> behave as with an empty enumerator */ | |
4035 | DUK_ASSERT(duk_is_null(ctx, (duk_idx_t) c)); | |
4036 | DUK_DDD(DUK_DDDPRINT("enum is null, execute jump slot")); | |
4037 | } | |
4038 | break; | |
4039 | } | |
4040 | ||
4041 | case DUK_EXTRAOP_INITSET: | |
4042 | case DUK_EXTRAOP_INITSETI: | |
4043 | case DUK_EXTRAOP_INITGET: | |
4044 | case DUK_EXTRAOP_INITGETI: { | |
4045 | duk_context *ctx = (duk_context *) thr; | |
4046 | duk_bool_t is_set = (extraop == DUK_EXTRAOP_INITSET || extraop == DUK_EXTRAOP_INITSETI); | |
4047 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
4048 | duk_uint_fast_t idx; | |
4049 | ||
4050 | /* B -> object register | |
4051 | * C -> C+0 contains key, C+1 closure (value) | |
4052 | */ | |
4053 | ||
4054 | /* | |
4055 | * INITSET/INITGET are only used to initialize object literal keys. | |
4056 | * The compiler ensures that there cannot be a previous data property | |
4057 | * of the same name. It also ensures that setter and getter can only | |
4058 | * be initialized once (or not at all). | |
4059 | */ | |
4060 | ||
4061 | idx = (duk_uint_fast_t) DUK_DEC_C(ins); | |
4062 | if (extraop == DUK_EXTRAOP_INITSETI || extraop == DUK_EXTRAOP_INITGETI) { | |
4063 | duk_tval *tv_ind = DUK__REGP(idx); | |
4064 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); | |
4065 | idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); | |
4066 | } | |
4067 | ||
4068 | #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK) | |
4069 | if (idx + 2 > (duk_uint_fast_t) duk_get_top(ctx)) { | |
4070 | /* XXX: use duk_is_valid_index() instead? */ | |
4071 | /* XXX: improve check; check against nregs, not against top */ | |
4072 | DUK__INTERNAL_ERROR("INITSET/INITGET out of bounds"); | |
4073 | } | |
4074 | #endif | |
4075 | ||
4076 | /* XXX: this is now a very unoptimal implementation -- this can be | |
4077 | * made very simple by direct manipulation of the object internals, | |
4078 | * given the guarantees above. | |
4079 | */ | |
4080 | ||
4081 | duk_push_hobject_bidx(ctx, DUK_BIDX_OBJECT_CONSTRUCTOR); | |
4082 | duk_get_prop_stridx(ctx, -1, DUK_STRIDX_DEFINE_PROPERTY); | |
4083 | duk_push_undefined(ctx); | |
4084 | duk_dup(ctx, (duk_idx_t) b); | |
4085 | duk_dup(ctx, (duk_idx_t) (idx + 0)); | |
4086 | duk_push_object(ctx); /* -> [ Object defineProperty undefined obj key desc ] */ | |
4087 | ||
4088 | duk_push_true(ctx); | |
4089 | duk_put_prop_stridx(ctx, -2, DUK_STRIDX_ENUMERABLE); | |
4090 | duk_push_true(ctx); | |
4091 | duk_put_prop_stridx(ctx, -2, DUK_STRIDX_CONFIGURABLE); | |
4092 | duk_dup(ctx, (duk_idx_t) (idx + 1)); | |
4093 | duk_put_prop_stridx(ctx, -2, (is_set ? DUK_STRIDX_SET : DUK_STRIDX_GET)); | |
4094 | ||
4095 | DUK_DDD(DUK_DDDPRINT("INITGET/INITSET: obj=%!T, key=%!T, desc=%!T", | |
4096 | (duk_tval *) duk_get_tval(ctx, -3), | |
4097 | (duk_tval *) duk_get_tval(ctx, -2), | |
4098 | (duk_tval *) duk_get_tval(ctx, -1))); | |
4099 | ||
4100 | duk_call_method(ctx, 3); /* -> [ Object res ] */ | |
4101 | duk_pop_2(ctx); | |
4102 | ||
4103 | DUK_DDD(DUK_DDDPRINT("INITGET/INITSET AFTER: obj=%!T", | |
4104 | (duk_tval *) duk_get_tval(ctx, (duk_idx_t) b))); | |
4105 | break; | |
4106 | } | |
4107 | ||
4108 | case DUK_EXTRAOP_ENDTRY: { | |
4109 | duk_catcher *cat; | |
4110 | duk_tval tv_tmp; | |
4111 | duk_tval *tv1; | |
4112 | ||
4113 | DUK_ASSERT(thr->catchstack_top >= 1); | |
4114 | DUK_ASSERT(thr->callstack_top >= 1); | |
4115 | DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1); | |
4116 | ||
4117 | cat = thr->catchstack + thr->catchstack_top - 1; | |
4118 | ||
4119 | DUK_DDD(DUK_DDDPRINT("ENDTRY: clearing catch active flag (regardless of whether it was set or not)")); | |
4120 | DUK_CAT_CLEAR_CATCH_ENABLED(cat); | |
4121 | ||
4122 | if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) { | |
4123 | DUK_DDD(DUK_DDDPRINT("ENDTRY: finally part is active, jump through 2nd jump slot with 'normal continuation'")); | |
4124 | ||
4125 | tv1 = thr->valstack + cat->idx_base; | |
4126 | DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); | |
4127 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
4128 | DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1); | |
4129 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
4130 | tv1 = NULL; | |
4131 | ||
4132 | tv1 = thr->valstack + cat->idx_base + 1; | |
4133 | DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); | |
4134 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
4135 | DUK_TVAL_SET_NUMBER(tv1, (duk_double_t) DUK_LJ_TYPE_NORMAL); /* XXX: set int */ | |
4136 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
4137 | tv1 = NULL; | |
4138 | ||
4139 | DUK_CAT_CLEAR_FINALLY_ENABLED(cat); | |
4140 | } else { | |
4141 | DUK_DDD(DUK_DDDPRINT("ENDTRY: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)")); | |
4142 | duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1); | |
4143 | /* no need to unwind callstack */ | |
4144 | } | |
4145 | ||
4146 | curr_pc = cat->pc_base + 1; | |
4147 | break; | |
4148 | } | |
4149 | ||
4150 | case DUK_EXTRAOP_ENDCATCH: { | |
4151 | duk_activation *act; | |
4152 | duk_catcher *cat; | |
4153 | duk_tval tv_tmp; | |
4154 | duk_tval *tv1; | |
4155 | ||
4156 | DUK_ASSERT(thr->catchstack_top >= 1); | |
4157 | DUK_ASSERT(thr->callstack_top >= 1); | |
4158 | DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1); | |
4159 | ||
4160 | cat = thr->catchstack + thr->catchstack_top - 1; | |
4161 | DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat)); /* cleared before entering catch part */ | |
4162 | ||
4163 | act = thr->callstack + thr->callstack_top - 1; | |
4164 | ||
4165 | if (DUK_CAT_HAS_LEXENV_ACTIVE(cat)) { | |
4166 | duk_hobject *prev_env; | |
4167 | ||
4168 | /* 'with' binding has no catch clause, so can't be here unless a normal try-catch */ | |
4169 | DUK_ASSERT(DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat)); | |
4170 | DUK_ASSERT(act->lex_env != NULL); | |
4171 | ||
4172 | DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment")); | |
4173 | ||
4174 | prev_env = act->lex_env; | |
4175 | DUK_ASSERT(prev_env != NULL); | |
4176 | act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, prev_env); | |
4177 | DUK_CAT_CLEAR_LEXENV_ACTIVE(cat); | |
4178 | DUK_HOBJECT_DECREF(thr, prev_env); /* side effects */ | |
4179 | } | |
4180 | ||
4181 | if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) { | |
4182 | DUK_DDD(DUK_DDDPRINT("ENDCATCH: finally part is active, jump through 2nd jump slot with 'normal continuation'")); | |
4183 | ||
4184 | tv1 = thr->valstack + cat->idx_base; | |
4185 | DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); | |
4186 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
4187 | DUK_TVAL_SET_UNDEFINED_ACTUAL(tv1); | |
4188 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
4189 | tv1 = NULL; | |
4190 | ||
4191 | tv1 = thr->valstack + cat->idx_base + 1; | |
4192 | DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); | |
4193 | DUK_TVAL_SET_TVAL(&tv_tmp, tv1); | |
4194 | DUK_TVAL_SET_NUMBER(tv1, (duk_double_t) DUK_LJ_TYPE_NORMAL); /* XXX: set int */ | |
4195 | DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */ | |
4196 | tv1 = NULL; | |
4197 | ||
4198 | DUK_CAT_CLEAR_FINALLY_ENABLED(cat); | |
4199 | } else { | |
4200 | DUK_DDD(DUK_DDDPRINT("ENDCATCH: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)")); | |
4201 | duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1); | |
4202 | /* no need to unwind callstack */ | |
4203 | } | |
4204 | ||
4205 | curr_pc = cat->pc_base + 1; | |
4206 | break; | |
4207 | } | |
4208 | ||
4209 | case DUK_EXTRAOP_ENDFIN: { | |
4210 | duk_context *ctx = (duk_context *) thr; | |
4211 | duk_catcher *cat; | |
4212 | duk_tval *tv1; | |
4213 | duk_small_uint_fast_t cont_type; | |
4214 | ||
4215 | DUK_ASSERT(thr->catchstack_top >= 1); | |
4216 | DUK_ASSERT(thr->callstack_top >= 1); | |
4217 | DUK_ASSERT(thr->catchstack[thr->catchstack_top - 1].callstack_index == thr->callstack_top - 1); | |
4218 | ||
4219 | cat = thr->catchstack + thr->catchstack_top - 1; | |
4220 | ||
4221 | /* CATCH flag may be enabled or disabled here; it may be enabled if | |
4222 | * the statement has a catch block but the try block does not throw | |
4223 | * an error. | |
4224 | */ | |
4225 | DUK_ASSERT(!DUK_CAT_HAS_FINALLY_ENABLED(cat)); /* cleared before entering finally */ | |
4226 | /* XXX: assert idx_base */ | |
4227 | ||
4228 | DUK_DDD(DUK_DDDPRINT("ENDFIN: completion value=%!T, type=%!T", | |
4229 | (duk_tval *) (thr->valstack + cat->idx_base + 0), | |
4230 | (duk_tval *) (thr->valstack + cat->idx_base + 1))); | |
4231 | ||
4232 | tv1 = thr->valstack + cat->idx_base + 1; /* type */ | |
4233 | DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1)); | |
4234 | cont_type = (duk_small_uint_fast_t) DUK_TVAL_GET_NUMBER(tv1); | |
4235 | ||
4236 | if (cont_type == DUK_LJ_TYPE_NORMAL) { | |
4237 | DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'normal' (non-abrupt) completion -> " | |
4238 | "dismantle catcher, resume execution after ENDFIN")); | |
4239 | duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1); | |
4240 | /* no need to unwind callstack */ | |
4241 | } else { | |
4242 | DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> " | |
4243 | "dismantle catcher, re-throw error", | |
4244 | (long) cont_type)); | |
4245 | ||
4246 | duk_push_tval(ctx, thr->valstack + cat->idx_base); | |
4247 | ||
4248 | /* XXX: assert lj type valid */ | |
4249 | duk_err_setup_heap_ljstate(thr, (duk_small_int_t) cont_type); | |
4250 | ||
4251 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */ | |
4252 | DUK__SYNC_AND_NULL_CURR_PC(); | |
4253 | duk_err_longjmp(thr); | |
4254 | DUK_UNREACHABLE(); | |
4255 | } | |
4256 | ||
4257 | /* continue execution after ENDFIN */ | |
4258 | break; | |
4259 | } | |
4260 | ||
4261 | case DUK_EXTRAOP_THROW: { | |
4262 | duk_context *ctx = (duk_context *) thr; | |
4263 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4264 | ||
4265 | /* Note: errors are augmented when they are created, not | |
4266 | * when they are thrown. So, don't augment here, it would | |
4267 | * break re-throwing for instance. | |
4268 | */ | |
4269 | ||
4270 | /* Sync so that augmentation sees up-to-date activations, NULL | |
4271 | * thr->ptr_curr_pc so that it's not used if side effects occur | |
4272 | * in augmentation or longjmp handling. | |
4273 | */ | |
4274 | DUK__SYNC_AND_NULL_CURR_PC(); | |
4275 | ||
4276 | duk_dup(ctx, (duk_idx_t) bc); | |
4277 | DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (before throw augment)", | |
4278 | (duk_tval *) duk_get_tval(ctx, -1))); | |
4279 | #if defined(DUK_USE_AUGMENT_ERROR_THROW) | |
4280 | duk_err_augment_error_throw(thr); | |
4281 | DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (after throw augment)", | |
4282 | (duk_tval *) duk_get_tval(ctx, -1))); | |
4283 | #endif | |
4284 | ||
4285 | duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_THROW); | |
4286 | ||
4287 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */ | |
4288 | duk_err_longjmp(thr); | |
4289 | DUK_UNREACHABLE(); | |
4290 | break; | |
4291 | } | |
4292 | ||
4293 | case DUK_EXTRAOP_INVLHS: { | |
4294 | DUK_ERROR(thr, DUK_ERR_REFERENCE_ERROR, "invalid lvalue"); | |
4295 | ||
4296 | DUK_UNREACHABLE(); | |
4297 | break; | |
4298 | } | |
4299 | ||
4300 | case DUK_EXTRAOP_UNM: | |
4301 | case DUK_EXTRAOP_UNP: { | |
4302 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4303 | duk__vm_arith_unary_op(thr, DUK__REGP(bc), bc, extraop); | |
4304 | break; | |
4305 | } | |
4306 | ||
4307 | case DUK_EXTRAOP_DEBUGGER: { | |
4308 | /* Opcode only emitted by compiler when debugger | |
4309 | * support is enabled. Ignore it silently without | |
4310 | * debugger support, in case it has been loaded | |
4311 | * from precompiled bytecode. | |
4312 | */ | |
4313 | #if defined(DUK_USE_DEBUGGER_SUPPORT) | |
4314 | DUK_D(DUK_DPRINT("DEBUGGER statement encountered, halt execution")); | |
4315 | if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { | |
4316 | DUK_HEAP_SET_PAUSED(thr->heap); | |
4317 | DUK__SYNC_CURR_PC(); | |
4318 | goto restart_execution; | |
4319 | } | |
4320 | #else | |
4321 | DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support")); | |
4322 | #endif | |
4323 | break; | |
4324 | } | |
4325 | ||
4326 | case DUK_EXTRAOP_BREAK: { | |
4327 | duk_context *ctx = (duk_context *) thr; | |
4328 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4329 | ||
4330 | /* always the "slow break" variant (longjmp'ing); a "fast break" is | |
4331 | * simply an DUK_OP_JUMP. | |
4332 | */ | |
4333 | ||
4334 | DUK_DDD(DUK_DDDPRINT("BREAK: %ld", (long) bc)); | |
4335 | ||
4336 | duk_push_uint(ctx, (duk_uint_t) bc); | |
4337 | duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_BREAK); | |
4338 | ||
4339 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */ | |
4340 | DUK__SYNC_AND_NULL_CURR_PC(); | |
4341 | duk_err_longjmp(thr); | |
4342 | DUK_UNREACHABLE(); | |
4343 | break; | |
4344 | } | |
4345 | ||
4346 | case DUK_EXTRAOP_CONTINUE: { | |
4347 | duk_context *ctx = (duk_context *) thr; | |
4348 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4349 | ||
4350 | /* always the "slow continue" variant (longjmp'ing); a "fast continue" is | |
4351 | * simply an DUK_OP_JUMP. | |
4352 | */ | |
4353 | ||
4354 | DUK_DDD(DUK_DDDPRINT("CONTINUE: %ld", (long) bc)); | |
4355 | ||
4356 | duk_push_uint(ctx, (duk_uint_t) bc); | |
4357 | duk_err_setup_heap_ljstate(thr, DUK_LJ_TYPE_CONTINUE); | |
4358 | ||
4359 | DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */ | |
4360 | DUK__SYNC_AND_NULL_CURR_PC(); | |
4361 | duk_err_longjmp(thr); | |
4362 | DUK_UNREACHABLE(); | |
4363 | break; | |
4364 | } | |
4365 | ||
4366 | case DUK_EXTRAOP_BNOT: { | |
4367 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4368 | ||
4369 | duk__vm_bitwise_not(thr, DUK__REGP(bc), bc); | |
4370 | break; | |
4371 | } | |
4372 | ||
4373 | case DUK_EXTRAOP_LNOT: { | |
4374 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4375 | duk_tval *tv1; | |
4376 | ||
4377 | tv1 = DUK__REGP(bc); | |
4378 | duk__vm_logical_not(thr, tv1, tv1); | |
4379 | break; | |
4380 | } | |
4381 | ||
4382 | case DUK_EXTRAOP_INSTOF: { | |
4383 | duk_context *ctx = (duk_context *) thr; | |
4384 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
4385 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
4386 | duk_bool_t tmp; | |
4387 | ||
4388 | tmp = duk_js_instanceof(thr, DUK__REGP(b), DUK__REGCONSTP(c)); | |
4389 | duk_push_boolean(ctx, tmp); | |
4390 | duk_replace(ctx, (duk_idx_t) b); | |
4391 | break; | |
4392 | } | |
4393 | ||
4394 | case DUK_EXTRAOP_IN: { | |
4395 | duk_context *ctx = (duk_context *) thr; | |
4396 | duk_small_uint_fast_t b = DUK_DEC_B(ins); | |
4397 | duk_small_uint_fast_t c = DUK_DEC_C(ins); | |
4398 | duk_bool_t tmp; | |
4399 | ||
4400 | tmp = duk_js_in(thr, DUK__REGP(b), DUK__REGCONSTP(c)); | |
4401 | duk_push_boolean(ctx, tmp); | |
4402 | duk_replace(ctx, (duk_idx_t) b); | |
4403 | break; | |
4404 | } | |
4405 | ||
4406 | case DUK_EXTRAOP_LABEL: { | |
4407 | duk_catcher *cat; | |
4408 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4409 | ||
4410 | /* allocate catcher and populate it (should be atomic) */ | |
4411 | ||
4412 | duk_hthread_catchstack_grow(thr); | |
4413 | cat = thr->catchstack + thr->catchstack_top; | |
4414 | thr->catchstack_top++; | |
4415 | ||
4416 | cat->flags = DUK_CAT_TYPE_LABEL | (bc << DUK_CAT_LABEL_SHIFT); | |
4417 | cat->callstack_index = thr->callstack_top - 1; | |
4418 | cat->pc_base = (duk_instr_t *) curr_pc; /* pre-incremented, points to first jump slot */ | |
4419 | cat->idx_base = 0; /* unused for label */ | |
4420 | cat->h_varname = NULL; | |
4421 | ||
4422 | DUK_DDD(DUK_DDDPRINT("LABEL catcher: flags=0x%08lx, callstack_index=%ld, pc_base=%ld, " | |
4423 | "idx_base=%ld, h_varname=%!O, label_id=%ld", | |
4424 | (long) cat->flags, (long) cat->callstack_index, (long) cat->pc_base, | |
4425 | (long) cat->idx_base, (duk_heaphdr *) cat->h_varname, (long) DUK_CAT_GET_LABEL(cat))); | |
4426 | ||
4427 | curr_pc += 2; /* skip jump slots */ | |
4428 | break; | |
4429 | } | |
4430 | ||
4431 | case DUK_EXTRAOP_ENDLABEL: { | |
4432 | duk_catcher *cat; | |
4433 | #if defined(DUK_USE_DDDPRINT) || defined(DUK_USE_ASSERTIONS) | |
4434 | duk_uint_fast_t bc = DUK_DEC_BC(ins); | |
4435 | #endif | |
4436 | #if defined(DUK_USE_DDDPRINT) | |
4437 | DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc)); | |
4438 | #endif | |
4439 | ||
4440 | DUK_ASSERT(thr->catchstack_top >= 1); | |
4441 | ||
4442 | cat = thr->catchstack + thr->catchstack_top - 1; | |
4443 | DUK_UNREF(cat); | |
4444 | DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL); | |
4445 | DUK_ASSERT((duk_uint_fast_t) DUK_CAT_GET_LABEL(cat) == bc); | |
4446 | ||
4447 | duk_hthread_catchstack_unwind(thr, thr->catchstack_top - 1); | |
4448 | /* no need to unwind callstack */ | |
4449 | break; | |
4450 | } | |
4451 | ||
4452 | default: { | |
4453 | DUK__INTERNAL_ERROR("invalid extra opcode"); | |
4454 | } | |
4455 | ||
4456 | } /* end switch */ | |
4457 | ||
4458 | break; | |
4459 | } | |
4460 | ||
4461 | default: { | |
4462 | /* this should never be possible, because the switch-case is | |
4463 | * comprehensive | |
4464 | */ | |
4465 | DUK__INTERNAL_ERROR("invalid opcode"); | |
4466 | break; | |
4467 | } | |
4468 | ||
4469 | } /* end switch */ | |
4470 | } | |
4471 | DUK_UNREACHABLE(); | |
4472 | ||
4473 | #ifndef DUK_USE_VERBOSE_EXECUTOR_ERRORS | |
4474 | internal_error: | |
4475 | DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "internal error in bytecode executor"); | |
4476 | #endif | |
4477 | } | |
4478 | ||
4479 | #undef DUK__INTERNAL_ERROR | |
4480 | #undef DUK__SYNC_CURR_PC | |
4481 | #undef DUK__SYNC_AND_NULL_CURR_PC |