Permalink
Browse files

Add API to define cfunc Proc with userdata.

The APIs are defined in mruby-proc-ext so include it before using this API.
See mruby-proc-ext's test code for usage.
This should resolve #1794.
  • Loading branch information...
take-cheeze committed Mar 31, 2014
1 parent 3eb3c99 commit bf6b1dfe216976f1adb7f41a74f20339fedcbec8
View
@@ -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)
{
@@ -0,0 +1,56 @@
+#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 @@
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

0 comments on commit bf6b1df

Please sign in to comment.