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(-) 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 1f692ab0ba1def0291bf63cd8c295180d8fcc0e3 Mon Sep 17 00:00:00 2001 From: "Xuejie \"Rafael\" Xiao" Date: Wed, 27 Feb 2013 21:29:49 -0500 Subject: Do not generate test_args_hash variable if it is not used --- tasks/mrbgems_test.rake | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tasks/mrbgems_test.rake b/tasks/mrbgems_test.rake index 6d40d092a..6ca5eaef2 100644 --- a/tasks/mrbgems_test.rake +++ b/tasks/mrbgems_test.rake @@ -18,7 +18,11 @@ MRuby.each_target do f.puts %Q[void GENERATED_TMP_mrb_#{g.funcname}_gem_test(mrb_state *mrb) {] unless g.test_rbfiles.empty? f.puts %Q[ mrb_state *mrb2;] - f.puts %Q[ mrb_value val1, val2, ary1, ary2, test_args_hash;] + if g.test_args.empty? + f.puts %Q[ mrb_value val1, val2, ary1, ary2;] + else + f.puts %Q[ mrb_value val1, val2, ary1, ary2, test_args_hash;] + end f.puts %Q[ int ai;] g.test_rbfiles.count.times do |i| f.puts %Q[ ai = mrb_gc_arena_save(mrb);] -- cgit v1.2.3 From f4485ee1eb6c8f586ca5313b1085e4f354db19d1 Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Thu, 28 Feb 2013 11:55:22 +0900 Subject: s/mgem/mrbgem/ --- build_config.rb | 2 +- mgems/mruby-time/mrbgem.rake | 4 - mgems/mruby-time/src/time.c | 755 ---------------------------------------- mgems/mruby-time/test/time.rb | 201 ----------- mrbgems/mruby-time/mrbgem.rake | 4 + mrbgems/mruby-time/src/time.c | 755 ++++++++++++++++++++++++++++++++++++++++ mrbgems/mruby-time/test/time.rb | 201 +++++++++++ 7 files changed, 961 insertions(+), 961 deletions(-) delete mode 100644 mgems/mruby-time/mrbgem.rake delete mode 100644 mgems/mruby-time/src/time.c delete mode 100644 mgems/mruby-time/test/time.rb create mode 100644 mrbgems/mruby-time/mrbgem.rake create mode 100644 mrbgems/mruby-time/src/time.c create mode 100644 mrbgems/mruby-time/test/time.rb diff --git a/build_config.rb b/build_config.rb index a66dab6ac..a296ce705 100644 --- a/build_config.rb +++ b/build_config.rb @@ -12,7 +12,7 @@ MRuby::Build.new do |conf| # conf.gem :git => 'git@github.com:masuidrive/mrbgems-example.git', :branch => 'master', :options => '-v' # Use standard Time class - conf.gem 'mgems/mruby-time' + conf.gem 'mrbgems/mruby-time' # Generate binaries # conf.bins = %w(mrbc mruby mirb) diff --git a/mgems/mruby-time/mrbgem.rake b/mgems/mruby-time/mrbgem.rake deleted file mode 100644 index 2cd3fd354..000000000 --- a/mgems/mruby-time/mrbgem.rake +++ /dev/null @@ -1,4 +0,0 @@ -MRuby::Gem::Specification.new('mruby-time') do |spec| - spec.license = 'MIT' - spec.authors = 'mruby team' -end diff --git a/mgems/mruby-time/src/time.c b/mgems/mruby-time/src/time.c deleted file mode 100644 index ed40c5279..000000000 --- a/mgems/mruby-time/src/time.c +++ /dev/null @@ -1,755 +0,0 @@ -/* -** time.c - Time class -** -** See Copyright Notice in mruby.h -*/ - - -#include "mruby.h" -#include -#include -#include -#include "mruby/class.h" -#include "mruby/data.h" - -/** Time class configuration */ - -/* gettimeofday(2) */ -/* C99 does not have gettimeofday that is required to retrieve microseconds */ -/* uncomment following macro on platforms without gettimeofday(2) */ -/* #define NO_GETTIMEOFDAY */ - -/* gmtime(3) */ -/* C99 does not have reentrant gmtime_r() so it might cause troubles under */ -/* multi-threading environment. undef following macro on platforms that */ -/* does not have gmtime_r() and localtime_r(). */ -/* #define NO_GMTIME_R */ - -#ifdef _WIN32 -#if _MSC_VER -/* Win32 platform do not provide gmtime_r/localtime_r; emulate them using gmtime_s/localtime_s */ -#define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL) -#define localtime_r(tp, tm) ((localtime_s((tm), (tp)) == 0) ? (tm) : NULL) -#else -#define NO_GMTIME_R -#endif -#endif - -/* timegm(3) */ -/* mktime() creates tm structure for localtime; timegm() is for UTF time */ -/* define following macro to use probably faster timegm() on the platform */ -/* #define USE_SYSTEM_TIMEGM */ - -/** end of Time class configuration */ - -#ifndef NO_GETTIMEOFDAY -#include -#endif -#ifdef NO_GMTIME_R -#define gmtime_r(t,r) gmtime(t) -#define localtime_r(t,r) (tzset(),localtime(t)) -#endif - -#ifndef USE_SYSTEM_TIMEGM -#define timegm my_timgm - -static unsigned int -is_leapyear(unsigned int y) -{ - return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0); -} - -static time_t -timegm(struct tm *tm) -{ - static const unsigned int ndays[2][12] = { - {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, - {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} - }; - time_t r = 0; - int i; - unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)]; - - for (i = 70; i < tm->tm_year; ++i) - r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60; - for (i = 0; i < tm->tm_mon; ++i) - r += nday[i] * 24 * 60 * 60; - r += (tm->tm_mday - 1) * 24 * 60 * 60; - r += tm->tm_hour * 60 * 60; - r += tm->tm_min * 60; - r += tm->tm_sec; - return r; -} -#endif - -/* Since we are limited to using ISO C89, this implementation is based -* on time_t. That means the resolution of time is only precise to the -* second level. Also, there are only 2 timezones, namely UTC and LOCAL. -*/ - -#ifndef mrb_bool_value -#define mrb_bool_value(val) ((val) ? mrb_true_value() : mrb_false_value()) -#endif - - -enum mrb_timezone { - MRB_TIMEZONE_NONE = 0, - MRB_TIMEZONE_UTC = 1, - MRB_TIMEZONE_LOCAL = 2, - MRB_TIMEZONE_LAST = 3 -}; - -static const char *timezone_names[] = { - "none", - "UTC", - "LOCAL", - NULL -}; - -static const char *mon_names[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", -}; - -static const char *wday_names[] = { - "Sun", "Mon", "Tus", "Wed", "Thu", "Fri", "Sat", -}; - -struct mrb_time { - time_t sec; - time_t usec; - enum mrb_timezone timezone; - struct tm datetime; -}; - -static void -mrb_time_free(mrb_state *mrb, void *ptr) -{ - mrb_free(mrb, ptr); -} - -static struct mrb_data_type mrb_time_type = { "Time", mrb_time_free }; - -/** Updates the datetime of a mrb_time based on it's timezone and -seconds setting. Returns self on cussess, NULL of failure. */ -static struct mrb_time* -mrb_time_update_datetime(struct mrb_time *self) -{ - struct tm *aid; - - if (self->timezone == MRB_TIMEZONE_UTC) { - aid = gmtime_r(&self->sec, &self->datetime); - } - else { - aid = localtime_r(&self->sec, &self->datetime); - } - if (!aid) return NULL; -#ifdef NO_GMTIME_R - self->datetime = *aid; // copy data -#endif - - return self; -} - -static mrb_value -mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm) -{ - return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm)); -} - - -/* Allocates a mrb_time object and initializes it. */ -static struct mrb_time* -mrb_time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time)); - tm->sec = (time_t)sec; - tm->usec = (sec - tm->sec) * 1.0e6 + usec; - while (tm->usec < 0) { - tm->sec--; - tm->usec += 1.0e6; - } - while (tm->usec > 1.0e6) { - tm->sec++; - tm->usec -= 1.0e6; - } - tm->timezone = timezone; - mrb_time_update_datetime(tm); - - return tm; -} - -static mrb_value -mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mrb_timezone timezone) -{ - return mrb_time_wrap(mrb, c, mrb_time_alloc(mrb, sec, usec, timezone)); -} - -static struct mrb_time* -current_mrb_time(mrb_state *mrb) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); -#ifdef NO_GETTIMEOFDAY - { - static time_t last_sec = 0, last_usec = 0; - - tm->sec = time(NULL); - if (tm->sec != last_sec) { - last_sec = tm->sec; - last_usec = 0; - } - else { - /* add 1 usec to differentiate two times */ - last_usec += 1; - } - tm->usec = last_usec; - } -#else - { - struct timeval tv; - - gettimeofday(&tv, NULL); - tm->sec = tv.tv_sec; - tm->usec = tv.tv_usec; - } -#endif - tm->timezone = MRB_TIMEZONE_LOCAL; - mrb_time_update_datetime(tm); - - return tm; -} - -/* Allocates a new Time object with given millis value. */ -static mrb_value -mrb_time_now(mrb_state *mrb, mrb_value self) -{ - return mrb_time_wrap(mrb, mrb_class_ptr(self), current_mrb_time(mrb)); -} - -/* 15.2.19.6.1 */ -/* Creates an instance of time at the given time in seconds, etc. */ -static mrb_value -mrb_time_at(mrb_state *mrb, mrb_value self) -{ - mrb_float f, f2 = 0; - - mrb_get_args(mrb, "f|f", &f, &f2); - return mrb_time_make(mrb, mrb_class_ptr(self), f, f2, MRB_TIMEZONE_LOCAL); -} - -static struct mrb_time* -time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday, - mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec, - enum mrb_timezone timezone) -{ - time_t nowsecs; - struct tm nowtime = { 0 }; - - nowtime.tm_year = (int)ayear - 1900; - nowtime.tm_mon = (int)amonth - 1; - nowtime.tm_mday = (int)aday; - nowtime.tm_hour = (int)ahour; - nowtime.tm_min = (int)amin; - nowtime.tm_sec = (int)asec; - nowtime.tm_isdst = -1; - if (timezone == MRB_TIMEZONE_UTC) { - nowsecs = timegm(&nowtime); - } - else { - nowsecs = mktime(&nowtime); - } - if (nowsecs < 0) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time."); - } - - return mrb_time_alloc(mrb, nowsecs, ausec, timezone); -} - -/* 15.2.19.6.2 */ -/* Creates an instance of time at the given time in UTC. */ -static mrb_value -mrb_time_gm(mrb_state *mrb, mrb_value self) -{ - mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0; - - mrb_get_args(mrb, "i|iiiiii", - &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); - return mrb_time_wrap(mrb, mrb_class_ptr(self), - time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_UTC)); -} - - -/* 15.2.19.6.3 */ -/* Creates an instance of time at the given time in local time zone. */ -static mrb_value -mrb_time_local(mrb_state *mrb, mrb_value self) -{ - mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0; - - mrb_get_args(mrb, "i|iiiiii", - &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); - return mrb_time_wrap(mrb, mrb_class_ptr(self), - time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL)); -} - - -static mrb_value -mrb_time_eq(mrb_state *mrb, mrb_value self) -{ - mrb_value other; - struct mrb_time *tm1, *tm2; - - mrb_get_args(mrb, "o", &other); - tm1 = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type); - if (!tm1 || !tm2) return mrb_false_value(); - if (tm1->sec == tm2->sec && tm1->usec == tm2->usec) { - return mrb_true_value(); - } - return mrb_false_value(); -} - -static mrb_value -mrb_time_cmp(mrb_state *mrb, mrb_value self) -{ - mrb_value other; - struct mrb_time *tm1, *tm2; - - mrb_get_args(mrb, "o", &other); - tm1 = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type); - if (!tm1 || !tm2) return mrb_nil_value(); - if (tm1->sec > tm2->sec) { - return mrb_fixnum_value(1); - } - else if (tm1->sec < tm2->sec) { - return mrb_fixnum_value(-1); - } - /* tm1->sec == tm2->sec */ - if (tm1->usec > tm2->usec) { - return mrb_fixnum_value(1); - } - else if (tm1->usec < tm2->usec) { - return mrb_fixnum_value(-1); - } - return mrb_fixnum_value(0); -} - -static mrb_value -mrb_time_plus(mrb_state *mrb, mrb_value self) -{ - mrb_float f; - struct mrb_time *tm; - - mrb_get_args(mrb, "f", &f); - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, tm->usec, tm->timezone); -} - -static mrb_value -mrb_time_minus(mrb_state *mrb, mrb_value self) -{ - mrb_float f; - mrb_value other; - struct mrb_time *tm, *tm2; - - mrb_get_args(mrb, "o", &other); - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - - tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type); - if (tm2) { - f = (mrb_float)(tm->sec - tm2->sec) - + (mrb_float)(tm->usec - tm2->usec) / 1.0e6; - return mrb_float_value(f); - } - else { - mrb_get_args(mrb, "f", &f); - return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec-f, tm->usec, tm->timezone); - } -} - -/* 15.2.19.7.30 */ -/* Returns week day number of time. */ -static mrb_value -mrb_time_wday(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_wday); -} - -/* 15.2.19.7.31 */ -/* Returns year day number of time. */ -static mrb_value -mrb_time_yday(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_check_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_yday + 1); -} - -/* 15.2.19.7.32 */ -/* Returns year of time. */ -static mrb_value -mrb_time_year(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_year + 1900); -} - -/* 15.2.19.7.33 */ -/* Returns name of time's timezone. */ -static mrb_value -mrb_time_zone(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value(); - if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value(); - return mrb_str_new_cstr(mrb, timezone_names[tm->timezone]); -} - -/* 15.2.19.7.4 */ -/* Returns a string that describes the time. */ -static mrb_value -mrb_time_asctime(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - struct tm *d; - char buf[256]; - int len; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - d = &tm->datetime; - len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d", - wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday, - d->tm_hour, d->tm_min, d->tm_sec, - tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "", - d->tm_year + 1900); - return mrb_str_new(mrb, buf, len); -} - -/* 15.2.19.7.6 */ -/* Returns the day in the month of the time. */ -static mrb_value -mrb_time_day(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_mday); -} - - -/* 15.2.19.7.7 */ -/* Returns true if daylight saving was applied for this time. */ -static mrb_value -mrb_time_dstp(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_bool_value(tm->datetime.tm_isdst); -} - -/* 15.2.19.7.8 */ -/* 15.2.19.7.10 */ -/* Returns the Time object of the UTC(GMT) timezone. */ -static mrb_value -mrb_time_getutc(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm, *tm2; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return self; - tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); - *tm2 = *tm; - tm2->timezone = MRB_TIMEZONE_UTC; - mrb_time_update_datetime(tm2); - return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2); -} - -/* 15.2.19.7.9 */ -/* Returns the Time object of the LOCAL timezone. */ -static mrb_value -mrb_time_getlocal(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm, *tm2; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return self; - tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); - *tm2 = *tm; - tm2->timezone = MRB_TIMEZONE_LOCAL; - mrb_time_update_datetime(tm2); - return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2); -} - -/* 15.2.19.7.15 */ -/* Returns hour of time. */ -static mrb_value -mrb_time_hour(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_hour); -} - -/* 15.2.19.7.16 */ -/* Initializes a time by setting the amount of milliseconds since the epoch.*/ -static mrb_value -mrb_time_initialize(mrb_state *mrb, mrb_value self) -{ - mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, - amin = 0, asec = 0, ausec = 0; - int n; - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (tm) { - mrb_time_free(mrb, tm); - } - - n = mrb_get_args(mrb, "|iiiiiii", - &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); - if (n == 0) { - tm = current_mrb_time(mrb); - } - else { - tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL); - } - DATA_PTR(self) = tm; - DATA_TYPE(self) = &mrb_time_type; - return self; -} - -/* 15.2.19.7.17(x) */ -/* Initializes a copy of this time object. */ -static mrb_value -mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy) -{ - mrb_value src; - - mrb_get_args(mrb, "o", &src); - if (mrb_obj_equal(mrb, copy, src)) return copy; - if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) { - mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); - } - if (!DATA_PTR(copy)) { - DATA_PTR(copy) = mrb_malloc(mrb, sizeof(struct mrb_time)); - DATA_TYPE(copy) = &mrb_time_type; - } - *(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src); - return copy; -} - -/* 15.2.19.7.18 */ -/* Sets the timezone attribute of the Time object to LOCAL. */ -static mrb_value -mrb_time_localtime(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return self; - tm->timezone = MRB_TIMEZONE_LOCAL; - mrb_time_update_datetime(tm); - return self; -} - -/* 15.2.19.7.19 */ -/* Returns day of month of time. */ -static mrb_value -mrb_time_mday(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_mday); -} - -/* 15.2.19.7.20 */ -/* Returns minutes of time. */ -static mrb_value -mrb_time_min(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_min); -} - -/* 15.2.19.7.21 and 15.2.19.7.22 */ -/* Returns month of time. */ -static mrb_value -mrb_time_mon(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_mon + 1); -} - -/* 15.2.19.7.23 */ -/* Returns seconds in minute of time. */ -static mrb_value -mrb_time_sec(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->datetime.tm_sec); -} - - -/* 15.2.19.7.24 */ -/* Returns a Float with the time since the epoch in seconds. */ -static mrb_value -mrb_time_to_f(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_float_value((mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6); -} - -/* 15.2.19.7.25 */ -/* Returns a Fixnum with the time since the epoch in seconds. */ -static mrb_value -mrb_time_to_i(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->sec); -} - -/* 15.2.19.7.26 */ -/* Returns a Float with the time since the epoch in microseconds. */ -static mrb_value -mrb_time_usec(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_fixnum_value(tm->usec); -} - -/* 15.2.19.7.27 */ -/* Sets the timzeone attribute of the Time object to UTC. */ -static mrb_value -mrb_time_utc(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (tm) { - tm->timezone = MRB_TIMEZONE_UTC; - mrb_time_update_datetime(tm); - } - return self; -} - -/* 15.2.19.7.28 */ -/* Returns true if this time is in the UTC timze zone false if not. */ -static mrb_value -mrb_time_utcp(mrb_state *mrb, mrb_value self) -{ - struct mrb_time *tm; - tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); - if (!tm) return mrb_nil_value(); - return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC); -} - - - -void -mrb_mruby_time_gem_init(mrb_state* mrb) -{ - struct RClass *tc; - /* ISO 15.2.19.2 */ - tc = mrb_define_class(mrb, "Time", mrb->object_class); - MRB_SET_INSTANCE_TT(tc, MRB_TT_DATA); - mrb_include_module(mrb, tc, mrb_class_get(mrb, "Comparable")); - mrb_define_class_method(mrb, tc, "at", mrb_time_at, ARGS_ANY()); /* 15.2.19.6.1 */ - mrb_define_class_method(mrb, tc, "gm", mrb_time_gm, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.2 */ - mrb_define_class_method(mrb, tc, "local", mrb_time_local, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.3 */ - mrb_define_class_method(mrb, tc, "mktime", mrb_time_local, ARGS_REQ(1)|ARGS_OPT(6));/* 15.2.19.6.4 */ - mrb_define_class_method(mrb, tc, "now", mrb_time_now, ARGS_NONE()); /* 15.2.19.6.5 */ - mrb_define_class_method(mrb, tc, "utc", mrb_time_gm, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.6 */ - - mrb_define_method(mrb, tc, "==" , mrb_time_eq , ARGS_REQ(1)); - mrb_define_method(mrb, tc, "<=>" , mrb_time_cmp , ARGS_REQ(1)); /* 15.2.19.7.1 */ - mrb_define_method(mrb, tc, "+" , mrb_time_plus , ARGS_REQ(1)); /* 15.2.19.7.2 */ - mrb_define_method(mrb, tc, "-" , mrb_time_minus , ARGS_REQ(1)); /* 15.2.19.7.3 */ - mrb_define_method(mrb, tc, "to_s" , mrb_time_asctime, ARGS_NONE()); - mrb_define_method(mrb, tc, "inspect", mrb_time_asctime, ARGS_NONE()); - mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, ARGS_NONE()); /* 15.2.19.7.4 */ - mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, ARGS_NONE()); /* 15.2.19.7.5 */ - mrb_define_method(mrb, tc, "day" , mrb_time_day , ARGS_NONE()); /* 15.2.19.7.6 */ - mrb_define_method(mrb, tc, "dst?" , mrb_time_dstp , ARGS_NONE()); /* 15.2.19.7.7 */ - mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , ARGS_NONE()); /* 15.2.19.7.8 */ - mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,ARGS_NONE()); /* 15.2.19.7.9 */ - mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , ARGS_NONE()); /* 15.2.19.7.10 */ - mrb_define_method(mrb, tc, "gmt?" , mrb_time_utcp , ARGS_NONE()); /* 15.2.19.7.11 */ - mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , ARGS_NONE()); /* 15.2.19.7.13 */ - mrb_define_method(mrb, tc, "hour" , mrb_time_hour, ARGS_NONE()); /* 15.2.19.7.15 */ - mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, ARGS_NONE()); /* 15.2.19.7.18 */ - mrb_define_method(mrb, tc, "mday" , mrb_time_mday, ARGS_NONE()); /* 15.2.19.7.19 */ - mrb_define_method(mrb, tc, "min" , mrb_time_min, ARGS_NONE()); /* 15.2.19.7.20 */ - - mrb_define_method(mrb, tc, "mon" , mrb_time_mon, ARGS_NONE()); /* 15.2.19.7.21 */ - mrb_define_method(mrb, tc, "month", mrb_time_mon, ARGS_NONE()); /* 15.2.19.7.22 */ - - mrb_define_method(mrb, tc, "sec" , mrb_time_sec, ARGS_NONE()); /* 15.2.19.7.23 */ - mrb_define_method(mrb, tc, "to_i", mrb_time_to_i, ARGS_NONE()); /* 15.2.19.7.25 */ - mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, ARGS_NONE()); /* 15.2.19.7.24 */ - mrb_define_method(mrb, tc, "usec", mrb_time_usec, ARGS_NONE()); /* 15.2.19.7.26 */ - mrb_define_method(mrb, tc, "utc" , mrb_time_utc, ARGS_NONE()); /* 15.2.19.7.27 */ - mrb_define_method(mrb, tc, "utc?", mrb_time_utcp, ARGS_NONE()); /* 15.2.19.7.28 */ - mrb_define_method(mrb, tc, "wday", mrb_time_wday, ARGS_NONE()); /* 15.2.19.7.30 */ - mrb_define_method(mrb, tc, "yday", mrb_time_yday, ARGS_NONE()); /* 15.2.19.7.31 */ - mrb_define_method(mrb, tc, "year", mrb_time_year, ARGS_NONE()); /* 15.2.19.7.32 */ - mrb_define_method(mrb, tc, "zone", mrb_time_zone, ARGS_NONE()); /* 15.2.19.7.33 */ - - mrb_define_method(mrb, tc, "initialize", mrb_time_initialize, ARGS_REQ(1)); /* 15.2.19.7.16 */ - mrb_define_method(mrb, tc, "initialize_copy", mrb_time_initialize_copy, ARGS_REQ(1)); /* 15.2.19.7.17 */ - - /* - methods not available: - gmt_offset(15.2.19.7.12) - gmtoff(15.2.19.7.14) - utc_offset(15.2.19.7.29) - */ -} - -void -mrb_mruby_time_gem_final(mrb_state* mrb) -{ -} diff --git a/mgems/mruby-time/test/time.rb b/mgems/mruby-time/test/time.rb deleted file mode 100644 index f92459d5e..000000000 --- a/mgems/mruby-time/test/time.rb +++ /dev/null @@ -1,201 +0,0 @@ -## -# Time ISO Test - -if Object.const_defined?(:Time) - assert('Time.new', '15.2.3.3.3') do - Time.new.class == Time - end - - assert('Time', '15.2.19') do - Time.class == Class - end - - assert('Time superclass', '15.2.19.2') do - Time.superclass == Object - end - - assert('Time.at', '15.2.19.6.1') do - Time.at(1300000000.0) - end - - assert('Time.gm', '15.2.19.6.2') do - Time.gm(2012, 12, 23) - end - - assert('Time.local', '15.2.19.6.3') do - Time.local(2012, 12, 23) - end - - assert('Time.mktime', '15.2.19.6.4') do - Time.mktime(2012, 12, 23) - end - - assert('Time.now', '15.2.19.6.5') do - Time.now.class == Time - end - - assert('Time.utc', '15.2.19.6.6') do - Time.utc(2012, 12, 23) - end - - assert('Time#+', '15.2.19.7.1') do - t1 = Time.at(1300000000.0) - t2 = t1.+(60) - - t2.utc.asctime == "Sun Mar 13 07:07:40 UTC 2011" - end - - assert('Time#-', '15.2.19.7.2') do - t1 = Time.at(1300000000.0) - t2 = t1.-(60) - - t2.utc.asctime == "Sun Mar 13 07:05:40 UTC 2011" - end - - assert('Time#<=>', '15.2.19.7.3') do - t1 = Time.at(1300000000.0) - t2 = Time.at(1400000000.0) - t3 = Time.at(1500000000.0) - - t2.<=>(t1) == 1 and - t2.<=>(t2) == 0 and - t2.<=>(t3) == -1 and - t2.<=>(nil) == nil - end - - assert('Time#asctime', '15.2.19.7.4') do - Time.at(1300000000.0).utc.asctime == "Sun Mar 13 07:06:40 UTC 2011" - end - - assert('Time#ctime', '15.2.19.7.5') do - Time.at(1300000000.0).utc.ctime == "Sun Mar 13 07:06:40 UTC 2011" - end - - assert('Time#day', '15.2.19.7.6') do - Time.gm(2012, 12, 23).day == 23 - end - - assert('Time#dst?', '15.2.19.7.7') do - not Time.gm(2012, 12, 23).utc.dst? - end - - assert('Time#getgm', '15.2.19.7.8') do - Time.at(1300000000.0).getgm.asctime == "Sun Mar 13 07:06:40 UTC 2011" - end - - assert('Time#getlocal', '15.2.19.7.9') do - t1 = Time.at(1300000000.0) - t2 = Time.at(1300000000.0) - t3 = t1.getlocal - - t1 == t3 and t3 == t2.getlocal - end - - assert('Time#getutc', '15.2.19.7.10') do - Time.at(1300000000.0).getutc.asctime == "Sun Mar 13 07:06:40 UTC 2011" - end - - assert('Time#gmt?', '15.2.19.7.11') do - Time.at(1300000000.0).utc.gmt? - end - - # ATM not implemented - # assert('Time#gmt_offset', '15.2.19.7.12') do - - assert('Time#gmtime', '15.2.19.7.13') do - Time.at(1300000000.0).gmtime - end - - # ATM not implemented - # assert('Time#gmtoff', '15.2.19.7.14') do - - assert('Time#hour', '15.2.19.7.15') do - Time.gm(2012, 12, 23, 7, 6).hour == 7 - end - - # ATM doesn't really work - # assert('Time#initialize', '15.2.19.7.16') do - - assert('Time#initialize_copy', '15.2.19.7.17') do - time_tmp_2 = Time.at(7.0e6) - time_tmp_2.clone == time_tmp_2 - end - - assert('Time#localtime', '15.2.19.7.18') do - t1 = Time.at(1300000000.0) - t2 = Time.at(1300000000.0) - - t1.localtime - t1 == t2.getlocal - end - - assert('Time#mday', '15.2.19.7.19') do - Time.gm(2012, 12, 23).mday == 23 - end - - assert('Time#min', '15.2.19.7.20') do - Time.gm(2012, 12, 23, 7, 6).min == 6 - end - - assert('Time#mon', '15.2.19.7.21') do - Time.gm(2012, 12, 23).mon == 12 - end - - assert('Time#month', '15.2.19.7.22') do - Time.gm(2012, 12, 23).month == 12 - end - - assert('Times#sec', '15.2.19.7.23') do - Time.gm(2012, 12, 23, 7, 6, 40).sec == 40 - end - - assert('Time#to_f', '15.2.19.7.24') do - Time.at(1300000000.0).to_f == 1300000000.0 - end - - assert('Time#to_i', '15.2.19.7.25') do - Time.at(1300000000.0).to_i == 1300000000 - end - - assert('Time#usec', '15.2.19.7.26') do - Time.at(1300000000.0).usec == 0 - end - - assert('Time#utc', '15.2.19.7.27') do - Time.at(1300000000.0).utc - end - - assert('Time#utc?', '15.2.19.7.28') do - Time.at(1300000000.0).utc.utc? - end - - # ATM not implemented - # assert('Time#utc_offset', '15.2.19.7.29') do - - assert('Time#wday', '15.2.19.7.30') do - Time.gm(2012, 12, 23).wday == 0 - end - - assert('Time#yday', '15.2.19.7.31') do - Time.gm(2012, 12, 23).yday == 358 - end - - assert('Time#year', '15.2.19.7.32') do - Time.gm(2012, 12, 23).year == 2012 - end - - assert('Time#zone', '15.2.19.7.33') do - Time.at(1300000000.0).utc.zone == 'UTC' - end - - # Not ISO specified - - assert('Time#to_s') do - Time.at(1300000000.0).utc.to_s == "Sun Mar 13 07:06:40 UTC 2011" - end - - assert('Time#inspect') do - Time.at(1300000000.0).utc.inspect == "Sun Mar 13 07:06:40 UTC 2011" - end -end - diff --git a/mrbgems/mruby-time/mrbgem.rake b/mrbgems/mruby-time/mrbgem.rake new file mode 100644 index 000000000..0f0b4899d --- /dev/null +++ b/mrbgems/mruby-time/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-time') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c new file mode 100644 index 000000000..ed40c5279 --- /dev/null +++ b/mrbgems/mruby-time/src/time.c @@ -0,0 +1,755 @@ +/* +** time.c - Time class +** +** See Copyright Notice in mruby.h +*/ + + +#include "mruby.h" +#include +#include +#include +#include "mruby/class.h" +#include "mruby/data.h" + +/** Time class configuration */ + +/* gettimeofday(2) */ +/* C99 does not have gettimeofday that is required to retrieve microseconds */ +/* uncomment following macro on platforms without gettimeofday(2) */ +/* #define NO_GETTIMEOFDAY */ + +/* gmtime(3) */ +/* C99 does not have reentrant gmtime_r() so it might cause troubles under */ +/* multi-threading environment. undef following macro on platforms that */ +/* does not have gmtime_r() and localtime_r(). */ +/* #define NO_GMTIME_R */ + +#ifdef _WIN32 +#if _MSC_VER +/* Win32 platform do not provide gmtime_r/localtime_r; emulate them using gmtime_s/localtime_s */ +#define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL) +#define localtime_r(tp, tm) ((localtime_s((tm), (tp)) == 0) ? (tm) : NULL) +#else +#define NO_GMTIME_R +#endif +#endif + +/* timegm(3) */ +/* mktime() creates tm structure for localtime; timegm() is for UTF time */ +/* define following macro to use probably faster timegm() on the platform */ +/* #define USE_SYSTEM_TIMEGM */ + +/** end of Time class configuration */ + +#ifndef NO_GETTIMEOFDAY +#include +#endif +#ifdef NO_GMTIME_R +#define gmtime_r(t,r) gmtime(t) +#define localtime_r(t,r) (tzset(),localtime(t)) +#endif + +#ifndef USE_SYSTEM_TIMEGM +#define timegm my_timgm + +static unsigned int +is_leapyear(unsigned int y) +{ + return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0); +} + +static time_t +timegm(struct tm *tm) +{ + static const unsigned int ndays[2][12] = { + {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, + {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} + }; + time_t r = 0; + int i; + unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)]; + + for (i = 70; i < tm->tm_year; ++i) + r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60; + for (i = 0; i < tm->tm_mon; ++i) + r += nday[i] * 24 * 60 * 60; + r += (tm->tm_mday - 1) * 24 * 60 * 60; + r += tm->tm_hour * 60 * 60; + r += tm->tm_min * 60; + r += tm->tm_sec; + return r; +} +#endif + +/* Since we are limited to using ISO C89, this implementation is based +* on time_t. That means the resolution of time is only precise to the +* second level. Also, there are only 2 timezones, namely UTC and LOCAL. +*/ + +#ifndef mrb_bool_value +#define mrb_bool_value(val) ((val) ? mrb_true_value() : mrb_false_value()) +#endif + + +enum mrb_timezone { + MRB_TIMEZONE_NONE = 0, + MRB_TIMEZONE_UTC = 1, + MRB_TIMEZONE_LOCAL = 2, + MRB_TIMEZONE_LAST = 3 +}; + +static const char *timezone_names[] = { + "none", + "UTC", + "LOCAL", + NULL +}; + +static const char *mon_names[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", +}; + +static const char *wday_names[] = { + "Sun", "Mon", "Tus", "Wed", "Thu", "Fri", "Sat", +}; + +struct mrb_time { + time_t sec; + time_t usec; + enum mrb_timezone timezone; + struct tm datetime; +}; + +static void +mrb_time_free(mrb_state *mrb, void *ptr) +{ + mrb_free(mrb, ptr); +} + +static struct mrb_data_type mrb_time_type = { "Time", mrb_time_free }; + +/** Updates the datetime of a mrb_time based on it's timezone and +seconds setting. Returns self on cussess, NULL of failure. */ +static struct mrb_time* +mrb_time_update_datetime(struct mrb_time *self) +{ + struct tm *aid; + + if (self->timezone == MRB_TIMEZONE_UTC) { + aid = gmtime_r(&self->sec, &self->datetime); + } + else { + aid = localtime_r(&self->sec, &self->datetime); + } + if (!aid) return NULL; +#ifdef NO_GMTIME_R + self->datetime = *aid; // copy data +#endif + + return self; +} + +static mrb_value +mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm) +{ + return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm)); +} + + +/* Allocates a mrb_time object and initializes it. */ +static struct mrb_time* +mrb_time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time)); + tm->sec = (time_t)sec; + tm->usec = (sec - tm->sec) * 1.0e6 + usec; + while (tm->usec < 0) { + tm->sec--; + tm->usec += 1.0e6; + } + while (tm->usec > 1.0e6) { + tm->sec++; + tm->usec -= 1.0e6; + } + tm->timezone = timezone; + mrb_time_update_datetime(tm); + + return tm; +} + +static mrb_value +mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mrb_timezone timezone) +{ + return mrb_time_wrap(mrb, c, mrb_time_alloc(mrb, sec, usec, timezone)); +} + +static struct mrb_time* +current_mrb_time(mrb_state *mrb) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); +#ifdef NO_GETTIMEOFDAY + { + static time_t last_sec = 0, last_usec = 0; + + tm->sec = time(NULL); + if (tm->sec != last_sec) { + last_sec = tm->sec; + last_usec = 0; + } + else { + /* add 1 usec to differentiate two times */ + last_usec += 1; + } + tm->usec = last_usec; + } +#else + { + struct timeval tv; + + gettimeofday(&tv, NULL); + tm->sec = tv.tv_sec; + tm->usec = tv.tv_usec; + } +#endif + tm->timezone = MRB_TIMEZONE_LOCAL; + mrb_time_update_datetime(tm); + + return tm; +} + +/* Allocates a new Time object with given millis value. */ +static mrb_value +mrb_time_now(mrb_state *mrb, mrb_value self) +{ + return mrb_time_wrap(mrb, mrb_class_ptr(self), current_mrb_time(mrb)); +} + +/* 15.2.19.6.1 */ +/* Creates an instance of time at the given time in seconds, etc. */ +static mrb_value +mrb_time_at(mrb_state *mrb, mrb_value self) +{ + mrb_float f, f2 = 0; + + mrb_get_args(mrb, "f|f", &f, &f2); + return mrb_time_make(mrb, mrb_class_ptr(self), f, f2, MRB_TIMEZONE_LOCAL); +} + +static struct mrb_time* +time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday, + mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec, + enum mrb_timezone timezone) +{ + time_t nowsecs; + struct tm nowtime = { 0 }; + + nowtime.tm_year = (int)ayear - 1900; + nowtime.tm_mon = (int)amonth - 1; + nowtime.tm_mday = (int)aday; + nowtime.tm_hour = (int)ahour; + nowtime.tm_min = (int)amin; + nowtime.tm_sec = (int)asec; + nowtime.tm_isdst = -1; + if (timezone == MRB_TIMEZONE_UTC) { + nowsecs = timegm(&nowtime); + } + else { + nowsecs = mktime(&nowtime); + } + if (nowsecs < 0) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time."); + } + + return mrb_time_alloc(mrb, nowsecs, ausec, timezone); +} + +/* 15.2.19.6.2 */ +/* Creates an instance of time at the given time in UTC. */ +static mrb_value +mrb_time_gm(mrb_state *mrb, mrb_value self) +{ + mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0; + + mrb_get_args(mrb, "i|iiiiii", + &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); + return mrb_time_wrap(mrb, mrb_class_ptr(self), + time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_UTC)); +} + + +/* 15.2.19.6.3 */ +/* Creates an instance of time at the given time in local time zone. */ +static mrb_value +mrb_time_local(mrb_state *mrb, mrb_value self) +{ + mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0; + + mrb_get_args(mrb, "i|iiiiii", + &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); + return mrb_time_wrap(mrb, mrb_class_ptr(self), + time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL)); +} + + +static mrb_value +mrb_time_eq(mrb_state *mrb, mrb_value self) +{ + mrb_value other; + struct mrb_time *tm1, *tm2; + + mrb_get_args(mrb, "o", &other); + tm1 = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type); + if (!tm1 || !tm2) return mrb_false_value(); + if (tm1->sec == tm2->sec && tm1->usec == tm2->usec) { + return mrb_true_value(); + } + return mrb_false_value(); +} + +static mrb_value +mrb_time_cmp(mrb_state *mrb, mrb_value self) +{ + mrb_value other; + struct mrb_time *tm1, *tm2; + + mrb_get_args(mrb, "o", &other); + tm1 = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type); + if (!tm1 || !tm2) return mrb_nil_value(); + if (tm1->sec > tm2->sec) { + return mrb_fixnum_value(1); + } + else if (tm1->sec < tm2->sec) { + return mrb_fixnum_value(-1); + } + /* tm1->sec == tm2->sec */ + if (tm1->usec > tm2->usec) { + return mrb_fixnum_value(1); + } + else if (tm1->usec < tm2->usec) { + return mrb_fixnum_value(-1); + } + return mrb_fixnum_value(0); +} + +static mrb_value +mrb_time_plus(mrb_state *mrb, mrb_value self) +{ + mrb_float f; + struct mrb_time *tm; + + mrb_get_args(mrb, "f", &f); + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, tm->usec, tm->timezone); +} + +static mrb_value +mrb_time_minus(mrb_state *mrb, mrb_value self) +{ + mrb_float f; + mrb_value other; + struct mrb_time *tm, *tm2; + + mrb_get_args(mrb, "o", &other); + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + + tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type); + if (tm2) { + f = (mrb_float)(tm->sec - tm2->sec) + + (mrb_float)(tm->usec - tm2->usec) / 1.0e6; + return mrb_float_value(f); + } + else { + mrb_get_args(mrb, "f", &f); + return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec-f, tm->usec, tm->timezone); + } +} + +/* 15.2.19.7.30 */ +/* Returns week day number of time. */ +static mrb_value +mrb_time_wday(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_wday); +} + +/* 15.2.19.7.31 */ +/* Returns year day number of time. */ +static mrb_value +mrb_time_yday(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_check_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_yday + 1); +} + +/* 15.2.19.7.32 */ +/* Returns year of time. */ +static mrb_value +mrb_time_year(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_year + 1900); +} + +/* 15.2.19.7.33 */ +/* Returns name of time's timezone. */ +static mrb_value +mrb_time_zone(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value(); + if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value(); + return mrb_str_new_cstr(mrb, timezone_names[tm->timezone]); +} + +/* 15.2.19.7.4 */ +/* Returns a string that describes the time. */ +static mrb_value +mrb_time_asctime(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + struct tm *d; + char buf[256]; + int len; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + d = &tm->datetime; + len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d", + wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday, + d->tm_hour, d->tm_min, d->tm_sec, + tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "", + d->tm_year + 1900); + return mrb_str_new(mrb, buf, len); +} + +/* 15.2.19.7.6 */ +/* Returns the day in the month of the time. */ +static mrb_value +mrb_time_day(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_mday); +} + + +/* 15.2.19.7.7 */ +/* Returns true if daylight saving was applied for this time. */ +static mrb_value +mrb_time_dstp(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_bool_value(tm->datetime.tm_isdst); +} + +/* 15.2.19.7.8 */ +/* 15.2.19.7.10 */ +/* Returns the Time object of the UTC(GMT) timezone. */ +static mrb_value +mrb_time_getutc(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm, *tm2; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return self; + tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); + *tm2 = *tm; + tm2->timezone = MRB_TIMEZONE_UTC; + mrb_time_update_datetime(tm2); + return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2); +} + +/* 15.2.19.7.9 */ +/* Returns the Time object of the LOCAL timezone. */ +static mrb_value +mrb_time_getlocal(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm, *tm2; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return self; + tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm)); + *tm2 = *tm; + tm2->timezone = MRB_TIMEZONE_LOCAL; + mrb_time_update_datetime(tm2); + return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2); +} + +/* 15.2.19.7.15 */ +/* Returns hour of time. */ +static mrb_value +mrb_time_hour(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_hour); +} + +/* 15.2.19.7.16 */ +/* Initializes a time by setting the amount of milliseconds since the epoch.*/ +static mrb_value +mrb_time_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, + amin = 0, asec = 0, ausec = 0; + int n; + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (tm) { + mrb_time_free(mrb, tm); + } + + n = mrb_get_args(mrb, "|iiiiiii", + &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); + if (n == 0) { + tm = current_mrb_time(mrb); + } + else { + tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL); + } + DATA_PTR(self) = tm; + DATA_TYPE(self) = &mrb_time_type; + return self; +} + +/* 15.2.19.7.17(x) */ +/* Initializes a copy of this time object. */ +static mrb_value +mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy) +{ + mrb_value src; + + mrb_get_args(mrb, "o", &src); + if (mrb_obj_equal(mrb, copy, src)) return copy; + if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) { + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); + } + if (!DATA_PTR(copy)) { + DATA_PTR(copy) = mrb_malloc(mrb, sizeof(struct mrb_time)); + DATA_TYPE(copy) = &mrb_time_type; + } + *(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src); + return copy; +} + +/* 15.2.19.7.18 */ +/* Sets the timezone attribute of the Time object to LOCAL. */ +static mrb_value +mrb_time_localtime(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return self; + tm->timezone = MRB_TIMEZONE_LOCAL; + mrb_time_update_datetime(tm); + return self; +} + +/* 15.2.19.7.19 */ +/* Returns day of month of time. */ +static mrb_value +mrb_time_mday(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_mday); +} + +/* 15.2.19.7.20 */ +/* Returns minutes of time. */ +static mrb_value +mrb_time_min(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_min); +} + +/* 15.2.19.7.21 and 15.2.19.7.22 */ +/* Returns month of time. */ +static mrb_value +mrb_time_mon(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_mon + 1); +} + +/* 15.2.19.7.23 */ +/* Returns seconds in minute of time. */ +static mrb_value +mrb_time_sec(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->datetime.tm_sec); +} + + +/* 15.2.19.7.24 */ +/* Returns a Float with the time since the epoch in seconds. */ +static mrb_value +mrb_time_to_f(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_float_value((mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6); +} + +/* 15.2.19.7.25 */ +/* Returns a Fixnum with the time since the epoch in seconds. */ +static mrb_value +mrb_time_to_i(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->sec); +} + +/* 15.2.19.7.26 */ +/* Returns a Float with the time since the epoch in microseconds. */ +static mrb_value +mrb_time_usec(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_fixnum_value(tm->usec); +} + +/* 15.2.19.7.27 */ +/* Sets the timzeone attribute of the Time object to UTC. */ +static mrb_value +mrb_time_utc(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (tm) { + tm->timezone = MRB_TIMEZONE_UTC; + mrb_time_update_datetime(tm); + } + return self; +} + +/* 15.2.19.7.28 */ +/* Returns true if this time is in the UTC timze zone false if not. */ +static mrb_value +mrb_time_utcp(mrb_state *mrb, mrb_value self) +{ + struct mrb_time *tm; + tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type); + if (!tm) return mrb_nil_value(); + return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC); +} + + + +void +mrb_mruby_time_gem_init(mrb_state* mrb) +{ + struct RClass *tc; + /* ISO 15.2.19.2 */ + tc = mrb_define_class(mrb, "Time", mrb->object_class); + MRB_SET_INSTANCE_TT(tc, MRB_TT_DATA); + mrb_include_module(mrb, tc, mrb_class_get(mrb, "Comparable")); + mrb_define_class_method(mrb, tc, "at", mrb_time_at, ARGS_ANY()); /* 15.2.19.6.1 */ + mrb_define_class_method(mrb, tc, "gm", mrb_time_gm, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.2 */ + mrb_define_class_method(mrb, tc, "local", mrb_time_local, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.3 */ + mrb_define_class_method(mrb, tc, "mktime", mrb_time_local, ARGS_REQ(1)|ARGS_OPT(6));/* 15.2.19.6.4 */ + mrb_define_class_method(mrb, tc, "now", mrb_time_now, ARGS_NONE()); /* 15.2.19.6.5 */ + mrb_define_class_method(mrb, tc, "utc", mrb_time_gm, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.6 */ + + mrb_define_method(mrb, tc, "==" , mrb_time_eq , ARGS_REQ(1)); + mrb_define_method(mrb, tc, "<=>" , mrb_time_cmp , ARGS_REQ(1)); /* 15.2.19.7.1 */ + mrb_define_method(mrb, tc, "+" , mrb_time_plus , ARGS_REQ(1)); /* 15.2.19.7.2 */ + mrb_define_method(mrb, tc, "-" , mrb_time_minus , ARGS_REQ(1)); /* 15.2.19.7.3 */ + mrb_define_method(mrb, tc, "to_s" , mrb_time_asctime, ARGS_NONE()); + mrb_define_method(mrb, tc, "inspect", mrb_time_asctime, ARGS_NONE()); + mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, ARGS_NONE()); /* 15.2.19.7.4 */ + mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, ARGS_NONE()); /* 15.2.19.7.5 */ + mrb_define_method(mrb, tc, "day" , mrb_time_day , ARGS_NONE()); /* 15.2.19.7.6 */ + mrb_define_method(mrb, tc, "dst?" , mrb_time_dstp , ARGS_NONE()); /* 15.2.19.7.7 */ + mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , ARGS_NONE()); /* 15.2.19.7.8 */ + mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,ARGS_NONE()); /* 15.2.19.7.9 */ + mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , ARGS_NONE()); /* 15.2.19.7.10 */ + mrb_define_method(mrb, tc, "gmt?" , mrb_time_utcp , ARGS_NONE()); /* 15.2.19.7.11 */ + mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , ARGS_NONE()); /* 15.2.19.7.13 */ + mrb_define_method(mrb, tc, "hour" , mrb_time_hour, ARGS_NONE()); /* 15.2.19.7.15 */ + mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, ARGS_NONE()); /* 15.2.19.7.18 */ + mrb_define_method(mrb, tc, "mday" , mrb_time_mday, ARGS_NONE()); /* 15.2.19.7.19 */ + mrb_define_method(mrb, tc, "min" , mrb_time_min, ARGS_NONE()); /* 15.2.19.7.20 */ + + mrb_define_method(mrb, tc, "mon" , mrb_time_mon, ARGS_NONE()); /* 15.2.19.7.21 */ + mrb_define_method(mrb, tc, "month", mrb_time_mon, ARGS_NONE()); /* 15.2.19.7.22 */ + + mrb_define_method(mrb, tc, "sec" , mrb_time_sec, ARGS_NONE()); /* 15.2.19.7.23 */ + mrb_define_method(mrb, tc, "to_i", mrb_time_to_i, ARGS_NONE()); /* 15.2.19.7.25 */ + mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, ARGS_NONE()); /* 15.2.19.7.24 */ + mrb_define_method(mrb, tc, "usec", mrb_time_usec, ARGS_NONE()); /* 15.2.19.7.26 */ + mrb_define_method(mrb, tc, "utc" , mrb_time_utc, ARGS_NONE()); /* 15.2.19.7.27 */ + mrb_define_method(mrb, tc, "utc?", mrb_time_utcp, ARGS_NONE()); /* 15.2.19.7.28 */ + mrb_define_method(mrb, tc, "wday", mrb_time_wday, ARGS_NONE()); /* 15.2.19.7.30 */ + mrb_define_method(mrb, tc, "yday", mrb_time_yday, ARGS_NONE()); /* 15.2.19.7.31 */ + mrb_define_method(mrb, tc, "year", mrb_time_year, ARGS_NONE()); /* 15.2.19.7.32 */ + mrb_define_method(mrb, tc, "zone", mrb_time_zone, ARGS_NONE()); /* 15.2.19.7.33 */ + + mrb_define_method(mrb, tc, "initialize", mrb_time_initialize, ARGS_REQ(1)); /* 15.2.19.7.16 */ + mrb_define_method(mrb, tc, "initialize_copy", mrb_time_initialize_copy, ARGS_REQ(1)); /* 15.2.19.7.17 */ + + /* + methods not available: + gmt_offset(15.2.19.7.12) + gmtoff(15.2.19.7.14) + utc_offset(15.2.19.7.29) + */ +} + +void +mrb_mruby_time_gem_final(mrb_state* mrb) +{ +} diff --git a/mrbgems/mruby-time/test/time.rb b/mrbgems/mruby-time/test/time.rb new file mode 100644 index 000000000..f92459d5e --- /dev/null +++ b/mrbgems/mruby-time/test/time.rb @@ -0,0 +1,201 @@ +## +# Time ISO Test + +if Object.const_defined?(:Time) + assert('Time.new', '15.2.3.3.3') do + Time.new.class == Time + end + + assert('Time', '15.2.19') do + Time.class == Class + end + + assert('Time superclass', '15.2.19.2') do + Time.superclass == Object + end + + assert('Time.at', '15.2.19.6.1') do + Time.at(1300000000.0) + end + + assert('Time.gm', '15.2.19.6.2') do + Time.gm(2012, 12, 23) + end + + assert('Time.local', '15.2.19.6.3') do + Time.local(2012, 12, 23) + end + + assert('Time.mktime', '15.2.19.6.4') do + Time.mktime(2012, 12, 23) + end + + assert('Time.now', '15.2.19.6.5') do + Time.now.class == Time + end + + assert('Time.utc', '15.2.19.6.6') do + Time.utc(2012, 12, 23) + end + + assert('Time#+', '15.2.19.7.1') do + t1 = Time.at(1300000000.0) + t2 = t1.+(60) + + t2.utc.asctime == "Sun Mar 13 07:07:40 UTC 2011" + end + + assert('Time#-', '15.2.19.7.2') do + t1 = Time.at(1300000000.0) + t2 = t1.-(60) + + t2.utc.asctime == "Sun Mar 13 07:05:40 UTC 2011" + end + + assert('Time#<=>', '15.2.19.7.3') do + t1 = Time.at(1300000000.0) + t2 = Time.at(1400000000.0) + t3 = Time.at(1500000000.0) + + t2.<=>(t1) == 1 and + t2.<=>(t2) == 0 and + t2.<=>(t3) == -1 and + t2.<=>(nil) == nil + end + + assert('Time#asctime', '15.2.19.7.4') do + Time.at(1300000000.0).utc.asctime == "Sun Mar 13 07:06:40 UTC 2011" + end + + assert('Time#ctime', '15.2.19.7.5') do + Time.at(1300000000.0).utc.ctime == "Sun Mar 13 07:06:40 UTC 2011" + end + + assert('Time#day', '15.2.19.7.6') do + Time.gm(2012, 12, 23).day == 23 + end + + assert('Time#dst?', '15.2.19.7.7') do + not Time.gm(2012, 12, 23).utc.dst? + end + + assert('Time#getgm', '15.2.19.7.8') do + Time.at(1300000000.0).getgm.asctime == "Sun Mar 13 07:06:40 UTC 2011" + end + + assert('Time#getlocal', '15.2.19.7.9') do + t1 = Time.at(1300000000.0) + t2 = Time.at(1300000000.0) + t3 = t1.getlocal + + t1 == t3 and t3 == t2.getlocal + end + + assert('Time#getutc', '15.2.19.7.10') do + Time.at(1300000000.0).getutc.asctime == "Sun Mar 13 07:06:40 UTC 2011" + end + + assert('Time#gmt?', '15.2.19.7.11') do + Time.at(1300000000.0).utc.gmt? + end + + # ATM not implemented + # assert('Time#gmt_offset', '15.2.19.7.12') do + + assert('Time#gmtime', '15.2.19.7.13') do + Time.at(1300000000.0).gmtime + end + + # ATM not implemented + # assert('Time#gmtoff', '15.2.19.7.14') do + + assert('Time#hour', '15.2.19.7.15') do + Time.gm(2012, 12, 23, 7, 6).hour == 7 + end + + # ATM doesn't really work + # assert('Time#initialize', '15.2.19.7.16') do + + assert('Time#initialize_copy', '15.2.19.7.17') do + time_tmp_2 = Time.at(7.0e6) + time_tmp_2.clone == time_tmp_2 + end + + assert('Time#localtime', '15.2.19.7.18') do + t1 = Time.at(1300000000.0) + t2 = Time.at(1300000000.0) + + t1.localtime + t1 == t2.getlocal + end + + assert('Time#mday', '15.2.19.7.19') do + Time.gm(2012, 12, 23).mday == 23 + end + + assert('Time#min', '15.2.19.7.20') do + Time.gm(2012, 12, 23, 7, 6).min == 6 + end + + assert('Time#mon', '15.2.19.7.21') do + Time.gm(2012, 12, 23).mon == 12 + end + + assert('Time#month', '15.2.19.7.22') do + Time.gm(2012, 12, 23).month == 12 + end + + assert('Times#sec', '15.2.19.7.23') do + Time.gm(2012, 12, 23, 7, 6, 40).sec == 40 + end + + assert('Time#to_f', '15.2.19.7.24') do + Time.at(1300000000.0).to_f == 1300000000.0 + end + + assert('Time#to_i', '15.2.19.7.25') do + Time.at(1300000000.0).to_i == 1300000000 + end + + assert('Time#usec', '15.2.19.7.26') do + Time.at(1300000000.0).usec == 0 + end + + assert('Time#utc', '15.2.19.7.27') do + Time.at(1300000000.0).utc + end + + assert('Time#utc?', '15.2.19.7.28') do + Time.at(1300000000.0).utc.utc? + end + + # ATM not implemented + # assert('Time#utc_offset', '15.2.19.7.29') do + + assert('Time#wday', '15.2.19.7.30') do + Time.gm(2012, 12, 23).wday == 0 + end + + assert('Time#yday', '15.2.19.7.31') do + Time.gm(2012, 12, 23).yday == 358 + end + + assert('Time#year', '15.2.19.7.32') do + Time.gm(2012, 12, 23).year == 2012 + end + + assert('Time#zone', '15.2.19.7.33') do + Time.at(1300000000.0).utc.zone == 'UTC' + end + + # Not ISO specified + + assert('Time#to_s') do + Time.at(1300000000.0).utc.to_s == "Sun Mar 13 07:06:40 UTC 2011" + end + + assert('Time#inspect') do + Time.at(1300000000.0).utc.inspect == "Sun Mar 13 07:06:40 UTC 2011" + end +end + -- 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 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 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 bdd908c92e04970e72e4d468ddefb81243767ee8 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Thu, 28 Feb 2013 10:30:30 +0000 Subject: Rake respects verbose flag also for test and clean target --- Rakefile | 7 +++---- tasks/mruby_build.rake | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Rakefile b/Rakefile index 1c65321f5..33967b07b 100644 --- a/Rakefile +++ b/Rakefile @@ -32,7 +32,6 @@ load "#{MRUBY_ROOT}/tools/mirb/mirb.rake" load "#{MRUBY_ROOT}/tasks/mrbgems_test.rake" load "#{MRUBY_ROOT}/test/mrbtest.rake" - ############################## # generic build targets, rules task :default => :all @@ -42,7 +41,7 @@ depfiles = MRuby.targets['host'].bins.map do |bin| source_path = MRuby.targets['host'].exefile("#{MRuby.targets['host'].build_dir}/bin/#{bin}") file install_path => source_path do |t| - FileUtils.cp t.prerequisites.first, t.name + FileUtils.cp t.prerequisites.first, t.name, { :verbose => $verbose } end install_path @@ -72,8 +71,8 @@ end desc "clean all built and in-repo installed artifacts" task :clean do MRuby.each_target do |t| - FileUtils.rm_rf t.build_dir + FileUtils.rm_rf t.build_dir, { :verbose => $verbose } end - FileUtils.rm_f depfiles + FileUtils.rm_f depfiles, { :verbose => $verbose } puts "Cleaned up build folder" end diff --git a/tasks/mruby_build.rake b/tasks/mruby_build.rake index 4d28b1d95..812e861a7 100644 --- a/tasks/mruby_build.rake +++ b/tasks/mruby_build.rake @@ -163,7 +163,7 @@ module MRuby def run_test puts ">>> Test #{name} <<<" mrbtest = exefile("#{build_dir}/test/mrbtest") - sh "#{filename mrbtest.relative_path}" + sh "#{filename mrbtest.relative_path}#{$verbose ? ' -v' : ''}" puts end -- cgit v1.2.3 From 12af04011fcaa80c6aa70da0586e39046613b3cf Mon Sep 17 00:00:00 2001 From: Yukihiro Matz Matsumoto Date: Thu, 28 Feb 2013 22:41:08 +0900 Subject: mv mrgems/mruby-math to mrbgems --- build_config.rb | 2 +- mgems/mruby-math/mrbgem.rake | 4 - mgems/mruby-math/src/math.c | 691 ---------------------------------------- mgems/mruby-math/test/math.rb | 125 -------- mrbgems/mruby-math/mrbgem.rake | 4 + mrbgems/mruby-math/src/math.c | 691 ++++++++++++++++++++++++++++++++++++++++ mrbgems/mruby-math/test/math.rb | 125 ++++++++ 7 files changed, 821 insertions(+), 821 deletions(-) delete mode 100644 mgems/mruby-math/mrbgem.rake delete mode 100644 mgems/mruby-math/src/math.c delete mode 100644 mgems/mruby-math/test/math.rb create mode 100644 mrbgems/mruby-math/mrbgem.rake create mode 100644 mrbgems/mruby-math/src/math.c create mode 100644 mrbgems/mruby-math/test/math.rb diff --git a/build_config.rb b/build_config.rb index d2f9597a0..c0a6eacc6 100644 --- a/build_config.rb +++ b/build_config.rb @@ -12,7 +12,7 @@ MRuby::Build.new do |conf| # conf.gem :git => 'git@github.com:masuidrive/mrbgems-example.git', :branch => 'master', :options => '-v' # Use standard Math module - conf.gem 'mgems/mruby-math' + conf.gem 'mrbgems/mruby-math' # Use standard Time class conf.gem 'mrbgems/mruby-time' diff --git a/mgems/mruby-math/mrbgem.rake b/mgems/mruby-math/mrbgem.rake deleted file mode 100644 index 4b0fa40fd..000000000 --- a/mgems/mruby-math/mrbgem.rake +++ /dev/null @@ -1,4 +0,0 @@ -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 deleted file mode 100644 index 9955d9862..000000000 --- a/mgems/mruby-math/src/math.c +++ /dev/null @@ -1,691 +0,0 @@ -/* -** 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 deleted file mode 100644 index 780b805d2..000000000 --- a/mgems/mruby-math/test/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 - diff --git a/mrbgems/mruby-math/mrbgem.rake b/mrbgems/mruby-math/mrbgem.rake new file mode 100644 index 000000000..4b0fa40fd --- /dev/null +++ b/mrbgems/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/mrbgems/mruby-math/src/math.c b/mrbgems/mruby-math/src/math.c new file mode 100644 index 000000000..9955d9862 --- /dev/null +++ b/mrbgems/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/mrbgems/mruby-math/test/math.rb b/mrbgems/mruby-math/test/math.rb new file mode 100644 index 000000000..780b805d2 --- /dev/null +++ b/mrbgems/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 + -- 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(-) 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 81f96cb9af239d2bf3ff97bcc5f511dad1d51985 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Thu, 28 Feb 2013 15:20:46 +0000 Subject: Move Math::TOLERANCE to mruby-math --- mrbgems/mruby-math/test/math.rb | 201 +++++++++++++++++++++------------------- test/assert.rb | 3 +- 2 files changed, 107 insertions(+), 97 deletions(-) diff --git a/mrbgems/mruby-math/test/math.rb b/mrbgems/mruby-math/test/math.rb index 780b805d2..1cc3a20b0 100644 --- a/mrbgems/mruby-math/test/math.rb +++ b/mrbgems/mruby-math/test/math.rb @@ -1,125 +1,136 @@ ## # Math Test -if Object.const_defined?(:Math) - assert('Math.sin 0') do - check_float(Math.sin(0), 0) +## +# Performs fuzzy check for equality on methods returning floats +# on the basis of the Math::TOLERANCE constant. +def check_float(a, b) + tolerance = Math::TOLERANCE + a = a.to_f + b = b.to_f + if a.finite? and b.finite? + (a-b).abs < tolerance + else + true end +end - assert('Math.sin PI/2') do - check_float(Math.sin(Math::PI / 2), 1) - end +assert('Math.sin 0') do + check_float(Math.sin(0), 0) +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.sin PI/2') do + check_float(Math.sin(Math::PI / 2), 1) +end - assert('Math.erf 0') do - check_float(Math.erf(0), 0) - 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.exp 0') do - check_float(Math.exp(0), 1.0) - end +assert('Math.erf 0') do + check_float(Math.erf(0), 0) +end - assert('Math.exp 1') do - check_float(Math.exp(1), 2.718281828459045) - end +assert('Math.exp 0') do + check_float(Math.exp(0), 1.0) +end - assert('Math.exp 1.5') do - check_float(Math.exp(1.5), 4.4816890703380645) - end +assert('Math.exp 1') do + check_float(Math.exp(1), 2.718281828459045) +end - assert('Math.log 1') do - check_float(Math.log(1), 0) - end +assert('Math.exp 1.5') do + check_float(Math.exp(1.5), 4.4816890703380645) +end - assert('Math.log E') do - check_float(Math.log(Math::E), 1.0) - end +assert('Math.log 1') do + check_float(Math.log(1), 0) +end - assert('Math.log E**3') do - check_float(Math.log(Math::E**3), 3.0) - end +assert('Math.log E') do + check_float(Math.log(Math::E), 1.0) +end - assert('Math.log2 1') do - check_float(Math.log2(1), 0.0) - end +assert('Math.log E**3') do + check_float(Math.log(Math::E**3), 3.0) +end - assert('Math.log2 2') do - check_float(Math.log2(2), 1.0) - end +assert('Math.log2 1') do + check_float(Math.log2(1), 0.0) +end - assert('Math.log10 1') do - check_float(Math.log10(1), 0.0) - end +assert('Math.log2 2') do + check_float(Math.log2(2), 1.0) +end - assert('Math.log10 10') do - check_float(Math.log10(10), 1.0) - end +assert('Math.log10 1') do + check_float(Math.log10(1), 0.0) +end - assert('Math.log10 10**100') do - check_float(Math.log10(10**100), 100.0) - end +assert('Math.log10 10') do + check_float(Math.log10(10), 1.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.log10 10**100') do + check_float(Math.log10(10**100), 100.0) +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 +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.hypot') do - check_float(Math.hypot(3, 4), 5.0) +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.frexp 1234') do - n = 1234 - fraction, exponent = Math.frexp(n) - check_float(Math.ldexp(fraction, exponent), n) - end +assert('Math.hypot') do + check_float(Math.hypot(3, 4), 5.0) +end - assert('Math.erf 1') do - check_float(Math.erf(1), 0.842700792949715) - end +assert('Math.frexp 1234') do + n = 1234 + fraction, exponent = Math.frexp(n) + check_float(Math.ldexp(fraction, exponent), n) +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.842700792949715) +end - assert('Math.erf -1') do - check_float(Math.erf(-1), -0.8427007929497148) - end +assert('Math.erfc 1') do + check_float(Math.erfc(1), 0.157299207050285) +end - assert('Math.erfc -1') do - check_float(Math.erfc(-1), 1.8427007929497148) - 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 diff --git a/test/assert.rb b/test/assert.rb index 269c435a7..86e99db5c 100644 --- a/test/assert.rb +++ b/test/assert.rb @@ -151,9 +151,8 @@ end ## # Performs fuzzy check for equality on methods returning floats -# on the basis of the Math::TOLERANCE constant. def check_float(a, b) - tolerance = Math::TOLERANCE + tolerance = 1e-12 a = a.to_f b = b.to_f if a.finite? and b.finite? -- cgit v1.2.3 From f6c9e40d48db5ae667451687c8b2ad36bd184ac2 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Fri, 1 Mar 2013 00:34:05 +0800 Subject: Modification of CONTRIBUTING.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some minor fixes of the  CONTRIBUTING.md file. --- CONTRIBUTING.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 08995978f..99bd55962 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ # How to contribute -mruby is an open-source project which is looking forward to each contribution. +mruby is an open-source project which is looking forward to each contribution. ## Your Pull Request @@ -8,10 +8,10 @@ To make it easy to review and understand your change please keep the following things in mind before submitting your pull request: * Work on the latest possible state of **mruby/master** -* Test your changes before creating a pull request (**make test**) +* Create a branch which is dedicated to your change +* Test your changes before creating a pull request (```./minirake test```) * If possible write a test case which confirms your change * Don't mix several features or bug-fixes in one pull request -* Create a branch which is dedicated to your change * Create a meaningful commit message * Explain your change (i.e. with a link to the issue you are fixing) @@ -27,16 +27,16 @@ C code: #### Comply with C99 (ISO/IEC 9899:1999) -mruby should be highly portable to other systems and compilers. For that it is +mruby should be highly portable to other systems and compilers. For this it is recommended to keep your code as close as possible to the C99 standard (http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf). -Although we target C99, VC is also an important target for mruby, so that we -avoid local variable declaration in the middle. +Although we target C99, Visual C++ is also an important target for mruby, so +you should local variable declarations in the middle. #### Reduce library dependencies to a minimum -The dependencies to libraries should be put to an absolute minimum. This +The dependencies to libraries should be kept to an absolute minimum. This increases the portability but makes it also easier to cut away parts of mruby on-demand. @@ -56,7 +56,7 @@ Use C++ style comments only for temporary comment e.g. commenting out some code ### Ruby code -Parts of the standard library of mruby is written in the Ruby programming language +Parts of the standard library of mruby are written in the Ruby programming language itself. Please note the following hints for your Ruby code: #### Comply with the Ruby standard (ISO/IEC 30170:2012) -- cgit v1.2.3 From cdc1c5b5d0ce02c3490a98b89fb5c4cfa44fd30f Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Fri, 1 Mar 2013 01:19:10 +0800 Subject: Fix styleguide based on @cremno's input --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 99bd55962..b8db5a7dc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,8 +31,8 @@ mruby should be highly portable to other systems and compilers. For this it is recommended to keep your code as close as possible to the C99 standard (http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf). -Although we target C99, Visual C++ is also an important target for mruby, so -you should local variable declarations in the middle. +Although we target C99, Visual C++ is also an important target for mruby. For this +reason a declaration of a local variable has to be at the beginning of a scope block. #### Reduce library dependencies to a minimum -- 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(-) 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 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