Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** vm.c - virtual machine for mruby |
3 | | ** |
4 | | ** See Copyright Notice in mruby.h |
5 | | */ |
6 | | |
7 | | #include <mruby.h> |
8 | | #include <mruby/array.h> |
9 | | #include <mruby/class.h> |
10 | | #include <mruby/hash.h> |
11 | | #include <mruby/irep.h> |
12 | | #include <mruby/numeric.h> |
13 | | #include <mruby/proc.h> |
14 | | #include <mruby/range.h> |
15 | | #include <mruby/string.h> |
16 | | #include <mruby/variable.h> |
17 | | #include <mruby/error.h> |
18 | | #include <mruby/opcode.h> |
19 | | #include "value_array.h" |
20 | | #include <mruby/throw.h> |
21 | | #include <mruby/dump.h> |
22 | | #include <mruby/internal.h> |
23 | | #include <mruby/presym.h> |
24 | | |
25 | | #ifdef MRB_NO_STDIO |
26 | | #if defined(__cplusplus) |
27 | | extern "C" { |
28 | | #endif |
29 | | void abort(void); |
30 | | #if defined(__cplusplus) |
31 | | } /* extern "C" */ |
32 | | #endif |
33 | | #endif |
34 | | |
35 | 952 | #define STACK_INIT_SIZE 128 |
36 | 952 | #define CALLINFO_INIT_SIZE 32 |
37 | | |
38 | | /* Define amount of linear stack growth. */ |
39 | | #ifndef MRB_STACK_GROWTH |
40 | 121 | #define MRB_STACK_GROWTH 128 |
41 | | #endif |
42 | | |
43 | | /* Maximum recursive depth. Should be set lower on memory constrained systems. */ |
44 | | #ifndef MRB_CALL_LEVEL_MAX |
45 | 5.22k | #define MRB_CALL_LEVEL_MAX 512 |
46 | | #endif |
47 | | |
48 | | /* Maximum stack depth. Should be set lower on memory constrained systems. |
49 | | The value below allows about 60000 recursive calls in the simplest case. */ |
50 | | #ifndef MRB_STACK_MAX |
51 | 60 | #define MRB_STACK_MAX (0x40000 - MRB_STACK_GROWTH) |
52 | | #endif |
53 | | |
54 | | #ifdef VM_DEBUG |
55 | | # define DEBUG(x) (x) |
56 | | #else |
57 | | # define DEBUG(x) |
58 | | #endif |
59 | | |
60 | | |
61 | | #ifndef MRB_GC_FIXED_ARENA |
62 | | static void |
63 | | mrb_gc_arena_shrink(mrb_state *mrb, int idx) |
64 | 306k | { |
65 | 306k | mrb_gc *gc = &mrb->gc; |
66 | 306k | int capa = gc->arena_capa; |
67 | | |
68 | 306k | gc->arena_idx = idx; |
69 | 306k | if (idx < capa / 4) { |
70 | 306k | capa >>= 2; |
71 | 306k | if (capa < MRB_GC_ARENA_SIZE) { |
72 | 306k | capa = MRB_GC_ARENA_SIZE; |
73 | 306k | } |
74 | 306k | if (capa != gc->arena_capa) { |
75 | 85 | gc->arena = (struct RBasic**)mrb_realloc(mrb, gc->arena, sizeof(struct RBasic*)*capa); |
76 | 85 | gc->arena_capa = capa; |
77 | 85 | } |
78 | 306k | } |
79 | 306k | } |
80 | | #else |
81 | | #define mrb_gc_arena_shrink(mrb,idx) mrb_gc_arena_restore(mrb,idx) |
82 | | #endif |
83 | | |
84 | 612k | #define CALL_MAXARGS 15 |
85 | | #define CALL_VARARGS (CALL_MAXARGS<<4 | CALL_MAXARGS) |
86 | | |
87 | | static inline void |
88 | | stack_clear(mrb_value *from, size_t count) |
89 | 128k | { |
90 | 540k | while (count-- > 0) { |
91 | 412k | SET_NIL_VALUE(*from); |
92 | 412k | from++; |
93 | 412k | } |
94 | 128k | } |
95 | | |
96 | | static inline void |
97 | | stack_copy(mrb_value *dst, const mrb_value *src, size_t size) |
98 | 11.4k | { |
99 | 11.4k | if (!src) return; |
100 | 10.9k | memcpy(dst, src, sizeof(mrb_value)*size); |
101 | 10.9k | } |
102 | | |
103 | | static void |
104 | | stack_init(mrb_state *mrb) |
105 | 476 | { |
106 | 476 | struct mrb_context *c = mrb->c; |
107 | | |
108 | | /* mrb_assert(mrb->stack == NULL); */ |
109 | 476 | c->stbase = (mrb_value*)mrb_calloc(mrb, STACK_INIT_SIZE, sizeof(mrb_value)); |
110 | 476 | c->stend = c->stbase + STACK_INIT_SIZE; |
111 | | |
112 | | /* mrb_assert(ci == NULL); */ |
113 | 476 | c->cibase = (mrb_callinfo*)mrb_calloc(mrb, CALLINFO_INIT_SIZE, sizeof(mrb_callinfo)); |
114 | 476 | c->ciend = c->cibase + CALLINFO_INIT_SIZE; |
115 | 476 | c->ci = c->cibase; |
116 | 476 | c->ci->u.target_class = mrb->object_class; |
117 | 476 | c->ci->stack = c->stbase; |
118 | 476 | } |
119 | | |
120 | | static inline void |
121 | | envadjust(mrb_state *mrb, mrb_value *oldbase, mrb_value *newbase, size_t oldsize) |
122 | 60 | { |
123 | 60 | mrb_callinfo *ci = mrb->c->cibase; |
124 | 60 | ptrdiff_t delta = newbase - oldbase; |
125 | | |
126 | 60 | if (delta == 0) return; |
127 | 180 | while (ci <= mrb->c->ci) { |
128 | 121 | struct REnv *e = mrb_vm_ci_env(ci); |
129 | 121 | mrb_value *st; |
130 | | |
131 | 121 | if (e && MRB_ENV_ONSTACK_P(e) && |
132 | 121 | (st = e->stack) && (size_t)(st - oldbase) < oldsize) { |
133 | 0 | e->stack += delta; |
134 | 0 | } |
135 | | |
136 | 121 | if (ci->proc && MRB_PROC_ENV_P(ci->proc) && e != MRB_PROC_ENV(ci->proc)) { |
137 | 0 | e = MRB_PROC_ENV(ci->proc); |
138 | |
|
139 | 0 | if (e && MRB_ENV_ONSTACK_P(e) && |
140 | 0 | (st = e->stack) && (size_t)(st - oldbase) < oldsize) { |
141 | 0 | e->stack += delta; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 121 | ci->stack += delta; |
146 | 121 | ci++; |
147 | 121 | } |
148 | 59 | } |
149 | | |
150 | | /** def rec; $deep =+ 1; if $deep > 1000; return 0; end; rec; end **/ |
151 | | |
152 | | static void |
153 | | stack_extend_alloc(mrb_state *mrb, mrb_int room) |
154 | 60 | { |
155 | 60 | mrb_value *oldbase = mrb->c->stbase; |
156 | 60 | mrb_value *newstack; |
157 | 60 | size_t oldsize = mrb->c->stend - mrb->c->stbase; |
158 | 60 | size_t size = oldsize; |
159 | 60 | size_t off = mrb->c->ci->stack ? mrb->c->stend - mrb->c->ci->stack : 0; |
160 | | |
161 | 60 | if (off > size) size = off; |
162 | | #ifdef MRB_STACK_EXTEND_DOUBLING |
163 | | if ((size_t)room <= size) |
164 | | size *= 2; |
165 | | else |
166 | | size += room; |
167 | | #else |
168 | | /* Use linear stack growth. |
169 | | It is slightly slower than doubling the stack space, |
170 | | but it saves memory on small devices. */ |
171 | 60 | if (room <= MRB_STACK_GROWTH) |
172 | 1 | size += MRB_STACK_GROWTH; |
173 | 59 | else |
174 | 59 | size += room; |
175 | 60 | #endif |
176 | | |
177 | 60 | newstack = (mrb_value*)mrb_realloc(mrb, mrb->c->stbase, sizeof(mrb_value) * size); |
178 | 60 | stack_clear(&(newstack[oldsize]), size - oldsize); |
179 | 60 | envadjust(mrb, oldbase, newstack, oldsize); |
180 | 60 | mrb->c->stbase = newstack; |
181 | 60 | mrb->c->stend = mrb->c->stbase + size; |
182 | | |
183 | | /* Raise an exception if the new stack size will be too large, |
184 | | to prevent infinite recursion. However, do this only after resizing the stack, so mrb_raise has stack space to work with. */ |
185 | 60 | if (size > MRB_STACK_MAX) { |
186 | 0 | mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err)); |
187 | 0 | } |
188 | 60 | } |
189 | | |
190 | | static inline void |
191 | | stack_extend(mrb_state *mrb, mrb_int room) |
192 | 230k | { |
193 | 230k | if (!mrb->c->ci->stack || mrb->c->ci->stack + room >= mrb->c->stend) { |
194 | 60 | stack_extend_alloc(mrb, room); |
195 | 60 | } |
196 | 230k | } |
197 | | |
198 | | MRB_API void |
199 | | mrb_stack_extend(mrb_state *mrb, mrb_int room) |
200 | 0 | { |
201 | 0 | stack_extend(mrb, room); |
202 | 0 | } |
203 | | |
204 | | static void |
205 | | stack_extend_adjust(mrb_state *mrb, mrb_int room, const mrb_value **argp) |
206 | 5.22k | { |
207 | 5.22k | const struct mrb_context *c = mrb->c; |
208 | 5.22k | ptrdiff_t voff = *argp - c->stbase; |
209 | | |
210 | 5.22k | if (voff < 0 || voff >= c->stend - c->stbase) { |
211 | 5.22k | stack_extend(mrb, room); |
212 | 5.22k | } |
213 | 0 | else { |
214 | 0 | stack_extend(mrb, room); |
215 | 0 | *argp = c->stbase + voff; |
216 | 0 | } |
217 | 5.22k | } |
218 | | |
219 | | static inline struct REnv* |
220 | | uvenv(mrb_state *mrb, mrb_int up) |
221 | 0 | { |
222 | 0 | const struct RProc *proc = mrb->c->ci->proc; |
223 | 0 | struct REnv *e; |
224 | |
|
225 | 0 | while (up--) { |
226 | 0 | proc = proc->upper; |
227 | 0 | if (!proc) return NULL; |
228 | 0 | } |
229 | 0 | e = MRB_PROC_ENV(proc); |
230 | 0 | if (e) return e; /* proc has enclosed env */ |
231 | 0 | else { |
232 | 0 | mrb_callinfo *ci = mrb->c->ci; |
233 | 0 | mrb_callinfo *cb = mrb->c->cibase; |
234 | |
|
235 | 0 | while (cb <= ci) { |
236 | 0 | if (ci->proc == proc) { |
237 | 0 | return mrb_vm_ci_env(ci); |
238 | 0 | } |
239 | 0 | ci--; |
240 | 0 | } |
241 | 0 | } |
242 | 0 | return NULL; |
243 | 0 | } |
244 | | |
245 | | static inline const struct RProc* |
246 | | top_proc(mrb_state *mrb, const struct RProc *proc) |
247 | 0 | { |
248 | 0 | while (proc->upper) { |
249 | 0 | if (MRB_PROC_SCOPE_P(proc) || MRB_PROC_STRICT_P(proc)) |
250 | 0 | return proc; |
251 | 0 | proc = proc->upper; |
252 | 0 | } |
253 | 0 | return proc; |
254 | 0 | } |
255 | | |
256 | 711k | #define CI_PROC_SET(ci, p) do {\ |
257 | 711k | ci->proc = p;\ |
258 | 711k | ci->pc = (p && !MRB_PROC_CFUNC_P(p) && p->body.irep) ? p->body.irep->iseq : NULL;\ |
259 | 711k | } while (0) |
260 | | |
261 | | void |
262 | | mrb_vm_ci_proc_set(mrb_callinfo *ci, const struct RProc *p) |
263 | 0 | { |
264 | 0 | CI_PROC_SET(ci, p); |
265 | 0 | } |
266 | | |
267 | 249k | #define CI_TARGET_CLASS(ci) (((ci)->u.env && (ci)->u.env->tt == MRB_TT_ENV)? (ci)->u.env->c : (ci)->u.target_class) |
268 | | |
269 | | struct RClass* |
270 | | mrb_vm_ci_target_class(const mrb_callinfo *ci) |
271 | 30.8k | { |
272 | 30.8k | return CI_TARGET_CLASS(ci); |
273 | 30.8k | } |
274 | | |
275 | | void |
276 | | mrb_vm_ci_target_class_set(mrb_callinfo *ci, struct RClass *tc) |
277 | 394 | { |
278 | 394 | struct REnv *e = ci->u.env; |
279 | 394 | if (e && e->tt == MRB_TT_ENV) { |
280 | 0 | e->c = tc; |
281 | 0 | } |
282 | 394 | else { |
283 | 394 | ci->u.target_class = tc; |
284 | 394 | } |
285 | 394 | } |
286 | | |
287 | 549k | #define CI_ENV(ci) (((ci)->u.env && (ci)->u.env->tt == MRB_TT_ENV)? (ci)->u.env : NULL) |
288 | | |
289 | | struct REnv* |
290 | | mrb_vm_ci_env(const mrb_callinfo *ci) |
291 | 6.30k | { |
292 | 6.30k | return CI_ENV(ci); |
293 | 6.30k | } |
294 | | |
295 | | static inline void |
296 | | ci_env_set(mrb_callinfo *ci, struct REnv *e) |
297 | 531k | { |
298 | 531k | if (ci->u.env) { |
299 | 530k | if (ci->u.env->tt == MRB_TT_ENV) { |
300 | 6.18k | if (e) { |
301 | 0 | e->c = ci->u.env->c; |
302 | 0 | ci->u.env = e; |
303 | 0 | } |
304 | 6.18k | else { |
305 | 6.18k | ci->u.target_class = ci->u.env->c; |
306 | 6.18k | } |
307 | 6.18k | } |
308 | 524k | else if (e) { |
309 | 0 | e->c = ci->u.target_class; |
310 | 0 | ci->u.env = e; |
311 | 0 | } |
312 | 530k | } |
313 | 476 | else { |
314 | 476 | ci->u.env = e; |
315 | 476 | } |
316 | 531k | } |
317 | | |
318 | | void |
319 | | mrb_vm_ci_env_set(mrb_callinfo *ci, struct REnv *e) |
320 | 0 | { |
321 | 0 | ci_env_set(ci, e); |
322 | 0 | } |
323 | | |
324 | | MRB_API void |
325 | | mrb_vm_ci_env_clear(mrb_state *mrb, mrb_callinfo *ci) |
326 | 18.0k | { |
327 | 18.0k | struct REnv *e = ci->u.env; |
328 | 18.0k | if (e && e->tt == MRB_TT_ENV) { |
329 | 0 | ci->u.target_class = e->c; |
330 | 0 | mrb_env_unshare(mrb, e, FALSE); |
331 | 0 | } |
332 | 18.0k | } |
333 | | |
334 | 480k | #define CINFO_NONE 0 // called method from mruby VM (without C functions) |
335 | 413k | #define CINFO_SKIP 1 // ignited mruby VM from C |
336 | 693k | #define CINFO_DIRECT 2 // called method from C |
337 | 476 | #define CINFO_RESUMED 3 // resumed by `Fiber.yield` (probably the main call is `mrb_fiber_resume()`) |
338 | | |
339 | 485k | #define BLK_PTR(b) ((mrb_proc_p(b)) ? mrb_proc_ptr(b) : NULL) |
340 | | |
341 | | static inline mrb_callinfo* |
342 | | cipush(mrb_state *mrb, mrb_int push_stacks, uint8_t cci, struct RClass *target_class, |
343 | | const struct RProc *proc, struct RProc *blk, mrb_sym mid, uint16_t argc) |
344 | 518k | { |
345 | 518k | struct mrb_context *c = mrb->c; |
346 | 518k | mrb_callinfo *ci = c->ci; |
347 | | |
348 | 518k | if (ci + 1 == c->ciend) { |
349 | 0 | ptrdiff_t size = ci - c->cibase; |
350 | |
|
351 | 0 | if (size > MRB_CALL_LEVEL_MAX) { |
352 | 0 | mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err)); |
353 | 0 | } |
354 | 0 | c->cibase = (mrb_callinfo*)mrb_realloc(mrb, c->cibase, sizeof(mrb_callinfo)*size*2); |
355 | 0 | c->ci = c->cibase + size; |
356 | 0 | c->ciend = c->cibase + size * 2; |
357 | 0 | } |
358 | 518k | ci = ++c->ci; |
359 | 518k | ci->mid = mid; |
360 | 518k | CI_PROC_SET(ci, proc); |
361 | 518k | ci->blk = blk; |
362 | 518k | ci->stack = ci[-1].stack + push_stacks; |
363 | 518k | ci->n = argc & 0xf; |
364 | 518k | ci->nk = (argc>>4) & 0xf; |
365 | 518k | ci->cci = cci; |
366 | 518k | ci->u.target_class = target_class; |
367 | | |
368 | 518k | return ci; |
369 | 518k | } |
370 | | |
371 | | mrb_bool |
372 | | mrb_env_unshare(mrb_state *mrb, struct REnv *e, mrb_bool noraise) |
373 | 18.9k | { |
374 | 18.9k | if (e == NULL) return TRUE; |
375 | 6.18k | if (!MRB_ENV_ONSTACK_P(e)) return TRUE; |
376 | 6.18k | if (e->cxt != mrb->c) return TRUE; |
377 | 6.18k | if (e == CI_ENV(mrb->c->cibase)) return TRUE; /* for mirb */ |
378 | | |
379 | 6.18k | size_t len = (size_t)MRB_ENV_LEN(e); |
380 | 6.18k | if (len == 0) { |
381 | 0 | e->stack = NULL; |
382 | 0 | MRB_ENV_CLOSE(e); |
383 | 0 | return TRUE; |
384 | 0 | } |
385 | | |
386 | 6.18k | size_t live = mrb->gc.live; |
387 | 6.18k | mrb_value *p = (mrb_value*)mrb_malloc_simple(mrb, sizeof(mrb_value)*len); |
388 | 6.18k | if (live != mrb->gc.live && mrb_object_dead_p(mrb, (struct RBasic*)e)) { |
389 | | // The e object is now subject to GC inside mrb_malloc_simple(). |
390 | | // Moreover, if NULL is returned due to mrb_malloc_simple() failure, simply ignore it. |
391 | 0 | mrb_free(mrb, p); |
392 | 0 | return TRUE; |
393 | 0 | } |
394 | 6.18k | else if (p) { |
395 | 6.18k | stack_copy(p, e->stack, len); |
396 | 6.18k | e->stack = p; |
397 | 6.18k | MRB_ENV_CLOSE(e); |
398 | 6.18k | mrb_write_barrier(mrb, (struct RBasic*)e); |
399 | 6.18k | return TRUE; |
400 | 6.18k | } |
401 | 0 | else { |
402 | 0 | e->stack = NULL; |
403 | 0 | MRB_ENV_CLOSE(e); |
404 | 0 | MRB_ENV_SET_LEN(e, 0); |
405 | 0 | MRB_ENV_SET_BIDX(e, 0); |
406 | 0 | if (!noraise) { |
407 | 0 | mrb_exc_raise(mrb, mrb_obj_value(mrb->nomem_err)); |
408 | 0 | } |
409 | 0 | return FALSE; |
410 | 0 | } |
411 | 6.18k | } |
412 | | |
413 | | static inline mrb_callinfo* |
414 | | cipop(mrb_state *mrb) |
415 | 518k | { |
416 | 518k | struct mrb_context *c = mrb->c; |
417 | 518k | mrb_callinfo *ci = c->ci; |
418 | 518k | struct REnv *env = CI_ENV(ci); |
419 | | |
420 | 518k | ci_env_set(ci, NULL); // make possible to free env by GC if not needed |
421 | 518k | struct RProc *b = ci->blk; |
422 | 518k | if (b && !mrb_object_dead_p(mrb, (struct RBasic*)b) && b->tt == MRB_TT_PROC && |
423 | 518k | !MRB_PROC_STRICT_P(b) && MRB_PROC_ENV(b) == CI_ENV(&ci[-1])) { |
424 | 6.18k | b->flags |= MRB_PROC_ORPHAN; |
425 | 6.18k | } |
426 | 518k | if (env && !mrb_env_unshare(mrb, env, TRUE)) { |
427 | 0 | c->ci--; // exceptions are handled at the method caller; see #3087 |
428 | 0 | mrb_exc_raise(mrb, mrb_obj_value(mrb->nomem_err)); |
429 | 0 | } |
430 | 518k | c->ci--; |
431 | 518k | return c->ci; |
432 | 518k | } |
433 | | |
434 | | MRB_API mrb_value |
435 | | mrb_protect_error(mrb_state *mrb, mrb_protect_error_func *body, void *userdata, mrb_bool *error) |
436 | 476 | { |
437 | 476 | struct mrb_jmpbuf *prev_jmp = mrb->jmp; |
438 | 476 | struct mrb_jmpbuf c_jmp; |
439 | 476 | mrb_value result = mrb_nil_value(); |
440 | 476 | int ai = mrb_gc_arena_save(mrb); |
441 | 476 | const struct mrb_context *c = mrb->c; |
442 | 476 | ptrdiff_t ci_index = c->ci - c->cibase; |
443 | | |
444 | 476 | if (error) { *error = FALSE; } |
445 | | |
446 | 476 | MRB_TRY(&c_jmp) { |
447 | 476 | mrb->jmp = &c_jmp; |
448 | 476 | result = body(mrb, userdata); |
449 | 476 | mrb->jmp = prev_jmp; |
450 | 476 | } |
451 | 476 | MRB_CATCH(&c_jmp) { |
452 | 0 | mrb->jmp = prev_jmp; |
453 | 0 | result = mrb_obj_value(mrb->exc); |
454 | 0 | mrb->exc = NULL; |
455 | 0 | if (error) { *error = TRUE; } |
456 | 0 | if (mrb->c == c) { |
457 | 0 | while (c->ci - c->cibase > ci_index) { |
458 | 0 | cipop(mrb); |
459 | 0 | } |
460 | 0 | } |
461 | 0 | else { |
462 | | // It was probably switched by mrb_fiber_resume(). |
463 | | // Simply destroy all successive CINFO_DIRECTs once the fiber has been switched. |
464 | 0 | c = mrb->c; |
465 | 0 | while (c->ci > c->cibase && c->ci->cci == CINFO_DIRECT) { |
466 | 0 | cipop(mrb); |
467 | 0 | } |
468 | 0 | } |
469 | 0 | } |
470 | 0 | MRB_END_EXC(&c_jmp); |
471 | | |
472 | 476 | mrb_gc_arena_restore(mrb, ai); |
473 | 476 | mrb_gc_protect(mrb, result); |
474 | 476 | return result; |
475 | 476 | } |
476 | | |
477 | | void mrb_exc_set(mrb_state *mrb, mrb_value exc); |
478 | | static mrb_value mrb_run(mrb_state *mrb, const struct RProc* proc, mrb_value self); |
479 | | |
480 | | #ifndef MRB_FUNCALL_ARGC_MAX |
481 | 0 | #define MRB_FUNCALL_ARGC_MAX 16 |
482 | | #endif |
483 | | |
484 | | MRB_API mrb_value |
485 | | mrb_funcall(mrb_state *mrb, mrb_value self, const char *name, mrb_int argc, ...) |
486 | 0 | { |
487 | 0 | mrb_value argv[MRB_FUNCALL_ARGC_MAX]; |
488 | 0 | va_list ap; |
489 | 0 | mrb_sym mid = mrb_intern_cstr(mrb, name); |
490 | |
|
491 | 0 | if (argc > MRB_FUNCALL_ARGC_MAX) { |
492 | 0 | mrb_raise(mrb, E_ARGUMENT_ERROR, "Too long arguments. (limit=" MRB_STRINGIZE(MRB_FUNCALL_ARGC_MAX) ")"); |
493 | 0 | } |
494 | | |
495 | 0 | va_start(ap, argc); |
496 | 0 | for (mrb_int i = 0; i < argc; i++) { |
497 | 0 | argv[i] = va_arg(ap, mrb_value); |
498 | 0 | } |
499 | 0 | va_end(ap); |
500 | 0 | return mrb_funcall_argv(mrb, self, mid, argc, argv); |
501 | 0 | } |
502 | | |
503 | | MRB_API mrb_value |
504 | | mrb_funcall_id(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc, ...) |
505 | 0 | { |
506 | 0 | mrb_value argv[MRB_FUNCALL_ARGC_MAX]; |
507 | 0 | va_list ap; |
508 | |
|
509 | 0 | if (argc > MRB_FUNCALL_ARGC_MAX) { |
510 | 0 | mrb_raise(mrb, E_ARGUMENT_ERROR, "Too long arguments. (limit=" MRB_STRINGIZE(MRB_FUNCALL_ARGC_MAX) ")"); |
511 | 0 | } |
512 | | |
513 | 0 | va_start(ap, argc); |
514 | 0 | for (mrb_int i = 0; i < argc; i++) { |
515 | 0 | argv[i] = va_arg(ap, mrb_value); |
516 | 0 | } |
517 | 0 | va_end(ap); |
518 | 0 | return mrb_funcall_argv(mrb, self, mid, argc, argv); |
519 | 0 | } |
520 | | |
521 | | static mrb_int |
522 | | mrb_ci_kidx(const mrb_callinfo *ci) |
523 | 30.2k | { |
524 | 30.2k | if (ci->nk == 0) return -1; |
525 | 30.2k | return (ci->n == CALL_MAXARGS) ? 2 : ci->n + 1; |
526 | 30.2k | } |
527 | | |
528 | | static inline mrb_int |
529 | | mrb_bidx(uint8_t n, uint8_t k) |
530 | 627k | { |
531 | 627k | if (n == 15) n = 1; |
532 | 627k | if (k == 15) n += 1; |
533 | 562k | else n += k*2; |
534 | 627k | return n + 1; /* self + args + kargs */ |
535 | 627k | } |
536 | | |
537 | | static inline mrb_int |
538 | | ci_bidx(mrb_callinfo *ci) |
539 | 146k | { |
540 | 146k | return mrb_bidx(ci->n, ci->nk); |
541 | 146k | } |
542 | | |
543 | | mrb_int |
544 | | mrb_ci_bidx(mrb_callinfo *ci) |
545 | 3.34k | { |
546 | 3.34k | return ci_bidx(ci); |
547 | 3.34k | } |
548 | | |
549 | | mrb_int |
550 | | mrb_ci_nregs(mrb_callinfo *ci) |
551 | 17.1k | { |
552 | 17.1k | const struct RProc *p; |
553 | | |
554 | 17.1k | if (!ci) return 4; |
555 | 17.1k | mrb_int nregs = ci_bidx(ci) + 1; /* self + args + kargs + blk */ |
556 | 17.1k | p = ci->proc; |
557 | 17.1k | if (p && !MRB_PROC_CFUNC_P(p) && p->body.irep && p->body.irep->nregs > nregs) { |
558 | 994 | return p->body.irep->nregs; |
559 | 994 | } |
560 | 16.1k | return nregs; |
561 | 17.1k | } |
562 | | |
563 | | mrb_value mrb_obj_missing(mrb_state *mrb, mrb_value mod); |
564 | | |
565 | | static mrb_method_t |
566 | | prepare_missing(mrb_state *mrb, mrb_callinfo *ci, mrb_value recv, mrb_sym mid, mrb_value blk, mrb_bool super) |
567 | 37 | { |
568 | 37 | mrb_sym missing = MRB_SYM(method_missing); |
569 | 37 | mrb_value *argv = &ci->stack[1]; |
570 | 37 | mrb_value args; |
571 | 37 | mrb_method_t m; |
572 | | |
573 | | /* pack positional arguments */ |
574 | 37 | if (ci->n == 15) args = argv[0]; |
575 | 37 | else args = mrb_ary_new_from_values(mrb, ci->n, argv); |
576 | | |
577 | 37 | if (mrb_func_basic_p(mrb, recv, missing, mrb_obj_missing)) { |
578 | 37 | method_missing: |
579 | 37 | if (super) mrb_no_method_error(mrb, mid, args, "no superclass method '%n'", mid); |
580 | 37 | else mrb_method_missing(mrb, mid, recv, args); |
581 | | /* not reached */ |
582 | 37 | } |
583 | 0 | if (mid != missing) { |
584 | 0 | ci->u.target_class = mrb_class(mrb, recv); |
585 | 0 | } |
586 | 0 | m = mrb_vm_find_method(mrb, ci->u.target_class, &ci->u.target_class, missing); |
587 | 0 | if (MRB_METHOD_UNDEF_P(m)) goto method_missing; /* just in case */ |
588 | 0 | stack_extend(mrb, 4); |
589 | |
|
590 | 0 | argv = &ci->stack[1]; /* maybe reallocated */ |
591 | 0 | argv[0] = args; |
592 | 0 | if (ci->nk == 0) { |
593 | 0 | argv[1] = blk; |
594 | 0 | } |
595 | 0 | else { |
596 | 0 | mrb_assert(ci->nk == 15); |
597 | 0 | argv[1] = argv[ci->n]; |
598 | 0 | argv[2] = blk; |
599 | 0 | } |
600 | 0 | ci->n = CALL_MAXARGS; |
601 | | /* ci->nk is already set to zero or CALL_MAXARGS */ |
602 | 0 | mrb_ary_unshift(mrb, args, mrb_symbol_value(mid)); |
603 | 0 | ci->mid = missing; |
604 | 0 | return m; |
605 | 0 | } |
606 | | |
607 | | static void |
608 | | funcall_args_capture(mrb_state *mrb, int stoff, mrb_int argc, const mrb_value *argv, mrb_value block, mrb_callinfo *ci) |
609 | 5.22k | { |
610 | 5.22k | if (argc < 0 || argc > INT32_MAX) { |
611 | 0 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative or too big argc for funcall (%i)", argc); |
612 | 0 | } |
613 | | |
614 | 5.22k | ci->nk = 0; /* funcall does not support keyword arguments */ |
615 | 5.22k | if (argc < CALL_MAXARGS) { |
616 | 5.22k | mrb_int extends = stoff + argc + 2 /* self + block */; |
617 | 5.22k | stack_extend_adjust(mrb, extends, &argv); |
618 | | |
619 | 5.22k | mrb_value *args = mrb->c->ci->stack + stoff + 1 /* self */; |
620 | 5.22k | stack_copy(args, argv, argc); |
621 | 5.22k | args[argc] = block; |
622 | 5.22k | ci->n = (uint8_t)argc; |
623 | 5.22k | } |
624 | 0 | else { |
625 | 0 | int extends = stoff + 3 /* self + splat + block */; |
626 | 0 | stack_extend_adjust(mrb, extends, &argv); |
627 | |
|
628 | 0 | mrb_value *args = mrb->c->ci->stack + stoff + 1 /* self */; |
629 | 0 | args[0] = mrb_ary_new_from_values(mrb, argc, argv); |
630 | 0 | args[1] = block; |
631 | 0 | ci->n = CALL_MAXARGS; |
632 | 0 | } |
633 | 5.22k | } |
634 | | |
635 | | static inline mrb_value |
636 | | ensure_block(mrb_state *mrb, mrb_value blk) |
637 | 110k | { |
638 | 110k | if (!mrb_nil_p(blk) && !mrb_proc_p(blk)) { |
639 | 0 | blk = mrb_type_convert(mrb, blk, MRB_TT_PROC, MRB_SYM(to_proc)); |
640 | | /* The stack might have been reallocated during mrb_type_convert(), see #3622 */ |
641 | 0 | } |
642 | 110k | return blk; |
643 | 110k | } |
644 | | |
645 | | MRB_API mrb_value |
646 | | mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc, const mrb_value *argv, mrb_value blk) |
647 | 5.22k | { |
648 | 5.22k | mrb_value val; |
649 | 5.22k | int ai = mrb_gc_arena_save(mrb); |
650 | | |
651 | 5.22k | if (!mrb->jmp) { |
652 | 0 | struct mrb_jmpbuf c_jmp; |
653 | 0 | ptrdiff_t nth_ci = mrb->c->ci - mrb->c->cibase; |
654 | |
|
655 | 0 | MRB_TRY(&c_jmp) { |
656 | 0 | mrb->jmp = &c_jmp; |
657 | | /* recursive call */ |
658 | 0 | val = mrb_funcall_with_block(mrb, self, mid, argc, argv, blk); |
659 | 0 | mrb->jmp = NULL; |
660 | 0 | } |
661 | 0 | MRB_CATCH(&c_jmp) { /* error */ |
662 | 0 | while (nth_ci < (mrb->c->ci - mrb->c->cibase)) { |
663 | 0 | cipop(mrb); |
664 | 0 | } |
665 | 0 | mrb->jmp = 0; |
666 | 0 | val = mrb_obj_value(mrb->exc); |
667 | 0 | } |
668 | 0 | MRB_END_EXC(&c_jmp); |
669 | 0 | mrb->jmp = NULL; |
670 | 0 | } |
671 | 5.22k | else { |
672 | 5.22k | mrb_method_t m; |
673 | 5.22k | mrb_callinfo *ci = mrb->c->ci; |
674 | 5.22k | mrb_int n = mrb_ci_nregs(ci); |
675 | | |
676 | 5.22k | if (!mrb->c->stbase) { |
677 | 0 | stack_init(mrb); |
678 | 0 | } |
679 | 5.22k | if (ci - mrb->c->cibase > MRB_CALL_LEVEL_MAX) { |
680 | 0 | mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err)); |
681 | 0 | } |
682 | 5.22k | blk = ensure_block(mrb, blk); |
683 | 5.22k | ci = cipush(mrb, n, CINFO_DIRECT, NULL, NULL, BLK_PTR(blk), 0, 0); |
684 | 5.22k | funcall_args_capture(mrb, 0, argc, argv, blk, ci); |
685 | 5.22k | ci->u.target_class = mrb_class(mrb, self); |
686 | 5.22k | m = mrb_vm_find_method(mrb, ci->u.target_class, &ci->u.target_class, mid); |
687 | 5.22k | if (MRB_METHOD_UNDEF_P(m)) { |
688 | 0 | m = prepare_missing(mrb, ci, self, mid, mrb_nil_value(), FALSE); |
689 | 0 | } |
690 | 5.22k | else { |
691 | 5.22k | ci->mid = mid; |
692 | 5.22k | } |
693 | 5.22k | ci->proc = MRB_METHOD_PROC_P(m) ? MRB_METHOD_PROC(m) : NULL; |
694 | | |
695 | 5.22k | if (MRB_METHOD_CFUNC_P(m)) { |
696 | 5.20k | ci->stack[0] = self; |
697 | 5.20k | val = MRB_METHOD_CFUNC(m)(mrb, self); |
698 | 5.20k | cipop(mrb); |
699 | 5.20k | } |
700 | 16 | else { |
701 | 16 | ci->cci = CINFO_SKIP; |
702 | 16 | val = mrb_run(mrb, ci->proc, self); |
703 | 16 | } |
704 | 5.22k | } |
705 | 5.22k | mrb_gc_arena_restore(mrb, ai); |
706 | 5.22k | mrb_gc_protect(mrb, val); |
707 | 5.22k | return val; |
708 | 5.22k | } |
709 | | |
710 | | MRB_API mrb_value |
711 | | mrb_funcall_argv(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc, const mrb_value *argv) |
712 | 5.22k | { |
713 | 5.22k | return mrb_funcall_with_block(mrb, self, mid, argc, argv, mrb_nil_value()); |
714 | 5.22k | } |
715 | | |
716 | | static void |
717 | | check_method_noarg(mrb_state *mrb, const mrb_callinfo *ci) |
718 | 64.0k | { |
719 | 64.0k | mrb_int argc = ci->n == CALL_MAXARGS ? RARRAY_LEN(ci->stack[1]) : ci->n; |
720 | 64.0k | if (ci->nk > 0) { |
721 | 476 | mrb_value kdict = ci->stack[mrb_ci_kidx(ci)]; |
722 | 476 | if (!(mrb_hash_p(kdict) && mrb_hash_empty_p(mrb, kdict))) { |
723 | 0 | argc++; |
724 | 0 | } |
725 | 476 | } |
726 | 64.0k | if (argc > 0) { |
727 | 3 | mrb_argnum_error(mrb, argc, 0, 0); |
728 | 3 | } |
729 | 64.0k | } |
730 | | |
731 | | static mrb_value |
732 | | exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p) |
733 | 0 | { |
734 | 0 | mrb_callinfo *ci = mrb->c->ci; |
735 | 0 | mrb_int keep, nregs; |
736 | |
|
737 | 0 | ci->stack[0] = self; |
738 | 0 | CI_PROC_SET(ci, p); |
739 | 0 | if (MRB_PROC_CFUNC_P(p)) { |
740 | 0 | if (MRB_PROC_NOARG_P(p)) { |
741 | 0 | check_method_noarg(mrb, ci); |
742 | 0 | } |
743 | 0 | return MRB_PROC_CFUNC(p)(mrb, self); |
744 | 0 | } |
745 | 0 | nregs = p->body.irep->nregs; |
746 | 0 | keep = ci_bidx(ci)+1; |
747 | 0 | if (nregs < keep) { |
748 | 0 | stack_extend(mrb, keep); |
749 | 0 | } |
750 | 0 | else { |
751 | 0 | stack_extend(mrb, nregs); |
752 | 0 | stack_clear(ci->stack+keep, nregs-keep); |
753 | 0 | } |
754 | |
|
755 | 0 | cipush(mrb, 0, 0, NULL, NULL, NULL, 0, 0); |
756 | |
|
757 | 0 | return self; |
758 | 0 | } |
759 | | |
760 | | mrb_value |
761 | | mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p) |
762 | 0 | { |
763 | 0 | mrb_callinfo *ci = mrb->c->ci; |
764 | 0 | if (ci->cci == CINFO_NONE) { |
765 | 0 | return exec_irep(mrb, self, p); |
766 | 0 | } |
767 | 0 | else { |
768 | 0 | mrb_value ret; |
769 | 0 | if (MRB_PROC_CFUNC_P(p)) { |
770 | 0 | if (MRB_PROC_NOARG_P(p)) { |
771 | 0 | check_method_noarg(mrb, ci); |
772 | 0 | } |
773 | 0 | cipush(mrb, 0, CINFO_DIRECT, CI_TARGET_CLASS(ci), p, NULL, ci->mid, ci->n|(ci->nk<<4)); |
774 | 0 | ret = MRB_PROC_CFUNC(p)(mrb, self); |
775 | 0 | cipop(mrb); |
776 | 0 | } |
777 | 0 | else { |
778 | 0 | mrb_int keep = ci_bidx(ci) + 1; /* receiver + block */ |
779 | 0 | ret = mrb_top_run(mrb, p, self, keep); |
780 | 0 | } |
781 | 0 | if (mrb->exc && mrb->jmp) { |
782 | 0 | mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); |
783 | 0 | } |
784 | 0 | return ret; |
785 | 0 | } |
786 | 0 | } |
787 | | |
788 | | /* 15.3.1.3.4 */ |
789 | | /* 15.3.1.3.44 */ |
790 | | /* |
791 | | * call-seq: |
792 | | * obj.send(symbol [, args...]) -> obj |
793 | | * obj.__send__(symbol [, args...]) -> obj |
794 | | * |
795 | | * Invokes the method identified by _symbol_, passing it any |
796 | | * arguments specified. You can use <code>__send__</code> if the name |
797 | | * +send+ clashes with an existing method in _obj_. |
798 | | * |
799 | | * class Klass |
800 | | * def hello(*args) |
801 | | * "Hello " + args.join(' ') |
802 | | * end |
803 | | * end |
804 | | * k = Klass.new |
805 | | * k.send :hello, "gentle", "readers" #=> "Hello gentle readers" |
806 | | */ |
807 | | mrb_value |
808 | | mrb_f_send(mrb_state *mrb, mrb_value self) |
809 | 0 | { |
810 | 0 | mrb_sym name; |
811 | 0 | mrb_value block, *regs; |
812 | 0 | mrb_method_t m; |
813 | 0 | struct RClass *c; |
814 | 0 | mrb_callinfo *ci = mrb->c->ci; |
815 | 0 | int n = ci->n; |
816 | |
|
817 | 0 | if (ci->cci > CINFO_NONE) { |
818 | 0 | funcall:; |
819 | 0 | const mrb_value *argv; |
820 | 0 | mrb_int argc; |
821 | 0 | mrb_get_args(mrb, "n*&", &name, &argv, &argc, &block); |
822 | 0 | return mrb_funcall_with_block(mrb, self, name, argc, argv, block); |
823 | 0 | } |
824 | | |
825 | 0 | regs = mrb->c->ci->stack+1; |
826 | |
|
827 | 0 | if (n == 0) { |
828 | 0 | argnum_error: |
829 | 0 | mrb_argnum_error(mrb, 0, 1, -1); |
830 | 0 | } |
831 | 0 | else if (n == 15) { |
832 | 0 | if (RARRAY_LEN(regs[0]) == 0) goto argnum_error; |
833 | 0 | name = mrb_obj_to_sym(mrb, RARRAY_PTR(regs[0])[0]); |
834 | 0 | } |
835 | 0 | else { |
836 | 0 | name = mrb_obj_to_sym(mrb, regs[0]); |
837 | 0 | } |
838 | | |
839 | 0 | c = mrb_class(mrb, self); |
840 | 0 | m = mrb_vm_find_method(mrb, c, &c, name); |
841 | 0 | if (MRB_METHOD_UNDEF_P(m)) { /* call method_mising */ |
842 | 0 | goto funcall; |
843 | 0 | } |
844 | | |
845 | 0 | ci->mid = name; |
846 | 0 | ci->u.target_class = c; |
847 | | /* remove first symbol from arguments */ |
848 | 0 | if (n == 15) { /* variable length arguments */ |
849 | 0 | regs[0] = mrb_ary_subseq(mrb, regs[0], 1, RARRAY_LEN(regs[0]) - 1); |
850 | 0 | } |
851 | 0 | else { /* n > 0 */ |
852 | 0 | for (int i=0; i<n; i++) { |
853 | 0 | regs[i] = regs[i+1]; |
854 | 0 | } |
855 | 0 | regs[n] = regs[n+1]; /* copy kdict or block */ |
856 | 0 | if (ci->nk > 0) { |
857 | 0 | regs[n+1] = regs[n+2]; /* copy block */ |
858 | 0 | } |
859 | 0 | ci->n--; |
860 | 0 | } |
861 | |
|
862 | 0 | if (MRB_METHOD_CFUNC_P(m)) { |
863 | 0 | if (MRB_METHOD_NOARG_P(m)) { |
864 | 0 | check_method_noarg(mrb, ci); |
865 | 0 | } |
866 | |
|
867 | 0 | if (MRB_METHOD_PROC_P(m)) { |
868 | 0 | const struct RProc *p = MRB_METHOD_PROC(m); |
869 | 0 | CI_PROC_SET(ci, p); |
870 | 0 | } |
871 | 0 | return MRB_METHOD_CFUNC(m)(mrb, self); |
872 | 0 | } |
873 | 0 | return exec_irep(mrb, self, MRB_METHOD_PROC(m)); |
874 | 0 | } |
875 | | |
876 | | static void |
877 | | check_block(mrb_state *mrb, mrb_value blk) |
878 | 476 | { |
879 | 476 | if (mrb_nil_p(blk)) { |
880 | 0 | mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given"); |
881 | 0 | } |
882 | 476 | if (!mrb_proc_p(blk)) { |
883 | 0 | mrb_raise(mrb, E_TYPE_ERROR, "not a block"); |
884 | 0 | } |
885 | 476 | } |
886 | | |
887 | | static mrb_value |
888 | | eval_under(mrb_state *mrb, mrb_value self, mrb_value blk, struct RClass *c) |
889 | 476 | { |
890 | 476 | struct RProc *p; |
891 | 476 | mrb_callinfo *ci; |
892 | 476 | int nregs; |
893 | | |
894 | 476 | check_block(mrb, blk); |
895 | 476 | ci = mrb->c->ci; |
896 | 476 | if (ci->cci == CINFO_DIRECT) { |
897 | 0 | return mrb_yield_with_class(mrb, blk, 1, &self, self, c); |
898 | 0 | } |
899 | 476 | ci->u.target_class = c; |
900 | 476 | p = mrb_proc_ptr(blk); |
901 | 476 | CI_PROC_SET(ci, p); |
902 | 476 | ci->n = 1; |
903 | 476 | ci->nk = 0; |
904 | 476 | ci->mid = ci[-1].mid; |
905 | 476 | if (MRB_PROC_CFUNC_P(p)) { |
906 | 0 | stack_extend(mrb, 4); |
907 | 0 | mrb->c->ci->stack[0] = self; |
908 | 0 | mrb->c->ci->stack[1] = self; |
909 | 0 | mrb->c->ci->stack[2] = mrb_nil_value(); |
910 | 0 | return MRB_PROC_CFUNC(p)(mrb, self); |
911 | 0 | } |
912 | 476 | nregs = p->body.irep->nregs; |
913 | 476 | if (nregs < 4) nregs = 4; |
914 | 476 | stack_extend(mrb, nregs); |
915 | 476 | mrb->c->ci->stack[0] = self; |
916 | 476 | mrb->c->ci->stack[1] = self; |
917 | 476 | stack_clear(mrb->c->ci->stack+2, nregs-2); |
918 | 476 | ci = cipush(mrb, 0, 0, NULL, NULL, NULL, 0, 0); |
919 | | |
920 | 476 | return self; |
921 | 476 | } |
922 | | |
923 | | /* 15.2.2.4.35 */ |
924 | | /* |
925 | | * call-seq: |
926 | | * mod.class_eval {| | block } -> obj |
927 | | * mod.module_eval {| | block } -> obj |
928 | | * |
929 | | * Evaluates block in the context of _mod_. This can |
930 | | * be used to add methods to a class. <code>module_eval</code> returns |
931 | | * the result of evaluating its argument. |
932 | | */ |
933 | | mrb_value |
934 | | mrb_mod_module_eval(mrb_state *mrb, mrb_value mod) |
935 | 476 | { |
936 | 476 | mrb_value a, b; |
937 | | |
938 | 476 | if (mrb_get_args(mrb, "|S&", &a, &b) == 1) { |
939 | 0 | mrb_raise(mrb, E_NOTIMP_ERROR, "module_eval/class_eval with string not implemented"); |
940 | 0 | } |
941 | 476 | return eval_under(mrb, mod, b, mrb_class_ptr(mod)); |
942 | 476 | } |
943 | | |
944 | | /* 15.3.1.3.18 */ |
945 | | /* |
946 | | * call-seq: |
947 | | * obj.instance_eval {| | block } -> obj |
948 | | * |
949 | | * Evaluates the given block,within the context of the receiver (_obj_). |
950 | | * In order to set the context, the variable +self+ is set to _obj_ while |
951 | | * the code is executing, giving the code access to _obj_'s |
952 | | * instance variables. In the version of <code>instance_eval</code> |
953 | | * that takes a +String+, the optional second and third |
954 | | * parameters supply a filename and starting line number that are used |
955 | | * when reporting compilation errors. |
956 | | * |
957 | | * class KlassWithSecret |
958 | | * def initialize |
959 | | * @secret = 99 |
960 | | * end |
961 | | * end |
962 | | * k = KlassWithSecret.new |
963 | | * k.instance_eval { @secret } #=> 99 |
964 | | */ |
965 | | mrb_value |
966 | | mrb_obj_instance_eval(mrb_state *mrb, mrb_value self) |
967 | 0 | { |
968 | 0 | mrb_value a, b; |
969 | |
|
970 | 0 | if (mrb_get_args(mrb, "|S&", &a, &b) == 1) { |
971 | 0 | mrb_raise(mrb, E_NOTIMP_ERROR, "instance_eval with string not implemented"); |
972 | 0 | } |
973 | 0 | return eval_under(mrb, self, b, mrb_singleton_class_ptr(mrb, self)); |
974 | 0 | } |
975 | | |
976 | | MRB_API mrb_value |
977 | | mrb_yield_with_class(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv, mrb_value self, struct RClass *c) |
978 | 0 | { |
979 | 0 | struct RProc *p; |
980 | 0 | mrb_sym mid; |
981 | 0 | mrb_callinfo *ci; |
982 | 0 | mrb_value val; |
983 | 0 | mrb_int n; |
984 | |
|
985 | 0 | check_block(mrb, b); |
986 | 0 | ci = mrb->c->ci; |
987 | 0 | n = mrb_ci_nregs(ci); |
988 | 0 | p = mrb_proc_ptr(b); |
989 | 0 | if (MRB_PROC_ENV_P(p)) { |
990 | 0 | mid = p->e.env->mid; |
991 | 0 | } |
992 | 0 | else { |
993 | 0 | mid = ci->mid; |
994 | 0 | } |
995 | 0 | ci = cipush(mrb, n, CINFO_DIRECT, NULL, NULL, NULL, mid, 0); |
996 | 0 | funcall_args_capture(mrb, 0, argc, argv, mrb_nil_value(), ci); |
997 | 0 | ci->u.target_class = c; |
998 | 0 | ci->proc = p; |
999 | |
|
1000 | 0 | if (MRB_PROC_CFUNC_P(p)) { |
1001 | 0 | ci->stack[0] = self; |
1002 | 0 | val = MRB_PROC_CFUNC(p)(mrb, self); |
1003 | 0 | cipop(mrb); |
1004 | 0 | } |
1005 | 0 | else { |
1006 | 0 | ci->cci = CINFO_SKIP; |
1007 | 0 | val = mrb_run(mrb, p, self); |
1008 | 0 | } |
1009 | 0 | return val; |
1010 | 0 | } |
1011 | | |
1012 | | MRB_API mrb_value |
1013 | | mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv) |
1014 | 0 | { |
1015 | 0 | struct RProc *p = mrb_proc_ptr(b); |
1016 | 0 | struct RClass *tc; |
1017 | 0 | mrb_value self = mrb_proc_get_self(mrb, p, &tc); |
1018 | |
|
1019 | 0 | return mrb_yield_with_class(mrb, b, argc, argv, self, tc); |
1020 | 0 | } |
1021 | | |
1022 | | MRB_API mrb_value |
1023 | | mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg) |
1024 | 0 | { |
1025 | 0 | struct RProc *p = mrb_proc_ptr(b); |
1026 | 0 | struct RClass *tc; |
1027 | 0 | mrb_value self = mrb_proc_get_self(mrb, p, &tc); |
1028 | |
|
1029 | 0 | return mrb_yield_with_class(mrb, b, 1, &arg, self, tc); |
1030 | 0 | } |
1031 | | |
1032 | | mrb_value |
1033 | | mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv) |
1034 | 0 | { |
1035 | 0 | struct RProc *p; |
1036 | 0 | mrb_callinfo *ci; |
1037 | |
|
1038 | 0 | check_block(mrb, b); |
1039 | 0 | p = mrb_proc_ptr(b); |
1040 | 0 | ci = mrb->c->ci; |
1041 | |
|
1042 | 0 | stack_extend_adjust(mrb, 4, &argv); |
1043 | 0 | mrb->c->ci->stack[1] = mrb_ary_new_from_values(mrb, argc, argv); |
1044 | 0 | mrb->c->ci->stack[2] = mrb_nil_value(); |
1045 | 0 | mrb->c->ci->stack[3] = mrb_nil_value(); |
1046 | 0 | ci->n = 15; |
1047 | 0 | ci->nk = 0; |
1048 | 0 | return exec_irep(mrb, self, p); |
1049 | 0 | } |
1050 | | |
1051 | | #define RBREAK_TAG_FOREACH(f) \ |
1052 | 0 | f(RBREAK_TAG_BREAK, 0) \ |
1053 | 0 | f(RBREAK_TAG_BREAK_UPPER, 1) \ |
1054 | 0 | f(RBREAK_TAG_BREAK_INTARGET, 2) \ |
1055 | 0 | f(RBREAK_TAG_RETURN_BLOCK, 3) \ |
1056 | 0 | f(RBREAK_TAG_RETURN, 4) \ |
1057 | 0 | f(RBREAK_TAG_RETURN_TOPLEVEL, 5) \ |
1058 | 0 | f(RBREAK_TAG_JUMP, 6) \ |
1059 | 0 | f(RBREAK_TAG_STOP, 7) |
1060 | | |
1061 | | #define RBREAK_TAG_DEFINE(tag, i) tag = i, |
1062 | | enum { |
1063 | | RBREAK_TAG_FOREACH(RBREAK_TAG_DEFINE) |
1064 | | }; |
1065 | | #undef RBREAK_TAG_DEFINE |
1066 | | |
1067 | 0 | #define RBREAK_TAG_BIT 3 |
1068 | 0 | #define RBREAK_TAG_BIT_OFF 8 |
1069 | 0 | #define RBREAK_TAG_MASK (~(~UINT32_C(0) << RBREAK_TAG_BIT)) |
1070 | | |
1071 | | static inline uint32_t |
1072 | | mrb_break_tag_get(struct RBreak *brk) |
1073 | 0 | { |
1074 | 0 | return (brk->flags >> RBREAK_TAG_BIT_OFF) & RBREAK_TAG_MASK; |
1075 | 0 | } |
1076 | | |
1077 | | static inline void |
1078 | | mrb_break_tag_set(struct RBreak *brk, uint32_t tag) |
1079 | 0 | { |
1080 | 0 | brk->flags &= ~(RBREAK_TAG_MASK << RBREAK_TAG_BIT_OFF); |
1081 | 0 | brk->flags |= (tag & RBREAK_TAG_MASK) << RBREAK_TAG_BIT_OFF; |
1082 | 0 | } |
1083 | | |
1084 | | static struct RBreak* |
1085 | | break_new(mrb_state *mrb, uint32_t tag, const struct RProc *p, mrb_value val) |
1086 | 0 | { |
1087 | 0 | struct RBreak *brk; |
1088 | |
|
1089 | 0 | brk = MRB_OBJ_ALLOC(mrb, MRB_TT_BREAK, NULL); |
1090 | 0 | mrb_break_proc_set(brk, p); |
1091 | 0 | mrb_break_value_set(brk, val); |
1092 | 0 | mrb_break_tag_set(brk, tag); |
1093 | |
|
1094 | 0 | return brk; |
1095 | 0 | } |
1096 | | |
1097 | 416 | #define MRB_CATCH_FILTER_RESCUE (UINT32_C(1) << MRB_CATCH_RESCUE) |
1098 | 219k | #define MRB_CATCH_FILTER_ENSURE (UINT32_C(1) << MRB_CATCH_ENSURE) |
1099 | 416 | #define MRB_CATCH_FILTER_ALL (MRB_CATCH_FILTER_RESCUE | MRB_CATCH_FILTER_ENSURE) |
1100 | | |
1101 | | static const struct mrb_irep_catch_handler * |
1102 | | catch_handler_find(mrb_state *mrb, mrb_callinfo *ci, const mrb_code *pc, uint32_t filter) |
1103 | 219k | { |
1104 | 219k | const mrb_irep *irep; |
1105 | 219k | ptrdiff_t xpc; |
1106 | 219k | size_t cnt; |
1107 | 219k | const struct mrb_irep_catch_handler *e; |
1108 | | |
1109 | | /* The comparison operators use `>` and `<=` because pc already points to the next instruction */ |
1110 | 219k | #define catch_cover_p(pc, beg, end) ((pc) > (ptrdiff_t)(beg) && (pc) <= (ptrdiff_t)(end)) |
1111 | | |
1112 | 219k | if (ci->proc == NULL || MRB_PROC_CFUNC_P(ci->proc)) return NULL; |
1113 | 219k | irep = ci->proc->body.irep; |
1114 | 219k | if (irep == NULL || irep->clen < 1) return NULL; |
1115 | 1.42k | xpc = pc - irep->iseq; |
1116 | | /* If it retry at the top level, pc will be 0, so check with -1 as the start position */ |
1117 | 1.42k | mrb_assert(catch_cover_p(xpc, -1, irep->ilen)); |
1118 | 1.42k | if (!catch_cover_p(xpc, -1, irep->ilen)) return NULL; |
1119 | | |
1120 | | /* Currently uses a simple linear search to avoid processing complexity. */ |
1121 | 1.42k | cnt = irep->clen; |
1122 | 1.42k | e = mrb_irep_catch_handler_table(irep) + cnt - 1; |
1123 | 4.28k | for (; cnt > 0; cnt --, e --) { |
1124 | 2.85k | if (((UINT32_C(1) << e->type) & filter) && |
1125 | 2.85k | catch_cover_p(xpc, mrb_irep_catch_handler_unpack(e->begin), mrb_irep_catch_handler_unpack(e->end))) { |
1126 | 0 | return e; |
1127 | 0 | } |
1128 | 2.85k | } |
1129 | | |
1130 | 1.42k | #undef catch_cover_p |
1131 | | |
1132 | 1.42k | return NULL; |
1133 | 1.42k | } |
1134 | | |
1135 | | typedef enum { |
1136 | | LOCALJUMP_ERROR_RETURN = 0, |
1137 | | LOCALJUMP_ERROR_BREAK = 1, |
1138 | | LOCALJUMP_ERROR_YIELD = 2 |
1139 | | } localjump_error_kind; |
1140 | | |
1141 | | static void |
1142 | | localjump_error(mrb_state *mrb, localjump_error_kind kind) |
1143 | 0 | { |
1144 | 0 | char kind_str[3][7] = { "return", "break", "yield" }; |
1145 | 0 | char kind_str_len[] = { 6, 5, 5 }; |
1146 | 0 | static const char lead[] = "unexpected "; |
1147 | 0 | mrb_value msg; |
1148 | 0 | mrb_value exc; |
1149 | |
|
1150 | 0 | msg = mrb_str_new_capa(mrb, sizeof(lead) + 7); |
1151 | 0 | mrb_str_cat(mrb, msg, lead, sizeof(lead) - 1); |
1152 | 0 | mrb_str_cat(mrb, msg, kind_str[kind], kind_str_len[kind]); |
1153 | 0 | exc = mrb_exc_new_str(mrb, E_LOCALJUMP_ERROR, msg); |
1154 | 0 | mrb_exc_set(mrb, exc); |
1155 | 0 | } |
1156 | | |
1157 | 0 | #define RAISE_EXC(mrb, exc) do { \ |
1158 | 0 | mrb_value exc_value = (exc); \ |
1159 | 0 | mrb_exc_set(mrb, exc_value); \ |
1160 | 0 | goto L_RAISE; \ |
1161 | 0 | } while (0) |
1162 | | |
1163 | 0 | #define RAISE_LIT(mrb, c, str) RAISE_EXC(mrb, mrb_exc_new_lit(mrb, c, str)) |
1164 | 0 | #define RAISE_FORMAT(mrb, c, fmt, ...) RAISE_EXC(mrb, mrb_exc_new_str(mrb, c, mrb_format(mrb, fmt, __VA_ARGS__))) |
1165 | | |
1166 | | static void |
1167 | | argnum_error(mrb_state *mrb, mrb_int num) |
1168 | 11 | { |
1169 | 11 | mrb_value exc; |
1170 | 11 | mrb_value str; |
1171 | 11 | mrb_int argc = mrb->c->ci->n; |
1172 | | |
1173 | 11 | if (argc == 15) { |
1174 | 0 | mrb_value args = mrb->c->ci->stack[1]; |
1175 | 0 | if (mrb_array_p(args)) { |
1176 | 0 | argc = RARRAY_LEN(args); |
1177 | 0 | } |
1178 | 0 | } |
1179 | 11 | if (argc == 0 && mrb->c->ci->nk != 0 && !mrb_hash_empty_p(mrb, mrb->c->ci->stack[1])) { |
1180 | 0 | argc++; |
1181 | 0 | } |
1182 | 11 | str = mrb_format(mrb, "wrong number of arguments (given %i, expected %i)", argc, num); |
1183 | 11 | exc = mrb_exc_new_str(mrb, E_ARGUMENT_ERROR, str); |
1184 | 11 | mrb_exc_set(mrb, exc); |
1185 | 11 | } |
1186 | | |
1187 | | static mrb_bool |
1188 | | break_tag_p(struct RBreak *brk, uint32_t tag) |
1189 | 0 | { |
1190 | 0 | return (brk != NULL && brk->tt == MRB_TT_BREAK) ? TRUE : FALSE; |
1191 | 0 | } |
1192 | | |
1193 | | static void |
1194 | | prepare_tagged_break(mrb_state *mrb, uint32_t tag, const struct RProc *proc, mrb_value val) |
1195 | 0 | { |
1196 | 0 | if (break_tag_p((struct RBreak*)mrb->exc, tag)) { |
1197 | 0 | mrb_break_tag_set((struct RBreak*)mrb->exc, tag); |
1198 | 0 | } |
1199 | 0 | else { |
1200 | 0 | mrb->exc = (struct RObject*)break_new(mrb, tag, proc, val); |
1201 | 0 | } |
1202 | 0 | } |
1203 | | |
1204 | | #define THROW_TAGGED_BREAK(mrb, tag, proc, val) \ |
1205 | 0 | do { \ |
1206 | 0 | prepare_tagged_break(mrb, tag, proc, val); \ |
1207 | 0 | goto L_CATCH_TAGGED_BREAK; \ |
1208 | 0 | } while (0) |
1209 | | |
1210 | | #define UNWIND_ENSURE(mrb, ci, pc, tag, proc, val) \ |
1211 | 219k | do { \ |
1212 | 219k | ch = catch_handler_find(mrb, ci, pc, MRB_CATCH_FILTER_ENSURE); \ |
1213 | 219k | if (ch) { \ |
1214 | 0 | THROW_TAGGED_BREAK(mrb, tag, proc, val); \ |
1215 | 0 | } \ |
1216 | 219k | } while (0) |
1217 | | |
1218 | | /* |
1219 | | * CHECKPOINT_RESTORE(tag) { |
1220 | | * This part is executed when jumping by the same "tag" of RBreak (it is not executed the first time). |
1221 | | * Write the code required (initialization of variables, etc.) for the subsequent processing. |
1222 | | * } |
1223 | | * CHECKPOINT_MAIN(tag) { |
1224 | | * This part is always executed. |
1225 | | * } |
1226 | | * CHECKPOINT_END(tag); |
1227 | | * |
1228 | | * ... |
1229 | | * |
1230 | | * // Jump to CHECKPOINT_RESTORE with the same "tag". |
1231 | | * goto CHECKPOINT_LABEL_MAKE(tag); |
1232 | | */ |
1233 | | |
1234 | 0 | #define CHECKPOINT_LABEL_MAKE(tag) L_CHECKPOINT_ ## tag |
1235 | | |
1236 | | #define CHECKPOINT_RESTORE(tag) \ |
1237 | 206k | do { \ |
1238 | 206k | if (FALSE) { \ |
1239 | 12.6k | CHECKPOINT_LABEL_MAKE(tag): \ |
1240 | 12.6k | do { |
1241 | | |
1242 | | #define CHECKPOINT_MAIN(tag) \ |
1243 | 12.6k | } while (0); \ |
1244 | 12.6k | } \ |
1245 | 219k | do { |
1246 | | |
1247 | | #define CHECKPOINT_END(tag) \ |
1248 | 219k | } while (0); \ |
1249 | 219k | } while (0) |
1250 | | |
1251 | | #ifdef MRB_USE_DEBUG_HOOK |
1252 | 5.22M | #define CODE_FETCH_HOOK(mrb, irep, pc, regs) if ((mrb)->code_fetch_hook) (mrb)->code_fetch_hook((mrb), (irep), (pc), (regs)); |
1253 | | #else |
1254 | | #define CODE_FETCH_HOOK(mrb, irep, pc, regs) |
1255 | | #endif |
1256 | | |
1257 | | #ifdef MRB_BYTECODE_DECODE_OPTION |
1258 | | #define BYTECODE_DECODER(x) ((mrb)->bytecode_decoder)?(mrb)->bytecode_decoder((mrb), (x)):(x) |
1259 | | #else |
1260 | 5.22M | #define BYTECODE_DECODER(x) (x) |
1261 | | #endif |
1262 | | |
1263 | | #ifndef MRB_USE_VM_SWITCH_DISPATCH |
1264 | | #if !defined __GNUC__ && !defined __clang__ && !defined __INTEL_COMPILER |
1265 | | #define MRB_USE_VM_SWITCH_DISPATCH |
1266 | | #endif |
1267 | | #endif /* ifndef MRB_USE_VM_SWITCH_DISPATCH */ |
1268 | | |
1269 | | #ifdef MRB_USE_VM_SWITCH_DISPATCH |
1270 | | |
1271 | | #define INIT_DISPATCH for (;;) { insn = BYTECODE_DECODER(*pc); CODE_FETCH_HOOK(mrb, irep, pc, regs); switch (insn) { |
1272 | | #define CASE(insn,ops) case insn: pc++; FETCH_ ## ops (); mrb->c->ci->pc = pc; L_ ## insn ## _BODY: |
1273 | | #define NEXT goto L_END_DISPATCH |
1274 | | #define JUMP NEXT |
1275 | | #define END_DISPATCH L_END_DISPATCH:;}} |
1276 | | |
1277 | | #else |
1278 | | |
1279 | 12.7k | #define INIT_DISPATCH JUMP; return mrb_nil_value(); |
1280 | 5.60M | #define CASE(insn,ops) L_ ## insn: pc++; FETCH_ ## ops (); mrb->c->ci->pc = pc; L_ ## insn ## _BODY: |
1281 | 5.22M | #define NEXT insn=BYTECODE_DECODER(*pc); CODE_FETCH_HOOK(mrb, irep, pc, regs); goto *optable[insn] |
1282 | 1.26M | #define JUMP NEXT |
1283 | | |
1284 | | #define END_DISPATCH |
1285 | | |
1286 | | #endif |
1287 | | |
1288 | | MRB_API mrb_value |
1289 | | mrb_vm_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep) |
1290 | 12.7k | { |
1291 | 12.7k | const mrb_irep *irep = proc->body.irep; |
1292 | 12.7k | mrb_value result; |
1293 | 12.7k | struct mrb_context *c = mrb->c; |
1294 | 12.7k | ptrdiff_t cioff = c->ci - c->cibase; |
1295 | 12.7k | mrb_int nregs = irep->nregs; |
1296 | | |
1297 | 12.7k | if (!c->stbase) { |
1298 | 476 | stack_init(mrb); |
1299 | 476 | } |
1300 | 12.7k | if (stack_keep > nregs) |
1301 | 0 | nregs = stack_keep; |
1302 | 12.7k | else { |
1303 | 12.7k | struct REnv *e = CI_ENV(mrb->c->ci); |
1304 | 12.7k | if (stack_keep == 0 || (e && irep->nlocals < MRB_ENV_LEN(e))) { |
1305 | 12.7k | ci_env_set(mrb->c->ci, NULL); |
1306 | 12.7k | mrb_env_unshare(mrb, e, FALSE); |
1307 | 12.7k | } |
1308 | 12.7k | } |
1309 | 12.7k | stack_extend(mrb, nregs); |
1310 | 12.7k | stack_clear(c->ci->stack + stack_keep, nregs - stack_keep); |
1311 | 12.7k | c->ci->stack[0] = self; |
1312 | 12.7k | result = mrb_vm_exec(mrb, proc, irep->iseq); |
1313 | 12.7k | if (mrb->c != c) { |
1314 | 0 | if (mrb->c->fib) { |
1315 | 0 | mrb_write_barrier(mrb, (struct RBasic*)mrb->c->fib); |
1316 | 0 | } |
1317 | 0 | mrb->c = c; |
1318 | 0 | } |
1319 | 12.7k | else if (c->ci - c->cibase > cioff) { |
1320 | 0 | c->ci = c->cibase + cioff; |
1321 | 0 | } |
1322 | 12.7k | return result; |
1323 | 12.7k | } |
1324 | | |
1325 | | static struct RClass* |
1326 | | check_target_class(mrb_state *mrb) |
1327 | 218k | { |
1328 | 218k | struct RClass *target = CI_TARGET_CLASS(mrb->c->ci); |
1329 | 218k | if (!target) { |
1330 | 0 | mrb_raise(mrb, E_TYPE_ERROR, "no class/module to add method"); |
1331 | 0 | } |
1332 | 218k | return target; |
1333 | 218k | } |
1334 | | |
1335 | 7.04M | #define regs (mrb->c->ci->stack) |
1336 | | |
1337 | | static mrb_value |
1338 | | hash_new_from_regs(mrb_state *mrb, mrb_int argc, mrb_int idx) |
1339 | 0 | { |
1340 | 0 | mrb_value hash = mrb_hash_new_capa(mrb, argc); |
1341 | 0 | while (argc--) { |
1342 | 0 | mrb_hash_set(mrb, hash, regs[idx+0], regs[idx+1]); |
1343 | 0 | idx += 2; |
1344 | 0 | } |
1345 | 0 | return hash; |
1346 | 0 | } |
1347 | | |
1348 | 69.3k | #define ary_new_from_regs(mrb, argc, idx) mrb_ary_new_from_values(mrb, (argc), ®s[idx]); |
1349 | | |
1350 | | MRB_API mrb_value |
1351 | | mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *pc) |
1352 | 12.7k | { |
1353 | | /* mrb_assert(MRB_PROC_CFUNC_P(proc)) */ |
1354 | 12.7k | const mrb_irep *irep = proc->body.irep; |
1355 | 12.7k | const mrb_pool_value *pool = irep->pool; |
1356 | 12.7k | const mrb_sym *syms = irep->syms; |
1357 | 12.7k | mrb_code insn; |
1358 | 12.7k | int ai = mrb_gc_arena_save(mrb); |
1359 | 12.7k | struct mrb_jmpbuf *prev_jmp = mrb->jmp; |
1360 | 12.7k | struct mrb_jmpbuf c_jmp; |
1361 | 12.7k | uint32_t a; |
1362 | 12.7k | uint16_t b; |
1363 | 12.7k | uint16_t c; |
1364 | 12.7k | mrb_sym mid; |
1365 | 12.7k | const struct mrb_irep_catch_handler *ch; |
1366 | | |
1367 | 12.7k | #ifndef MRB_USE_VM_SWITCH_DISPATCH |
1368 | 12.7k | static const void * const optable[] = { |
1369 | 1.35M | #define OPCODE(x,_) &&L_OP_ ## x, |
1370 | 12.7k | #include "mruby/ops.h" |
1371 | 12.7k | #undef OPCODE |
1372 | 12.7k | }; |
1373 | 12.7k | #endif |
1374 | | |
1375 | 12.7k | mrb_bool exc_catched = FALSE; |
1376 | 12.9k | RETRY_TRY_BLOCK: |
1377 | | |
1378 | 12.9k | MRB_TRY(&c_jmp) { |
1379 | | |
1380 | 12.9k | if (exc_catched) { |
1381 | 137 | exc_catched = FALSE; |
1382 | 137 | mrb_gc_arena_restore(mrb, ai); |
1383 | 137 | if (mrb->exc && mrb->exc->tt == MRB_TT_BREAK) |
1384 | 0 | goto L_BREAK; |
1385 | 137 | goto L_RAISE; |
1386 | 137 | } |
1387 | 12.7k | mrb->jmp = &c_jmp; |
1388 | 12.7k | CI_PROC_SET(mrb->c->ci, proc); |
1389 | | |
1390 | 12.7k | INIT_DISPATCH { |
1391 | 0 | CASE(OP_NOP, Z) { |
1392 | | /* do nothing */ |
1393 | 0 | NEXT; |
1394 | 0 | } |
1395 | |
|
1396 | 2.04M | CASE(OP_MOVE, BB) { |
1397 | 2.04M | regs[a] = regs[b]; |
1398 | 2.04M | NEXT; |
1399 | 2.04M | } |
1400 | | |
1401 | 2.04M | CASE(OP_LOADL, BB) { |
1402 | 396 | switch (pool[b].tt) { /* number */ |
1403 | 0 | case IREP_TT_INT32: |
1404 | 0 | regs[a] = mrb_int_value(mrb, (mrb_int)pool[b].u.i32); |
1405 | 0 | break; |
1406 | 271 | case IREP_TT_INT64: |
1407 | 271 | #if defined(MRB_INT64) |
1408 | 271 | regs[a] = mrb_int_value(mrb, (mrb_int)pool[b].u.i64); |
1409 | 271 | break; |
1410 | | #else |
1411 | | #if defined(MRB_64BIT) |
1412 | | if (INT32_MIN <= pool[b].u.i64 && pool[b].u.i64 <= INT32_MAX) { |
1413 | | regs[a] = mrb_int_value(mrb, (mrb_int)pool[b].u.i64); |
1414 | | break; |
1415 | | } |
1416 | | #endif |
1417 | | goto L_INT_OVERFLOW; |
1418 | | #endif |
1419 | 0 | case IREP_TT_BIGINT: |
1420 | 0 | #ifdef MRB_USE_BIGINT |
1421 | 0 | { |
1422 | 0 | const char *s = pool[b].u.str; |
1423 | 0 | regs[a] = mrb_bint_new_str(mrb, s+2, (uint8_t)s[0], s[1]); |
1424 | 0 | } |
1425 | 0 | break; |
1426 | | #else |
1427 | | goto L_INT_OVERFLOW; |
1428 | | #endif |
1429 | 0 | #ifndef MRB_NO_FLOAT |
1430 | 0 | case IREP_TT_FLOAT: |
1431 | 0 | regs[a] = mrb_float_value(mrb, pool[b].u.f); |
1432 | 0 | break; |
1433 | 0 | #endif |
1434 | 0 | default: |
1435 | | /* should not happen (tt:string) */ |
1436 | 0 | regs[a] = mrb_nil_value(); |
1437 | 0 | break; |
1438 | 396 | } |
1439 | 271 | NEXT; |
1440 | 271 | } |
1441 | | |
1442 | 7.98k | CASE(OP_LOADI, BB) { |
1443 | 7.98k | SET_FIXNUM_VALUE(regs[a], b); |
1444 | 7.98k | NEXT; |
1445 | 7.98k | } |
1446 | | |
1447 | 7.98k | CASE(OP_LOADINEG, BB) { |
1448 | 2.00k | SET_FIXNUM_VALUE(regs[a], -b); |
1449 | 2.00k | NEXT; |
1450 | 2.00k | } |
1451 | | |
1452 | 2.00k | CASE(OP_LOADI__1,B) goto L_LOADI; |
1453 | 23.8k | CASE(OP_LOADI_0,B) goto L_LOADI; |
1454 | 229k | CASE(OP_LOADI_1,B) goto L_LOADI; |
1455 | 229k | CASE(OP_LOADI_2,B) goto L_LOADI; |
1456 | 1.96k | CASE(OP_LOADI_3,B) goto L_LOADI; |
1457 | 819 | CASE(OP_LOADI_4,B) goto L_LOADI; |
1458 | 819 | CASE(OP_LOADI_5,B) goto L_LOADI; |
1459 | 2.38k | CASE(OP_LOADI_6,B) goto L_LOADI; |
1460 | 2.38k | CASE(OP_LOADI_7, B) { |
1461 | 260k | L_LOADI: |
1462 | 260k | SET_FIXNUM_VALUE(regs[a], (mrb_int)insn - (mrb_int)OP_LOADI_0); |
1463 | 260k | NEXT; |
1464 | 260k | } |
1465 | | |
1466 | 260k | CASE(OP_LOADI16, BS) { |
1467 | 40 | SET_FIXNUM_VALUE(regs[a], (mrb_int)(int16_t)b); |
1468 | 40 | NEXT; |
1469 | 40 | } |
1470 | | |
1471 | 86 | CASE(OP_LOADI32, BSS) { |
1472 | 86 | SET_INT_VALUE(mrb, regs[a], (int32_t)(((uint32_t)b<<16)+c)); |
1473 | 86 | NEXT; |
1474 | 86 | } |
1475 | | |
1476 | 80.0k | CASE(OP_LOADSYM, BB) { |
1477 | 80.0k | SET_SYM_VALUE(regs[a], syms[b]); |
1478 | 80.0k | NEXT; |
1479 | 80.0k | } |
1480 | | |
1481 | 390k | CASE(OP_LOADNIL, B) { |
1482 | 390k | SET_NIL_VALUE(regs[a]); |
1483 | 390k | NEXT; |
1484 | 390k | } |
1485 | | |
1486 | 476k | CASE(OP_LOADSELF, B) { |
1487 | 476k | regs[a] = regs[0]; |
1488 | 476k | NEXT; |
1489 | 476k | } |
1490 | | |
1491 | 476k | CASE(OP_LOADT, B) { |
1492 | 3.57k | SET_TRUE_VALUE(regs[a]); |
1493 | 3.57k | NEXT; |
1494 | 3.57k | } |
1495 | | |
1496 | 157k | CASE(OP_LOADF, B) { |
1497 | 157k | SET_FALSE_VALUE(regs[a]); |
1498 | 157k | NEXT; |
1499 | 157k | } |
1500 | | |
1501 | 157k | CASE(OP_GETGV, BB) { |
1502 | 0 | mrb_value val = mrb_gv_get(mrb, syms[b]); |
1503 | 0 | regs[a] = val; |
1504 | 0 | NEXT; |
1505 | 0 | } |
1506 | |
|
1507 | 2.85k | CASE(OP_SETGV, BB) { |
1508 | 2.85k | mrb_gv_set(mrb, syms[b], regs[a]); |
1509 | 2.85k | NEXT; |
1510 | 2.85k | } |
1511 | | |
1512 | 2.85k | CASE(OP_GETSV, BB) { |
1513 | 0 | mrb_value val = mrb_vm_special_get(mrb, syms[b]); |
1514 | 0 | regs[a] = val; |
1515 | 0 | NEXT; |
1516 | 0 | } |
1517 | |
|
1518 | 0 | CASE(OP_SETSV, BB) { |
1519 | 0 | mrb_vm_special_set(mrb, syms[b], regs[a]); |
1520 | 0 | NEXT; |
1521 | 0 | } |
1522 | |
|
1523 | 25.3k | CASE(OP_GETIV, BB) { |
1524 | 25.3k | regs[a] = mrb_iv_get(mrb, regs[0], syms[b]); |
1525 | 25.3k | NEXT; |
1526 | 25.3k | } |
1527 | | |
1528 | 451k | CASE(OP_SETIV, BB) { |
1529 | 451k | mrb_iv_set(mrb, regs[0], syms[b], regs[a]); |
1530 | 451k | NEXT; |
1531 | 451k | } |
1532 | | |
1533 | 451k | CASE(OP_GETCV, BB) { |
1534 | 0 | mrb_value val; |
1535 | 0 | val = mrb_vm_cv_get(mrb, syms[b]); |
1536 | 0 | regs[a] = val; |
1537 | 0 | NEXT; |
1538 | 0 | } |
1539 | |
|
1540 | 952 | CASE(OP_SETCV, BB) { |
1541 | 952 | mrb_vm_cv_set(mrb, syms[b], regs[a]); |
1542 | 952 | NEXT; |
1543 | 952 | } |
1544 | | |
1545 | 121k | CASE(OP_GETIDX, B) { |
1546 | 121k | mrb_value va = regs[a], vb = regs[a+1]; |
1547 | 121k | switch (mrb_type(va)) { |
1548 | 60.8k | case MRB_TT_ARRAY: |
1549 | 60.8k | if (!mrb_integer_p(vb)) goto getidx_fallback; |
1550 | 60.6k | else { |
1551 | 60.6k | mrb_int idx = mrb_integer(vb); |
1552 | 60.6k | if (0 <= idx && idx < RARRAY_LEN(va)) { |
1553 | 59.9k | regs[a] = RARRAY_PTR(va)[idx]; |
1554 | 59.9k | } |
1555 | 702 | else { |
1556 | 702 | regs[a] = mrb_ary_entry(va, idx); |
1557 | 702 | } |
1558 | 60.6k | } |
1559 | 60.6k | break; |
1560 | 60.6k | case MRB_TT_HASH: |
1561 | 0 | va = mrb_hash_get(mrb, va, vb); |
1562 | 0 | regs[a] = va; |
1563 | 0 | break; |
1564 | 0 | case MRB_TT_STRING: |
1565 | 0 | switch (mrb_type(vb)) { |
1566 | 0 | case MRB_TT_INTEGER: |
1567 | 0 | case MRB_TT_STRING: |
1568 | 0 | case MRB_TT_RANGE: |
1569 | 0 | va = mrb_str_aref(mrb, va, vb, mrb_undef_value()); |
1570 | 0 | regs[a] = va; |
1571 | 0 | break; |
1572 | 0 | default: |
1573 | 0 | goto getidx_fallback; |
1574 | 0 | } |
1575 | 0 | break; |
1576 | 0 | default: |
1577 | 258 | getidx_fallback: |
1578 | 258 | mid = MRB_OPSYM(aref); |
1579 | 258 | goto L_SEND_SYM; |
1580 | 121k | } |
1581 | 60.6k | NEXT; |
1582 | 60.6k | } |
1583 | | |
1584 | 60.6k | CASE(OP_SETIDX, B) { |
1585 | 25.5k | c = 2; |
1586 | 25.5k | mid = MRB_OPSYM(aset); |
1587 | 25.5k | SET_NIL_VALUE(regs[a+3]); |
1588 | 25.5k | goto L_SENDB_SYM; |
1589 | 25.5k | } |
1590 | | |
1591 | 310k | CASE(OP_GETCONST, BB) { |
1592 | 310k | mrb_value v = mrb_vm_const_get(mrb, syms[b]); |
1593 | 310k | regs[a] = v; |
1594 | 310k | NEXT; |
1595 | 310k | } |
1596 | | |
1597 | 310k | CASE(OP_SETCONST, BB) { |
1598 | 8.56k | mrb_vm_const_set(mrb, syms[b], regs[a]); |
1599 | 8.56k | NEXT; |
1600 | 8.56k | } |
1601 | | |
1602 | 8.56k | CASE(OP_GETMCNST, BB) { |
1603 | 1.67k | mrb_value v = mrb_const_get(mrb, regs[a], syms[b]); |
1604 | 1.67k | regs[a] = v; |
1605 | 1.67k | NEXT; |
1606 | 1.67k | } |
1607 | | |
1608 | 1.67k | CASE(OP_SETMCNST, BB) { |
1609 | 0 | mrb_const_set(mrb, regs[a+1], syms[b], regs[a]); |
1610 | 0 | NEXT; |
1611 | 0 | } |
1612 | |
|
1613 | 0 | CASE(OP_GETUPVAR, BBB) { |
1614 | 0 | struct REnv *e = uvenv(mrb, c); |
1615 | |
|
1616 | 0 | if (e && b < MRB_ENV_LEN(e)) { |
1617 | 0 | regs[a] = e->stack[b]; |
1618 | 0 | } |
1619 | 0 | else { |
1620 | 0 | regs[a] = mrb_nil_value(); |
1621 | 0 | } |
1622 | 0 | NEXT; |
1623 | 0 | } |
1624 | |
|
1625 | 0 | CASE(OP_SETUPVAR, BBB) { |
1626 | 0 | struct REnv *e = uvenv(mrb, c); |
1627 | |
|
1628 | 0 | if (e) { |
1629 | 0 | if (b < MRB_ENV_LEN(e)) { |
1630 | 0 | e->stack[b] = regs[a]; |
1631 | 0 | mrb_write_barrier(mrb, (struct RBasic*)e); |
1632 | 0 | } |
1633 | 0 | } |
1634 | 0 | NEXT; |
1635 | 0 | } |
1636 | |
|
1637 | 329k | CASE(OP_JMP, S) { |
1638 | 329k | pc += (int16_t)a; |
1639 | 329k | JUMP; |
1640 | 329k | } |
1641 | 329k | CASE(OP_JMPIF, BS) { |
1642 | 90.7k | if (mrb_test(regs[a])) { |
1643 | 6.88k | pc += (int16_t)b; |
1644 | 6.88k | JUMP; |
1645 | 6.88k | } |
1646 | 90.7k | NEXT; |
1647 | 90.7k | } |
1648 | 655k | CASE(OP_JMPNOT, BS) { |
1649 | 655k | if (!mrb_test(regs[a])) { |
1650 | 185k | pc += (int16_t)b; |
1651 | 185k | JUMP; |
1652 | 185k | } |
1653 | 655k | NEXT; |
1654 | 655k | } |
1655 | 655k | CASE(OP_JMPNIL, BS) { |
1656 | 0 | if (mrb_nil_p(regs[a])) { |
1657 | 0 | pc += (int16_t)b; |
1658 | 0 | JUMP; |
1659 | 0 | } |
1660 | 0 | NEXT; |
1661 | 0 | } |
1662 | |
|
1663 | 0 | CASE(OP_JMPUW, S) { |
1664 | 0 | a = (uint32_t)((pc - irep->iseq) + (int16_t)a); |
1665 | 0 | CHECKPOINT_RESTORE(RBREAK_TAG_JUMP) { |
1666 | 0 | struct RBreak *brk = (struct RBreak*)mrb->exc; |
1667 | 0 | mrb_value target = mrb_break_value_get(brk); |
1668 | 0 | mrb_assert(mrb_integer_p(target)); |
1669 | 0 | a = (uint32_t)mrb_integer(target); |
1670 | 0 | mrb_assert(a >= 0 && a < irep->ilen); |
1671 | 0 | } |
1672 | 0 | CHECKPOINT_MAIN(RBREAK_TAG_JUMP) { |
1673 | 0 | ch = catch_handler_find(mrb, mrb->c->ci, pc, MRB_CATCH_FILTER_ENSURE); |
1674 | 0 | if (ch) { |
1675 | | /* avoiding a jump from a catch handler into the same handler */ |
1676 | 0 | if (a < mrb_irep_catch_handler_unpack(ch->begin) || a >= mrb_irep_catch_handler_unpack(ch->end)) { |
1677 | 0 | THROW_TAGGED_BREAK(mrb, RBREAK_TAG_JUMP, proc, mrb_fixnum_value(a)); |
1678 | 0 | } |
1679 | 0 | } |
1680 | 0 | } |
1681 | 0 | CHECKPOINT_END(RBREAK_TAG_JUMP); |
1682 | | |
1683 | 0 | mrb->exc = NULL; /* clear break object */ |
1684 | 0 | pc = irep->iseq + a; |
1685 | 0 | JUMP; |
1686 | 0 | } |
1687 | | |
1688 | 0 | CASE(OP_EXCEPT, B) { |
1689 | 0 | mrb_value exc; |
1690 | |
|
1691 | 0 | if (mrb->exc == NULL) { |
1692 | 0 | exc = mrb_nil_value(); |
1693 | 0 | } |
1694 | 0 | else { |
1695 | 0 | switch (mrb->exc->tt) { |
1696 | 0 | case MRB_TT_BREAK: |
1697 | 0 | case MRB_TT_EXCEPTION: |
1698 | 0 | exc = mrb_obj_value(mrb->exc); |
1699 | 0 | break; |
1700 | 0 | default: |
1701 | 0 | mrb_assert(!"bad mrb_type"); |
1702 | 0 | exc = mrb_nil_value(); |
1703 | 0 | break; |
1704 | 0 | } |
1705 | 0 | mrb->exc = NULL; |
1706 | 0 | } |
1707 | 0 | regs[a] = exc; |
1708 | 0 | NEXT; |
1709 | 0 | } |
1710 | 0 | CASE(OP_RESCUE, BB) { |
1711 | 0 | mrb_value exc = regs[a]; /* exc on stack */ |
1712 | 0 | mrb_value e = regs[b]; |
1713 | 0 | struct RClass *ec; |
1714 | |
|
1715 | 0 | switch (mrb_type(e)) { |
1716 | 0 | case MRB_TT_CLASS: |
1717 | 0 | case MRB_TT_MODULE: |
1718 | 0 | break; |
1719 | 0 | default: |
1720 | 0 | RAISE_LIT(mrb, E_TYPE_ERROR, "class or module required for rescue clause"); |
1721 | 0 | } |
1722 | 0 | ec = mrb_class_ptr(e); |
1723 | 0 | regs[b] = mrb_bool_value(mrb_obj_is_kind_of(mrb, exc, ec)); |
1724 | 0 | NEXT; |
1725 | 0 | } |
1726 | | |
1727 | 0 | CASE(OP_RAISEIF, B) { |
1728 | 0 | mrb_value exc = regs[a]; |
1729 | 0 | if (mrb_break_p(exc)) { |
1730 | 0 | mrb->exc = mrb_obj_ptr(exc); |
1731 | 0 | goto L_BREAK; |
1732 | 0 | } |
1733 | 0 | mrb_exc_set(mrb, exc); |
1734 | 0 | if (mrb->exc) { |
1735 | 0 | goto L_RAISE; |
1736 | 0 | } |
1737 | 0 | NEXT; |
1738 | 0 | } |
1739 | | |
1740 | 63.2k | CASE(OP_SSEND, BBB) { |
1741 | 63.2k | regs[a] = regs[0]; |
1742 | 63.2k | insn = OP_SEND; |
1743 | 63.2k | } |
1744 | 63.2k | goto L_SENDB; |
1745 | | |
1746 | 63.2k | CASE(OP_SSENDB, BBB) { |
1747 | 31.7k | regs[a] = regs[0]; |
1748 | 31.7k | } |
1749 | 31.7k | goto L_SENDB; |
1750 | | |
1751 | 312k | CASE(OP_SEND, BBB) |
1752 | 312k | goto L_SENDB; |
1753 | | |
1754 | 41.7k | L_SEND_SYM: |
1755 | 41.7k | c = 1; |
1756 | | /* push nil after arguments */ |
1757 | 41.7k | SET_NIL_VALUE(regs[a+2]); |
1758 | 41.7k | goto L_SENDB_SYM; |
1759 | | |
1760 | 41.7k | CASE(OP_SENDB, BBB) |
1761 | 413k | L_SENDB: |
1762 | 413k | mid = syms[b]; |
1763 | 480k | L_SENDB_SYM: |
1764 | 480k | { |
1765 | 480k | mrb_callinfo *ci; |
1766 | 480k | mrb_method_t m; |
1767 | 480k | mrb_value recv, blk; |
1768 | 480k | int n = c&0xf; |
1769 | 480k | int nk = (c>>4)&0xf; |
1770 | 480k | mrb_int bidx = a + mrb_bidx(n,nk); |
1771 | 480k | mrb_int new_bidx = bidx; |
1772 | | |
1773 | 480k | if (nk == CALL_MAXARGS) { |
1774 | 31.7k | mrb_ensure_hash_type(mrb, regs[a+(n==CALL_MAXARGS?1:n)+1]); |
1775 | 31.7k | } |
1776 | 448k | else if (nk > 0) { /* pack keyword arguments */ |
1777 | 0 | mrb_int kidx = a+(n==CALL_MAXARGS?1:n)+1; |
1778 | 0 | mrb_value kdict = hash_new_from_regs(mrb, nk, kidx); |
1779 | 0 | regs[kidx] = kdict; |
1780 | 0 | nk = CALL_MAXARGS; |
1781 | 0 | c = n | (nk<<4); |
1782 | 0 | new_bidx = a+mrb_bidx(n, nk); |
1783 | 0 | } |
1784 | | |
1785 | 480k | mrb_assert(bidx < irep->nregs); |
1786 | 480k | if (insn == OP_SEND) { |
1787 | | /* clear block argument */ |
1788 | 375k | SET_NIL_VALUE(regs[new_bidx]); |
1789 | 375k | SET_NIL_VALUE(blk); |
1790 | 375k | } |
1791 | 105k | else { |
1792 | 105k | blk = ensure_block(mrb, regs[bidx]); |
1793 | 105k | regs[new_bidx] = blk; |
1794 | 105k | } |
1795 | | |
1796 | 480k | ci = cipush(mrb, a, CINFO_DIRECT, NULL, NULL, BLK_PTR(blk), 0, c); |
1797 | 480k | recv = regs[0]; |
1798 | 480k | ci->u.target_class = (insn == OP_SUPER) ? CI_TARGET_CLASS(ci - 1)->super : mrb_class(mrb, recv); |
1799 | 480k | m = mrb_vm_find_method(mrb, ci->u.target_class, &ci->u.target_class, mid); |
1800 | 480k | if (MRB_METHOD_UNDEF_P(m)) { |
1801 | 37 | m = prepare_missing(mrb, ci, recv, mid, blk, (insn == OP_SUPER)); |
1802 | 37 | } |
1803 | 480k | else { |
1804 | 480k | ci->mid = mid; |
1805 | 480k | } |
1806 | 480k | ci->cci = CINFO_NONE; |
1807 | | |
1808 | 480k | if (MRB_METHOD_CFUNC_P(m)) { |
1809 | 306k | if (MRB_METHOD_PROC_P(m)) { |
1810 | 0 | struct RProc *p = MRB_METHOD_PROC(m); |
1811 | 0 | CI_PROC_SET(ci, p); |
1812 | 0 | recv = p->body.func(mrb, recv); |
1813 | 0 | } |
1814 | 306k | else { |
1815 | 306k | if (MRB_METHOD_NOARG_P(m)) { |
1816 | 64.0k | check_method_noarg(mrb, ci); |
1817 | 64.0k | } |
1818 | 306k | recv = MRB_METHOD_FUNC(m)(mrb, recv); |
1819 | 306k | } |
1820 | 306k | mrb_gc_arena_shrink(mrb, ai); |
1821 | 306k | if (mrb->exc) goto L_RAISE; |
1822 | 306k | ci = mrb->c->ci; |
1823 | 306k | if (!ci->u.target_class) { /* return from context modifying method (resume/yield) */ |
1824 | 476 | if (ci->cci == CINFO_RESUMED) { |
1825 | 0 | mrb->jmp = prev_jmp; |
1826 | 0 | return recv; |
1827 | 0 | } |
1828 | 476 | else { |
1829 | 476 | mrb_assert(!MRB_PROC_CFUNC_P(ci[-1].proc)); |
1830 | 476 | proc = ci[-1].proc; |
1831 | 476 | irep = proc->body.irep; |
1832 | 476 | pool = irep->pool; |
1833 | 476 | syms = irep->syms; |
1834 | 476 | } |
1835 | 476 | } |
1836 | 306k | ci->stack[0] = recv; |
1837 | | /* pop stackpos */ |
1838 | 306k | ci = cipop(mrb); |
1839 | 306k | pc = ci->pc; |
1840 | 306k | } |
1841 | 174k | else { |
1842 | | /* setup environment for calling method */ |
1843 | 174k | proc = MRB_METHOD_PROC(m); |
1844 | 174k | CI_PROC_SET(ci, proc); |
1845 | 174k | irep = proc->body.irep; |
1846 | 174k | pool = irep->pool; |
1847 | 174k | syms = irep->syms; |
1848 | 174k | stack_extend(mrb, (irep->nregs < 4) ? 4 : irep->nregs); |
1849 | 174k | pc = irep->iseq; |
1850 | 174k | } |
1851 | 480k | } |
1852 | 480k | JUMP; |
1853 | | |
1854 | 480k | CASE(OP_CALL, Z) { |
1855 | 11.4k | mrb_callinfo *ci = mrb->c->ci; |
1856 | 11.4k | mrb_value recv = ci->stack[0]; |
1857 | 11.4k | struct RProc *m = mrb_proc_ptr(recv); |
1858 | | |
1859 | | /* replace callinfo */ |
1860 | 11.4k | ci->u.target_class = MRB_PROC_TARGET_CLASS(m); |
1861 | 11.4k | CI_PROC_SET(ci, m); |
1862 | 11.4k | if (MRB_PROC_ENV_P(m)) { |
1863 | 5.71k | ci->mid = MRB_PROC_ENV(m)->mid; |
1864 | 5.71k | } |
1865 | | |
1866 | | /* prepare stack */ |
1867 | 11.4k | if (MRB_PROC_CFUNC_P(m)) { |
1868 | 0 | recv = MRB_PROC_CFUNC(m)(mrb, recv); |
1869 | 0 | mrb_gc_arena_shrink(mrb, ai); |
1870 | 0 | if (mrb->exc) goto L_RAISE; |
1871 | | /* pop stackpos */ |
1872 | 0 | ci = cipop(mrb); |
1873 | 0 | pc = ci->pc; |
1874 | 0 | ci[1].stack[0] = recv; |
1875 | 0 | irep = mrb->c->ci->proc->body.irep; |
1876 | 0 | } |
1877 | 5.71k | else { |
1878 | | /* setup environment for calling method */ |
1879 | 5.71k | proc = m; |
1880 | 5.71k | irep = m->body.irep; |
1881 | 5.71k | if (!irep) { |
1882 | 0 | mrb->c->ci->stack[0] = mrb_nil_value(); |
1883 | 0 | a = 0; |
1884 | 0 | c = OP_R_NORMAL; |
1885 | 0 | goto L_OP_RETURN_BODY; |
1886 | 0 | } |
1887 | 5.71k | mrb_int nargs = ci_bidx(ci)+1; |
1888 | 5.71k | if (nargs < irep->nregs) { |
1889 | 5.71k | stack_extend(mrb, irep->nregs); |
1890 | 5.71k | stack_clear(regs+nargs, irep->nregs-nargs); |
1891 | 5.71k | } |
1892 | 5.71k | if (MRB_PROC_ENV_P(m)) { |
1893 | 5.71k | regs[0] = MRB_PROC_ENV(m)->stack[0]; |
1894 | 5.71k | } |
1895 | 5.71k | pc = irep->iseq; |
1896 | 5.71k | } |
1897 | 5.71k | pool = irep->pool; |
1898 | 5.71k | syms = irep->syms; |
1899 | 5.71k | JUMP; |
1900 | 5.71k | } |
1901 | | |
1902 | 5.71k | CASE(OP_SUPER, BB) { |
1903 | 0 | mrb_callinfo *ci = mrb->c->ci; |
1904 | 0 | mrb_value recv; |
1905 | 0 | const struct RProc *p = ci->proc; |
1906 | 0 | struct RClass* target_class = CI_TARGET_CLASS(ci); |
1907 | |
|
1908 | 0 | mid = ci->mid; |
1909 | 0 | if (MRB_PROC_ENV_P(p) && p->e.env->mid && p->e.env->mid != mid) { /* alias support */ |
1910 | 0 | mid = p->e.env->mid; /* restore old mid */ |
1911 | 0 | } |
1912 | |
|
1913 | 0 | if (mid == 0 || !target_class) { |
1914 | 0 | RAISE_LIT(mrb, E_NOMETHOD_ERROR, "super called outside of method"); |
1915 | 0 | } |
1916 | 0 | if ((target_class->flags & MRB_FL_CLASS_IS_PREPENDED) || target_class->tt == MRB_TT_MODULE) { |
1917 | 0 | goto super_typeerror; |
1918 | 0 | } |
1919 | 0 | recv = regs[0]; |
1920 | 0 | if (!mrb_obj_is_kind_of(mrb, recv, target_class)) { |
1921 | 0 | super_typeerror: |
1922 | 0 | RAISE_LIT(mrb, E_TYPE_ERROR, "self has wrong type to call super in this context"); |
1923 | 0 | } |
1924 | | |
1925 | 0 | c = b; // arg info |
1926 | 0 | regs[a] = recv; |
1927 | 0 | goto L_SENDB_SYM; |
1928 | 0 | } |
1929 | | |
1930 | 0 | CASE(OP_ARGARY, BS) { |
1931 | 0 | mrb_int m1 = (b>>11)&0x3f; |
1932 | 0 | mrb_int r = (b>>10)&0x1; |
1933 | 0 | mrb_int m2 = (b>>5)&0x1f; |
1934 | 0 | mrb_int kd = (b>>4)&0x1; |
1935 | 0 | mrb_int lv = (b>>0)&0xf; |
1936 | 0 | mrb_value *stack; |
1937 | |
|
1938 | 0 | if (mrb->c->ci->mid == 0 || CI_TARGET_CLASS(mrb->c->ci) == NULL) { |
1939 | 0 | L_NOSUPER: |
1940 | 0 | RAISE_LIT(mrb, E_NOMETHOD_ERROR, "super called outside of method"); |
1941 | 0 | } |
1942 | 0 | if (lv == 0) stack = regs + 1; |
1943 | 0 | else { |
1944 | 0 | struct REnv *e = uvenv(mrb, lv-1); |
1945 | 0 | if (!e) goto L_NOSUPER; |
1946 | 0 | if (MRB_ENV_LEN(e) <= m1+r+m2+1) |
1947 | 0 | goto L_NOSUPER; |
1948 | 0 | stack = e->stack + 1; |
1949 | 0 | } |
1950 | 0 | if (r == 0) { |
1951 | 0 | regs[a] = mrb_ary_new_from_values(mrb, m1+m2, stack); |
1952 | 0 | } |
1953 | 0 | else { |
1954 | 0 | mrb_value *pp = NULL; |
1955 | 0 | struct RArray *rest; |
1956 | 0 | mrb_int len = 0; |
1957 | |
|
1958 | 0 | if (mrb_array_p(stack[m1])) { |
1959 | 0 | struct RArray *ary = mrb_ary_ptr(stack[m1]); |
1960 | |
|
1961 | 0 | pp = ARY_PTR(ary); |
1962 | 0 | len = ARY_LEN(ary); |
1963 | 0 | } |
1964 | 0 | regs[a] = mrb_ary_new_capa(mrb, m1+len+m2); |
1965 | 0 | rest = mrb_ary_ptr(regs[a]); |
1966 | 0 | if (m1 > 0) { |
1967 | 0 | stack_copy(ARY_PTR(rest), stack, m1); |
1968 | 0 | } |
1969 | 0 | if (len > 0) { |
1970 | 0 | stack_copy(ARY_PTR(rest)+m1, pp, len); |
1971 | 0 | } |
1972 | 0 | if (m2 > 0) { |
1973 | 0 | stack_copy(ARY_PTR(rest)+m1+len, stack+m1+1, m2); |
1974 | 0 | } |
1975 | 0 | ARY_SET_LEN(rest, m1+len+m2); |
1976 | 0 | } |
1977 | 0 | if (kd) { |
1978 | 0 | regs[a+1] = stack[m1+r+m2]; |
1979 | 0 | regs[a+2] = stack[m1+r+m2+1]; |
1980 | 0 | } |
1981 | 0 | else { |
1982 | 0 | regs[a+1] = stack[m1+r+m2]; |
1983 | 0 | } |
1984 | 0 | mrb_gc_arena_restore(mrb, ai); |
1985 | 0 | NEXT; |
1986 | 0 | } |
1987 | | |
1988 | 349k | CASE(OP_ENTER, W) { |
1989 | 349k | mrb_callinfo *ci = mrb->c->ci; |
1990 | 349k | mrb_int argc = ci->n; |
1991 | 349k | mrb_value *argv = regs+1; |
1992 | | |
1993 | 349k | mrb_int m1 = MRB_ASPEC_REQ(a); |
1994 | | |
1995 | | /* no other args */ |
1996 | 349k | if ((a & ~0x7c0001) == 0 && argc < 15 && MRB_PROC_STRICT_P(proc)) { |
1997 | 54.5k | if (argc+(ci->nk==15) != m1) { /* count kdict too */ |
1998 | 11 | argnum_error(mrb, m1); |
1999 | 11 | goto L_RAISE; |
2000 | 11 | } |
2001 | | /* clear local (but non-argument) variables */ |
2002 | 54.4k | mrb_int pos = m1+2; /* self+m1+blk */ |
2003 | 54.4k | if (irep->nlocals-pos > 0) { |
2004 | 52.0k | stack_clear(®s[pos], irep->nlocals-pos); |
2005 | 52.0k | } |
2006 | 54.4k | NEXT; |
2007 | 54.4k | } |
2008 | | |
2009 | 174k | mrb_int o = MRB_ASPEC_OPT(a); |
2010 | 174k | mrb_int r = MRB_ASPEC_REST(a); |
2011 | 174k | mrb_int m2 = MRB_ASPEC_POST(a); |
2012 | 174k | mrb_int kd = (MRB_ASPEC_KEY(a) > 0 || MRB_ASPEC_KDICT(a))? 1 : 0; |
2013 | | /* unused |
2014 | | int b = MRB_ASPEC_BLOCK(a); |
2015 | | */ |
2016 | 174k | mrb_int const len = m1 + o + r + m2; |
2017 | | |
2018 | 174k | mrb_value * const argv0 = argv; |
2019 | 174k | mrb_value blk = regs[ci_bidx(ci)]; |
2020 | 174k | mrb_value kdict = mrb_nil_value(); |
2021 | | |
2022 | | /* keyword arguments */ |
2023 | 174k | if (ci->nk == 15) { |
2024 | 29.8k | kdict = regs[mrb_ci_kidx(ci)]; |
2025 | 29.8k | } |
2026 | 174k | if (!kd) { |
2027 | 64.7k | if (!mrb_nil_p(kdict) && mrb_hash_size(mrb, kdict) > 0) { |
2028 | 0 | if (argc < 14) { |
2029 | 0 | ci->n++; |
2030 | 0 | argc++; /* include kdict in normal arguments */ |
2031 | 0 | } |
2032 | 0 | else if (argc == 14) { |
2033 | | /* pack arguments and kdict */ |
2034 | 0 | regs[1] = ary_new_from_regs(mrb, argc+1, 1); |
2035 | 0 | argc = ci->n = 15; |
2036 | 0 | } |
2037 | 0 | else {/* argc == 15 */ |
2038 | | /* push kdict to packed arguments */ |
2039 | 0 | mrb_ary_push(mrb, regs[1], kdict); |
2040 | 0 | } |
2041 | 0 | } |
2042 | 64.7k | kdict = mrb_nil_value(); |
2043 | 64.7k | ci->nk = 0; |
2044 | 64.7k | } |
2045 | 110k | else if (MRB_ASPEC_KEY(a) > 0 && !mrb_nil_p(kdict)) { |
2046 | 0 | kdict = mrb_hash_dup(mrb, kdict); |
2047 | 0 | } |
2048 | 110k | else if (!mrb_nil_p(kdict)) { |
2049 | 23.8k | mrb_gc_protect(mrb, kdict); |
2050 | 23.8k | } |
2051 | | |
2052 | | /* arguments is passed with Array */ |
2053 | 174k | if (argc == 15) { |
2054 | 55.0k | struct RArray *ary = mrb_ary_ptr(regs[1]); |
2055 | 55.0k | argv = ARY_PTR(ary); |
2056 | 55.0k | argc = (int)ARY_LEN(ary); |
2057 | 55.0k | mrb_gc_protect(mrb, regs[1]); |
2058 | 55.0k | } |
2059 | | |
2060 | | /* strict argument check */ |
2061 | 174k | if (ci->proc && MRB_PROC_STRICT_P(ci->proc)) { |
2062 | 114k | if (argc < m1 + m2 || (r == 0 && argc > len)) { |
2063 | 0 | argnum_error(mrb, m1+m2); |
2064 | 0 | goto L_RAISE; |
2065 | 0 | } |
2066 | 114k | } |
2067 | | /* extract first argument array to arguments */ |
2068 | 60.6k | else if (len > 1 && argc == 1 && mrb_array_p(argv[0])) { |
2069 | 0 | mrb_gc_protect(mrb, argv[0]); |
2070 | 0 | argc = (int)RARRAY_LEN(argv[0]); |
2071 | 0 | argv = RARRAY_PTR(argv[0]); |
2072 | 0 | } |
2073 | | |
2074 | | /* rest arguments */ |
2075 | 174k | mrb_value rest = mrb_nil_value(); |
2076 | 174k | if (argc < len) { |
2077 | 10.5k | mrb_int mlen = m2; |
2078 | 10.5k | if (argc < m1+m2) { |
2079 | 0 | mlen = m1 < argc ? argc - m1 : 0; |
2080 | 0 | } |
2081 | | |
2082 | | /* copy mandatory and optional arguments */ |
2083 | 10.5k | if (argv0 != argv && argv) { |
2084 | 5.99k | value_move(®s[1], argv, argc-mlen); /* m1 + o */ |
2085 | 5.99k | } |
2086 | 10.5k | if (argc < m1) { |
2087 | 0 | stack_clear(®s[argc+1], m1-argc); |
2088 | 0 | } |
2089 | | /* copy post mandatory arguments */ |
2090 | 10.5k | if (mlen) { |
2091 | 0 | value_move(®s[len-m2+1], &argv[argc-mlen], mlen); |
2092 | 0 | } |
2093 | 10.5k | if (mlen < m2) { |
2094 | 0 | stack_clear(®s[len-m2+mlen+1], m2-mlen); |
2095 | 0 | } |
2096 | | /* initialize rest arguments with empty Array */ |
2097 | 10.5k | if (r) { |
2098 | 2.51k | rest = mrb_ary_new_capa(mrb, 0); |
2099 | 2.51k | regs[m1+o+1] = rest; |
2100 | 2.51k | } |
2101 | | /* skip initializer of passed arguments */ |
2102 | 10.5k | if (o > 0 && argc > m1+m2) |
2103 | 6.70k | pc += (argc - m1 - m2)*3; |
2104 | 10.5k | } |
2105 | 164k | else { |
2106 | 164k | mrb_int rnum = 0; |
2107 | 164k | if (argv0 != argv) { |
2108 | 49.0k | mrb_gc_protect(mrb, blk); |
2109 | 49.0k | value_move(®s[1], argv, m1+o); |
2110 | 49.0k | } |
2111 | 164k | if (r) { |
2112 | 88.4k | rnum = argc-m1-o-m2; |
2113 | 88.4k | rest = mrb_ary_new_from_values(mrb, rnum, argv+m1+o); |
2114 | 88.4k | regs[m1+o+1] = rest; |
2115 | 88.4k | } |
2116 | 164k | if (m2 > 0 && argc-m2 > m1) { |
2117 | 0 | value_move(®s[m1+o+r+1], &argv[m1+o+rnum], m2); |
2118 | 0 | } |
2119 | 164k | pc += o*3; |
2120 | 164k | } |
2121 | | |
2122 | | /* need to be update blk first to protect blk from GC */ |
2123 | 174k | mrb_int const kw_pos = len + kd; /* where kwhash should be */ |
2124 | 174k | mrb_int const blk_pos = kw_pos + 1; /* where block should be */ |
2125 | 174k | regs[blk_pos] = blk; /* move block */ |
2126 | 174k | if (kd) { |
2127 | 55.5k | if (mrb_nil_p(kdict)) { |
2128 | 31.7k | kdict = mrb_hash_new_capa(mrb, 0); |
2129 | 31.7k | } |
2130 | 55.5k | regs[kw_pos] = kdict; /* set kwhash */ |
2131 | 55.5k | ci->nk = 15; |
2132 | 55.5k | } |
2133 | | |
2134 | | /* format arguments for generated code */ |
2135 | 174k | mrb->c->ci->n = (uint8_t)len; |
2136 | | |
2137 | | /* clear local (but non-argument) variables */ |
2138 | 174k | if (irep->nlocals-blk_pos-1 > 0) { |
2139 | 25.4k | stack_clear(®s[blk_pos+1], irep->nlocals-blk_pos-1); |
2140 | 25.4k | } |
2141 | 174k | JUMP; |
2142 | 174k | } |
2143 | | |
2144 | 174k | CASE(OP_KARG, BB) { |
2145 | 0 | mrb_value k = mrb_symbol_value(syms[b]); |
2146 | 0 | mrb_int kidx = mrb_ci_kidx(mrb->c->ci); |
2147 | 0 | mrb_value kdict, v; |
2148 | |
|
2149 | 0 | if (kidx < 0 || !mrb_hash_p(kdict=regs[kidx]) || !mrb_hash_key_p(mrb, kdict, k)) { |
2150 | 0 | RAISE_FORMAT(mrb, E_ARGUMENT_ERROR, "missing keyword: %v", k); |
2151 | 0 | } |
2152 | 0 | v = mrb_hash_get(mrb, kdict, k); |
2153 | 0 | regs[a] = v; |
2154 | 0 | mrb_hash_delete_key(mrb, kdict, k); |
2155 | 0 | NEXT; |
2156 | 0 | } |
2157 | | |
2158 | 0 | CASE(OP_KEY_P, BB) { |
2159 | 0 | mrb_value k = mrb_symbol_value(syms[b]); |
2160 | 0 | mrb_int kidx = mrb_ci_kidx(mrb->c->ci); |
2161 | 0 | mrb_value kdict; |
2162 | 0 | mrb_bool key_p = FALSE; |
2163 | |
|
2164 | 0 | if (kidx >= 0 && mrb_hash_p(kdict=regs[kidx])) { |
2165 | 0 | key_p = mrb_hash_key_p(mrb, kdict, k); |
2166 | 0 | } |
2167 | 0 | regs[a] = mrb_bool_value(key_p); |
2168 | 0 | NEXT; |
2169 | 0 | } |
2170 | |
|
2171 | 0 | CASE(OP_KEYEND, Z) { |
2172 | 0 | mrb_int kidx = mrb_ci_kidx(mrb->c->ci); |
2173 | 0 | mrb_value kdict; |
2174 | |
|
2175 | 0 | if (kidx >= 0 && mrb_hash_p(kdict=regs[kidx]) && !mrb_hash_empty_p(mrb, kdict)) { |
2176 | 0 | mrb_value keys = mrb_hash_keys(mrb, kdict); |
2177 | 0 | mrb_value key1 = RARRAY_PTR(keys)[0]; |
2178 | 0 | RAISE_FORMAT(mrb, E_ARGUMENT_ERROR, "unknown keyword: %v", key1); |
2179 | 0 | } |
2180 | 0 | NEXT; |
2181 | 0 | } |
2182 | | |
2183 | 0 | CASE(OP_BREAK, B) { |
2184 | 0 | c = OP_R_BREAK; |
2185 | 0 | goto L_RETURN; |
2186 | 0 | } |
2187 | 0 | CASE(OP_RETURN_BLK, B) { |
2188 | 0 | c = OP_R_RETURN; |
2189 | 0 | goto L_RETURN; |
2190 | 0 | } |
2191 | 438k | CASE(OP_RETURN, B) |
2192 | 438k | c = OP_R_NORMAL; |
2193 | 438k | L_RETURN: |
2194 | 219k | { |
2195 | 219k | mrb_callinfo *ci; |
2196 | | |
2197 | 219k | ci = mrb->c->ci; |
2198 | 219k | if (mrb->exc) { |
2199 | 148 | L_RAISE: |
2200 | 148 | ci = mrb->c->ci; |
2201 | 416 | while ((ch = catch_handler_find(mrb, ci, pc, MRB_CATCH_FILTER_ALL)) == NULL) { |
2202 | 416 | if (ci != mrb->c->cibase) { |
2203 | 268 | ci = cipop(mrb); |
2204 | 268 | if (ci[1].cci == CINFO_SKIP && prev_jmp) { |
2205 | 0 | mrb->jmp = prev_jmp; |
2206 | 0 | MRB_THROW(prev_jmp); |
2207 | 0 | } |
2208 | 268 | pc = ci[0].pc; |
2209 | 268 | } |
2210 | 148 | else if (mrb->c == mrb->root_c) { |
2211 | 148 | mrb->c->ci->stack = mrb->c->stbase; |
2212 | 148 | goto L_STOP; |
2213 | 148 | } |
2214 | 0 | else { |
2215 | 0 | struct mrb_context *c = mrb->c; |
2216 | |
|
2217 | 0 | c->status = MRB_FIBER_TERMINATED; |
2218 | 0 | mrb->c = c->prev; |
2219 | 0 | if (!mrb->c) mrb->c = mrb->root_c; |
2220 | 0 | else c->prev = NULL; |
2221 | 0 | goto L_RAISE; |
2222 | 0 | } |
2223 | 416 | } |
2224 | | |
2225 | 0 | if (FALSE) { |
2226 | 0 | L_CATCH_TAGGED_BREAK: /* from THROW_TAGGED_BREAK() or UNWIND_ENSURE() */ |
2227 | 0 | ci = mrb->c->ci; |
2228 | 0 | } |
2229 | 0 | proc = ci->proc; |
2230 | 0 | irep = proc->body.irep; |
2231 | 0 | pool = irep->pool; |
2232 | 0 | syms = irep->syms; |
2233 | 0 | stack_extend(mrb, irep->nregs); |
2234 | 0 | pc = irep->iseq + mrb_irep_catch_handler_unpack(ch->target); |
2235 | 0 | } |
2236 | 219k | else { |
2237 | 219k | mrb_int acc; |
2238 | 219k | mrb_value v; |
2239 | | |
2240 | 219k | ci = mrb->c->ci; |
2241 | 219k | v = regs[a]; |
2242 | 219k | mrb_gc_protect(mrb, v); |
2243 | 219k | switch (c) { |
2244 | 0 | case OP_R_RETURN: |
2245 | | /* Fall through to OP_R_NORMAL otherwise */ |
2246 | 0 | if (ci->cci == CINFO_NONE && MRB_PROC_ENV_P(proc) && !MRB_PROC_STRICT_P(proc)) { |
2247 | 0 | const struct RProc *dst; |
2248 | 0 | mrb_callinfo *cibase; |
2249 | 0 | cibase = mrb->c->cibase; |
2250 | 0 | dst = top_proc(mrb, proc); |
2251 | |
|
2252 | 0 | if (MRB_PROC_ENV_P(dst)) { |
2253 | 0 | struct REnv *e = MRB_PROC_ENV(dst); |
2254 | |
|
2255 | 0 | if (!MRB_ENV_ONSTACK_P(e) || (e->cxt && e->cxt != mrb->c)) { |
2256 | 0 | localjump_error(mrb, LOCALJUMP_ERROR_RETURN); |
2257 | 0 | goto L_RAISE; |
2258 | 0 | } |
2259 | 0 | } |
2260 | | /* check jump destination */ |
2261 | 0 | while (cibase <= ci && ci->proc != dst) { |
2262 | 0 | if (ci->cci > CINFO_NONE) { /* jump cross C boundary */ |
2263 | 0 | localjump_error(mrb, LOCALJUMP_ERROR_RETURN); |
2264 | 0 | goto L_RAISE; |
2265 | 0 | } |
2266 | 0 | ci--; |
2267 | 0 | } |
2268 | 0 | if (ci <= cibase) { /* no jump destination */ |
2269 | 0 | localjump_error(mrb, LOCALJUMP_ERROR_RETURN); |
2270 | 0 | goto L_RAISE; |
2271 | 0 | } |
2272 | 0 | ci = mrb->c->ci; |
2273 | 0 | while (cibase <= ci && ci->proc != dst) { |
2274 | 0 | CHECKPOINT_RESTORE(RBREAK_TAG_RETURN_BLOCK) { |
2275 | 0 | cibase = mrb->c->cibase; |
2276 | 0 | dst = top_proc(mrb, proc); |
2277 | 0 | } |
2278 | 0 | CHECKPOINT_MAIN(RBREAK_TAG_RETURN_BLOCK) { |
2279 | 0 | UNWIND_ENSURE(mrb, ci, pc, RBREAK_TAG_RETURN_BLOCK, proc, v); |
2280 | 0 | } |
2281 | 0 | CHECKPOINT_END(RBREAK_TAG_RETURN_BLOCK); |
2282 | 0 | ci = cipop(mrb); |
2283 | 0 | pc = ci->pc; |
2284 | 0 | } |
2285 | 0 | proc = ci->proc; |
2286 | 0 | mrb->exc = NULL; /* clear break object */ |
2287 | 0 | break; |
2288 | 0 | } |
2289 | | /* fallthrough */ |
2290 | 219k | case OP_R_NORMAL: |
2291 | 219k | NORMAL_RETURN: |
2292 | 219k | if (ci == mrb->c->cibase) { |
2293 | 12.6k | struct mrb_context *c; |
2294 | 12.6k | c = mrb->c; |
2295 | | |
2296 | 12.6k | if (!c->prev) { |
2297 | 12.6k | if (c != mrb->root_c) { |
2298 | | /* fiber termination should transfer to root */ |
2299 | 0 | c->prev = mrb->root_c; |
2300 | 0 | } |
2301 | 12.6k | else { /* toplevel return */ |
2302 | 12.6k | regs[irep->nlocals] = v; |
2303 | 12.6k | goto CHECKPOINT_LABEL_MAKE(RBREAK_TAG_STOP); |
2304 | 12.6k | } |
2305 | 12.6k | } |
2306 | 0 | else if (!c->vmexec && c->prev->ci == c->prev->cibase) { |
2307 | 0 | RAISE_LIT(mrb, E_FIBER_ERROR, "double resume"); |
2308 | 0 | } |
2309 | 0 | CHECKPOINT_RESTORE(RBREAK_TAG_RETURN_TOPLEVEL) { |
2310 | 0 | c = mrb->c; |
2311 | 0 | } |
2312 | 0 | CHECKPOINT_MAIN(RBREAK_TAG_RETURN_TOPLEVEL) { |
2313 | 0 | UNWIND_ENSURE(mrb, ci, pc, RBREAK_TAG_RETURN_TOPLEVEL, proc, v); |
2314 | 0 | } |
2315 | 0 | CHECKPOINT_END(RBREAK_TAG_RETURN_TOPLEVEL); |
2316 | | /* automatic yield at the end */ |
2317 | 0 | c->status = MRB_FIBER_TERMINATED; |
2318 | 0 | mrb->c = c->prev; |
2319 | 0 | mrb->c->status = MRB_FIBER_RUNNING; |
2320 | 0 | c->prev = NULL; |
2321 | 0 | if (c->vmexec) { |
2322 | 0 | mrb_gc_arena_restore(mrb, ai); |
2323 | 0 | c->vmexec = FALSE; |
2324 | 0 | mrb->jmp = prev_jmp; |
2325 | 0 | return v; |
2326 | 0 | } |
2327 | 0 | ci = mrb->c->ci; |
2328 | 0 | } |
2329 | 206k | CHECKPOINT_RESTORE(RBREAK_TAG_RETURN) { |
2330 | | /* do nothing */ |
2331 | 0 | } |
2332 | 413k | CHECKPOINT_MAIN(RBREAK_TAG_RETURN) { |
2333 | 413k | UNWIND_ENSURE(mrb, ci, pc, RBREAK_TAG_RETURN, proc, v); |
2334 | 413k | } |
2335 | 413k | CHECKPOINT_END(RBREAK_TAG_RETURN); |
2336 | 206k | mrb->exc = NULL; /* clear break object */ |
2337 | 206k | break; |
2338 | 0 | case OP_R_BREAK: |
2339 | 0 | if (MRB_PROC_STRICT_P(proc)) goto NORMAL_RETURN; |
2340 | 0 | if (MRB_PROC_ORPHAN_P(proc) || !MRB_PROC_ENV_P(proc) || !MRB_ENV_ONSTACK_P(MRB_PROC_ENV(proc))) { |
2341 | 0 | L_BREAK_ERROR: |
2342 | 0 | RAISE_LIT(mrb, E_LOCALJUMP_ERROR, "break from proc-closure"); |
2343 | 0 | } |
2344 | 0 | else { |
2345 | 0 | struct REnv *e = MRB_PROC_ENV(proc); |
2346 | |
|
2347 | 0 | if (e->cxt != mrb->c) { |
2348 | 0 | goto L_BREAK_ERROR; |
2349 | 0 | } |
2350 | 0 | } |
2351 | 0 | CHECKPOINT_RESTORE(RBREAK_TAG_BREAK) { |
2352 | | /* do nothing */ |
2353 | 0 | } |
2354 | 0 | CHECKPOINT_MAIN(RBREAK_TAG_BREAK) { |
2355 | 0 | UNWIND_ENSURE(mrb, ci, pc, RBREAK_TAG_BREAK, proc, v); |
2356 | 0 | } |
2357 | 0 | CHECKPOINT_END(RBREAK_TAG_BREAK); |
2358 | | /* break from fiber block */ |
2359 | 0 | if (ci == mrb->c->cibase && ci->pc) { |
2360 | 0 | struct mrb_context *c = mrb->c; |
2361 | |
|
2362 | 0 | mrb->c = c->prev; |
2363 | 0 | c->prev = NULL; |
2364 | 0 | ci = mrb->c->ci; |
2365 | 0 | } |
2366 | 0 | if (ci->cci > CINFO_NONE) { |
2367 | 0 | ci = cipop(mrb); |
2368 | 0 | mrb->exc = (struct RObject*)break_new(mrb, RBREAK_TAG_BREAK, proc, v); |
2369 | 0 | mrb_gc_arena_restore(mrb, ai); |
2370 | 0 | mrb->c->vmexec = FALSE; |
2371 | 0 | mrb->jmp = prev_jmp; |
2372 | 0 | MRB_THROW(prev_jmp); |
2373 | 0 | } |
2374 | 0 | if (FALSE) { |
2375 | 0 | struct RBreak *brk; |
2376 | |
|
2377 | 0 | L_BREAK: |
2378 | 0 | brk = (struct RBreak*)mrb->exc; |
2379 | 0 | proc = mrb_break_proc_get(brk); |
2380 | 0 | v = mrb_break_value_get(brk); |
2381 | 0 | ci = mrb->c->ci; |
2382 | |
|
2383 | 0 | switch (mrb_break_tag_get(brk)) { |
2384 | 0 | #define DISPATCH_CHECKPOINTS(n, i) case n: goto CHECKPOINT_LABEL_MAKE(n); |
2385 | 0 | RBREAK_TAG_FOREACH(DISPATCH_CHECKPOINTS) |
2386 | 0 | #undef DISPATCH_CHECKPOINTS |
2387 | 0 | default: |
2388 | 0 | mrb_assert(!"wrong break tag"); |
2389 | 0 | } |
2390 | 0 | } |
2391 | 0 | while (mrb->c->cibase < ci && ci[-1].proc != proc->upper) { |
2392 | 0 | if (ci[-1].cci == CINFO_SKIP) { |
2393 | 0 | goto L_BREAK_ERROR; |
2394 | 0 | } |
2395 | 0 | CHECKPOINT_RESTORE(RBREAK_TAG_BREAK_UPPER) { |
2396 | | /* do nothing */ |
2397 | 0 | } |
2398 | 0 | CHECKPOINT_MAIN(RBREAK_TAG_BREAK_UPPER) { |
2399 | 0 | UNWIND_ENSURE(mrb, ci, pc, RBREAK_TAG_BREAK_UPPER, proc, v); |
2400 | 0 | } |
2401 | 0 | CHECKPOINT_END(RBREAK_TAG_BREAK_UPPER); |
2402 | 0 | ci = cipop(mrb); |
2403 | 0 | pc = ci->pc; |
2404 | 0 | } |
2405 | 0 | CHECKPOINT_RESTORE(RBREAK_TAG_BREAK_INTARGET) { |
2406 | | /* do nothing */ |
2407 | 0 | } |
2408 | 0 | CHECKPOINT_MAIN(RBREAK_TAG_BREAK_INTARGET) { |
2409 | 0 | UNWIND_ENSURE(mrb, ci, pc, RBREAK_TAG_BREAK_INTARGET, proc, v); |
2410 | 0 | } |
2411 | 0 | CHECKPOINT_END(RBREAK_TAG_BREAK_INTARGET); |
2412 | 0 | if (ci == mrb->c->cibase) { |
2413 | 0 | goto L_BREAK_ERROR; |
2414 | 0 | } |
2415 | 0 | mrb->exc = NULL; /* clear break object */ |
2416 | 0 | break; |
2417 | 0 | default: |
2418 | | /* cannot happen */ |
2419 | 0 | break; |
2420 | 219k | } |
2421 | 206k | mrb_assert(ci == mrb->c->ci); |
2422 | 206k | mrb_assert(mrb->exc == NULL); |
2423 | | |
2424 | 206k | if (mrb->c->vmexec && !CI_TARGET_CLASS(ci)) { |
2425 | 0 | mrb_gc_arena_restore(mrb, ai); |
2426 | 0 | mrb->c->vmexec = FALSE; |
2427 | 0 | mrb->jmp = prev_jmp; |
2428 | 0 | return v; |
2429 | 0 | } |
2430 | 206k | acc = ci->cci; |
2431 | 206k | ci = cipop(mrb); |
2432 | 206k | if (acc == CINFO_SKIP || acc == CINFO_DIRECT) { |
2433 | 16 | mrb_gc_arena_restore(mrb, ai); |
2434 | 16 | mrb->jmp = prev_jmp; |
2435 | 16 | return v; |
2436 | 16 | } |
2437 | 206k | pc = ci->pc; |
2438 | 206k | DEBUG(fprintf(stderr, "from :%s\n", mrb_sym_name(mrb, ci->mid))); |
2439 | 206k | proc = ci->proc; |
2440 | 206k | irep = proc->body.irep; |
2441 | 206k | pool = irep->pool; |
2442 | 206k | syms = irep->syms; |
2443 | | |
2444 | 206k | ci[1].stack[0] = v; |
2445 | 206k | mrb_gc_arena_restore(mrb, ai); |
2446 | 206k | } |
2447 | 206k | JUMP; |
2448 | 206k | } |
2449 | | |
2450 | 206k | CASE(OP_BLKPUSH, BS) { |
2451 | 0 | int m1 = (b>>11)&0x3f; |
2452 | 0 | int r = (b>>10)&0x1; |
2453 | 0 | int m2 = (b>>5)&0x1f; |
2454 | 0 | int kd = (b>>4)&0x1; |
2455 | 0 | int lv = (b>>0)&0xf; |
2456 | 0 | mrb_value *stack; |
2457 | |
|
2458 | 0 | if (lv == 0) stack = regs + 1; |
2459 | 0 | else { |
2460 | 0 | struct REnv *e = uvenv(mrb, lv-1); |
2461 | 0 | if (!e || (!MRB_ENV_ONSTACK_P(e) && e->mid == 0) || |
2462 | 0 | MRB_ENV_LEN(e) <= m1+r+m2+1) { |
2463 | 0 | localjump_error(mrb, LOCALJUMP_ERROR_YIELD); |
2464 | 0 | goto L_RAISE; |
2465 | 0 | } |
2466 | 0 | stack = e->stack + 1; |
2467 | 0 | } |
2468 | 0 | if (mrb_nil_p(stack[m1+r+m2+kd])) { |
2469 | 0 | localjump_error(mrb, LOCALJUMP_ERROR_YIELD); |
2470 | 0 | goto L_RAISE; |
2471 | 0 | } |
2472 | 0 | regs[a] = stack[m1+r+m2+kd]; |
2473 | 0 | NEXT; |
2474 | 0 | } |
2475 | | |
2476 | | #if !defined(MRB_USE_BIGINT) || defined(MRB_INT32) |
2477 | | L_INT_OVERFLOW: |
2478 | | RAISE_LIT(mrb, E_RANGE_ERROR, "integer overflow"); |
2479 | | #endif |
2480 | | |
2481 | 285k | #define TYPES2(a,b) ((((uint16_t)(a))<<8)|(((uint16_t)(b))&0xff)) |
2482 | 0 | #define OP_MATH(op_name) \ |
2483 | | /* need to check if op is overridden */ \ |
2484 | 16.5k | switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) { \ |
2485 | 6.72k | OP_MATH_CASE_INTEGER(op_name); \ |
2486 | 732 | OP_MATH_CASE_FLOAT(op_name, integer, float); \ |
2487 | 818 | OP_MATH_CASE_FLOAT(op_name, float, integer); \ |
2488 | 826 | OP_MATH_CASE_FLOAT(op_name, float, float); \ |
2489 | 0 | OP_MATH_CASE_STRING_##op_name(); \ |
2490 | 7.42k | default: \ |
2491 | 7.42k | mid = MRB_OPSYM(op_name); \ |
2492 | 7.42k | goto L_SEND_SYM; \ |
2493 | 16.5k | } \ |
2494 | 16.5k | NEXT; |
2495 | 0 | #define OP_MATH_CASE_INTEGER(op_name) \ |
2496 | 6.72k | case TYPES2(MRB_TT_INTEGER, MRB_TT_INTEGER): \ |
2497 | 6.72k | { \ |
2498 | 6.72k | mrb_int x = mrb_integer(regs[a]), y = mrb_integer(regs[a+1]), z; \ |
2499 | 6.72k | if (mrb_int_##op_name##_overflow(x, y, &z)) { \ |
2500 | 2.32k | OP_MATH_OVERFLOW_INT(op_name,x,y); \ |
2501 | 2.32k | } \ |
2502 | 6.72k | else \ |
2503 | 6.72k | SET_INT_VALUE(mrb,regs[a], z); \ |
2504 | 6.72k | } \ |
2505 | 6.72k | break |
2506 | | #ifdef MRB_NO_FLOAT |
2507 | | #define OP_MATH_CASE_FLOAT(op_name, t1, t2) (void)0 |
2508 | | #else |
2509 | 0 | #define OP_MATH_CASE_FLOAT(op_name, t1, t2) \ |
2510 | 2.37k | case TYPES2(OP_MATH_TT_##t1, OP_MATH_TT_##t2): \ |
2511 | 2.37k | { \ |
2512 | 2.37k | mrb_float z = mrb_##t1(regs[a]) OP_MATH_OP_##op_name mrb_##t2(regs[a+1]); \ |
2513 | 2.37k | SET_FLOAT_VALUE(mrb, regs[a], z); \ |
2514 | 2.37k | } \ |
2515 | 2.37k | break |
2516 | 0 | #endif |
2517 | 0 | #ifdef MRB_USE_BIGINT |
2518 | 2.84k | #define OP_MATH_OVERFLOW_INT(op,x,y) regs[a] = mrb_bint_##op##_ii(mrb,x,y) |
2519 | | #else |
2520 | | #define OP_MATH_OVERFLOW_INT(op,x,y) goto L_INT_OVERFLOW |
2521 | | #endif |
2522 | 0 | #define OP_MATH_CASE_STRING_add() \ |
2523 | 0 | case TYPES2(MRB_TT_STRING, MRB_TT_STRING): \ |
2524 | 0 | regs[a] = mrb_str_plus(mrb, regs[a], regs[a+1]); \ |
2525 | 0 | mrb_gc_arena_restore(mrb, ai); \ |
2526 | 0 | break |
2527 | 0 | #define OP_MATH_CASE_STRING_sub() (void)0 |
2528 | 0 | #define OP_MATH_CASE_STRING_mul() (void)0 |
2529 | 0 | #define OP_MATH_OP_add + |
2530 | 0 | #define OP_MATH_OP_sub - |
2531 | 0 | #define OP_MATH_OP_mul * |
2532 | 0 | #define OP_MATH_TT_integer MRB_TT_INTEGER |
2533 | 0 | #define OP_MATH_TT_float MRB_TT_FLOAT |
2534 | | |
2535 | 10.8k | CASE(OP_ADD, B) { |
2536 | 10.8k | OP_MATH(add); |
2537 | 3.21k | } |
2538 | | |
2539 | 13.1k | CASE(OP_SUB, B) { |
2540 | 13.1k | OP_MATH(sub); |
2541 | 3.29k | } |
2542 | | |
2543 | 8.57k | CASE(OP_MUL, B) { |
2544 | 8.57k | OP_MATH(mul); |
2545 | 2.58k | } |
2546 | | |
2547 | 16.5k | CASE(OP_DIV, B) { |
2548 | 16.5k | #ifndef MRB_NO_FLOAT |
2549 | 16.5k | mrb_float x, y, f; |
2550 | 16.5k | #endif |
2551 | | |
2552 | | /* need to check if op is overridden */ |
2553 | 16.5k | switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) { |
2554 | 2.66k | case TYPES2(MRB_TT_INTEGER,MRB_TT_INTEGER): |
2555 | 2.66k | { |
2556 | 2.66k | mrb_int x = mrb_integer(regs[a]); |
2557 | 2.66k | mrb_int y = mrb_integer(regs[a+1]); |
2558 | 2.66k | regs[a] = mrb_div_int_value(mrb, x, y); |
2559 | 2.66k | } |
2560 | 2.66k | NEXT; |
2561 | 2.66k | #ifndef MRB_NO_FLOAT |
2562 | 3.24k | case TYPES2(MRB_TT_INTEGER,MRB_TT_FLOAT): |
2563 | 3.24k | x = (mrb_float)mrb_integer(regs[a]); |
2564 | 3.24k | y = mrb_float(regs[a+1]); |
2565 | 3.24k | break; |
2566 | 539 | case TYPES2(MRB_TT_FLOAT,MRB_TT_INTEGER): |
2567 | 539 | x = mrb_float(regs[a]); |
2568 | 539 | y = (mrb_float)mrb_integer(regs[a+1]); |
2569 | 539 | break; |
2570 | 407 | case TYPES2(MRB_TT_FLOAT,MRB_TT_FLOAT): |
2571 | 407 | x = mrb_float(regs[a]); |
2572 | 407 | y = mrb_float(regs[a+1]); |
2573 | 407 | break; |
2574 | 0 | #endif |
2575 | 4.22k | default: |
2576 | 4.22k | mid = MRB_OPSYM(div); |
2577 | 4.22k | goto L_SEND_SYM; |
2578 | 16.5k | } |
2579 | | |
2580 | 1.52k | #ifndef MRB_NO_FLOAT |
2581 | 1.52k | f = mrb_div_float(x, y); |
2582 | 1.52k | SET_FLOAT_VALUE(mrb, regs[a], f); |
2583 | 1.52k | #endif |
2584 | 1.52k | NEXT; |
2585 | 1.52k | } |
2586 | | |
2587 | 0 | #define OP_MATHI(op_name) \ |
2588 | | /* need to check if op is overridden */ \ |
2589 | 87.4k | switch (mrb_type(regs[a])) { \ |
2590 | 86.3k | OP_MATHI_CASE_INTEGER(op_name); \ |
2591 | 495 | OP_MATHI_CASE_FLOAT(op_name); \ |
2592 | 702 | default: \ |
2593 | 702 | SET_INT_VALUE(mrb,regs[a+1], b); \ |
2594 | 702 | mid = MRB_OPSYM(op_name); \ |
2595 | 702 | goto L_SEND_SYM; \ |
2596 | 87.4k | } \ |
2597 | 87.4k | NEXT; |
2598 | 0 | #define OP_MATHI_CASE_INTEGER(op_name) \ |
2599 | 86.3k | case MRB_TT_INTEGER: \ |
2600 | 86.3k | { \ |
2601 | 86.3k | mrb_int x = mrb_integer(regs[a]), y = (mrb_int)b, z; \ |
2602 | 86.3k | if (mrb_int_##op_name##_overflow(x, y, &z)) { \ |
2603 | 524 | OP_MATH_OVERFLOW_INT(op_name,x,y); \ |
2604 | 524 | } \ |
2605 | 86.3k | else \ |
2606 | 86.3k | SET_INT_VALUE(mrb,regs[a], z); \ |
2607 | 86.3k | } \ |
2608 | 86.3k | break |
2609 | | #ifdef MRB_NO_FLOAT |
2610 | | #define OP_MATHI_CASE_FLOAT(op_name) (void)0 |
2611 | | #else |
2612 | 0 | #define OP_MATHI_CASE_FLOAT(op_name) \ |
2613 | 495 | case MRB_TT_FLOAT: \ |
2614 | 495 | { \ |
2615 | 495 | mrb_float z = mrb_float(regs[a]) OP_MATH_OP_##op_name b; \ |
2616 | 495 | SET_FLOAT_VALUE(mrb, regs[a], z); \ |
2617 | 495 | } \ |
2618 | 495 | break |
2619 | 0 | #endif |
2620 | | |
2621 | 171k | CASE(OP_ADDI, BB) { |
2622 | 171k | OP_MATHI(add); |
2623 | 85.4k | } |
2624 | | |
2625 | 85.4k | CASE(OP_SUBI, BB) { |
2626 | 3.45k | OP_MATHI(sub); |
2627 | 1.31k | } |
2628 | | |
2629 | 107k | #define OP_CMP_BODY(op,v1,v2) (v1(regs[a]) op v2(regs[a+1])) |
2630 | | |
2631 | | #ifdef MRB_NO_FLOAT |
2632 | | #define OP_CMP(op,sym) do {\ |
2633 | | int result;\ |
2634 | | /* need to check if - is overridden */\ |
2635 | | switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {\ |
2636 | | case TYPES2(MRB_TT_INTEGER,MRB_TT_INTEGER):\ |
2637 | | result = OP_CMP_BODY(op,mrb_fixnum,mrb_fixnum);\ |
2638 | | break;\ |
2639 | | default:\ |
2640 | | mid = MRB_OPSYM(sym);\ |
2641 | | goto L_SEND_SYM;\ |
2642 | | }\ |
2643 | | if (result) {\ |
2644 | | SET_TRUE_VALUE(regs[a]);\ |
2645 | | }\ |
2646 | | else {\ |
2647 | | SET_FALSE_VALUE(regs[a]);\ |
2648 | | }\ |
2649 | | } while(0) |
2650 | | #else |
2651 | 137k | #define OP_CMP(op, sym) do {\ |
2652 | 137k | int result;\ |
2653 | | /* need to check if - is overridden */\ |
2654 | 137k | switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {\ |
2655 | 104k | case TYPES2(MRB_TT_INTEGER,MRB_TT_INTEGER):\ |
2656 | 104k | result = OP_CMP_BODY(op,mrb_integer,mrb_integer);\ |
2657 | 104k | break;\ |
2658 | 1.24k | case TYPES2(MRB_TT_INTEGER,MRB_TT_FLOAT):\ |
2659 | 1.24k | result = OP_CMP_BODY(op,mrb_integer,mrb_float);\ |
2660 | 1.24k | break;\ |
2661 | 1.23k | case TYPES2(MRB_TT_FLOAT,MRB_TT_INTEGER):\ |
2662 | 1.23k | result = OP_CMP_BODY(op,mrb_float,mrb_integer);\ |
2663 | 1.23k | break;\ |
2664 | 426 | case TYPES2(MRB_TT_FLOAT,MRB_TT_FLOAT):\ |
2665 | 426 | result = OP_CMP_BODY(op,mrb_float,mrb_float);\ |
2666 | 426 | break;\ |
2667 | 29.1k | default:\ |
2668 | 29.1k | mid = MRB_OPSYM(sym);\ |
2669 | 29.1k | goto L_SEND_SYM;\ |
2670 | 137k | }\ |
2671 | 137k | if (result) {\ |
2672 | 88.2k | SET_TRUE_VALUE(regs[a]);\ |
2673 | 88.2k | }\ |
2674 | 107k | else {\ |
2675 | 19.6k | SET_FALSE_VALUE(regs[a]);\ |
2676 | 19.6k | }\ |
2677 | 107k | } while(0) |
2678 | 0 | #endif |
2679 | | |
2680 | 129k | CASE(OP_EQ, B) { |
2681 | 129k | if (mrb_obj_eq(mrb, regs[a], regs[a+1])) { |
2682 | 36.9k | SET_TRUE_VALUE(regs[a]); |
2683 | 36.9k | } |
2684 | 27.8k | else { |
2685 | 27.8k | OP_CMP(==,eq); |
2686 | 27.8k | } |
2687 | 37.7k | NEXT; |
2688 | 37.7k | } |
2689 | | |
2690 | 201k | CASE(OP_LT, B) { |
2691 | 201k | OP_CMP(<,lt); |
2692 | 100k | NEXT; |
2693 | 100k | } |
2694 | | |
2695 | 100k | CASE(OP_LE, B) { |
2696 | 8.35k | OP_CMP(<=,le); |
2697 | 2.83k | NEXT; |
2698 | 2.83k | } |
2699 | | |
2700 | 5.68k | CASE(OP_GT, B) { |
2701 | 5.68k | OP_CMP(>,gt); |
2702 | 2.12k | NEXT; |
2703 | 2.12k | } |
2704 | | |
2705 | 2.43k | CASE(OP_GE, B) { |
2706 | 2.43k | OP_CMP(>=,ge); |
2707 | 1.17k | NEXT; |
2708 | 1.17k | } |
2709 | | |
2710 | 138k | CASE(OP_ARRAY, BB) { |
2711 | 138k | regs[a] = ary_new_from_regs(mrb, b, a); |
2712 | 138k | mrb_gc_arena_restore(mrb, ai); |
2713 | 138k | NEXT; |
2714 | 138k | } |
2715 | 138k | CASE(OP_ARRAY2, BBB) { |
2716 | 0 | regs[a] = ary_new_from_regs(mrb, c, b); |
2717 | 0 | mrb_gc_arena_restore(mrb, ai); |
2718 | 0 | NEXT; |
2719 | 0 | } |
2720 | |
|
2721 | 60.0k | CASE(OP_ARYCAT, B) { |
2722 | 60.0k | mrb_value splat = mrb_ary_splat(mrb, regs[a+1]); |
2723 | 60.0k | if (mrb_nil_p(regs[a])) { |
2724 | 6.18k | regs[a] = splat; |
2725 | 6.18k | } |
2726 | 23.8k | else { |
2727 | 23.8k | mrb_assert(mrb_array_p(regs[a])); |
2728 | 23.8k | mrb_ary_concat(mrb, regs[a], splat); |
2729 | 23.8k | } |
2730 | 60.0k | mrb_gc_arena_restore(mrb, ai); |
2731 | 60.0k | NEXT; |
2732 | 60.0k | } |
2733 | | |
2734 | 68.8k | CASE(OP_ARYPUSH, BB) { |
2735 | 68.8k | mrb_assert(mrb_array_p(regs[a])); |
2736 | 93.0k | for (mrb_int i=0; i<b; i++) { |
2737 | 58.5k | mrb_ary_push(mrb, regs[a], regs[a+i+1]); |
2738 | 58.5k | } |
2739 | 68.8k | NEXT; |
2740 | 68.8k | } |
2741 | | |
2742 | 68.8k | CASE(OP_ARYSPLAT, B) { |
2743 | 0 | mrb_value ary = mrb_ary_splat(mrb, regs[a]); |
2744 | 0 | regs[a] = ary; |
2745 | 0 | mrb_gc_arena_restore(mrb, ai); |
2746 | 0 | NEXT; |
2747 | 0 | } |
2748 | |
|
2749 | 0 | CASE(OP_AREF, BBB) { |
2750 | 0 | mrb_value v = regs[b]; |
2751 | |
|
2752 | 0 | if (!mrb_array_p(v)) { |
2753 | 0 | if (c == 0) { |
2754 | 0 | regs[a] = v; |
2755 | 0 | } |
2756 | 0 | else { |
2757 | 0 | SET_NIL_VALUE(regs[a]); |
2758 | 0 | } |
2759 | 0 | } |
2760 | 0 | else { |
2761 | 0 | v = mrb_ary_ref(mrb, v, c); |
2762 | 0 | regs[a] = v; |
2763 | 0 | } |
2764 | 0 | NEXT; |
2765 | 0 | } |
2766 | |
|
2767 | 0 | CASE(OP_ASET, BBB) { |
2768 | 0 | mrb_assert(mrb_array_p(regs[a])); |
2769 | 0 | mrb_ary_set(mrb, regs[b], c, regs[a]); |
2770 | 0 | NEXT; |
2771 | 0 | } |
2772 | |
|
2773 | 0 | CASE(OP_APOST, BBB) { |
2774 | 0 | mrb_value v = regs[a]; |
2775 | 0 | int pre = b; |
2776 | 0 | int post = c; |
2777 | 0 | struct RArray *ary; |
2778 | 0 | int len, idx; |
2779 | |
|
2780 | 0 | if (!mrb_array_p(v)) { |
2781 | 0 | v = ary_new_from_regs(mrb, 1, a); |
2782 | 0 | } |
2783 | 0 | ary = mrb_ary_ptr(v); |
2784 | 0 | len = (int)ARY_LEN(ary); |
2785 | 0 | if (len > pre + post) { |
2786 | 0 | v = mrb_ary_new_from_values(mrb, len - pre - post, ARY_PTR(ary)+pre); |
2787 | 0 | regs[a++] = v; |
2788 | 0 | while (post--) { |
2789 | 0 | regs[a++] = ARY_PTR(ary)[len-post-1]; |
2790 | 0 | } |
2791 | 0 | } |
2792 | 0 | else { |
2793 | 0 | v = mrb_ary_new_capa(mrb, 0); |
2794 | 0 | regs[a++] = v; |
2795 | 0 | for (idx=0; idx+pre<len; idx++) { |
2796 | 0 | regs[a+idx] = ARY_PTR(ary)[pre+idx]; |
2797 | 0 | } |
2798 | 0 | while (idx < post) { |
2799 | 0 | SET_NIL_VALUE(regs[a+idx]); |
2800 | 0 | idx++; |
2801 | 0 | } |
2802 | 0 | } |
2803 | 0 | mrb_gc_arena_restore(mrb, ai); |
2804 | 0 | NEXT; |
2805 | 0 | } |
2806 | |
|
2807 | 0 | CASE(OP_INTERN, B) { |
2808 | 0 | mrb_assert(mrb_string_p(regs[a])); |
2809 | 0 | mrb_sym sym = mrb_intern_str(mrb, regs[a]); |
2810 | 0 | regs[a] = mrb_symbol_value(sym); |
2811 | 0 | NEXT; |
2812 | 0 | } |
2813 | |
|
2814 | 0 | CASE(OP_SYMBOL, BB) { |
2815 | 0 | size_t len; |
2816 | 0 | mrb_sym sym; |
2817 | |
|
2818 | 0 | mrb_assert((pool[b].tt&IREP_TT_NFLAG)==0); |
2819 | 0 | len = pool[b].tt >> 2; |
2820 | 0 | if (pool[b].tt & IREP_TT_SFLAG) { |
2821 | 0 | sym = mrb_intern_static(mrb, pool[b].u.str, len); |
2822 | 0 | } |
2823 | 0 | else { |
2824 | 0 | sym = mrb_intern(mrb, pool[b].u.str, len); |
2825 | 0 | } |
2826 | 0 | regs[a] = mrb_symbol_value(sym); |
2827 | 0 | NEXT; |
2828 | 0 | } |
2829 | |
|
2830 | 200k | CASE(OP_STRING, BB) { |
2831 | 200k | mrb_int len; |
2832 | | |
2833 | 200k | mrb_assert((pool[b].tt&IREP_TT_NFLAG)==0); |
2834 | 200k | len = pool[b].tt >> 2; |
2835 | 200k | if (pool[b].tt & IREP_TT_SFLAG) { |
2836 | 0 | regs[a] = mrb_str_new_static(mrb, pool[b].u.str, len); |
2837 | 0 | } |
2838 | 131k | else { |
2839 | 131k | regs[a] = mrb_str_new(mrb, pool[b].u.str, len); |
2840 | 131k | } |
2841 | 200k | mrb_gc_arena_restore(mrb, ai); |
2842 | 200k | NEXT; |
2843 | 200k | } |
2844 | | |
2845 | 200k | CASE(OP_STRCAT, B) { |
2846 | 160 | mrb_assert(mrb_string_p(regs[a])); |
2847 | 160 | mrb_str_concat(mrb, regs[a], regs[a+1]); |
2848 | 160 | NEXT; |
2849 | 160 | } |
2850 | | |
2851 | 1.64k | CASE(OP_HASH, BB) { |
2852 | 1.64k | mrb_value hash = mrb_hash_new_capa(mrb, b); |
2853 | 1.64k | int lim = a+b*2; |
2854 | | |
2855 | 38.6k | for (int i=a; i<lim; i+=2) { |
2856 | 37.7k | mrb_hash_set(mrb, hash, regs[i], regs[i+1]); |
2857 | 37.7k | } |
2858 | 1.64k | regs[a] = hash; |
2859 | 1.64k | mrb_gc_arena_restore(mrb, ai); |
2860 | 1.64k | NEXT; |
2861 | 1.64k | } |
2862 | | |
2863 | 1.69k | CASE(OP_HASHADD, BB) { |
2864 | 1.69k | mrb_value hash; |
2865 | 1.69k | int lim = a+b*2+1; |
2866 | | |
2867 | 1.69k | hash = regs[a]; |
2868 | 1.69k | mrb_ensure_hash_type(mrb, hash); |
2869 | 28.2k | for (int i=a+1; i<lim; i+=2) { |
2870 | 27.4k | mrb_hash_set(mrb, hash, regs[i], regs[i+1]); |
2871 | 27.4k | } |
2872 | 1.69k | mrb_gc_arena_restore(mrb, ai); |
2873 | 1.69k | NEXT; |
2874 | 1.69k | } |
2875 | 1.69k | CASE(OP_HASHCAT, B) { |
2876 | 0 | mrb_value hash = regs[a]; |
2877 | |
|
2878 | 0 | mrb_assert(mrb_hash_p(hash)); |
2879 | 0 | mrb_hash_merge(mrb, hash, regs[a+1]); |
2880 | 0 | mrb_gc_arena_restore(mrb, ai); |
2881 | 0 | NEXT; |
2882 | 0 | } |
2883 | |
|
2884 | 0 | CASE(OP_LAMBDA, BB) |
2885 | 0 | c = OP_L_LAMBDA; |
2886 | 218k | L_MAKE_LAMBDA: |
2887 | 218k | { |
2888 | 218k | struct RProc *p; |
2889 | 218k | const mrb_irep *nirep = irep->reps[b]; |
2890 | | |
2891 | 218k | if (c & OP_L_CAPTURE) { |
2892 | 6.18k | p = mrb_closure_new(mrb, nirep); |
2893 | 6.18k | } |
2894 | 212k | else { |
2895 | 212k | p = mrb_proc_new(mrb, nirep); |
2896 | 212k | p->flags |= MRB_PROC_SCOPE; |
2897 | 212k | } |
2898 | 218k | if (c & OP_L_STRICT) p->flags |= MRB_PROC_STRICT; |
2899 | 218k | regs[a] = mrb_obj_value(p); |
2900 | 218k | mrb_gc_arena_restore(mrb, ai); |
2901 | 218k | NEXT; |
2902 | 218k | } |
2903 | 218k | CASE(OP_BLOCK, BB) { |
2904 | 6.18k | c = OP_L_BLOCK; |
2905 | 6.18k | goto L_MAKE_LAMBDA; |
2906 | 6.18k | } |
2907 | 212k | CASE(OP_METHOD, BB) { |
2908 | 212k | c = OP_L_METHOD; |
2909 | 212k | goto L_MAKE_LAMBDA; |
2910 | 212k | } |
2911 | | |
2912 | 212k | CASE(OP_RANGE_INC, B) { |
2913 | 0 | mrb_value v = mrb_range_new(mrb, regs[a], regs[a+1], FALSE); |
2914 | 0 | regs[a] = v; |
2915 | 0 | mrb_gc_arena_restore(mrb, ai); |
2916 | 0 | NEXT; |
2917 | 0 | } |
2918 | |
|
2919 | 0 | CASE(OP_RANGE_EXC, B) { |
2920 | 0 | mrb_value v = mrb_range_new(mrb, regs[a], regs[a+1], TRUE); |
2921 | 0 | regs[a] = v; |
2922 | 0 | mrb_gc_arena_restore(mrb, ai); |
2923 | 0 | NEXT; |
2924 | 0 | } |
2925 | |
|
2926 | 0 | CASE(OP_OCLASS, B) { |
2927 | 0 | regs[a] = mrb_obj_value(mrb->object_class); |
2928 | 0 | NEXT; |
2929 | 0 | } |
2930 | |
|
2931 | 60.9k | CASE(OP_CLASS, BB) { |
2932 | 60.9k | struct RClass *c = 0, *baseclass; |
2933 | 60.9k | mrb_value base, super; |
2934 | 60.9k | mrb_sym id = syms[b]; |
2935 | | |
2936 | 60.9k | base = regs[a]; |
2937 | 60.9k | super = regs[a+1]; |
2938 | 60.9k | if (mrb_nil_p(base)) { |
2939 | 30.4k | baseclass = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc); |
2940 | 30.4k | if (!baseclass) baseclass = mrb->object_class; |
2941 | 30.4k | base = mrb_obj_value(baseclass); |
2942 | 30.4k | } |
2943 | 60.9k | c = mrb_vm_define_class(mrb, base, super, id); |
2944 | 60.9k | regs[a] = mrb_obj_value(c); |
2945 | 60.9k | mrb_gc_arena_restore(mrb, ai); |
2946 | 60.9k | NEXT; |
2947 | 60.9k | } |
2948 | | |
2949 | 60.9k | CASE(OP_MODULE, BB) { |
2950 | 13.3k | struct RClass *cls = 0, *baseclass; |
2951 | 13.3k | mrb_value base; |
2952 | 13.3k | mrb_sym id = syms[b]; |
2953 | | |
2954 | 13.3k | base = regs[a]; |
2955 | 13.3k | if (mrb_nil_p(base)) { |
2956 | 6.66k | baseclass = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc); |
2957 | 6.66k | if (!baseclass) baseclass = mrb->object_class; |
2958 | 6.66k | base = mrb_obj_value(baseclass); |
2959 | 6.66k | } |
2960 | 13.3k | cls = mrb_vm_define_module(mrb, base, id); |
2961 | 13.3k | regs[a] = mrb_obj_value(cls); |
2962 | 13.3k | mrb_gc_arena_restore(mrb, ai); |
2963 | 13.3k | NEXT; |
2964 | 13.3k | } |
2965 | | |
2966 | 63.7k | CASE(OP_EXEC, BB) |
2967 | 63.7k | { |
2968 | 63.7k | mrb_value recv = regs[a]; |
2969 | 63.7k | struct RProc *p; |
2970 | 63.7k | const mrb_irep *nirep = irep->reps[b]; |
2971 | | |
2972 | | /* prepare closure */ |
2973 | 63.7k | p = mrb_proc_new(mrb, nirep); |
2974 | 63.7k | p->c = NULL; |
2975 | 63.7k | mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)proc); |
2976 | 63.7k | MRB_PROC_SET_TARGET_CLASS(p, mrb_class_ptr(recv)); |
2977 | 63.7k | p->flags |= MRB_PROC_SCOPE; |
2978 | | |
2979 | | /* prepare call stack */ |
2980 | 63.7k | cipush(mrb, a, 0, mrb_class_ptr(recv), p, NULL, 0, 0); |
2981 | | |
2982 | 63.7k | irep = p->body.irep; |
2983 | 63.7k | pool = irep->pool; |
2984 | 63.7k | syms = irep->syms; |
2985 | 63.7k | stack_extend(mrb, irep->nregs); |
2986 | 63.7k | stack_clear(regs+1, irep->nregs-1); |
2987 | 63.7k | pc = irep->iseq; |
2988 | 63.7k | JUMP; |
2989 | 63.7k | } |
2990 | | |
2991 | 425k | CASE(OP_DEF, BB) { |
2992 | 425k | struct RClass *target = mrb_class_ptr(regs[a]); |
2993 | 425k | struct RProc *p = mrb_proc_ptr(regs[a+1]); |
2994 | 425k | mrb_method_t m; |
2995 | 425k | mrb_sym mid = syms[b]; |
2996 | | |
2997 | 425k | MRB_METHOD_FROM_PROC(m, p); |
2998 | 425k | mrb_define_method_raw(mrb, target, mid, m); |
2999 | 425k | mrb_method_added(mrb, target, mid); |
3000 | 425k | mrb_gc_arena_restore(mrb, ai); |
3001 | 425k | regs[a] = mrb_symbol_value(mid); |
3002 | 425k | NEXT; |
3003 | 425k | } |
3004 | | |
3005 | 425k | CASE(OP_SCLASS, B) { |
3006 | 47.6k | regs[a] = mrb_singleton_class(mrb, regs[a]); |
3007 | 47.6k | mrb_gc_arena_restore(mrb, ai); |
3008 | 47.6k | NEXT; |
3009 | 47.6k | } |
3010 | | |
3011 | 380k | CASE(OP_TCLASS, B) { |
3012 | 380k | struct RClass *target = check_target_class(mrb); |
3013 | 380k | if (!target) goto L_RAISE; |
3014 | 190k | regs[a] = mrb_obj_value(target); |
3015 | 190k | NEXT; |
3016 | 190k | } |
3017 | | |
3018 | 190k | CASE(OP_ALIAS, BB) { |
3019 | 56.1k | struct RClass *target = check_target_class(mrb); |
3020 | | |
3021 | 56.1k | if (!target) goto L_RAISE; |
3022 | 28.0k | mrb_alias_method(mrb, target, syms[a], syms[b]); |
3023 | 28.0k | mrb_method_added(mrb, target, syms[a]); |
3024 | 28.0k | NEXT; |
3025 | 28.0k | } |
3026 | 28.0k | CASE(OP_UNDEF, B) { |
3027 | 952 | struct RClass *target = check_target_class(mrb); |
3028 | | |
3029 | 952 | if (!target) goto L_RAISE; |
3030 | 476 | mrb_undef_method_id(mrb, target, syms[a]); |
3031 | 476 | NEXT; |
3032 | 476 | } |
3033 | | |
3034 | 476 | CASE(OP_DEBUG, Z) { |
3035 | 0 | FETCH_BBB(); |
3036 | 0 | #ifdef MRB_USE_DEBUG_HOOK |
3037 | 0 | mrb->debug_op_hook(mrb, irep, pc, regs); |
3038 | | #else |
3039 | | #ifndef MRB_NO_STDIO |
3040 | | printf("OP_DEBUG %d %d %d\n", a, b, c); |
3041 | | #else |
3042 | | abort(); |
3043 | | #endif |
3044 | | #endif |
3045 | 0 | NEXT; |
3046 | 0 | } |
3047 | |
|
3048 | 0 | CASE(OP_ERR, B) { |
3049 | 0 | size_t len = pool[a].tt >> 2; |
3050 | 0 | mrb_value exc; |
3051 | |
|
3052 | 0 | mrb_assert((pool[a].tt&IREP_TT_NFLAG)==0); |
3053 | 0 | exc = mrb_exc_new(mrb, E_LOCALJUMP_ERROR, pool[a].u.str, len); |
3054 | 0 | RAISE_EXC(mrb, exc); |
3055 | 0 | } |
3056 | | |
3057 | 232k | CASE(OP_EXT1, Z) { |
3058 | 232k | insn = READ_B(); |
3059 | 232k | switch (insn) { |
3060 | 116k | #define OPCODE(insn,ops) case OP_ ## insn: FETCH_ ## ops ## _1(); mrb->c->ci->pc = pc; goto L_OP_ ## insn ## _BODY; |
3061 | 232k | #include "mruby/ops.h" |
3062 | 232k | #undef OPCODE |
3063 | 232k | } |
3064 | 0 | pc--; |
3065 | 0 | NEXT; |
3066 | 0 | } |
3067 | 246 | CASE(OP_EXT2, Z) { |
3068 | 246 | insn = READ_B(); |
3069 | 246 | switch (insn) { |
3070 | 123 | #define OPCODE(insn,ops) case OP_ ## insn: FETCH_ ## ops ## _2(); mrb->c->ci->pc = pc; goto L_OP_ ## insn ## _BODY; |
3071 | 246 | #include "mruby/ops.h" |
3072 | 246 | #undef OPCODE |
3073 | 246 | } |
3074 | 0 | pc--; |
3075 | 0 | NEXT; |
3076 | 0 | } |
3077 | 164 | CASE(OP_EXT3, Z) { |
3078 | 164 | insn = READ_B(); |
3079 | 164 | switch (insn) { |
3080 | 82 | #define OPCODE(insn,ops) case OP_ ## insn: FETCH_ ## ops ## _3(); mrb->c->ci->pc = pc; goto L_OP_ ## insn ## _BODY; |
3081 | 164 | #include "mruby/ops.h" |
3082 | 164 | #undef OPCODE |
3083 | 164 | } |
3084 | 0 | pc--; |
3085 | 0 | NEXT; |
3086 | 0 | } |
3087 | | |
3088 | 0 | CASE(OP_STOP, Z) { |
3089 | | /* stop VM */ |
3090 | 25.2k | CHECKPOINT_RESTORE(RBREAK_TAG_STOP) { |
3091 | | /* do nothing */ |
3092 | 25.2k | } |
3093 | 25.2k | CHECKPOINT_MAIN(RBREAK_TAG_STOP) { |
3094 | 25.2k | UNWIND_ENSURE(mrb, mrb->c->ci, pc, RBREAK_TAG_STOP, proc, mrb_nil_value()); |
3095 | 25.2k | } |
3096 | 25.2k | CHECKPOINT_END(RBREAK_TAG_STOP); |
3097 | 12.7k | L_STOP: |
3098 | 12.7k | mrb->jmp = prev_jmp; |
3099 | 12.7k | if (mrb->exc) { |
3100 | 148 | mrb_assert(mrb->exc->tt == MRB_TT_EXCEPTION); |
3101 | 148 | return mrb_obj_value(mrb->exc); |
3102 | 148 | } |
3103 | 12.6k | return regs[irep->nlocals]; |
3104 | 12.7k | } |
3105 | 12.7k | } |
3106 | 0 | END_DISPATCH; |
3107 | 0 | #undef regs |
3108 | 0 | } |
3109 | 0 | MRB_CATCH(&c_jmp) { |
3110 | 0 | mrb_callinfo *ci = mrb->c->ci; |
3111 | 174 | while (ci > mrb->c->cibase && ci->cci == CINFO_DIRECT) { |
3112 | 37 | ci = cipop(mrb); |
3113 | 37 | } |
3114 | 0 | exc_catched = TRUE; |
3115 | 0 | pc = ci->pc; |
3116 | 0 | goto RETRY_TRY_BLOCK; |
3117 | 0 | } |
3118 | 0 | MRB_END_EXC(&c_jmp); |
3119 | 0 | } |
3120 | | |
3121 | | static mrb_value |
3122 | | mrb_run(mrb_state *mrb, const struct RProc *proc, mrb_value self) |
3123 | 16 | { |
3124 | 16 | return mrb_vm_run(mrb, proc, self, ci_bidx(mrb->c->ci) + 1); |
3125 | 16 | } |
3126 | | |
3127 | | MRB_API mrb_value |
3128 | | mrb_top_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep) |
3129 | 12.7k | { |
3130 | 12.7k | if (mrb->c->cibase && mrb->c->ci > mrb->c->cibase) { |
3131 | 0 | cipush(mrb, 0, CINFO_SKIP, mrb->object_class, NULL, NULL, 0, 0); |
3132 | 0 | } |
3133 | 12.7k | return mrb_vm_run(mrb, proc, self, stack_keep); |
3134 | 12.7k | } |
3135 | | |
3136 | | #if defined(MRB_USE_CXX_EXCEPTION) && defined(__cplusplus) |
3137 | | mrb_int mrb_jmpbuf_id = 0; |
3138 | | #endif |