mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Try Binding class
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
MRuby::Gem::Specification.new('mruby-binding') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'Binding class'
|
||||
|
||||
spec.add_dependency('mruby-eval', :core => 'mruby-eval')
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class Binding
|
||||
def eval(expr, *args)
|
||||
Kernel.eval(expr, self, *args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "mruby.h"
|
||||
#include "mruby/array.h"
|
||||
#include "mruby/hash.h"
|
||||
#include "mruby/proc.h"
|
||||
#include "mruby/variable.h"
|
||||
|
||||
mrb_value proc_local_variables(mrb_state *mrb, struct RProc *proc);
|
||||
|
||||
static mrb_value
|
||||
binding_local_variables(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RProc *proc = mrb_proc_ptr(mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "proc")));
|
||||
return proc_local_variables(mrb, proc);
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
mrb_f_binding(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RObject *obj;
|
||||
mrb_value binding;
|
||||
struct RProc *proc;
|
||||
mrb_int i;
|
||||
|
||||
obj = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb_class_get(mrb, "Binding"));
|
||||
binding = mrb_obj_value(obj);
|
||||
proc = mrb->c->ci[-1].proc;
|
||||
mrb_iv_set(mrb, binding, mrb_intern_lit(mrb, "proc"), mrb_obj_value(proc));
|
||||
mrb_iv_set(mrb, binding, mrb_intern_lit(mrb, "recv"), self);
|
||||
return binding;
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_binding_gem_init(mrb_state *mrb)
|
||||
{
|
||||
struct RClass *binding = mrb_define_class(mrb, "Binding", mrb->object_class);
|
||||
mrb_undef_class_method(mrb, binding, "new");
|
||||
|
||||
mrb_define_method(mrb, mrb->kernel_module, "binding", mrb_f_binding, MRB_ARGS_NONE());
|
||||
|
||||
mrb_define_method(mrb, binding, "local_variables", binding_local_variables, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_binding_gem_final(mrb_state *mrb)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
assert("Kernel.#binding") do
|
||||
assert_kind_of Binding, binding
|
||||
end
|
||||
|
||||
assert("Binding#local_variables") do
|
||||
block = Proc.new do |a|
|
||||
b = 1
|
||||
binding
|
||||
end
|
||||
assert_equal [:a, :b, :block], block.call(0).local_variables
|
||||
end
|
||||
|
||||
assert("Binding#eval") do
|
||||
b = nil
|
||||
1.times { x, y, z = 1, 2, 3; [x,y,z]; b = binding }
|
||||
assert_equal([1, 2, 3], b.eval("[x, y, z]"))
|
||||
end
|
||||
@@ -4,19 +4,19 @@
|
||||
#include <mruby/irep.h>
|
||||
#include <mruby/proc.h>
|
||||
#include <mruby/opcode.h>
|
||||
#include <mruby/variable.h>
|
||||
|
||||
mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p);
|
||||
mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
|
||||
|
||||
static struct mrb_irep *
|
||||
get_closure_irep(mrb_state *mrb, int level)
|
||||
get_closure_irep(mrb_state *mrb, int level, struct RProc *scope)
|
||||
{
|
||||
struct mrb_context *c = mrb->c;
|
||||
struct REnv *e = c->ci[-1].proc->env;
|
||||
struct REnv *e = scope->env;
|
||||
struct RProc *proc;
|
||||
|
||||
if (level == 0) {
|
||||
proc = c->ci[-1].proc;
|
||||
proc = scope;
|
||||
if (MRB_PROC_CFUNC_P(proc)) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -40,13 +40,13 @@ get_closure_irep(mrb_state *mrb, int level)
|
||||
}
|
||||
|
||||
static inline mrb_code
|
||||
search_variable(mrb_state *mrb, mrb_sym vsym, int bnest)
|
||||
search_variable(mrb_state *mrb, mrb_sym vsym, int bnest, struct RProc *scope)
|
||||
{
|
||||
mrb_irep *virep;
|
||||
int level;
|
||||
int pos;
|
||||
|
||||
for (level = 0; (virep = get_closure_irep(mrb, level)); level++) {
|
||||
for (level = 0; (virep = get_closure_irep(mrb, level, scope)); level++) {
|
||||
if (!virep || virep->lv == NULL) {
|
||||
continue;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ potential_upvar_p(struct mrb_locals *lv, uint16_t v, int argc, uint16_t nlocals)
|
||||
}
|
||||
|
||||
static void
|
||||
patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest)
|
||||
patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, struct RProc *scope)
|
||||
{
|
||||
size_t i;
|
||||
mrb_code c;
|
||||
@@ -88,14 +88,14 @@ patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest)
|
||||
break;
|
||||
|
||||
case OP_EPUSH:
|
||||
patch_irep(mrb, irep->reps[GETARG_Bx(c)], bnest + 1);
|
||||
patch_irep(mrb, irep->reps[GETARG_Bx(c)], bnest + 1, scope);
|
||||
break;
|
||||
|
||||
case OP_LAMBDA:
|
||||
{
|
||||
int arg_c = GETARG_c(c);
|
||||
if (arg_c & OP_L_CAPTURE) {
|
||||
patch_irep(mrb, irep->reps[GETARG_b(c)], bnest + 1);
|
||||
patch_irep(mrb, irep->reps[GETARG_b(c)], bnest + 1, scope);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -105,7 +105,7 @@ patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest)
|
||||
break;
|
||||
}
|
||||
{
|
||||
mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest);
|
||||
mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest, scope);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg;
|
||||
@@ -116,7 +116,7 @@ patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest)
|
||||
case OP_MOVE:
|
||||
/* src part */
|
||||
if (potential_upvar_p(irep->lv, GETARG_B(c), argc, irep->nlocals)) {
|
||||
mrb_code arg = search_variable(mrb, irep->lv[GETARG_B(c) - 1].name, bnest);
|
||||
mrb_code arg = search_variable(mrb, irep->lv[GETARG_B(c) - 1].name, bnest, scope);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg;
|
||||
@@ -124,7 +124,7 @@ patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest)
|
||||
}
|
||||
/* dst part */
|
||||
if (potential_upvar_p(irep->lv, GETARG_A(c), argc, irep->nlocals)) {
|
||||
mrb_code arg = search_variable(mrb, irep->lv[GETARG_A(c) - 1].name, bnest);
|
||||
mrb_code arg = search_variable(mrb, irep->lv[GETARG_A(c) - 1].name, bnest, scope);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
irep->iseq[i] = MKOPCODE(OP_SETUPVAR) | MKARG_A(GETARG_B(c)) | arg;
|
||||
@@ -146,12 +146,22 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, con
|
||||
{
|
||||
mrbc_context *cxt;
|
||||
struct mrb_parser_state *p;
|
||||
struct RProc *proc;
|
||||
struct RProc *proc, *scope;
|
||||
struct REnv *e;
|
||||
struct mrb_context *c = mrb->c;
|
||||
mrb_bool is_binding = FALSE;
|
||||
|
||||
if (!mrb_nil_p(binding)) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil.");
|
||||
if (!mrb_class_defined(mrb, "Binding")
|
||||
|| !mrb_obj_is_kind_of(mrb, binding, mrb_class_get(mrb, "Binding"))) {
|
||||
mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected binding)",
|
||||
mrb_obj_value(mrb_obj_class(mrb, binding)));
|
||||
}
|
||||
scope = mrb_proc_ptr(mrb_iv_get(mrb, binding, mrb_intern_lit(mrb, "proc")));
|
||||
is_binding = TRUE;
|
||||
}
|
||||
else {
|
||||
scope = c->ci[-1].proc;
|
||||
}
|
||||
|
||||
cxt = mrbc_context_new(mrb);
|
||||
@@ -188,20 +198,20 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, con
|
||||
mrbc_context_free(mrb, cxt);
|
||||
mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
|
||||
}
|
||||
if (c->ci[-1].proc->target_class) {
|
||||
proc->target_class = c->ci[-1].proc->target_class;
|
||||
if (scope->target_class) {
|
||||
proc->target_class = scope->target_class;
|
||||
}
|
||||
e = c->ci[-1].proc->env;
|
||||
e = scope->env;
|
||||
if (!e) e = c->ci[-1].env;
|
||||
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)e);
|
||||
e->mid = c->ci[-1].mid;
|
||||
e->cioff = c->ci - c->cibase - 1;
|
||||
e->stack = c->ci->stackent;
|
||||
MRB_SET_ENV_STACK_LEN(e, c->ci[-1].proc->body.irep->nlocals);
|
||||
MRB_SET_ENV_STACK_LEN(e, scope->body.irep->nlocals);
|
||||
c->ci->target_class = proc->target_class;
|
||||
c->ci->env = 0;
|
||||
proc->env = e;
|
||||
patch_irep(mrb, proc->body.irep, 0);
|
||||
patch_irep(mrb, proc->body.irep, 0, scope);
|
||||
|
||||
mrb_parser_free(p);
|
||||
mrbc_context_free(mrb, cxt);
|
||||
@@ -222,6 +232,9 @@ f_eval(mrb_state *mrb, mrb_value self)
|
||||
mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line);
|
||||
|
||||
proc = create_proc_from_string(mrb, s, len, binding, file, line);
|
||||
if (!mrb_nil_p(binding)) {
|
||||
self = mrb_iv_get(mrb, binding, mrb_intern_lit(mrb, "recv"));
|
||||
}
|
||||
mrb_assert(!MRB_PROC_CFUNC_P(proc));
|
||||
return mrb_exec_irep(mrb, self, proc);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ assert('Kernel#eval', '15.3.1.3.12') do
|
||||
end
|
||||
|
||||
assert('rest arguments of eval') do
|
||||
assert_raise(ArgumentError) { Kernel.eval('0', 0, 'test', 0) }
|
||||
assert_raise(TypeError) { Kernel.eval('0', 0, 'test', 0) }
|
||||
assert_equal ['test', 'test.rb', 10] do
|
||||
Kernel.eval('[\'test\', __FILE__, __LINE__]', nil, 'test.rb', 10)
|
||||
end
|
||||
|
||||
+9
-5
@@ -1129,16 +1129,13 @@ mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
|
||||
return mrb_false_value();
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
mrb_local_variables(mrb_state *mrb, mrb_value self)
|
||||
mrb_value
|
||||
proc_local_variables(mrb_state *mrb, struct RProc *proc)
|
||||
{
|
||||
struct RProc *proc;
|
||||
mrb_value vars;
|
||||
struct mrb_irep *irep;
|
||||
size_t i;
|
||||
|
||||
proc = mrb->c->ci[-1].proc;
|
||||
|
||||
if (MRB_PROC_CFUNC_P(proc)) {
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
@@ -1176,6 +1173,13 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
|
||||
}
|
||||
|
||||
mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);
|
||||
|
||||
static mrb_value
|
||||
mrb_local_variables(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
return proc_local_variables(mrb, mrb->c->ci[-1].proc);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_init_kernel(mrb_state *mrb)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user