Merge pull request #1187 from h2so5/mruby-proc-ext

Add mruby-proc-ext
This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-04-17 23:05:47 -07:00
5 changed files with 185 additions and 0 deletions
+3
View File
@@ -44,6 +44,9 @@ MRuby::Build.new do |conf|
# Use extensional Range class
conf.gem "#{root}/mrbgems/mruby-range-ext"
# Use extensional Proc class
conf.gem "#{root}/mrbgems/mruby-proc-ext"
# Use Random class
conf.gem "#{root}/mrbgems/mruby-random"
+4
View File
@@ -0,0 +1,4 @@
MRuby::Gem::Specification.new('mruby-proc-ext') do |spec|
spec.license = 'MIT'
spec.authors = 'mruby developers'
end
+40
View File
@@ -0,0 +1,40 @@
class Proc
def ===(*args)
call(*args)
end
def yield(*args)
call(*args)
end
def to_proc
self
end
def curry(arity=self.arity)
abs = lambda {|a| a < 0 ? -a - 1 : a}
arity = abs[arity]
if 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=[]|
proc 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
end
+97
View File
@@ -0,0 +1,97 @@
#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/array.h"
#include "mruby/string.h"
static mrb_value
mrb_proc_lambda(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;
mrb_value filename = mrb_nil_value();
mrb_value lines = mrb_nil_value();
if (irep->filename) filename = mrb_str_new_cstr(mrb, irep->filename);
if (irep->lines) lines = mrb_fixnum_value(*irep->lines);
return mrb_assoc_new(mrb, filename, lines);
}
}
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_cstr(mrb, "#<Proc:");
mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, mrb_voidp(self)));
if (!MRB_PROC_CFUNC_P(p)) {
mrb_irep *irep = p->body.irep;
mrb_str_cat_cstr(mrb, str, "@");
if (irep->filename) {
mrb_str_cat_cstr(mrb, str, irep->filename);
}
else {
mrb_str_cat_cstr(mrb, str, "-");
}
mrb_str_cat_cstr(mrb, str, ":");
if (irep->lines) {
mrb_str_append(mrb, str, mrb_fixnum_value(*irep->lines));
}
else {
mrb_str_cat_cstr(mrb, str, "-");
}
}
if (MRB_PROC_STRICT_P(p)) {
mrb_str_cat_cstr(mrb, str, " (lambda)");
}
mrb_str_cat_cstr(mrb, str, ">");
return str;
}
static mrb_value
mrb_kernel_proc(mrb_state *mrb, mrb_value self)
{
mrb_value blk;
mrb_get_args(mrb, "&", &blk);
if (mrb_nil_p(blk)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block");
}
return blk;
}
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, ARGS_NONE());
mrb_define_method(mrb, p, "source_location", mrb_proc_source_location, ARGS_NONE());
mrb_define_method(mrb, p, "to_s", mrb_proc_inspect, ARGS_NONE());
mrb_define_method(mrb, p, "inspect", mrb_proc_inspect, ARGS_NONE());
mrb_define_class_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, ARGS_NONE());
mrb_define_method(mrb, mrb->kernel_module, "proc", mrb_kernel_proc, ARGS_NONE());
}
void
mrb_mruby_proc_ext_gem_final(mrb_state* mrb)
{
}
+41
View File
@@ -0,0 +1,41 @@
##
# Proc(Ext) Test
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 (proc === 10), 20
end
assert('Proc#yield') do
proc = Proc.new {|a| a * 2}
assert_equal proc.yield(10), 20
end
assert('Proc#curry') do
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
assert_equal b.curry[1][2][3], 6
assert_equal b.curry[1, 2][3, 4], 6
assert_equal b.curry(5)[1][2][3][4][5], 6
assert_equal b.curry(5)[1, 2][3, 4][5], 6
assert_equal b.curry(1)[1], 1
b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
assert_equal b.curry[1][2][3], 6
assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
assert_raise(ArgumentError) { b.curry(5) }
assert_raise(ArgumentError) { b.curry(1) }
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?
end