first commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
MRuby::Gem::Specification.new('mruby-proc-ext') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'Proc class extension'
|
||||
end
|
||||
@@ -0,0 +1,50 @@
|
||||
class Proc
|
||||
|
||||
def ===(*args)
|
||||
call(*args)
|
||||
end
|
||||
|
||||
def yield(*args)
|
||||
call(*args)
|
||||
end
|
||||
|
||||
def to_proc
|
||||
self
|
||||
end
|
||||
|
||||
def curry(arity=self.arity)
|
||||
type = :proc
|
||||
abs = lambda {|a| a < 0 ? -a - 1 : a}
|
||||
arity = abs[arity]
|
||||
if lambda?
|
||||
type = :lambda
|
||||
self_arity = self.arity
|
||||
if (self_arity >= 0 && arity != self_arity) ||
|
||||
(self_arity < 0 && abs[self_arity] > arity)
|
||||
raise ArgumentError, "wrong number of arguments (#{arity} for #{abs[self_arity]})"
|
||||
end
|
||||
end
|
||||
|
||||
pproc = self
|
||||
make_curry = proc do |given_args=[]|
|
||||
__send__(type) do |*args|
|
||||
new_args = given_args + args
|
||||
if new_args.size >= arity
|
||||
pproc[*new_args]
|
||||
else
|
||||
make_curry[new_args]
|
||||
end
|
||||
end
|
||||
end
|
||||
make_curry.call
|
||||
end
|
||||
|
||||
def <<(other)
|
||||
->(*args, &block) { call(other.call(*args, &block)) }
|
||||
end
|
||||
|
||||
def >>(other)
|
||||
->(*args, &block) { other.call(call(*args, &block)) }
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,185 @@
|
||||
#include <mruby.h>
|
||||
#include <mruby/proc.h>
|
||||
#include <mruby/opcode.h>
|
||||
#include <mruby/array.h>
|
||||
#include <mruby/string.h>
|
||||
#include <mruby/debug.h>
|
||||
|
||||
static mrb_value
|
||||
mrb_proc_lambda_p(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RProc *p = mrb_proc_ptr(self);
|
||||
return mrb_bool_value(MRB_PROC_STRICT_P(p));
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
mrb_proc_source_location(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RProc *p = mrb_proc_ptr(self);
|
||||
|
||||
if (MRB_PROC_CFUNC_P(p)) {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
else {
|
||||
mrb_irep *irep = p->body.irep;
|
||||
int32_t line;
|
||||
const char *filename;
|
||||
|
||||
filename = mrb_debug_get_filename(mrb, irep, 0);
|
||||
line = mrb_debug_get_line(mrb, irep, 0);
|
||||
|
||||
return (!filename && line == -1)? mrb_nil_value()
|
||||
: mrb_assoc_new(mrb, mrb_str_new_cstr(mrb, filename), mrb_fixnum_value(line));
|
||||
}
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
mrb_proc_inspect(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RProc *p = mrb_proc_ptr(self);
|
||||
mrb_value str = mrb_str_new_lit(mrb, "#<Proc:");
|
||||
mrb_str_cat_str(mrb, str, mrb_ptr_to_str(mrb, mrb_cptr(self)));
|
||||
|
||||
if (!MRB_PROC_CFUNC_P(p)) {
|
||||
mrb_irep *irep = p->body.irep;
|
||||
const char *filename;
|
||||
int32_t line;
|
||||
mrb_str_cat_lit(mrb, str, "@");
|
||||
|
||||
filename = mrb_debug_get_filename(mrb, irep, 0);
|
||||
mrb_str_cat_cstr(mrb, str, filename ? filename : "-");
|
||||
mrb_str_cat_lit(mrb, str, ":");
|
||||
|
||||
line = mrb_debug_get_line(mrb, irep, 0);
|
||||
if (line != -1) {
|
||||
mrb_str_concat(mrb, str, mrb_fixnum_value(line));
|
||||
}
|
||||
else {
|
||||
mrb_str_cat_lit(mrb, str, "-");
|
||||
}
|
||||
}
|
||||
|
||||
if (MRB_PROC_STRICT_P(p)) {
|
||||
mrb_str_cat_lit(mrb, str, " (lambda)");
|
||||
}
|
||||
|
||||
mrb_str_cat_lit(mrb, str, ">");
|
||||
return str;
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
mrb_kernel_proc(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value blk;
|
||||
|
||||
mrb_get_args(mrb, "&!", &blk);
|
||||
|
||||
return blk;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* prc.parameters -> array
|
||||
*
|
||||
* Returns the parameter information of this proc.
|
||||
*
|
||||
* prc = lambda{|x, y=42, *other|}
|
||||
* prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
|
||||
*/
|
||||
|
||||
static mrb_value
|
||||
mrb_proc_parameters(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct parameters_type {
|
||||
size_t len;
|
||||
const char *name;
|
||||
int size;
|
||||
} *p, parameters_list [] = {
|
||||
{sizeof("req") - 1, "req", 0},
|
||||
{sizeof("opt") - 1, "opt", 0},
|
||||
{sizeof("rest") - 1, "rest", 0},
|
||||
{sizeof("req") - 1, "req", 0},
|
||||
{sizeof("block") - 1, "block", 0},
|
||||
{0, NULL, 0}
|
||||
};
|
||||
const struct RProc *proc = mrb_proc_ptr(self);
|
||||
const struct mrb_irep *irep = proc->body.irep;
|
||||
mrb_aspec aspec;
|
||||
mrb_value parameters;
|
||||
int i, j;
|
||||
int max = -1;
|
||||
|
||||
if (MRB_PROC_CFUNC_P(proc)) {
|
||||
// TODO cfunc aspec is not implemented yet
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
if (!irep) {
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
if (!irep->lv) {
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
if (*irep->iseq != OP_ENTER) {
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
|
||||
if (!MRB_PROC_STRICT_P(proc)) {
|
||||
parameters_list[0].len = sizeof("opt") - 1;
|
||||
parameters_list[0].name = "opt";
|
||||
parameters_list[3].len = sizeof("opt") - 1;
|
||||
parameters_list[3].name = "opt";
|
||||
}
|
||||
|
||||
aspec = PEEK_W(irep->iseq+1);
|
||||
parameters_list[0].size = MRB_ASPEC_REQ(aspec);
|
||||
parameters_list[1].size = MRB_ASPEC_OPT(aspec);
|
||||
parameters_list[2].size = MRB_ASPEC_REST(aspec);
|
||||
parameters_list[3].size = MRB_ASPEC_POST(aspec);
|
||||
parameters_list[4].size = MRB_ASPEC_BLOCK(aspec);
|
||||
|
||||
parameters = mrb_ary_new_capa(mrb, irep->nlocals-1);
|
||||
|
||||
max = irep->nlocals-1;
|
||||
for (i = 0, p = parameters_list; p->name; p++) {
|
||||
mrb_value sname = mrb_symbol_value(mrb_intern_static(mrb, p->name, p->len));
|
||||
|
||||
for (j = 0; j < p->size; i++, j++) {
|
||||
mrb_value a;
|
||||
|
||||
a = mrb_ary_new(mrb);
|
||||
mrb_ary_push(mrb, a, sname);
|
||||
if (i < max && irep->lv[i].name) {
|
||||
mrb_sym sym = irep->lv[i].name;
|
||||
const char *name = mrb_sym_name(mrb, sym);
|
||||
switch (name[0]) {
|
||||
case '*': case '&':
|
||||
break;
|
||||
default:
|
||||
mrb_ary_push(mrb, a, mrb_symbol_value(sym));
|
||||
break;
|
||||
}
|
||||
}
|
||||
mrb_ary_push(mrb, parameters, a);
|
||||
}
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_proc_ext_gem_init(mrb_state* mrb)
|
||||
{
|
||||
struct RClass *p = mrb->proc_class;
|
||||
mrb_define_method(mrb, p, "lambda?", mrb_proc_lambda_p, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, p, "source_location", mrb_proc_source_location, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, p, "to_s", mrb_proc_inspect, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, p, "inspect", mrb_proc_inspect, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, p, "parameters", mrb_proc_parameters, MRB_ARGS_NONE());
|
||||
|
||||
mrb_define_class_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE()|MRB_ARGS_BLOCK());
|
||||
mrb_define_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, MRB_ARGS_NONE()|MRB_ARGS_BLOCK());
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_proc_ext_gem_final(mrb_state* mrb)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#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_method_t m;
|
||||
struct RProc *p;
|
||||
mrb_get_args(mrb, "n", &n);
|
||||
n_val = mrb_symbol_value(n);
|
||||
p = mrb_proc_new_cfunc_with_env(mrb, return_func_name, 1, &n_val);
|
||||
MRB_METHOD_FROM_PROC(m, p);
|
||||
mrb_define_method_raw(mrb, mrb_class_ptr(self), n, m);
|
||||
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_method_t m;
|
||||
struct RProc *p;
|
||||
mrb_get_args(mrb, "na", &n, &argv, &argc);
|
||||
p = mrb_proc_new_cfunc_with_env(mrb, return_env, argc, argv);
|
||||
MRB_METHOD_FROM_PROC(m, p);
|
||||
mrb_define_method_raw(mrb, mrb_class_ptr(self), n, m);
|
||||
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());
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
##
|
||||
# Proc(Ext) Test
|
||||
|
||||
def enable_debug_info?
|
||||
return @enable_debug_info unless @enable_debug_info == nil
|
||||
begin
|
||||
raise
|
||||
rescue => e
|
||||
@enable_debug_info = !e.backtrace.empty?
|
||||
end
|
||||
end
|
||||
|
||||
assert('Proc#source_location') do
|
||||
skip unless enable_debug_info?
|
||||
file, line = Proc.new{}.source_location
|
||||
assert_equal __FILE__, file
|
||||
assert_equal __LINE__ - 2, line
|
||||
end
|
||||
|
||||
assert('Proc#inspect') do
|
||||
ins = Proc.new{}.inspect
|
||||
if enable_debug_info?
|
||||
metas = %w(\\ * ? [ ] { })
|
||||
file = __FILE__.split("").map{|c| metas.include?(c) ? "\\#{c}" : c}.join
|
||||
line = __LINE__ - 4
|
||||
else
|
||||
file = line = "-"
|
||||
end
|
||||
assert_match "#<Proc:0x*@#{file}:#{line}>", ins
|
||||
end
|
||||
|
||||
assert('Proc#parameters') do
|
||||
parameters = Proc.new{|x,y=42,*other|}.parameters
|
||||
assert_equal [[:opt, :x], [:opt, :y], [:rest, :other]], parameters
|
||||
end
|
||||
|
||||
assert('Proc#lambda?') do
|
||||
assert_true lambda{}.lambda?
|
||||
assert_true !Proc.new{}.lambda?
|
||||
end
|
||||
|
||||
assert('Proc#===') do
|
||||
proc = Proc.new {|a| a * 2}
|
||||
assert_equal 20, (proc === 10)
|
||||
end
|
||||
|
||||
assert('Proc#yield') do
|
||||
proc = Proc.new {|a| a * 2}
|
||||
assert_equal 20, proc.yield(10)
|
||||
end
|
||||
|
||||
assert('Proc#curry') do
|
||||
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
|
||||
assert_equal 6, b.curry[1][2][3]
|
||||
assert_equal 6, b.curry[1, 2][3, 4]
|
||||
assert_equal 6, b.curry(5)[1][2][3][4][5]
|
||||
assert_equal 6, b.curry(5)[1, 2][3, 4][5]
|
||||
assert_equal 1, b.curry(1)[1]
|
||||
|
||||
b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
|
||||
assert_equal 6, b.curry[1][2][3]
|
||||
assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
|
||||
assert_raise(ArgumentError) { b.curry(5) }
|
||||
assert_raise(ArgumentError) { b.curry(1) }
|
||||
|
||||
assert_false(proc{}.curry.lambda?)
|
||||
assert_true(lambda{}.curry.lambda?)
|
||||
end
|
||||
|
||||
assert('Proc#parameters') do
|
||||
assert_equal([], Proc.new {}.parameters)
|
||||
assert_equal([], Proc.new {||}.parameters)
|
||||
assert_equal([[:opt, :a]], Proc.new {|a|}.parameters)
|
||||
assert_equal([[:req, :a]], lambda {|a|}.parameters)
|
||||
assert_equal([[:opt, :a]], lambda {|a=nil|}.parameters)
|
||||
assert_equal([[:req, :a]], ->(a){}.parameters)
|
||||
assert_equal([[:rest]], lambda { |*| }.parameters)
|
||||
assert_equal([[:rest, :a]], Proc.new {|*a|}.parameters)
|
||||
assert_equal([[:opt, :a], [:opt, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:opt, :f], [:opt, :g], [:block, :h]], Proc.new {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters)
|
||||
assert_equal([[:req, :a], [:req, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:req, :f], [:req, :g], [:block, :h]], lambda {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters)
|
||||
end
|
||||
|
||||
assert('Proc#to_proc') do
|
||||
proc = Proc.new {}
|
||||
assert_equal proc, proc.to_proc
|
||||
end
|
||||
|
||||
assert('Kernel#proc') do
|
||||
assert_true !proc{|a|}.lambda?
|
||||
|
||||
assert_raise LocalJumpError do
|
||||
proc{ break }.call
|
||||
end
|
||||
end
|
||||
|
||||
assert "Proc#<< and Proc#>>" do
|
||||
add3 = ->(n) { n + 3 }
|
||||
mul2 = ->(n) { n * 2 }
|
||||
|
||||
f1 = mul2 << add3
|
||||
assert_kind_of Proc, f1
|
||||
assert_equal 16, f1.call(5)
|
||||
|
||||
f2 = mul2 >> add3
|
||||
assert_kind_of Proc, f2
|
||||
assert_equal 13, f2.call(5)
|
||||
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
|
||||
Reference in New Issue
Block a user