Project

General

Profile

Feature #12093 ยป 0001-RubyVM-InstructionSequence-eval_with.patch

dalehamel (Dale Hamel), 07/23/2019 04:35 AM

View differences:

iseq.c
1345 1345
    return rb_iseq_eval(iseqw_check(self));
1346 1346
}
1347 1347

  
1348
/*
1349
 *  call-seq:
1350
 *     iseq.eval_with(binding) -> obj
1351
 *
1352
 *  Evaluates the instruction sequence and returns the result.
1353
 *
1354
 *      obj = Struct.new(:a, :b).new(1, 2)
1355
 *      bind = obj.instance_eval {binding}
1356
 *      RubyVM::InstructionSequence.compile("a + b").eval_with(bind) #=> 3
1357
 */
1358
static VALUE
1359
iseq_eval_with(VALUE self, VALUE scope)
1360
{
1361
    rb_secure(1);
1362
    return rb_iseq_eval_in_scope(iseqw_check(self), scope);
1363
}
1364

  
1348 1365
/*
1349 1366
 *  Returns a human-readable string representation of this instruction
1350 1367
 *  sequence, including the #label and #path.
......
3483 3500
    rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
3484 3501
    rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
3485 3502
    rb_define_method(rb_cISeq, "eval", iseqw_eval, 0);
3503
    rb_define_method(rb_cISeq, "eval_with", iseq_eval_with, 1);
3486 3504

  
3487 3505
    rb_define_method(rb_cISeq, "to_binary", iseqw_to_binary, -1);
3488 3506
    rb_define_singleton_method(rb_cISeq, "load_from_binary", iseqw_s_load_from_binary, 1);
vm.c
2138 2138
    return val;
2139 2139
}
2140 2140

  
2141
VALUE
2142
rb_iseq_eval_in_scope(const rb_iseq_t *iseq, VALUE scope)
2143
{
2144
    rb_thread_t *th = GET_THREAD();
2145
    rb_binding_t *bind = rb_check_typeddata(scope, &ruby_binding_data_type);
2146
    struct rb_block *base_block = &bind->block;
2147

  
2148
    if (iseq->body->local_table_size > 0) {
2149
	vm_bind_update_env(bind, vm_make_env_object(th, th->cfp));
2150
    }
2151
#if 0
2152
    iseq_set_parent_block(iseq, base_block);
2153
    iseq_set_local_table(iseq, rb_vm_cref()->nd_tbl);
2154
#endif
2155
    vm_set_eval_stack(th, iseq, 0, base_block);
2156

  
2157
    return vm_exec(th);
2158
}
2159

  
2141 2160
VALUE
2142 2161
rb_iseq_eval_main(const rb_iseq_t *iseq)
2143 2162
{
vm_core.h
1616 1616
/* functions about thread/vm execution */
1617 1617
RUBY_SYMBOL_EXPORT_BEGIN
1618 1618
VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1619
VALUE rb_iseq_eval_in_scope(const rb_iseq_t *iseq, VALUE scope);
1619 1620
VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1620 1621
VALUE rb_iseq_path(const rb_iseq_t *iseq);
1621 1622
VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
1622
-