Created
March 16, 2018 15:46
-
-
Save yannick/4571aa04af2d14aef234b9653cf5e204 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void | |
| replace_stop_with_return(mrb_state *mrb, mrb_irep *irep) | |
| { | |
| if (irep->iseq[irep->ilen - 1] == MKOP_A(OP_STOP, 0)) { | |
| if (irep->flags == MRB_ISEQ_NO_FREE) { | |
| mrb_code* iseq = (mrb_code *)mrb_malloc(mrb, (irep->ilen + 1) * sizeof(mrb_code)); | |
| memcpy(iseq, irep->iseq, irep->ilen * sizeof(mrb_code)); | |
| irep->iseq = iseq; | |
| irep->flags &= ~MRB_ISEQ_NO_FREE; | |
| } else { | |
| irep->iseq = (mrb_code *)mrb_realloc(mrb, irep->iseq, (irep->ilen + 1) * sizeof(mrb_code)); | |
| } | |
| irep->iseq[irep->ilen - 1] = MKOP_A(OP_LOADNIL, 0); | |
| irep->iseq[irep->ilen] = MKOP_AB(OP_RETURN, 0, OP_R_NORMAL); | |
| irep->ilen++; | |
| } | |
| } | |
| static mrb_value | |
| b_eval(mrb_state *mrb, mrb_value self) | |
| { | |
| char *s; | |
| mrb_int len; | |
| mrb_value binding = mrb_nil_value(); | |
| char *file = NULL; | |
| mrb_int line = 1; | |
| struct RProc *proc; | |
| int ai; | |
| mrb_get_args(mrb, "s", &s); | |
| mrb_irep* irep = mrb_read_irep( mrb, (uint8_t*)s); | |
| replace_stop_with_return(mrb, irep); | |
| proc = mrb_proc_new(mrb, irep); | |
| MRB_PROC_SET_TARGET_CLASS(proc, mrb->object_class); | |
| ai = mrb_gc_arena_save(mrb); | |
| mrb_yield_with_class(mrb, mrb_obj_value(proc), 0, NULL, mrb_top_self(mrb), mrb->object_class); | |
| mrb_gc_arena_restore(mrb, ai); | |
| return mrb_true_value(); | |
| } | |
| mrb_define_module_function(mrb, mrb->kernel_module, "beval", bin_eval, MRB_ARGS_ARG(1, 3)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment