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