first commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
MRuby::Gem::Specification.new('mruby-kernel-ext') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'extensional function-like methods'
|
||||
end
|
||||
@@ -0,0 +1,226 @@
|
||||
#include <mruby.h>
|
||||
#include <mruby/error.h>
|
||||
#include <mruby/array.h>
|
||||
#include <mruby/hash.h>
|
||||
#include <mruby/range.h>
|
||||
|
||||
static mrb_value
|
||||
mrb_f_caller(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value bt, v, length;
|
||||
mrb_int bt_len, argc, lev, n;
|
||||
|
||||
bt = mrb_get_backtrace(mrb);
|
||||
bt_len = RARRAY_LEN(bt);
|
||||
argc = mrb_get_args(mrb, "|oo", &v, &length);
|
||||
|
||||
switch (argc) {
|
||||
case 0:
|
||||
lev = 1;
|
||||
n = bt_len - lev;
|
||||
break;
|
||||
case 1:
|
||||
if (mrb_range_p(v)) {
|
||||
mrb_int beg, len;
|
||||
if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == MRB_RANGE_OK) {
|
||||
lev = beg;
|
||||
n = len;
|
||||
}
|
||||
else {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
}
|
||||
else {
|
||||
lev = mrb_int(mrb, v);
|
||||
if (lev < 0) {
|
||||
mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%v)", v);
|
||||
}
|
||||
n = bt_len - lev;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
lev = mrb_int(mrb, v);
|
||||
n = mrb_int(mrb, length);
|
||||
if (lev < 0) {
|
||||
mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%v)", v);
|
||||
}
|
||||
if (n < 0) {
|
||||
mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative size (%v)", length);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
lev = n = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
|
||||
return mrb_funcall(mrb, bt, "[]", 2, mrb_fixnum_value(lev), mrb_fixnum_value(n));
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* __method__ -> symbol
|
||||
*
|
||||
* Returns the name at the definition of the current method as a
|
||||
* Symbol.
|
||||
* If called outside of a method, it returns <code>nil</code>.
|
||||
*
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_method(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
ci--;
|
||||
if (ci->mid)
|
||||
return mrb_symbol_value(ci->mid);
|
||||
else
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* Integer(arg,base=0) -> integer
|
||||
*
|
||||
* Converts <i>arg</i> to a <code>Fixnum</code>.
|
||||
* Numeric types are converted directly (with floating point numbers
|
||||
* being truncated). <i>base</i> (0, or between 2 and 36) is a base for
|
||||
* integer string representation. If <i>arg</i> is a <code>String</code>,
|
||||
* when <i>base</i> is omitted or equals to zero, radix indicators
|
||||
* (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
|
||||
* In any case, strings should be strictly conformed to numeric
|
||||
* representation. This behavior is different from that of
|
||||
* <code>String#to_i</code>. Non string values will be treated as integers.
|
||||
* Passing <code>nil</code> raises a TypeError.
|
||||
*
|
||||
* Integer(123.999) #=> 123
|
||||
* Integer("0x1a") #=> 26
|
||||
* Integer(Time.new) #=> 1204973019
|
||||
* Integer("0930", 10) #=> 930
|
||||
* Integer("111", 2) #=> 7
|
||||
* Integer(nil) #=> TypeError
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_integer(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value arg;
|
||||
mrb_int base = 0;
|
||||
|
||||
mrb_get_args(mrb, "o|i", &arg, &base);
|
||||
return mrb_convert_to_integer(mrb, arg, base);
|
||||
}
|
||||
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
/*
|
||||
* call-seq:
|
||||
* Float(arg) -> float
|
||||
*
|
||||
* Returns <i>arg</i> converted to a float. Numeric types are converted
|
||||
* directly, the rest are converted using <i>arg</i>.to_f.
|
||||
*
|
||||
* Float(1) #=> 1.0
|
||||
* Float(123.456) #=> 123.456
|
||||
* Float("123.456") #=> 123.456
|
||||
* Float(nil) #=> TypeError
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_float(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value arg = mrb_get_arg1(mrb);
|
||||
|
||||
return mrb_Float(mrb, arg);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* String(arg) -> string
|
||||
*
|
||||
* Returns <i>arg</i> as an <code>String</code>.
|
||||
* converted using <code>to_s</code> method.
|
||||
*
|
||||
* String(self) #=> "main"
|
||||
* String(self.class) #=> "Object"
|
||||
* String(123456) #=> "123456"
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_string(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value arg = mrb_get_arg1(mrb);
|
||||
mrb_value tmp;
|
||||
|
||||
tmp = mrb_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* Array(arg) -> array
|
||||
*
|
||||
* Returns +arg+ as an Array using to_a method.
|
||||
*
|
||||
* Array(1..5) #=> [1, 2, 3, 4, 5]
|
||||
*
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_array(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value arg = mrb_get_arg1(mrb);
|
||||
mrb_value tmp;
|
||||
|
||||
tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a");
|
||||
if (mrb_nil_p(tmp)) {
|
||||
return mrb_ary_new_from_values(mrb, 1, &arg);
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* Hash(arg) -> hash
|
||||
*
|
||||
* Returns a <code>Hash</code> if <i>arg</i> is a <code>Hash</code>.
|
||||
* Returns an empty <code>Hash</code> when <i>arg</i> is <tt>nil</tt>
|
||||
* or <tt>[]</tt>.
|
||||
*
|
||||
* Hash([]) #=> {}
|
||||
* Hash(nil) #=> {}
|
||||
* Hash(key: :value) #=> {:key => :value}
|
||||
* Hash([1, 2, 3]) #=> TypeError
|
||||
*
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_hash(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value arg = mrb_get_arg1(mrb);
|
||||
|
||||
if (mrb_nil_p(arg) || (mrb_array_p(arg) && RARRAY_LEN(arg) == 0)) {
|
||||
return mrb_hash_new(mrb);
|
||||
}
|
||||
return mrb_ensure_hash_type(mrb, arg);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
|
||||
{
|
||||
struct RClass *krn = mrb->kernel_module;
|
||||
|
||||
mrb_define_module_function(mrb, krn, "fail", mrb_f_raise, MRB_ARGS_OPT(2));
|
||||
mrb_define_module_function(mrb, krn, "caller", mrb_f_caller, MRB_ARGS_OPT(2));
|
||||
mrb_define_method(mrb, krn, "__method__", mrb_f_method, MRB_ARGS_NONE());
|
||||
mrb_define_module_function(mrb, krn, "Integer", mrb_f_integer, MRB_ARGS_ARG(1,1));
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
mrb_define_module_function(mrb, krn, "Float", mrb_f_float, MRB_ARGS_REQ(1));
|
||||
#endif
|
||||
mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1));
|
||||
mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1));
|
||||
mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1));
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_kernel_ext_gem_final(mrb_state *mrb)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
assert('Kernel.fail, Kernel#fail') do
|
||||
assert_raise(RuntimeError) { fail }
|
||||
assert_raise(RuntimeError) { Kernel.fail }
|
||||
end
|
||||
|
||||
assert('Kernel.caller, Kernel#caller') do
|
||||
skip "backtrace isn't available" if caller(0).empty?
|
||||
|
||||
caller_lineno = __LINE__ + 3
|
||||
c = Class.new do
|
||||
def foo(*args)
|
||||
caller(*args)
|
||||
end
|
||||
|
||||
def bar(*args)
|
||||
foo(*args)
|
||||
end
|
||||
|
||||
def baz(*args)
|
||||
bar(*args)
|
||||
end
|
||||
end
|
||||
assert_equal "kernel.rb:#{caller_lineno}:in foo", c.new.baz(0)[0][-19..-1]
|
||||
assert_equal "bar", c.new.baz[0][-3..-1]
|
||||
assert_equal "foo", c.new.baz(0)[0][-3..-1]
|
||||
assert_equal "bar", c.new.baz(1)[0][-3..-1]
|
||||
assert_equal "baz", c.new.baz(2)[0][-3..-1]
|
||||
assert_equal ["foo", "bar"], c.new.baz(0, 2).map { |i| i[-3..-1] }
|
||||
assert_equal ["bar", "baz"], c.new.baz(1..2).map { |i| i[-3..-1] }
|
||||
assert_nil c.new.baz(10..20)
|
||||
assert_raise(ArgumentError) { c.new.baz(-1) }
|
||||
assert_raise(ArgumentError) { c.new.baz(-1, 1) }
|
||||
assert_raise(ArgumentError) { c.new.baz(1, -1) }
|
||||
assert_raise(TypeError) { c.new.baz(nil) }
|
||||
end
|
||||
|
||||
assert('Kernel#__method__') do
|
||||
assert_equal(:m, Class.new {def m; __method__; end}.new.m)
|
||||
assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m)
|
||||
c = Class.new do
|
||||
[:m1, :m2].each do |m|
|
||||
define_method(m) do
|
||||
__method__
|
||||
end
|
||||
end
|
||||
end
|
||||
assert_equal(:m1, c.new.m1)
|
||||
assert_equal(:m2, c.new.m2)
|
||||
end
|
||||
|
||||
assert('Kernel#Integer') do
|
||||
assert_operator(26, :eql?, Integer("0x1a"))
|
||||
assert_operator(930, :eql?, Integer("0930", 10))
|
||||
assert_operator(7, :eql?, Integer("111", 2))
|
||||
assert_operator(0, :eql?, Integer("0"))
|
||||
assert_operator(0, :eql?, Integer("00000"))
|
||||
assert_operator(123, :eql?, Integer('1_2_3'))
|
||||
assert_operator(123, :eql?, Integer("\t\r\n\f\v 123 \t\r\n\f\v"))
|
||||
assert_raise(TypeError) { Integer(nil) }
|
||||
assert_raise(ArgumentError) { Integer('a') }
|
||||
assert_raise(ArgumentError) { Integer('4a5') }
|
||||
assert_raise(ArgumentError) { Integer('1_2__3') }
|
||||
assert_raise(ArgumentError) { Integer('68_') }
|
||||
assert_raise(ArgumentError) { Integer('68_ ') }
|
||||
assert_raise(ArgumentError) { Integer('_68') }
|
||||
assert_raise(ArgumentError) { Integer(' _68') }
|
||||
assert_raise(ArgumentError) { Integer('6 8') }
|
||||
assert_raise(ArgumentError) { Integer("15\0") }
|
||||
assert_raise(ArgumentError) { Integer("15.0") }
|
||||
skip unless Object.const_defined?(:Float)
|
||||
assert_operator(123, :eql?, Integer(123.999))
|
||||
end
|
||||
|
||||
assert('Kernel#Float') do
|
||||
skip unless Object.const_defined?(:Float)
|
||||
assert_operator(1.0, :eql?, Float(1))
|
||||
assert_operator(123.456, :eql?, Float(123.456))
|
||||
assert_operator(123.456, :eql?, Float("123.456"))
|
||||
assert_operator(123.0, :eql?, Float('1_2_3'))
|
||||
assert_operator(12.34, :eql?, Float('1_2.3_4'))
|
||||
assert_operator(0.9, :eql?, Float('.9'))
|
||||
assert_operator(0.9, :eql?, Float(" \t\r\n\f\v.9 \t\r\n\f\v"))
|
||||
assert_operator(16.0, :eql?, Float("0x10"))
|
||||
assert_raise(TypeError) { Float(nil) }
|
||||
assert_raise(ArgumentError) { Float("1. 5") }
|
||||
assert_raise(ArgumentError) { Float("1.5a") }
|
||||
assert_raise(ArgumentError) { Float("1.5\0") }
|
||||
assert_raise(ArgumentError) { Float('a') }
|
||||
assert_raise(ArgumentError) { Float('4a5') }
|
||||
assert_raise(ArgumentError) { Float('1_2__3') }
|
||||
assert_raise(ArgumentError) { Float('68_') }
|
||||
assert_raise(ArgumentError) { Float('68._7') }
|
||||
assert_raise(ArgumentError) { Float('68.7_') }
|
||||
assert_raise(ArgumentError) { Float('68.7_ ') }
|
||||
assert_raise(ArgumentError) { Float('_68') }
|
||||
assert_raise(ArgumentError) { Float(' _68') }
|
||||
assert_raise(ArgumentError) { Float('1_2.3__4') }
|
||||
end
|
||||
|
||||
assert('Kernel#String') do
|
||||
assert_equal("main", String(self))
|
||||
assert_equal("Object", String(self.class))
|
||||
assert_equal("123456", String(123456))
|
||||
end
|
||||
|
||||
assert('Kernel#Array') do
|
||||
assert_equal([1], Kernel.Array(1))
|
||||
assert_equal([1, 2, 3, 4, 5], Kernel.Array([1, 2, 3, 4, 5]))
|
||||
assert_equal([1, 2, 3, 4, 5], Kernel.Array(1..5))
|
||||
assert_equal([[:a, 1], [:b, 2], [:c, 3]], Kernel.Array({a:1, b:2, c:3}))
|
||||
end
|
||||
|
||||
assert('Kernel#Hash') do
|
||||
assert_equal({}, Hash([]))
|
||||
assert_equal({}, Hash(nil))
|
||||
assert_equal({:key => :value}, Hash(key: :value))
|
||||
assert_raise(TypeError) { Hash([1, 2, 3]) }
|
||||
end
|
||||
Reference in New Issue
Block a user