Commit 2c3530ff authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #1985 from take-cheeze/cfunc_with_env

Add API to define cfunc Proc with userdata.
parents d1e755df bf6b1dfe
......@@ -55,6 +55,10 @@ void mrb_proc_copy(struct RProc *a, struct RProc *b);
/* implementation of #send method */
mrb_value mrb_f_send(mrb_state *mrb, mrb_value self);
/* following functions are defined in mruby-proc-ext so please include it when using */
struct RProc *mrb_proc_new_cfunc_with_env(mrb_state*, mrb_func_t, mrb_int, const mrb_value*);
mrb_value mrb_cfunc_env_get(mrb_state*, mrb_int);
#include "mruby/khash.h"
KHASH_DECLARE(mt, mrb_sym, struct RProc*, 1)
......
......@@ -4,6 +4,49 @@
#include "mruby/string.h"
#include "mruby/debug.h"
struct RProc *
mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t f, mrb_int argc, const mrb_value *argv)
{
struct RProc *p;
struct REnv *e;
int ai, i;
p = mrb_proc_new_cfunc(mrb, f);
ai = mrb_gc_arena_save(mrb);
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, NULL);
p->env = e;
mrb_gc_arena_restore(mrb, ai);
e->cioff = -1;
e->flags = argc;
e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc);
for (i = 0; i < argc; ++i) {
e->stack[i] = argv[i];
}
return p;
}
mrb_value
mrb_cfunc_env_get(mrb_state *mrb, mrb_int idx)
{
struct RProc *p = mrb->c->ci->proc;
struct REnv *e = p->env;
if (!MRB_PROC_CFUNC_P(p)) {
mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from non-cfunc proc.");
}
if (!e) {
mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from cfunc Proc without REnv.");
}
if (idx < 0 || e->flags <= idx) {
mrb_raisef(mrb, E_INDEX_ERROR, "Env index out of range: %S (expected: 0 <= index < %S)",
mrb_fixnum_value(idx), mrb_fixnum_value(e->flags));
}
return e->stack[idx];
}
static mrb_value
mrb_proc_lambda(mrb_state *mrb, mrb_value self)
{
......
#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/class.h"
static mrb_value
return_func_name(mrb_state *mrb, mrb_value self)
{
return mrb_cfunc_env_get(mrb, 0);
}
static mrb_value
proc_new_cfunc_with_env(mrb_state *mrb, mrb_value self)
{
mrb_sym n;
mrb_value n_val;
mrb_get_args(mrb, "n", &n);
n_val = mrb_symbol_value(n);
mrb_define_method_raw(mrb, mrb_class_ptr(self), n,
mrb_proc_new_cfunc_with_env(mrb, return_func_name, 1, &n_val));
return self;
}
static mrb_value
return_env(mrb_state *mrb, mrb_value self)
{
mrb_int idx;
mrb_get_args(mrb, "i", &idx);
return mrb_cfunc_env_get(mrb, idx);
}
static mrb_value
cfunc_env_get(mrb_state *mrb, mrb_value self)
{
mrb_sym n;
mrb_value *argv; mrb_int argc;
mrb_get_args(mrb, "na", &n, &argv, &argc);
mrb_define_method_raw(mrb, mrb_class_ptr(self), n,
mrb_proc_new_cfunc_with_env(mrb, return_env, argc, argv));
return self;
}
static mrb_value
cfunc_without_env(mrb_state *mrb, mrb_value self)
{
return mrb_cfunc_env_get(mrb, 0);
}
void mrb_mruby_proc_ext_gem_test(mrb_state *mrb)
{
struct RClass *cls;
cls = mrb_define_class(mrb, "ProcExtTest", mrb->object_class);
mrb_define_module_function(mrb, cls, "mrb_proc_new_cfunc_with_env", proc_new_cfunc_with_env, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, cls, "mrb_cfunc_env_get", cfunc_env_get, MRB_ARGS_REQ(2));
mrb_define_module_function(mrb, cls, "cfunc_without_env", cfunc_without_env, MRB_ARGS_NONE());
}
......@@ -46,3 +46,26 @@ end
assert('Kernel#proc') do
assert_true !proc{|a|}.lambda?
end
assert('mrb_proc_new_cfunc_with_env') do
ProcExtTest.mrb_proc_new_cfunc_with_env(:test)
ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby)
t = ProcExtTest.new
assert_equal :test, t.test
assert_equal :mruby, t.mruby
end
assert('mrb_cfunc_env_get') do
ProcExtTest.mrb_cfunc_env_get :get_int, [0, 1, 2]
t = ProcExtTest.new
assert_raise(TypeError) { t.cfunc_without_env }
assert_raise(IndexError) { t.get_int(-1) }
assert_raise(IndexError) { t.get_int(3) }
assert_equal 1, t.get_int(1)
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment