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