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