From 1ea13483cb3a5d6e0007b49b0ab9cfdb3e9055eb Mon Sep 17 00:00:00 2001 From: Kouki Ooyatsu Date: Thu, 28 Feb 2013 10:56:58 +0900 Subject: Regexp: %r notation bug fix. close #85. be able to parse any delimiters. variable expansion isn't supported yet. * sample code ``` def matchpath(str) puts "#{str} is ..." if %r!img/! =~ str then puts "match" else puts "not match" end end matchpath("img/a.png") # => match matchpath("/usr/local/bin") # => not match matchpath("book/img/local") # => match ``` --- src/parse.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/parse.y b/src/parse.y index 365bc5f18..2c5690ea3 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4440,7 +4440,7 @@ parser_yylex(parser_state *p) p->lex_strterm = new_strterm(p, str_regexp, term, paren); #endif p->regexp = 1; - p->sterm = '/'; + p->sterm = term; return tREGEXP_BEG; case 's': -- cgit v1.2.3 From 473abbd0ff7c32f449f4d20ec04adebf951c4edf Mon Sep 17 00:00:00 2001 From: mattn Date: Thu, 28 Feb 2013 17:39:38 +0900 Subject: Cleanup Regexp codes --- src/init.c | 2 -- src/re.c | 42 ------------------------------------------ 2 files changed, 44 deletions(-) delete mode 100644 src/re.c (limited to 'src') diff --git a/src/init.c b/src/init.c index a5727561d..c5235396b 100644 --- a/src/init.c +++ b/src/init.c @@ -22,7 +22,6 @@ void mrb_init_numeric(mrb_state*); void mrb_init_range(mrb_state*); void mrb_init_struct(mrb_state*); void mrb_init_gc(mrb_state*); -void mrb_init_regexp(mrb_state*); void mrb_init_print(mrb_state*); void mrb_init_math(mrb_state*); void mrb_init_mrblib(mrb_state*); @@ -53,7 +52,6 @@ mrb_init_core(mrb_state *mrb) mrb_init_struct(mrb); DONE; #endif mrb_init_gc(mrb); DONE; - mrb_init_regexp(mrb); DONE; #ifdef ENABLE_STDIO mrb_init_print(mrb); DONE; #endif diff --git a/src/re.c b/src/re.c deleted file mode 100644 index 744732e58..000000000 --- a/src/re.c +++ /dev/null @@ -1,42 +0,0 @@ -/* -** re.c - Regexp class -** -** See Copyright Notice in mruby.h -*/ - -#include "mruby.h" -#include -#include "mruby/string.h" -#include "re.h" -#include "mruby/array.h" -#include "mruby/class.h" -#include "error.h" - -/* - * Document-class: RegexpError - * - * Raised when given an invalid regexp expression. - * - * Regexp.new("?") - * - * raises the exception: - * - * RegexpError: target of repeat operator is not specified: /?/ - */ - -/* - * Document-class: Regexp - * - * A Regexp holds a regular expression, used to match a pattern - * against strings. Regexps are created using the /.../ and - * %r{...} literals, and by the Regexp::new - * constructor. - * - * :include: doc/re.rdoc - */ - -void -mrb_init_regexp(mrb_state *mrb) -{ - //mrb_define_class(mrb, REGEXP_CLASS, mrb->object_class); -} -- cgit v1.2.3 From 6bee2fd193b94f00c7f4025fc3d62282f867e60d Mon Sep 17 00:00:00 2001 From: mattn Date: Thu, 28 Feb 2013 17:52:18 +0900 Subject: Pluggable Math --- build_config.rb | 3 + include/mrbconf.h | 4 - mgems/mruby-math/mrbgem.rake | 4 + mgems/mruby-math/src/math.c | 691 ++++++++++++++++++++++++++++++++++++++++++ mgems/mruby-math/test/math.rb | 125 ++++++++ src/init.c | 3 - src/math.c | 688 ----------------------------------------- test/t/math.rb | 125 -------- 8 files changed, 823 insertions(+), 820 deletions(-) create mode 100644 mgems/mruby-math/mrbgem.rake create mode 100644 mgems/mruby-math/src/math.c create mode 100644 mgems/mruby-math/test/math.rb delete mode 100644 src/math.c delete mode 100644 test/t/math.rb (limited to 'src') diff --git a/build_config.rb b/build_config.rb index a66dab6ac..99761a795 100644 --- a/build_config.rb +++ b/build_config.rb @@ -11,6 +11,9 @@ MRuby::Build.new do |conf| # conf.gem :github => 'masuidrive/mrbgems-example', :branch => 'master' # conf.gem :git => 'git@github.com:masuidrive/mrbgems-example.git', :branch => 'master', :options => '-v' + # Use standard Math module + conf.gem 'mgems/mruby-math' + # Use standard Time class conf.gem 'mgems/mruby-time' diff --git a/include/mrbconf.h b/include/mrbconf.h index 092e02d11..45e3d6034 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -45,7 +45,6 @@ /* -DDISABLE_XXXX to drop following features */ //#define DISABLE_SPRINTF /* Kernel.sprintf method */ -//#define DISABLE_MATH /* Math functions */ //#define DISABLE_STRUCT /* Struct class */ //#define DISABLE_STDIO /* use of stdio */ @@ -85,9 +84,6 @@ typedef short mrb_sym; #ifndef DISABLE_SPRINTF #define ENABLE_SPRINTF #endif -#ifndef DISABLE_MATH -#define ENABLE_MATH -#endif #ifndef DISABLE_STRUCT #define ENABLE_STRUCT #endif diff --git a/mgems/mruby-math/mrbgem.rake b/mgems/mruby-math/mrbgem.rake new file mode 100644 index 000000000..4b0fa40fd --- /dev/null +++ b/mgems/mruby-math/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-math') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mgems/mruby-math/src/math.c b/mgems/mruby-math/src/math.c new file mode 100644 index 000000000..9955d9862 --- /dev/null +++ b/mgems/mruby-math/src/math.c @@ -0,0 +1,691 @@ +/* +** math.c - Math module +** +** See Copyright Notice in mruby.h +*/ + +#include "mruby.h" +#include "mruby/array.h" + +#include + +#define domain_error(msg) \ + mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg) + +/* math functions not provided under Microsoft Visual C++ */ +#ifdef _MSC_VER + +#define MATH_TOLERANCE 1E-12 + +#define asinh(x) log(x + sqrt(pow(x,2.0) + 1)) +#define acosh(x) log(x + sqrt(pow(x,2.0) - 1)) +#define atanh(x) (log(1+x) - log(1-x))/2.0 +#define cbrt(x) pow(x,1.0/3.0) + +/* Declaration of complementary Error function */ +double +erfc(double x); + +/* +** Implementations of error functions +** credits to http://www.digitalmars.com/archives/cplusplus/3634.html +*/ + +/* Implementation of Error function */ +double +erf(double x) +{ + static const double two_sqrtpi = 1.128379167095512574; + double sum = x; + double term = x; + double xsqr = x*x; + int j= 1; + if (fabs(x) > 2.2) { + return 1.0 - erfc(x); + } + do { + term *= xsqr/j; + sum -= term/(2*j+1); + ++j; + term *= xsqr/j; + sum += term/(2*j+1); + ++j; + } while (fabs(term/sum) > MATH_TOLERANCE); + return two_sqrtpi*sum; +} + +/* Implementation of complementary Error function */ +double +erfc(double x) +{ + static const double one_sqrtpi= 0.564189583547756287; + double a = 1; + double b = x; + double c = x; + double d = x*x+0.5; + double q1; + double q2 = b/d; + double n = 1.0; + double t; + if (fabs(x) < 2.2) { + return 1.0 - erf(x); + } + if (x < 0.0) { /*signbit(x)*/ + return 2.0 - erfc(-x); + } + do { + t = a*n+b*x; + a = b; + b = t; + t = c*n+d*x; + c = d; + d = t; + n += 0.5; + q1 = q2; + q2 = b/d; + } while (fabs(q1-q2)/q2 > MATH_TOLERANCE); + return one_sqrtpi*exp(-x*x)*q2; +} + +#endif + +/* + TRIGONOMETRIC FUNCTIONS +*/ + +/* + * call-seq: + * Math.sin(x) -> float + * + * Computes the sine of x (expressed in radians). Returns + * -1..1. + */ +static mrb_value +math_sin(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = sin(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.cos(x) -> float + * + * Computes the cosine of x (expressed in radians). Returns + * -1..1. + */ +static mrb_value +math_cos(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = cos(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.tan(x) -> float + * + * Returns the tangent of x (expressed in radians). + */ +static mrb_value +math_tan(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = tan(x); + + return mrb_float_value(x); +} + +/* + INVERSE TRIGONOMETRIC FUNCTIONS +*/ + +/* + * call-seq: + * Math.asin(x) -> float + * + * Computes the arc sine of x. Returns -{PI/2} .. {PI/2}. + */ +static mrb_value +math_asin(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = asin(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.acos(x) -> float + * + * Computes the arc cosine of x. Returns 0..PI. + */ +static mrb_value +math_acos(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = acos(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.atan(x) -> float + * + * Computes the arc tangent of x. Returns -{PI/2} .. {PI/2}. + */ +static mrb_value +math_atan(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = atan(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.atan2(y, x) -> float + * + * Computes the arc tangent given y and x. Returns + * -PI..PI. + * + * Math.atan2(-0.0, -1.0) #=> -3.141592653589793 + * Math.atan2(-1.0, -1.0) #=> -2.356194490192345 + * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966 + * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483 + * Math.atan2(-0.0, 1.0) #=> -0.0 + * Math.atan2(0.0, 1.0) #=> 0.0 + * Math.atan2(1.0, 1.0) #=> 0.7853981633974483 + * Math.atan2(1.0, 0.0) #=> 1.5707963267948966 + * Math.atan2(1.0, -1.0) #=> 2.356194490192345 + * Math.atan2(0.0, -1.0) #=> 3.141592653589793 + * + */ +static mrb_value +math_atan2(mrb_state *mrb, mrb_value obj) +{ + mrb_float x, y; + + mrb_get_args(mrb, "ff", &x, &y); + x = atan2(x, y); + + return mrb_float_value(x); +} + + + +/* + HYPERBOLIC TRIG FUNCTIONS +*/ +/* + * call-seq: + * Math.sinh(x) -> float + * + * Computes the hyperbolic sine of x (expressed in + * radians). + */ +static mrb_value +math_sinh(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = sinh(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.cosh(x) -> float + * + * Computes the hyperbolic cosine of x (expressed in radians). + */ +static mrb_value +math_cosh(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = cosh(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.tanh() -> float + * + * Computes the hyperbolic tangent of x (expressed in + * radians). + */ +static mrb_value +math_tanh(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = tanh(x); + + return mrb_float_value(x); +} + + +/* + INVERSE HYPERBOLIC TRIG FUNCTIONS +*/ + +/* + * call-seq: + * Math.asinh(x) -> float + * + * Computes the inverse hyperbolic sine of x. + */ +static mrb_value +math_asinh(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + + x = asinh(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.acosh(x) -> float + * + * Computes the inverse hyperbolic cosine of x. + */ +static mrb_value +math_acosh(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = acosh(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.atanh(x) -> float + * + * Computes the inverse hyperbolic tangent of x. + */ +static mrb_value +math_atanh(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = atanh(x); + + return mrb_float_value(x); +} + +/* + EXPONENTIALS AND LOGARITHMS +*/ +#if defined __CYGWIN__ +# include +# if CYGWIN_VERSION_DLL_MAJOR < 1005 +# define nan(x) nan() +# endif +# define log(x) ((x) < 0.0 ? nan("") : log(x)) +# define log10(x) ((x) < 0.0 ? nan("") : log10(x)) +#endif + +#ifndef log2 +#ifndef HAVE_LOG2 +double +log2(double x) +{ + return log10(x)/log10(2.0); +} +#else +extern double log2(double); +#endif +#endif + +/* + * call-seq: + * Math.exp(x) -> float + * + * Returns e**x. + * + * Math.exp(0) #=> 1.0 + * Math.exp(1) #=> 2.718281828459045 + * Math.exp(1.5) #=> 4.4816890703380645 + * + */ +static mrb_value +math_exp(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = exp(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.log(numeric) -> float + * Math.log(num,base) -> float + * + * Returns the natural logarithm of numeric. + * If additional second argument is given, it will be the base + * of logarithm. + * + * Math.log(1) #=> 0.0 + * Math.log(Math::E) #=> 1.0 + * Math.log(Math::E**3) #=> 3.0 + * Math.log(12,3) #=> 2.2618595071429146 + * + */ +static mrb_value +math_log(mrb_state *mrb, mrb_value obj) +{ + mrb_float x, base; + int argc; + + argc = mrb_get_args(mrb, "f|f", &x, &base); + x = log(x); + if (argc == 2) { + x /= log(base); + } + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.log2(numeric) -> float + * + * Returns the base 2 logarithm of numeric. + * + * Math.log2(1) #=> 0.0 + * Math.log2(2) #=> 1.0 + * Math.log2(32768) #=> 15.0 + * Math.log2(65536) #=> 16.0 + * + */ +static mrb_value +math_log2(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = log2(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.log10(numeric) -> float + * + * Returns the base 10 logarithm of numeric. + * + * Math.log10(1) #=> 0.0 + * Math.log10(10) #=> 1.0 + * Math.log10(10**100) #=> 100.0 + * + */ +static mrb_value +math_log10(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = log10(x); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.sqrt(numeric) -> float + * + * Returns the square root of numeric. + * + */ +static mrb_value +math_sqrt(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = sqrt(x); + + return mrb_float_value(x); +} + + +/* + * call-seq: + * Math.cbrt(numeric) -> float + * + * Returns the cube root of numeric. + * + * -9.upto(9) {|x| + * p [x, Math.cbrt(x), Math.cbrt(x)**3] + * } + * #=> + * [-9, -2.0800838230519, -9.0] + * [-8, -2.0, -8.0] + * [-7, -1.91293118277239, -7.0] + * [-6, -1.81712059283214, -6.0] + * [-5, -1.7099759466767, -5.0] + * [-4, -1.5874010519682, -4.0] + * [-3, -1.44224957030741, -3.0] + * [-2, -1.25992104989487, -2.0] + * [-1, -1.0, -1.0] + * [0, 0.0, 0.0] + * [1, 1.0, 1.0] + * [2, 1.25992104989487, 2.0] + * [3, 1.44224957030741, 3.0] + * [4, 1.5874010519682, 4.0] + * [5, 1.7099759466767, 5.0] + * [6, 1.81712059283214, 6.0] + * [7, 1.91293118277239, 7.0] + * [8, 2.0, 8.0] + * [9, 2.0800838230519, 9.0] + * + */ +static mrb_value +math_cbrt(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = cbrt(x); + + return mrb_float_value(x); +} + + +/* + * call-seq: + * Math.frexp(numeric) -> [ fraction, exponent ] + * + * Returns a two-element array containing the normalized fraction (a + * Float) and exponent (a Fixnum) of + * numeric. + * + * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11] + * fraction * 2**exponent #=> 1234.0 + */ +static mrb_value +math_frexp(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + int exp; + + mrb_get_args(mrb, "f", &x); + x = frexp(x, &exp); + + return mrb_assoc_new(mrb, mrb_float_value(x), mrb_fixnum_value(exp)); +} + +/* + * call-seq: + * Math.ldexp(flt, int) -> float + * + * Returns the value of flt*(2**int). + * + * fraction, exponent = Math.frexp(1234) + * Math.ldexp(fraction, exponent) #=> 1234.0 + */ +static mrb_value +math_ldexp(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + mrb_int i; + + mrb_get_args(mrb, "fi", &x, &i); + x = ldexp(x, i); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.hypot(x, y) -> float + * + * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle + * with sides x and y. + * + * Math.hypot(3, 4) #=> 5.0 + */ +static mrb_value +math_hypot(mrb_state *mrb, mrb_value obj) +{ + mrb_float x, y; + + mrb_get_args(mrb, "ff", &x, &y); + x = hypot(x, y); + + return mrb_float_value(x); +} + +/* + * call-seq: + * Math.erf(x) -> float + * + * Calculates the error function of x. + */ +static mrb_value +math_erf(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = erf(x); + + return mrb_float_value(x); +} + + +/* + * call-seq: + * Math.erfc(x) -> float + * + * Calculates the complementary error function of x. + */ +static mrb_value +math_erfc(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = erfc(x); + + return mrb_float_value(x); +} + +/* ------------------------------------------------------------------------*/ +void +mrb_mruby_math_gem_init(mrb_state* mrb) +{ + struct RClass *mrb_math; + mrb_math = mrb_define_module(mrb, "Math"); + +#ifdef M_PI + mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(M_PI)); +#else + mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(atan(1.0)*4.0)); +#endif + +#ifdef M_E + mrb_define_const(mrb, mrb_math, "E", mrb_float_value(M_E)); +#else + mrb_define_const(mrb, mrb_math, "E", mrb_float_value(exp(1.0))); +#endif + +#ifdef MRB_USE_FLOAT + mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-5)); +#else + mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-12)); +#endif + + mrb_define_module_function(mrb, mrb_math, "sin", math_sin, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "cos", math_cos, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "tan", math_tan, ARGS_REQ(1)); + + mrb_define_module_function(mrb, mrb_math, "asin", math_asin, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "acos", math_acos, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "atan", math_atan, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, ARGS_REQ(2)); + + mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, ARGS_REQ(1)); + + mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, ARGS_REQ(1)); + + mrb_define_module_function(mrb, mrb_math, "exp", math_exp, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "log", math_log, ARGS_REQ(1)|ARGS_OPT(1)); + mrb_define_module_function(mrb, mrb_math, "log2", math_log2, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "log10", math_log10, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, ARGS_REQ(1)); + + mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, ARGS_REQ(2)); + + mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, ARGS_REQ(2)); + + mrb_define_module_function(mrb, mrb_math, "erf", math_erf, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, ARGS_REQ(1)); +} + +void +mrb_mruby_math_gem_final(mrb_state* mrb) +{ +} diff --git a/mgems/mruby-math/test/math.rb b/mgems/mruby-math/test/math.rb new file mode 100644 index 000000000..780b805d2 --- /dev/null +++ b/mgems/mruby-math/test/math.rb @@ -0,0 +1,125 @@ +## +# Math Test + +if Object.const_defined?(:Math) + assert('Math.sin 0') do + check_float(Math.sin(0), 0) + end + + assert('Math.sin PI/2') do + check_float(Math.sin(Math::PI / 2), 1) + end + + assert('Fundamental trig identities') do + result = true + N = 13 + N.times do |i| + a = Math::PI / N * i + ca = Math::PI / 2 - a + s = Math.sin(a) + c = Math.cos(a) + t = Math.tan(a) + result &= check_float(s, Math.cos(ca)) + result &= check_float(t, 1 / Math.tan(ca)) + result &= check_float(s ** 2 + c ** 2, 1) + result &= check_float(t ** 2 + 1, (1/c) ** 2) + result &= check_float((1/t) ** 2 + 1, (1/s) ** 2) + end + result + end + + assert('Math.erf 0') do + check_float(Math.erf(0), 0) + end + + assert('Math.exp 0') do + check_float(Math.exp(0), 1.0) + end + + assert('Math.exp 1') do + check_float(Math.exp(1), 2.718281828459045) + end + + assert('Math.exp 1.5') do + check_float(Math.exp(1.5), 4.4816890703380645) + end + + assert('Math.log 1') do + check_float(Math.log(1), 0) + end + + assert('Math.log E') do + check_float(Math.log(Math::E), 1.0) + end + + assert('Math.log E**3') do + check_float(Math.log(Math::E**3), 3.0) + end + + assert('Math.log2 1') do + check_float(Math.log2(1), 0.0) + end + + assert('Math.log2 2') do + check_float(Math.log2(2), 1.0) + end + + assert('Math.log10 1') do + check_float(Math.log10(1), 0.0) + end + + assert('Math.log10 10') do + check_float(Math.log10(10), 1.0) + end + + assert('Math.log10 10**100') do + check_float(Math.log10(10**100), 100.0) + end + + assert('Math.sqrt') do + num = [0.0, 1.0, 2.0, 3.0, 4.0] + sqr = [0, 1, 4, 9, 16] + result = true + sqr.each_with_index do |v,i| + result &= check_float(Math.sqrt(v), num[i]) + end + result + end + + assert('Math.cbrt') do + num = [-2.0, -1.0, 0.0, 1.0, 2.0] + cub = [-8, -1, 0, 1, 8] + result = true + cub.each_with_index do |v,i| + result &= check_float(Math.cbrt(v), num[i]) + end + result + end + + assert('Math.hypot') do + check_float(Math.hypot(3, 4), 5.0) + end + + assert('Math.frexp 1234') do + n = 1234 + fraction, exponent = Math.frexp(n) + check_float(Math.ldexp(fraction, exponent), n) + end + + assert('Math.erf 1') do + check_float(Math.erf(1), 0.842700792949715) + end + + assert('Math.erfc 1') do + check_float(Math.erfc(1), 0.157299207050285) + end + + assert('Math.erf -1') do + check_float(Math.erf(-1), -0.8427007929497148) + end + + assert('Math.erfc -1') do + check_float(Math.erfc(-1), 1.8427007929497148) + end +end + diff --git a/src/init.c b/src/init.c index a5727561d..a630a3307 100644 --- a/src/init.c +++ b/src/init.c @@ -56,9 +56,6 @@ mrb_init_core(mrb_state *mrb) mrb_init_regexp(mrb); DONE; #ifdef ENABLE_STDIO mrb_init_print(mrb); DONE; -#endif -#ifdef ENABLE_MATH - mrb_init_math(mrb); DONE; #endif mrb_init_mrblib(mrb); DONE; #ifndef DISABLE_GEMS diff --git a/src/math.c b/src/math.c deleted file mode 100644 index 782b75c97..000000000 --- a/src/math.c +++ /dev/null @@ -1,688 +0,0 @@ -/* -** math.c - Math module -** -** See Copyright Notice in mruby.h -*/ - -#include "mruby.h" -#include "mruby/array.h" - -#ifdef ENABLE_MATH -#include - -#define domain_error(msg) \ - mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg) - -/* math functions not provided under Microsoft Visual C++ */ -#ifdef _MSC_VER - -#define MATH_TOLERANCE 1E-12 - -#define asinh(x) log(x + sqrt(pow(x,2.0) + 1)) -#define acosh(x) log(x + sqrt(pow(x,2.0) - 1)) -#define atanh(x) (log(1+x) - log(1-x))/2.0 -#define cbrt(x) pow(x,1.0/3.0) - -/* Declaration of complementary Error function */ -double -erfc(double x); - -/* -** Implementations of error functions -** credits to http://www.digitalmars.com/archives/cplusplus/3634.html -*/ - -/* Implementation of Error function */ -double -erf(double x) -{ - static const double two_sqrtpi = 1.128379167095512574; - double sum = x; - double term = x; - double xsqr = x*x; - int j= 1; - if (fabs(x) > 2.2) { - return 1.0 - erfc(x); - } - do { - term *= xsqr/j; - sum -= term/(2*j+1); - ++j; - term *= xsqr/j; - sum += term/(2*j+1); - ++j; - } while (fabs(term/sum) > MATH_TOLERANCE); - return two_sqrtpi*sum; -} - -/* Implementation of complementary Error function */ -double -erfc(double x) -{ - static const double one_sqrtpi= 0.564189583547756287; - double a = 1; - double b = x; - double c = x; - double d = x*x+0.5; - double q1; - double q2 = b/d; - double n = 1.0; - double t; - if (fabs(x) < 2.2) { - return 1.0 - erf(x); - } - if (x < 0.0) { /*signbit(x)*/ - return 2.0 - erfc(-x); - } - do { - t = a*n+b*x; - a = b; - b = t; - t = c*n+d*x; - c = d; - d = t; - n += 0.5; - q1 = q2; - q2 = b/d; - } while (fabs(q1-q2)/q2 > MATH_TOLERANCE); - return one_sqrtpi*exp(-x*x)*q2; -} - -#endif - -/* - TRIGONOMETRIC FUNCTIONS -*/ - -/* - * call-seq: - * Math.sin(x) -> float - * - * Computes the sine of x (expressed in radians). Returns - * -1..1. - */ -static mrb_value -math_sin(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = sin(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.cos(x) -> float - * - * Computes the cosine of x (expressed in radians). Returns - * -1..1. - */ -static mrb_value -math_cos(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = cos(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.tan(x) -> float - * - * Returns the tangent of x (expressed in radians). - */ -static mrb_value -math_tan(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = tan(x); - - return mrb_float_value(x); -} - -/* - INVERSE TRIGONOMETRIC FUNCTIONS -*/ - -/* - * call-seq: - * Math.asin(x) -> float - * - * Computes the arc sine of x. Returns -{PI/2} .. {PI/2}. - */ -static mrb_value -math_asin(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = asin(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.acos(x) -> float - * - * Computes the arc cosine of x. Returns 0..PI. - */ -static mrb_value -math_acos(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = acos(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.atan(x) -> float - * - * Computes the arc tangent of x. Returns -{PI/2} .. {PI/2}. - */ -static mrb_value -math_atan(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = atan(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.atan2(y, x) -> float - * - * Computes the arc tangent given y and x. Returns - * -PI..PI. - * - * Math.atan2(-0.0, -1.0) #=> -3.141592653589793 - * Math.atan2(-1.0, -1.0) #=> -2.356194490192345 - * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966 - * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483 - * Math.atan2(-0.0, 1.0) #=> -0.0 - * Math.atan2(0.0, 1.0) #=> 0.0 - * Math.atan2(1.0, 1.0) #=> 0.7853981633974483 - * Math.atan2(1.0, 0.0) #=> 1.5707963267948966 - * Math.atan2(1.0, -1.0) #=> 2.356194490192345 - * Math.atan2(0.0, -1.0) #=> 3.141592653589793 - * - */ -static mrb_value -math_atan2(mrb_state *mrb, mrb_value obj) -{ - mrb_float x, y; - - mrb_get_args(mrb, "ff", &x, &y); - x = atan2(x, y); - - return mrb_float_value(x); -} - - - -/* - HYPERBOLIC TRIG FUNCTIONS -*/ -/* - * call-seq: - * Math.sinh(x) -> float - * - * Computes the hyperbolic sine of x (expressed in - * radians). - */ -static mrb_value -math_sinh(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = sinh(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.cosh(x) -> float - * - * Computes the hyperbolic cosine of x (expressed in radians). - */ -static mrb_value -math_cosh(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = cosh(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.tanh() -> float - * - * Computes the hyperbolic tangent of x (expressed in - * radians). - */ -static mrb_value -math_tanh(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = tanh(x); - - return mrb_float_value(x); -} - - -/* - INVERSE HYPERBOLIC TRIG FUNCTIONS -*/ - -/* - * call-seq: - * Math.asinh(x) -> float - * - * Computes the inverse hyperbolic sine of x. - */ -static mrb_value -math_asinh(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - - x = asinh(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.acosh(x) -> float - * - * Computes the inverse hyperbolic cosine of x. - */ -static mrb_value -math_acosh(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = acosh(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.atanh(x) -> float - * - * Computes the inverse hyperbolic tangent of x. - */ -static mrb_value -math_atanh(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = atanh(x); - - return mrb_float_value(x); -} - -/* - EXPONENTIALS AND LOGARITHMS -*/ -#if defined __CYGWIN__ -# include -# if CYGWIN_VERSION_DLL_MAJOR < 1005 -# define nan(x) nan() -# endif -# define log(x) ((x) < 0.0 ? nan("") : log(x)) -# define log10(x) ((x) < 0.0 ? nan("") : log10(x)) -#endif - -#ifndef log2 -#ifndef HAVE_LOG2 -double -log2(double x) -{ - return log10(x)/log10(2.0); -} -#else -extern double log2(double); -#endif -#endif - -/* - * call-seq: - * Math.exp(x) -> float - * - * Returns e**x. - * - * Math.exp(0) #=> 1.0 - * Math.exp(1) #=> 2.718281828459045 - * Math.exp(1.5) #=> 4.4816890703380645 - * - */ -static mrb_value -math_exp(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = exp(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.log(numeric) -> float - * Math.log(num,base) -> float - * - * Returns the natural logarithm of numeric. - * If additional second argument is given, it will be the base - * of logarithm. - * - * Math.log(1) #=> 0.0 - * Math.log(Math::E) #=> 1.0 - * Math.log(Math::E**3) #=> 3.0 - * Math.log(12,3) #=> 2.2618595071429146 - * - */ -static mrb_value -math_log(mrb_state *mrb, mrb_value obj) -{ - mrb_float x, base; - int argc; - - argc = mrb_get_args(mrb, "f|f", &x, &base); - x = log(x); - if (argc == 2) { - x /= log(base); - } - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.log2(numeric) -> float - * - * Returns the base 2 logarithm of numeric. - * - * Math.log2(1) #=> 0.0 - * Math.log2(2) #=> 1.0 - * Math.log2(32768) #=> 15.0 - * Math.log2(65536) #=> 16.0 - * - */ -static mrb_value -math_log2(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = log2(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.log10(numeric) -> float - * - * Returns the base 10 logarithm of numeric. - * - * Math.log10(1) #=> 0.0 - * Math.log10(10) #=> 1.0 - * Math.log10(10**100) #=> 100.0 - * - */ -static mrb_value -math_log10(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = log10(x); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.sqrt(numeric) -> float - * - * Returns the square root of numeric. - * - */ -static mrb_value -math_sqrt(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = sqrt(x); - - return mrb_float_value(x); -} - - -/* - * call-seq: - * Math.cbrt(numeric) -> float - * - * Returns the cube root of numeric. - * - * -9.upto(9) {|x| - * p [x, Math.cbrt(x), Math.cbrt(x)**3] - * } - * #=> - * [-9, -2.0800838230519, -9.0] - * [-8, -2.0, -8.0] - * [-7, -1.91293118277239, -7.0] - * [-6, -1.81712059283214, -6.0] - * [-5, -1.7099759466767, -5.0] - * [-4, -1.5874010519682, -4.0] - * [-3, -1.44224957030741, -3.0] - * [-2, -1.25992104989487, -2.0] - * [-1, -1.0, -1.0] - * [0, 0.0, 0.0] - * [1, 1.0, 1.0] - * [2, 1.25992104989487, 2.0] - * [3, 1.44224957030741, 3.0] - * [4, 1.5874010519682, 4.0] - * [5, 1.7099759466767, 5.0] - * [6, 1.81712059283214, 6.0] - * [7, 1.91293118277239, 7.0] - * [8, 2.0, 8.0] - * [9, 2.0800838230519, 9.0] - * - */ -static mrb_value -math_cbrt(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = cbrt(x); - - return mrb_float_value(x); -} - - -/* - * call-seq: - * Math.frexp(numeric) -> [ fraction, exponent ] - * - * Returns a two-element array containing the normalized fraction (a - * Float) and exponent (a Fixnum) of - * numeric. - * - * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11] - * fraction * 2**exponent #=> 1234.0 - */ -static mrb_value -math_frexp(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - int exp; - - mrb_get_args(mrb, "f", &x); - x = frexp(x, &exp); - - return mrb_assoc_new(mrb, mrb_float_value(x), mrb_fixnum_value(exp)); -} - -/* - * call-seq: - * Math.ldexp(flt, int) -> float - * - * Returns the value of flt*(2**int). - * - * fraction, exponent = Math.frexp(1234) - * Math.ldexp(fraction, exponent) #=> 1234.0 - */ -static mrb_value -math_ldexp(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - mrb_int i; - - mrb_get_args(mrb, "fi", &x, &i); - x = ldexp(x, i); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.hypot(x, y) -> float - * - * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle - * with sides x and y. - * - * Math.hypot(3, 4) #=> 5.0 - */ -static mrb_value -math_hypot(mrb_state *mrb, mrb_value obj) -{ - mrb_float x, y; - - mrb_get_args(mrb, "ff", &x, &y); - x = hypot(x, y); - - return mrb_float_value(x); -} - -/* - * call-seq: - * Math.erf(x) -> float - * - * Calculates the error function of x. - */ -static mrb_value -math_erf(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = erf(x); - - return mrb_float_value(x); -} - - -/* - * call-seq: - * Math.erfc(x) -> float - * - * Calculates the complementary error function of x. - */ -static mrb_value -math_erfc(mrb_state *mrb, mrb_value obj) -{ - mrb_float x; - - mrb_get_args(mrb, "f", &x); - x = erfc(x); - - return mrb_float_value(x); -} - -/* ------------------------------------------------------------------------*/ -void -mrb_init_math(mrb_state *mrb) -{ - struct RClass *mrb_math; - mrb_math = mrb_define_module(mrb, "Math"); - -#ifdef M_PI - mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(M_PI)); -#else - mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(atan(1.0)*4.0)); -#endif - -#ifdef M_E - mrb_define_const(mrb, mrb_math, "E", mrb_float_value(M_E)); -#else - mrb_define_const(mrb, mrb_math, "E", mrb_float_value(exp(1.0))); -#endif - -#ifdef MRB_USE_FLOAT - mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-5)); -#else - mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-12)); -#endif - - mrb_define_module_function(mrb, mrb_math, "sin", math_sin, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "cos", math_cos, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "tan", math_tan, ARGS_REQ(1)); - - mrb_define_module_function(mrb, mrb_math, "asin", math_asin, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "acos", math_acos, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "atan", math_atan, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, ARGS_REQ(2)); - - mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, ARGS_REQ(1)); - - mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, ARGS_REQ(1)); - - mrb_define_module_function(mrb, mrb_math, "exp", math_exp, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "log", math_log, ARGS_REQ(1)|ARGS_OPT(1)); - mrb_define_module_function(mrb, mrb_math, "log2", math_log2, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "log10", math_log10, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, ARGS_REQ(1)); - - mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, ARGS_REQ(2)); - - mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, ARGS_REQ(2)); - - mrb_define_module_function(mrb, mrb_math, "erf", math_erf, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, ARGS_REQ(1)); -} -#endif /* ENABLE_MATH */ diff --git a/test/t/math.rb b/test/t/math.rb deleted file mode 100644 index 780b805d2..000000000 --- a/test/t/math.rb +++ /dev/null @@ -1,125 +0,0 @@ -## -# Math Test - -if Object.const_defined?(:Math) - assert('Math.sin 0') do - check_float(Math.sin(0), 0) - end - - assert('Math.sin PI/2') do - check_float(Math.sin(Math::PI / 2), 1) - end - - assert('Fundamental trig identities') do - result = true - N = 13 - N.times do |i| - a = Math::PI / N * i - ca = Math::PI / 2 - a - s = Math.sin(a) - c = Math.cos(a) - t = Math.tan(a) - result &= check_float(s, Math.cos(ca)) - result &= check_float(t, 1 / Math.tan(ca)) - result &= check_float(s ** 2 + c ** 2, 1) - result &= check_float(t ** 2 + 1, (1/c) ** 2) - result &= check_float((1/t) ** 2 + 1, (1/s) ** 2) - end - result - end - - assert('Math.erf 0') do - check_float(Math.erf(0), 0) - end - - assert('Math.exp 0') do - check_float(Math.exp(0), 1.0) - end - - assert('Math.exp 1') do - check_float(Math.exp(1), 2.718281828459045) - end - - assert('Math.exp 1.5') do - check_float(Math.exp(1.5), 4.4816890703380645) - end - - assert('Math.log 1') do - check_float(Math.log(1), 0) - end - - assert('Math.log E') do - check_float(Math.log(Math::E), 1.0) - end - - assert('Math.log E**3') do - check_float(Math.log(Math::E**3), 3.0) - end - - assert('Math.log2 1') do - check_float(Math.log2(1), 0.0) - end - - assert('Math.log2 2') do - check_float(Math.log2(2), 1.0) - end - - assert('Math.log10 1') do - check_float(Math.log10(1), 0.0) - end - - assert('Math.log10 10') do - check_float(Math.log10(10), 1.0) - end - - assert('Math.log10 10**100') do - check_float(Math.log10(10**100), 100.0) - end - - assert('Math.sqrt') do - num = [0.0, 1.0, 2.0, 3.0, 4.0] - sqr = [0, 1, 4, 9, 16] - result = true - sqr.each_with_index do |v,i| - result &= check_float(Math.sqrt(v), num[i]) - end - result - end - - assert('Math.cbrt') do - num = [-2.0, -1.0, 0.0, 1.0, 2.0] - cub = [-8, -1, 0, 1, 8] - result = true - cub.each_with_index do |v,i| - result &= check_float(Math.cbrt(v), num[i]) - end - result - end - - assert('Math.hypot') do - check_float(Math.hypot(3, 4), 5.0) - end - - assert('Math.frexp 1234') do - n = 1234 - fraction, exponent = Math.frexp(n) - check_float(Math.ldexp(fraction, exponent), n) - end - - assert('Math.erf 1') do - check_float(Math.erf(1), 0.842700792949715) - end - - assert('Math.erfc 1') do - check_float(Math.erfc(1), 0.157299207050285) - end - - assert('Math.erf -1') do - check_float(Math.erf(-1), -0.8427007929497148) - end - - assert('Math.erfc -1') do - check_float(Math.erfc(-1), 1.8427007929497148) - end -end - -- cgit v1.2.3 From 3d3ebbc9cd3c7b745e98fd3232044fa0f97fd891 Mon Sep 17 00:00:00 2001 From: Carson McDonald Date: Thu, 28 Feb 2013 09:24:08 -0500 Subject: Fix arena size check. --- src/gc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/gc.c b/src/gc.c index 58326d396..5cc794fd9 100644 --- a/src/gc.c +++ b/src/gc.c @@ -314,7 +314,7 @@ mrb_free_heap(mrb_state *mrb) static void gc_protect(mrb_state *mrb, struct RBasic *p) { - if (mrb->arena_idx > MRB_ARENA_SIZE) { + if (mrb->arena_idx >= MRB_ARENA_SIZE) { /* arena overflow error */ mrb->arena_idx = MRB_ARENA_SIZE - 4; /* force room in arena */ mrb_raise(mrb, E_RUNTIME_ERROR, "arena overflow error"); -- cgit v1.2.3 From e66de39a02433933c0966c91864650220c6f80d5 Mon Sep 17 00:00:00 2001 From: MATSUMOTO Ryosuke Date: Thu, 28 Feb 2013 15:24:36 -0800 Subject: Adjust space --- src/class.c | 81 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/class.c b/src/class.c index e70bd7e73..c2def3e8c 100644 --- a/src/class.c +++ b/src/class.c @@ -1777,9 +1777,9 @@ mrb_init_class(mrb_state *mrb) /* name basic classes */ mrb_define_const(mrb, bob, "BasicObject", mrb_obj_value(bob)); mrb_define_const(mrb, obj, "BasicObject", mrb_obj_value(bob)); - mrb_define_const(mrb, obj, "Object", mrb_obj_value(obj)); - mrb_define_const(mrb, obj, "Module", mrb_obj_value(mod)); - mrb_define_const(mrb, obj, "Class", mrb_obj_value(cls)); + mrb_define_const(mrb, obj, "Object", mrb_obj_value(obj)); + mrb_define_const(mrb, obj, "Module", mrb_obj_value(mod)); + mrb_define_const(mrb, obj, "Class", mrb_obj_value(cls)); /* name each classes */ mrb_name_class(mrb, bob, mrb_intern(mrb, "BasicObject")); @@ -1789,43 +1789,44 @@ mrb_init_class(mrb_state *mrb) mrb_undef_method(mrb, mod, "new"); MRB_SET_INSTANCE_TT(cls, MRB_TT_CLASS); - mrb_define_method(mrb, bob, "initialize", mrb_bob_init, ARGS_NONE()); - mrb_define_method(mrb, bob, "!", mrb_bob_not, ARGS_NONE()); - mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, ARGS_ANY()); /* 15.3.1.3.30 */ - mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, ARGS_ANY()); - mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, ARGS_NONE()); /* 15.2.3.3.4 */ - mrb_define_method(mrb, cls, "new", mrb_instance_new, ARGS_ANY()); /* 15.2.3.3.3 */ - mrb_define_method(mrb, cls, "inherited", mrb_bob_init, ARGS_REQ(1)); - mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, ARGS_REQ(1)); /* 15.2.2.4.16 */ - mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, ARGS_REQ(1)); /* 15.2.2.4.17 */ - mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, ARGS_REQ(2)); /* 15.2.2.4.18 */ - mrb_define_method(mrb, mod, "extend_object", mrb_mod_extend_object, ARGS_REQ(1)); /* 15.2.2.4.25 */ - mrb_define_method(mrb, mod, "extended", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.26 */ - mrb_define_method(mrb, mod, "include", mrb_mod_include, ARGS_ANY()); /* 15.2.2.4.27 */ - mrb_define_method(mrb, mod, "include?", mrb_mod_include_p, ARGS_REQ(1)); /* 15.2.2.4.28 */ - mrb_define_method(mrb, mod, "append_features", mrb_mod_append_features, ARGS_REQ(1)); /* 15.2.2.4.10 */ - mrb_define_method(mrb, mod, "class_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.15 */ - mrb_define_method(mrb, mod, "included", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.29 */ - mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, ARGS_NONE()); /* 15.2.2.4.30 */ - mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, ARGS_ANY()); /* 15.2.2.4.33 */ - mrb_define_method(mrb, mod, "method_defined?", mrb_mod_method_defined, ARGS_REQ(1)); /* 15.2.2.4.34 */ - mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.35 */ - mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, ARGS_REQ(1)); /* 15.2.2.4.39 */ - mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, ARGS_ANY()); /* 15.2.2.4.41 */ - - mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, ARGS_NONE()); - mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE()); - mrb_define_method(mrb, mod, "alias_method", mrb_mod_alias, ARGS_ANY()); /* 15.2.2.4.8 */ - mrb_define_method(mrb, mod, "ancestors", mrb_mod_ancestors, ARGS_NONE()); /* 15.2.2.4.9 */ - mrb_define_method(mrb, mod, "undef_method", mrb_mod_undef, ARGS_ANY()); /* 15.2.2.4.41 */ - mrb_define_method(mrb, mod, "const_defined?", mrb_mod_const_defined, ARGS_REQ(1)); /* 15.2.2.4.20 */ - mrb_define_method(mrb, mod, "const_get", mrb_mod_const_get, ARGS_REQ(1)); /* 15.2.2.4.21 */ - mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, ARGS_REQ(2)); /* 15.2.2.4.23 */ - mrb_define_method(mrb, mod, "remove_const", mrb_mod_remove_const, ARGS_REQ(1)); /* 15.2.2.4.40 */ - mrb_define_method(mrb, mod, "define_method", mod_define_method, ARGS_REQ(1)); - mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, ARGS_NONE()); /* 15.2.2.4.19 */ - - mrb_define_method(mrb, mod, "===", mrb_mod_eqq, ARGS_REQ(1)); + mrb_define_method(mrb, bob, "initialize", mrb_bob_init, ARGS_NONE()); + mrb_define_method(mrb, bob, "!", mrb_bob_not, ARGS_NONE()); + mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, ARGS_ANY()); /* 15.3.1.3.30 */ + + mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, ARGS_ANY()); + mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, ARGS_NONE()); /* 15.2.3.3.4 */ + mrb_define_method(mrb, cls, "new", mrb_instance_new, ARGS_ANY()); /* 15.2.3.3.3 */ + mrb_define_method(mrb, cls, "inherited", mrb_bob_init, ARGS_REQ(1)); + + mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, ARGS_REQ(1)); /* 15.2.2.4.16 */ + mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, ARGS_REQ(1)); /* 15.2.2.4.17 */ + mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, ARGS_REQ(2)); /* 15.2.2.4.18 */ + mrb_define_method(mrb, mod, "extend_object", mrb_mod_extend_object, ARGS_REQ(1)); /* 15.2.2.4.25 */ + mrb_define_method(mrb, mod, "extended", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.26 */ + mrb_define_method(mrb, mod, "include", mrb_mod_include, ARGS_ANY()); /* 15.2.2.4.27 */ + mrb_define_method(mrb, mod, "include?", mrb_mod_include_p, ARGS_REQ(1)); /* 15.2.2.4.28 */ + mrb_define_method(mrb, mod, "append_features", mrb_mod_append_features, ARGS_REQ(1)); /* 15.2.2.4.10 */ + mrb_define_method(mrb, mod, "class_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.15 */ + mrb_define_method(mrb, mod, "included", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.29 */ + mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, ARGS_NONE()); /* 15.2.2.4.30 */ + mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, ARGS_ANY()); /* 15.2.2.4.33 */ + mrb_define_method(mrb, mod, "method_defined?", mrb_mod_method_defined, ARGS_REQ(1)); /* 15.2.2.4.34 */ + mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.35 */ + mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, ARGS_REQ(1)); /* 15.2.2.4.39 */ + mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, ARGS_ANY()); /* 15.2.2.4.41 */ + mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, ARGS_NONE()); + mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE()); + mrb_define_method(mrb, mod, "alias_method", mrb_mod_alias, ARGS_ANY()); /* 15.2.2.4.8 */ + mrb_define_method(mrb, mod, "ancestors", mrb_mod_ancestors, ARGS_NONE()); /* 15.2.2.4.9 */ + mrb_define_method(mrb, mod, "undef_method", mrb_mod_undef, ARGS_ANY()); /* 15.2.2.4.41 */ + mrb_define_method(mrb, mod, "const_defined?", mrb_mod_const_defined, ARGS_REQ(1)); /* 15.2.2.4.20 */ + mrb_define_method(mrb, mod, "const_get", mrb_mod_const_get, ARGS_REQ(1)); /* 15.2.2.4.21 */ + mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, ARGS_REQ(2)); /* 15.2.2.4.23 */ + mrb_define_method(mrb, mod, "remove_const", mrb_mod_remove_const, ARGS_REQ(1)); /* 15.2.2.4.40 */ + mrb_define_method(mrb, mod, "define_method", mod_define_method, ARGS_REQ(1)); + mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, ARGS_NONE()); /* 15.2.2.4.19 */ + mrb_define_method(mrb, mod, "===", mrb_mod_eqq, ARGS_REQ(1)); + mrb_undef_method(mrb, cls, "append_features"); mrb_undef_method(mrb, cls, "extend_object"); } -- cgit v1.2.3 From 138ecf4723078cf8ef4342fb3995db23003eff01 Mon Sep 17 00:00:00 2001 From: mattn Date: Fri, 1 Mar 2013 13:37:46 +0900 Subject: Pluggable Struct --- include/mrbconf.h | 4 - include/mruby/struct.h | 27 -- include/mruby/value.h | 22 +- mrbgems/mruby-struct/mrbgem.rake | 4 + mrbgems/mruby-struct/src/struct.c | 785 ++++++++++++++++++++++++++++++++++++ mrbgems/mruby-struct/test/struct.rb | 77 ++++ src/etc.c | 1 - src/gc.c | 3 - src/init.c | 4 - src/object.c | 1 - src/struct.c | 781 ----------------------------------- test/t/struct.rb | 77 ---- 12 files changed, 876 insertions(+), 910 deletions(-) delete mode 100644 include/mruby/struct.h create mode 100644 mrbgems/mruby-struct/mrbgem.rake create mode 100644 mrbgems/mruby-struct/src/struct.c create mode 100644 mrbgems/mruby-struct/test/struct.rb delete mode 100644 src/struct.c delete mode 100644 test/t/struct.rb (limited to 'src') diff --git a/include/mrbconf.h b/include/mrbconf.h index 45e3d6034..c84480af7 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -45,7 +45,6 @@ /* -DDISABLE_XXXX to drop following features */ //#define DISABLE_SPRINTF /* Kernel.sprintf method */ -//#define DISABLE_STRUCT /* Struct class */ //#define DISABLE_STDIO /* use of stdio */ /* -DENABLE_XXXX to enable following features */ @@ -84,9 +83,6 @@ typedef short mrb_sym; #ifndef DISABLE_SPRINTF #define ENABLE_SPRINTF #endif -#ifndef DISABLE_STRUCT -#define ENABLE_STRUCT -#endif #ifndef DISABLE_STDIO #define ENABLE_STDIO #endif diff --git a/include/mruby/struct.h b/include/mruby/struct.h deleted file mode 100644 index cfe6df135..000000000 --- a/include/mruby/struct.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -** mruby/struct.h - Struct class -** -** See Copyright Notice in mruby.h -*/ - -#ifndef MRUBY_STRUCT_H -#define MRUBY_STRUCT_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct RStruct { - struct RBasic basic; - long len; - mrb_value *ptr; -}; -#define RSTRUCT(st) ((struct RStruct*)((st).value.p)) -#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len)) -#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr) - -#if defined(__cplusplus) -} /* extern "C" { */ -#endif - -#endif /* MRUBY_STRUCT_H */ diff --git a/include/mruby/value.h b/include/mruby/value.h index 17f51db94..1dfa7b975 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -29,12 +29,11 @@ enum mrb_vtype { MRB_TT_HASH, /* 16 */ MRB_TT_STRING, /* 17 */ MRB_TT_RANGE, /* 18 */ - MRB_TT_STRUCT, /* 19 */ - MRB_TT_EXCEPTION, /* 20 */ - MRB_TT_FILE, /* 21 */ - MRB_TT_ENV, /* 22 */ - MRB_TT_DATA, /* 23 */ - MRB_TT_MAXDEFINE /* 24 */ + MRB_TT_EXCEPTION, /* 19 */ + MRB_TT_FILE, /* 20 */ + MRB_TT_ENV, /* 21 */ + MRB_TT_DATA, /* 22 */ + MRB_TT_MAXDEFINE /* 23 */ }; typedef struct mrb_value { @@ -89,12 +88,11 @@ enum mrb_vtype { MRB_TT_HASH, /* 17 */ MRB_TT_STRING, /* 18 */ MRB_TT_RANGE, /* 19 */ - MRB_TT_STRUCT, /* 20 */ - MRB_TT_EXCEPTION, /* 21 */ - MRB_TT_FILE, /* 22 */ - MRB_TT_ENV, /* 23 */ - MRB_TT_DATA, /* 24 */ - MRB_TT_MAXDEFINE /* 25 */ + MRB_TT_EXCEPTION, /* 20 */ + MRB_TT_FILE, /* 21 */ + MRB_TT_ENV, /* 22 */ + MRB_TT_DATA, /* 23 */ + MRB_TT_MAXDEFINE /* 24 */ }; #ifdef MRB_ENDIAN_BIG diff --git a/mrbgems/mruby-struct/mrbgem.rake b/mrbgems/mruby-struct/mrbgem.rake new file mode 100644 index 000000000..476e990da --- /dev/null +++ b/mrbgems/mruby-struct/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-struct') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c new file mode 100644 index 000000000..131702e9c --- /dev/null +++ b/mrbgems/mruby-struct/src/struct.c @@ -0,0 +1,785 @@ +/* +** struct.c - Struct class +** +** See Copyright Notice in mruby.h +*/ + +#include +#include +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include "mruby/class.h" +#include "mruby/variable.h" + +struct RStruct { + struct RBasic basic; + long len; + mrb_value *ptr; +}; + +#define RSTRUCT(st) ((struct RStruct*)((st).value.p)) +#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len)) +#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr) + +static struct RClass * +struct_class(mrb_state *mrb) +{ + return mrb_class_get(mrb, "Struct"); +} + +static inline mrb_value +struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) +{ + struct RClass* kclass; + struct RClass* sclass = struct_class(mrb); + + mrb_value ans; + for (;;) { + ans = mrb_iv_get(mrb, c, id); + if (!mrb_nil_p(ans)) return ans; + kclass = RCLASS_SUPER(c); + if (kclass == 0 || kclass == sclass) + return mrb_nil_value(); + c = mrb_obj_value(kclass); + } +} + +mrb_value +mrb_struct_iv_get(mrb_state *mrb, mrb_value c, const char *name) +{ + return struct_ivar_get(mrb, c, mrb_intern(mrb, name)); +} + +mrb_value +mrb_struct_s_members(mrb_state *mrb, mrb_value klass) +{ + mrb_value members = struct_ivar_get(mrb, klass, mrb_intern(mrb, "__members__")); + + if (mrb_nil_p(members)) { + mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct"); + } + if (!mrb_array_p(members)) { + mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); + } + return members; +} + +mrb_value +mrb_struct_members(mrb_state *mrb, mrb_value s) +{ + mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s))); + if (mrb_type(s) == MRB_TT_STRUCT) { + if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { + mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", + RARRAY_LEN(members), RSTRUCT_LEN(s)); + } + } + return members; +} + +static mrb_value +mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) +{ + mrb_value members, ary; + mrb_value *p, *pend; + + members = mrb_struct_s_members(mrb, klass); + ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); + p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members); + while (p < pend) { + mrb_ary_push(mrb, ary, *p); + p++; + } + + return ary; +} + +static inline void +struct_copy(mrb_value *dst, const mrb_value *src, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) { + dst[i] = src[i]; + } +} + +/* 15.2.18.4.6 */ +/* + * call-seq: + * struct.members -> array + * + * Returns an array of strings representing the names of the instance + * variables. + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * joe.members #=> [:name, :address, :zip] + */ + +static mrb_value +mrb_struct_members_m(mrb_state *mrb, mrb_value obj) +{ + return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj))); +} + +mrb_value +mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) +{ + mrb_value members, slot, *ptr, *ptr_members; + long i, len; + + ptr = RSTRUCT_PTR(obj); + members = mrb_struct_members(mrb, obj); + ptr_members = RARRAY_PTR(members); + slot = mrb_symbol_value(id); + len = RARRAY_LEN(members); + for (i=0; ici->mid); +} + +static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];} +static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];} +static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];} +static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];} +static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];} +static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];} +static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];} +static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];} +static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];} +static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];} + +#define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) +#define N_REF_FUNC numberof(ref_func) + +static mrb_value (*const ref_func[])(mrb_state*, mrb_value) = { + mrb_struct_ref0, + mrb_struct_ref1, + mrb_struct_ref2, + mrb_struct_ref3, + mrb_struct_ref4, + mrb_struct_ref5, + mrb_struct_ref6, + mrb_struct_ref7, + mrb_struct_ref8, + mrb_struct_ref9, +}; + +mrb_sym +mrb_id_attrset(mrb_state *mrb, mrb_sym id) +{ + const char *name; + char *buf; + int len; + mrb_sym mid; + + name = mrb_sym2name_len(mrb, id, &len); + buf = (char *)mrb_malloc(mrb, len+2); + memcpy(buf, name, len); + buf[len] = '='; + buf[len+1] = '\0'; + + mid = mrb_intern2(mrb, buf, len+1); + mrb_free(mrb, buf); + return mid; +} + +static mrb_value +mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) +{ + const char *name; + int i, len; + mrb_sym mid; + mrb_value members, slot, *ptr, *ptr_members; + + /* get base id */ + name = mrb_sym2name_len(mrb, mrb->ci->mid, &len); + mid = mrb_intern2(mrb, name, len-1); /* omit last "=" */ + + members = mrb_struct_members(mrb, obj); + ptr_members = RARRAY_PTR(members); + len = RARRAY_LEN(members); + ptr = RSTRUCT_PTR(obj); + for (i=0; itLAST_TOKEN) +#define is_local_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_LOCAL) +int +mrb_is_local_id(mrb_sym id) +{ + return is_local_id(id); +} + +#define is_const_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_CONST) +int +mrb_is_const_id(mrb_sym id) +{ + return is_const_id(id); +} + +static mrb_value +make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass) +{ + mrb_value nstr, *ptr_members; + mrb_sym id; + long i, len; + struct RClass *c; + + if (mrb_nil_p(name)) { + c = mrb_class_new(mrb, klass); + } + else { + /* old style: should we warn? */ + name = mrb_str_to_str(mrb, name); + id = mrb_to_id(mrb, name); + if (!mrb_is_const_id(id)) { + mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); + } + if (mrb_const_defined_at(mrb, klass, id)) { + mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); + //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); + } + c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); + } + MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT); + nstr = mrb_obj_value(c); + mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members); + + mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY()); + mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY()); + mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE()); + //RSTRUCT(nstr)->basic.c->super = c->c; + ptr_members = RARRAY_PTR(members); + len = RARRAY_LEN(members); + for (i=0; i< len; i++) { + mrb_sym id = mrb_symbol(ptr_members[i]); + if (mrb_is_local_id(id) || mrb_is_const_id(id)) { + if (i < N_REF_FUNC) { + mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE()); + } + else { + mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE()); + } + mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1)); + } + } + + return nstr; +} + +mrb_value +mrb_struct_define(mrb_state *mrb, const char *name, ...) +{ + va_list ar; + mrb_value nm, ary; + char *mem; + + if (!name) nm = mrb_nil_value(); + else nm = mrb_str_new2(mrb, name); + ary = mrb_ary_new(mrb); + + va_start(ar, name); + while ((mem = va_arg(ar, char*)) != 0) { + mrb_sym slot = mrb_intern(mrb, mem); + mrb_ary_push(mrb, ary, mrb_symbol_value(slot)); + } + va_end(ar); + + return make_struct(mrb, nm, ary, struct_class(mrb)); +} + +/* 15.2.18.3.1 */ +/* + * call-seq: + * Struct.new( [aString] [, aSym]+> ) -> StructClass + * StructClass.new(arg, ...) -> obj + * StructClass[arg, ...] -> obj + * + * Creates a new class, named by aString, containing accessor + * methods for the given symbols. If the name aString is + * omitted, an anonymous structure class will be created. Otherwise, + * the name of this struct will appear as a constant in class + * Struct, so it must be unique for all + * Structs in the system and should start with a capital + * letter. Assigning a structure class to a constant effectively gives + * the class the name of the constant. + * + * Struct::new returns a new Class object, + * which can then be used to create specific instances of the new + * structure. The number of actual parameters must be + * less than or equal to the number of attributes defined for this + * class; unset parameters default to nil. Passing too many + * parameters will raise an ArgumentError. + * + * The remaining methods listed in this section (class and instance) + * are defined for this generated class. + * + * # Create a structure with a name in Struct + * Struct.new("Customer", :name, :address) #=> Struct::Customer + * Struct::Customer.new("Dave", "123 Main") #=> # + * + * # Create a structure named by its constant + * Customer = Struct.new(:name, :address) #=> Customer + * Customer.new("Dave", "123 Main") #=> # + */ +static mrb_value +mrb_struct_s_def(mrb_state *mrb, mrb_value klass) +{ + mrb_value name, rest; + mrb_value *pargv; + int argcnt; + long i; + mrb_value b, st; + mrb_sym id; + mrb_value *argv; + int argc; + + name = mrb_nil_value(); + rest = mrb_nil_value(); + mrb_get_args(mrb, "*&", &argv, &argc, &b); + if (argc == 0) { /* special case to avoid crash */ + rest = mrb_ary_new(mrb); + } + else { + if (argc > 0) name = argv[0]; + if (argc > 1) rest = argv[1]; + if (mrb_array_p(rest)) { + if (!mrb_nil_p(name) && mrb_symbol_p(name)) { + /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ + mrb_ary_unshift(mrb, rest, name); + name = mrb_nil_value(); + } + } + else { + pargv = &argv[1]; + argcnt = argc-1; + if (!mrb_nil_p(name) && mrb_symbol_p(name)) { + /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ + name = mrb_nil_value(); + pargv = &argv[0]; + argcnt++; + } + rest = mrb_ary_new_from_values(mrb, argcnt, pargv); + } + for (i=0; iptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); + st->len = n; + struct_copy(st->ptr, argv, argc); + + return self; +} + +static mrb_value +mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self) +{ + mrb_value *argv; + int argc; + + mrb_get_args(mrb, "*", &argv, &argc); + return mrb_struct_initialize_withArg(mrb, argc, argv, self); +} + +mrb_value +mrb_struct_initialize(mrb_state *mrb, mrb_value self, mrb_value values) +{ + return mrb_struct_initialize_withArg(mrb, RARRAY_LEN(values), RARRAY_PTR(values), self); +} + +static mrb_value +inspect_struct(mrb_state *mrb, mrb_value s, int recur) +{ + const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s)); + mrb_value members, str = mrb_str_new(mrb, "#"); + } + + members = mrb_struct_members(mrb, s); + ptr_members = RARRAY_PTR(members); + ptr = RSTRUCT_PTR(s); + len = RSTRUCT_LEN(s); + for (i=0; i 0) { + mrb_str_cat2(mrb, str, ", "); + } + else if (cn) { + mrb_str_cat2(mrb, str, " "); + } + slot = ptr_members[i]; + id = mrb_symbol(slot); + if (mrb_is_local_id(id) || mrb_is_const_id(id)) { + const char *name; + int len; + + name = mrb_sym2name_len(mrb, id, &len); + mrb_str_append(mrb, str, mrb_str_new(mrb, name, len)); + } + else { + mrb_str_append(mrb, str, mrb_inspect(mrb, slot)); + } + mrb_str_cat2(mrb, str, "="); + mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i])); + } + mrb_str_cat2(mrb, str, ">"); + + return str; +} + +/* + * call-seq: + * struct.to_s -> string + * struct.inspect -> string + * + * Describe the contents of this struct in a string. + */ +static mrb_value +mrb_struct_inspect(mrb_state *mrb, mrb_value s) +{ + return inspect_struct(mrb, s, 0); +} + +/* 15.2.18.4.9 */ +/* :nodoc: */ +mrb_value +mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) +{ + mrb_value s; + + mrb_get_args(mrb, "o", &s); + + if (mrb_obj_equal(mrb, copy, s)) return copy; + if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) { + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); + } + if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { + mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); + } + struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy)); + + return copy; +} + +static mrb_value +mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id) +{ + mrb_value *ptr, members, *ptr_members; + long i, len; + + ptr = RSTRUCT_PTR(s); + members = mrb_struct_members(mrb, s); + ptr_members = RARRAY_PTR(members); + len = RARRAY_LEN(members); + for (i=0; i anObject + * struct[fixnum] -> anObject + * + * Attribute Reference---Returns the value of the instance variable + * named by symbol, or indexed (0..length-1) by + * fixnum. Will raise NameError if the named + * variable does not exist, or IndexError if the index is + * out of range. + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * + * joe["name"] #=> "Joe Smith" + * joe[:name] #=> "Joe Smith" + * joe[0] #=> "Joe Smith" + */ +mrb_value +mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx) +{ + long i; + + if (mrb_string_p(idx) || mrb_symbol_p(idx)) { + return mrb_struct_aref_id(mrb, s, mrb_to_id(mrb, idx)); + } + + i = mrb_fixnum(idx); + if (i < 0) i = RSTRUCT_LEN(s) + i; + if (i < 0) + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + if (RSTRUCT_LEN(s) <= i) + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + return RSTRUCT_PTR(s)[i]; +} + +mrb_value +mrb_struct_aref(mrb_state *mrb, mrb_value s) +{ + mrb_value idx; + + mrb_get_args(mrb, "o", &idx); + return mrb_struct_aref_n(mrb, s, idx); +} + +static mrb_value +mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) +{ + mrb_value members, *ptr, *ptr_members; + long i, len; + + members = mrb_struct_members(mrb, s); + len = RARRAY_LEN(members); + if (RSTRUCT_LEN(s) != len) { + mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", + len, RSTRUCT_LEN(s)); + } + ptr = RSTRUCT_PTR(s); + ptr_members = RARRAY_PTR(members); + for (i=0; i obj + * struct[fixnum] = obj -> obj + * + * Attribute Assignment---Assigns to the instance variable named by + * symbol or fixnum the value obj and + * returns it. Will raise a NameError if the named + * variable does not exist, or an IndexError if the index + * is out of range. + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * + * joe["name"] = "Luke" + * joe[:zip] = "90210" + * + * joe.name #=> "Luke" + * joe.zip #=> "90210" + */ + +mrb_value +mrb_struct_aset(mrb_state *mrb, mrb_value s) +{ + long i; + mrb_value idx; + mrb_value val; + + mrb_get_args(mrb, "oo", &idx, &val); + + if (mrb_string_p(idx) || mrb_symbol_p(idx)) { + return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); + } + + i = mrb_fixnum(idx); + if (i < 0) i = RSTRUCT_LEN(s) + i; + if (i < 0) { + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + } + if (RSTRUCT_LEN(s) <= i) { + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + } + return RSTRUCT_PTR(s)[i] = val; +} + +/* 15.2.18.4.1 */ +/* + * call-seq: + * struct == other_struct -> true or false + * + * Equality---Returns true if other_struct is + * equal to this one: they must be of the same class as generated by + * Struct::new, and the values of all instance variables + * must be equal (according to Object#==). + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345) + * joe == joejr #=> true + * joe == jane #=> false + */ + +static mrb_value +mrb_struct_equal(mrb_state *mrb, mrb_value s) +{ + mrb_value s2; + mrb_value *ptr, *ptr2; + long i, len; + + mrb_get_args(mrb, "o", &s2); + if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); + if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); + if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); + if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { + mrb_bug("inconsistent struct"); /* should never happen */ + } + ptr = RSTRUCT_PTR(s); + ptr2 = RSTRUCT_PTR(s2); + len = RSTRUCT_LEN(s); + for (i=0; i true or false + * + * Two structures are equal if they are the same object, or if all their + * fields are equal (using eql?). + */ +static mrb_value +mrb_struct_eql(mrb_state *mrb, mrb_value s) +{ + mrb_value s2; + mrb_value *ptr, *ptr2; + long i, len; + + mrb_get_args(mrb, "o", &s2); + if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); + if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); + if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); + if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { + mrb_bug("inconsistent struct"); /* should never happen */ + } + + ptr = RSTRUCT_PTR(s); + ptr2 = RSTRUCT_PTR(s2); + len = RSTRUCT_LEN(s); + for (i=0; iStruct is a convenient way to bundle a number of + * attributes together, using accessor methods, without having to write + * an explicit class. + * + * The Struct class is a generator of specific classes, + * each one of which is defined to hold a set of variables and their + * accessors. In these examples, we'll call the generated class + * ``CustomerClass,'' and we'll show an example instance of that + * class as ``CustomerInst.'' + * + * In the descriptions that follow, the parameter symbol refers + * to a symbol, which is either a quoted string or a + * Symbol (such as :name). + */ +void +mrb_init_struct(mrb_state *mrb) +{ + struct RClass *st; + st = mrb_define_class(mrb, "Struct", mrb->object_class); + + mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, ARGS_ANY()); /* 15.2.18.3.1 */ + + mrb_define_method(mrb, st, "==", mrb_struct_equal, ARGS_REQ(1)); /* 15.2.18.4.1 */ + mrb_define_method(mrb, st, "[]", mrb_struct_aref, ARGS_REQ(1)); /* 15.2.18.4.2 */ + mrb_define_method(mrb, st, "[]=", mrb_struct_aset, ARGS_REQ(2)); /* 15.2.18.4.3 */ + mrb_define_method(mrb, st, "members", mrb_struct_members_m, ARGS_NONE()); /* 15.2.18.4.6 */ + mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,ARGS_ANY()); /* 15.2.18.4.8 */ + mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, ARGS_REQ(1)); /* 15.2.18.4.9 */ + mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, ARGS_NONE()); /* 15.2.18.4.10(x) */ + mrb_define_alias(mrb, st, "to_s", "inspect"); /* 15.2.18.4.11(x) */ + mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */ + +} diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb new file mode 100644 index 000000000..d79b30c0e --- /dev/null +++ b/mrbgems/mruby-struct/test/struct.rb @@ -0,0 +1,77 @@ +## +# Struct ISO Test + +if Object.const_defined?(:Struct) + assert('Struct', '15.2.18') do + Struct.class == Class + end + + assert('Struct superclass', '15.2.18.2') do + Struct.superclass == Object + end + + assert('Struct.new', '15.2.18.3.1') do + c = Struct.new(:m1, :m2) + c.superclass == Struct and + c.members == [:m1,:m2] + end + + # Check crash bug with Struc.new and no params. + assert('Struct.new', '15.2.18.3.1') do + c = Struct.new() + c.superclass == Struct and c.members == [] + end + + assert('Struct#==', '15.2.18.4.1') do + c = Struct.new(:m1, :m2) + cc1 = c.new(1,2) + cc2 = c.new(1,2) + cc1 == cc2 + end + + assert('Struct#[]', '15.2.18.4.2') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc[:m1] == 1 and cc["m2"] == 2 + end + + assert('Struct#[]=', '15.2.18.4.3') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc[:m1] = 3 + cc[:m1] == 3 + end + + assert('Struct#each', '15.2.18.4.4') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + a = [] + cc.each{|x| + a << x + } + a[0] == 1 and a[1] == 2 + end + + assert('Struct#each_pair', '15.2.18.4.5') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + a = [] + cc.each_pair{|k,v| + a << [k,v] + } + a[0] == [:m1, 1] and a[1] == [:m2, 2] + end + + assert('Struct#members', '15.2.18.4.6') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc.members == [:m1,:m2] + end + + assert('Struct#select', '15.2.18.4.7') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc.select{|v| v % 2 == 0} == [2] + end +end + diff --git a/src/etc.c b/src/etc.c index 6a43ddd31..644465c80 100644 --- a/src/etc.c +++ b/src/etc.c @@ -170,7 +170,6 @@ mrb_obj_id(mrb_value obj) case MRB_TT_ARRAY: case MRB_TT_HASH: case MRB_TT_RANGE: - case MRB_TT_STRUCT: case MRB_TT_EXCEPTION: case MRB_TT_FILE: case MRB_TT_DATA: diff --git a/src/gc.c b/src/gc.c index 5cc794fd9..c48e6949a 100644 --- a/src/gc.c +++ b/src/gc.c @@ -86,9 +86,6 @@ typedef struct { struct RArray array; struct RHash hash; struct RRange range; -#ifdef ENABLE_STRUCT - struct RStruct strct; -#endif struct RData data; struct RProc proc; } as; diff --git a/src/init.c b/src/init.c index fa2d5d305..0d1a24881 100644 --- a/src/init.c +++ b/src/init.c @@ -20,7 +20,6 @@ void mrb_init_array(mrb_state*); void mrb_init_hash(mrb_state*); void mrb_init_numeric(mrb_state*); void mrb_init_range(mrb_state*); -void mrb_init_struct(mrb_state*); void mrb_init_gc(mrb_state*); void mrb_init_print(mrb_state*); void mrb_init_math(mrb_state*); @@ -48,9 +47,6 @@ mrb_init_core(mrb_state *mrb) mrb_init_hash(mrb); DONE; mrb_init_numeric(mrb); DONE; mrb_init_range(mrb); DONE; -#ifdef ENABLE_STRUCT - mrb_init_struct(mrb); DONE; -#endif mrb_init_gc(mrb); DONE; #ifdef ENABLE_STDIO mrb_init_print(mrb); DONE; diff --git a/src/object.c b/src/object.c index e087c35c0..6707fc6e4 100644 --- a/src/object.c +++ b/src/object.c @@ -379,7 +379,6 @@ static const struct types { {MRB_TT_HASH, "Hash"}, {MRB_TT_STRING, "String"}, {MRB_TT_RANGE, "Range"}, - {MRB_TT_STRUCT, "Struct"}, // {MRB_TT_BIGNUM, "Bignum"}, {MRB_TT_FILE, "File"}, {MRB_TT_DATA, "Data"}, /* internal use: wrapped C pointers */ diff --git a/src/struct.c b/src/struct.c deleted file mode 100644 index d7b63259e..000000000 --- a/src/struct.c +++ /dev/null @@ -1,781 +0,0 @@ -/* -** struct.c - Struct class -** -** See Copyright Notice in mruby.h -*/ - -#include "mruby.h" -#ifdef ENABLE_STRUCT -#include -#include "error.h" -#include "mruby/struct.h" -#include "mruby/array.h" -#include - -#include "mruby/string.h" -#include "mruby/class.h" -#include "mruby/variable.h" - - -static struct RClass * -struct_class(mrb_state *mrb) -{ - return mrb_class_get(mrb, "Struct"); -} - -static inline mrb_value -struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) -{ - struct RClass* kclass; - struct RClass* sclass = struct_class(mrb); - - mrb_value ans; - for (;;) { - ans = mrb_iv_get(mrb, c, id); - if (!mrb_nil_p(ans)) return ans; - kclass = RCLASS_SUPER(c); - if (kclass == 0 || kclass == sclass) - return mrb_nil_value(); - c = mrb_obj_value(kclass); - } -} - -mrb_value -mrb_struct_iv_get(mrb_state *mrb, mrb_value c, const char *name) -{ - return struct_ivar_get(mrb, c, mrb_intern(mrb, name)); -} - -mrb_value -mrb_struct_s_members(mrb_state *mrb, mrb_value klass) -{ - mrb_value members = struct_ivar_get(mrb, klass, mrb_intern(mrb, "__members__")); - - if (mrb_nil_p(members)) { - mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct"); - } - if (!mrb_array_p(members)) { - mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); - } - return members; -} - -mrb_value -mrb_struct_members(mrb_state *mrb, mrb_value s) -{ - mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s))); - if (mrb_type(s) == MRB_TT_STRUCT) { - if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { - mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", - RARRAY_LEN(members), RSTRUCT_LEN(s)); - } - } - return members; -} - -static mrb_value -mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) -{ - mrb_value members, ary; - mrb_value *p, *pend; - - members = mrb_struct_s_members(mrb, klass); - ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); - p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members); - while (p < pend) { - mrb_ary_push(mrb, ary, *p); - p++; - } - - return ary; -} - -static inline void -struct_copy(mrb_value *dst, const mrb_value *src, size_t size) -{ - size_t i; - - for (i = 0; i < size; i++) { - dst[i] = src[i]; - } -} - -/* 15.2.18.4.6 */ -/* - * call-seq: - * struct.members -> array - * - * Returns an array of strings representing the names of the instance - * variables. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * joe.members #=> [:name, :address, :zip] - */ - -static mrb_value -mrb_struct_members_m(mrb_state *mrb, mrb_value obj) -{ - return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj))); -} - -mrb_value -mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) -{ - mrb_value members, slot, *ptr, *ptr_members; - long i, len; - - ptr = RSTRUCT_PTR(obj); - members = mrb_struct_members(mrb, obj); - ptr_members = RARRAY_PTR(members); - slot = mrb_symbol_value(id); - len = RARRAY_LEN(members); - for (i=0; ici->mid); -} - -static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];} -static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];} -static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];} -static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];} -static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];} -static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];} -static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];} -static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];} -static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];} -static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];} - -#define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) -#define N_REF_FUNC numberof(ref_func) - -static mrb_value (*const ref_func[])(mrb_state*, mrb_value) = { - mrb_struct_ref0, - mrb_struct_ref1, - mrb_struct_ref2, - mrb_struct_ref3, - mrb_struct_ref4, - mrb_struct_ref5, - mrb_struct_ref6, - mrb_struct_ref7, - mrb_struct_ref8, - mrb_struct_ref9, -}; - -mrb_sym -mrb_id_attrset(mrb_state *mrb, mrb_sym id) -{ - const char *name; - char *buf; - int len; - mrb_sym mid; - - name = mrb_sym2name_len(mrb, id, &len); - buf = (char *)mrb_malloc(mrb, len+2); - memcpy(buf, name, len); - buf[len] = '='; - buf[len+1] = '\0'; - - mid = mrb_intern2(mrb, buf, len+1); - mrb_free(mrb, buf); - return mid; -} - -static mrb_value -mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) -{ - const char *name; - int i, len; - mrb_sym mid; - mrb_value members, slot, *ptr, *ptr_members; - - /* get base id */ - name = mrb_sym2name_len(mrb, mrb->ci->mid, &len); - mid = mrb_intern2(mrb, name, len-1); /* omit last "=" */ - - members = mrb_struct_members(mrb, obj); - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); - ptr = RSTRUCT_PTR(obj); - for (i=0; itLAST_TOKEN) -#define is_local_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_LOCAL) -int -mrb_is_local_id(mrb_sym id) -{ - return is_local_id(id); -} - -#define is_const_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_CONST) -int -mrb_is_const_id(mrb_sym id) -{ - return is_const_id(id); -} - -static mrb_value -make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass) -{ - mrb_value nstr, *ptr_members; - mrb_sym id; - long i, len; - struct RClass *c; - - if (mrb_nil_p(name)) { - c = mrb_class_new(mrb, klass); - } - else { - /* old style: should we warn? */ - name = mrb_str_to_str(mrb, name); - id = mrb_to_id(mrb, name); - if (!mrb_is_const_id(id)) { - mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); - } - if (mrb_const_defined_at(mrb, klass, id)) { - mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); - //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); - } - c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); - } - MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT); - nstr = mrb_obj_value(c); - mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members); - - mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY()); - mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY()); - mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE()); - //RSTRUCT(nstr)->basic.c->super = c->c; - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); - for (i=0; i< len; i++) { - mrb_sym id = mrb_symbol(ptr_members[i]); - if (mrb_is_local_id(id) || mrb_is_const_id(id)) { - if (i < N_REF_FUNC) { - mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE()); - } - else { - mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE()); - } - mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1)); - } - } - - return nstr; -} - -mrb_value -mrb_struct_define(mrb_state *mrb, const char *name, ...) -{ - va_list ar; - mrb_value nm, ary; - char *mem; - - if (!name) nm = mrb_nil_value(); - else nm = mrb_str_new2(mrb, name); - ary = mrb_ary_new(mrb); - - va_start(ar, name); - while ((mem = va_arg(ar, char*)) != 0) { - mrb_sym slot = mrb_intern(mrb, mem); - mrb_ary_push(mrb, ary, mrb_symbol_value(slot)); - } - va_end(ar); - - return make_struct(mrb, nm, ary, struct_class(mrb)); -} - -/* 15.2.18.3.1 */ -/* - * call-seq: - * Struct.new( [aString] [, aSym]+> ) -> StructClass - * StructClass.new(arg, ...) -> obj - * StructClass[arg, ...] -> obj - * - * Creates a new class, named by aString, containing accessor - * methods for the given symbols. If the name aString is - * omitted, an anonymous structure class will be created. Otherwise, - * the name of this struct will appear as a constant in class - * Struct, so it must be unique for all - * Structs in the system and should start with a capital - * letter. Assigning a structure class to a constant effectively gives - * the class the name of the constant. - * - * Struct::new returns a new Class object, - * which can then be used to create specific instances of the new - * structure. The number of actual parameters must be - * less than or equal to the number of attributes defined for this - * class; unset parameters default to nil. Passing too many - * parameters will raise an ArgumentError. - * - * The remaining methods listed in this section (class and instance) - * are defined for this generated class. - * - * # Create a structure with a name in Struct - * Struct.new("Customer", :name, :address) #=> Struct::Customer - * Struct::Customer.new("Dave", "123 Main") #=> # - * - * # Create a structure named by its constant - * Customer = Struct.new(:name, :address) #=> Customer - * Customer.new("Dave", "123 Main") #=> # - */ -static mrb_value -mrb_struct_s_def(mrb_state *mrb, mrb_value klass) -{ - mrb_value name, rest; - mrb_value *pargv; - int argcnt; - long i; - mrb_value b, st; - mrb_sym id; - mrb_value *argv; - int argc; - - name = mrb_nil_value(); - rest = mrb_nil_value(); - mrb_get_args(mrb, "*&", &argv, &argc, &b); - if (argc == 0) { /* special case to avoid crash */ - rest = mrb_ary_new(mrb); - } - else { - if (argc > 0) name = argv[0]; - if (argc > 1) rest = argv[1]; - if (mrb_array_p(rest)) { - if (!mrb_nil_p(name) && mrb_symbol_p(name)) { - /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ - mrb_ary_unshift(mrb, rest, name); - name = mrb_nil_value(); - } - } - else { - pargv = &argv[1]; - argcnt = argc-1; - if (!mrb_nil_p(name) && mrb_symbol_p(name)) { - /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ - name = mrb_nil_value(); - pargv = &argv[0]; - argcnt++; - } - rest = mrb_ary_new_from_values(mrb, argcnt, pargv); - } - for (i=0; iptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); - st->len = n; - struct_copy(st->ptr, argv, argc); - - return self; -} - -static mrb_value -mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self) -{ - mrb_value *argv; - int argc; - - mrb_get_args(mrb, "*", &argv, &argc); - return mrb_struct_initialize_withArg(mrb, argc, argv, self); -} - -mrb_value -mrb_struct_initialize(mrb_state *mrb, mrb_value self, mrb_value values) -{ - return mrb_struct_initialize_withArg(mrb, RARRAY_LEN(values), RARRAY_PTR(values), self); -} - -static mrb_value -inspect_struct(mrb_state *mrb, mrb_value s, int recur) -{ - const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s)); - mrb_value members, str = mrb_str_new(mrb, "#"); - } - - members = mrb_struct_members(mrb, s); - ptr_members = RARRAY_PTR(members); - ptr = RSTRUCT_PTR(s); - len = RSTRUCT_LEN(s); - for (i=0; i 0) { - mrb_str_cat2(mrb, str, ", "); - } - else if (cn) { - mrb_str_cat2(mrb, str, " "); - } - slot = ptr_members[i]; - id = mrb_symbol(slot); - if (mrb_is_local_id(id) || mrb_is_const_id(id)) { - const char *name; - int len; - - name = mrb_sym2name_len(mrb, id, &len); - mrb_str_append(mrb, str, mrb_str_new(mrb, name, len)); - } - else { - mrb_str_append(mrb, str, mrb_inspect(mrb, slot)); - } - mrb_str_cat2(mrb, str, "="); - mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i])); - } - mrb_str_cat2(mrb, str, ">"); - - return str; -} - -/* - * call-seq: - * struct.to_s -> string - * struct.inspect -> string - * - * Describe the contents of this struct in a string. - */ -static mrb_value -mrb_struct_inspect(mrb_state *mrb, mrb_value s) -{ - return inspect_struct(mrb, s, 0); -} - -/* 15.2.18.4.9 */ -/* :nodoc: */ -mrb_value -mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) -{ - mrb_value s; - - mrb_get_args(mrb, "o", &s); - - if (mrb_obj_equal(mrb, copy, s)) return copy; - if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) { - mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); - } - if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { - mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); - } - struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy)); - - return copy; -} - -static mrb_value -mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id) -{ - mrb_value *ptr, members, *ptr_members; - long i, len; - - ptr = RSTRUCT_PTR(s); - members = mrb_struct_members(mrb, s); - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); - for (i=0; i anObject - * struct[fixnum] -> anObject - * - * Attribute Reference---Returns the value of the instance variable - * named by symbol, or indexed (0..length-1) by - * fixnum. Will raise NameError if the named - * variable does not exist, or IndexError if the index is - * out of range. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * - * joe["name"] #=> "Joe Smith" - * joe[:name] #=> "Joe Smith" - * joe[0] #=> "Joe Smith" - */ -mrb_value -mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx) -{ - long i; - - if (mrb_string_p(idx) || mrb_symbol_p(idx)) { - return mrb_struct_aref_id(mrb, s, mrb_to_id(mrb, idx)); - } - - i = mrb_fixnum(idx); - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - if (RSTRUCT_LEN(s) <= i) - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - return RSTRUCT_PTR(s)[i]; -} - -mrb_value -mrb_struct_aref(mrb_state *mrb, mrb_value s) -{ - mrb_value idx; - - mrb_get_args(mrb, "o", &idx); - return mrb_struct_aref_n(mrb, s, idx); -} - -static mrb_value -mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) -{ - mrb_value members, *ptr, *ptr_members; - long i, len; - - members = mrb_struct_members(mrb, s); - len = RARRAY_LEN(members); - if (RSTRUCT_LEN(s) != len) { - mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", - len, RSTRUCT_LEN(s)); - } - ptr = RSTRUCT_PTR(s); - ptr_members = RARRAY_PTR(members); - for (i=0; i obj - * struct[fixnum] = obj -> obj - * - * Attribute Assignment---Assigns to the instance variable named by - * symbol or fixnum the value obj and - * returns it. Will raise a NameError if the named - * variable does not exist, or an IndexError if the index - * is out of range. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * - * joe["name"] = "Luke" - * joe[:zip] = "90210" - * - * joe.name #=> "Luke" - * joe.zip #=> "90210" - */ - -mrb_value -mrb_struct_aset(mrb_state *mrb, mrb_value s) -{ - long i; - mrb_value idx; - mrb_value val; - - mrb_get_args(mrb, "oo", &idx, &val); - - if (mrb_string_p(idx) || mrb_symbol_p(idx)) { - return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); - } - - i = mrb_fixnum(idx); - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) { - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - } - if (RSTRUCT_LEN(s) <= i) { - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - } - return RSTRUCT_PTR(s)[i] = val; -} - -/* 15.2.18.4.1 */ -/* - * call-seq: - * struct == other_struct -> true or false - * - * Equality---Returns true if other_struct is - * equal to this one: they must be of the same class as generated by - * Struct::new, and the values of all instance variables - * must be equal (according to Object#==). - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345) - * joe == joejr #=> true - * joe == jane #=> false - */ - -static mrb_value -mrb_struct_equal(mrb_state *mrb, mrb_value s) -{ - mrb_value s2; - mrb_value *ptr, *ptr2; - long i, len; - - mrb_get_args(mrb, "o", &s2); - if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); - if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); - if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); - if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { - mrb_bug("inconsistent struct"); /* should never happen */ - } - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - for (i=0; i true or false - * - * Two structures are equal if they are the same object, or if all their - * fields are equal (using eql?). - */ -static mrb_value -mrb_struct_eql(mrb_state *mrb, mrb_value s) -{ - mrb_value s2; - mrb_value *ptr, *ptr2; - long i, len; - - mrb_get_args(mrb, "o", &s2); - if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); - if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); - if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); - if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { - mrb_bug("inconsistent struct"); /* should never happen */ - } - - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - for (i=0; iStruct is a convenient way to bundle a number of - * attributes together, using accessor methods, without having to write - * an explicit class. - * - * The Struct class is a generator of specific classes, - * each one of which is defined to hold a set of variables and their - * accessors. In these examples, we'll call the generated class - * ``CustomerClass,'' and we'll show an example instance of that - * class as ``CustomerInst.'' - * - * In the descriptions that follow, the parameter symbol refers - * to a symbol, which is either a quoted string or a - * Symbol (such as :name). - */ -void -mrb_init_struct(mrb_state *mrb) -{ - struct RClass *st; - st = mrb_define_class(mrb, "Struct", mrb->object_class); - - mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, ARGS_ANY()); /* 15.2.18.3.1 */ - - mrb_define_method(mrb, st, "==", mrb_struct_equal, ARGS_REQ(1)); /* 15.2.18.4.1 */ - mrb_define_method(mrb, st, "[]", mrb_struct_aref, ARGS_REQ(1)); /* 15.2.18.4.2 */ - mrb_define_method(mrb, st, "[]=", mrb_struct_aset, ARGS_REQ(2)); /* 15.2.18.4.3 */ - mrb_define_method(mrb, st, "members", mrb_struct_members_m, ARGS_NONE()); /* 15.2.18.4.6 */ - mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,ARGS_ANY()); /* 15.2.18.4.8 */ - mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, ARGS_REQ(1)); /* 15.2.18.4.9 */ - mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, ARGS_NONE()); /* 15.2.18.4.10(x) */ - mrb_define_alias(mrb, st, "to_s", "inspect"); /* 15.2.18.4.11(x) */ - mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */ - -} -#endif /* ENABLE_STRUCT */ diff --git a/test/t/struct.rb b/test/t/struct.rb deleted file mode 100644 index d79b30c0e..000000000 --- a/test/t/struct.rb +++ /dev/null @@ -1,77 +0,0 @@ -## -# Struct ISO Test - -if Object.const_defined?(:Struct) - assert('Struct', '15.2.18') do - Struct.class == Class - end - - assert('Struct superclass', '15.2.18.2') do - Struct.superclass == Object - end - - assert('Struct.new', '15.2.18.3.1') do - c = Struct.new(:m1, :m2) - c.superclass == Struct and - c.members == [:m1,:m2] - end - - # Check crash bug with Struc.new and no params. - assert('Struct.new', '15.2.18.3.1') do - c = Struct.new() - c.superclass == Struct and c.members == [] - end - - assert('Struct#==', '15.2.18.4.1') do - c = Struct.new(:m1, :m2) - cc1 = c.new(1,2) - cc2 = c.new(1,2) - cc1 == cc2 - end - - assert('Struct#[]', '15.2.18.4.2') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc[:m1] == 1 and cc["m2"] == 2 - end - - assert('Struct#[]=', '15.2.18.4.3') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc[:m1] = 3 - cc[:m1] == 3 - end - - assert('Struct#each', '15.2.18.4.4') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - a = [] - cc.each{|x| - a << x - } - a[0] == 1 and a[1] == 2 - end - - assert('Struct#each_pair', '15.2.18.4.5') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - a = [] - cc.each_pair{|k,v| - a << [k,v] - } - a[0] == [:m1, 1] and a[1] == [:m2, 2] - end - - assert('Struct#members', '15.2.18.4.6') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc.members == [:m1,:m2] - end - - assert('Struct#select', '15.2.18.4.7') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc.select{|v| v % 2 == 0} == [2] - end -end - -- cgit v1.2.3