diff options
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | CMakeLists.txt | 9 | ||||
| -rw-r--r-- | include/mruby.h | 11 | ||||
| -rw-r--r-- | include/mruby/array.h | 20 | ||||
| -rw-r--r-- | include/mruby/hash.h | 11 | ||||
| -rw-r--r-- | include/mruby/object.h | 2 | ||||
| -rw-r--r-- | include/mruby/variable.h | 4 | ||||
| -rw-r--r-- | mrblib/hash.rb | 56 | ||||
| -rw-r--r-- | src/array.c | 169 | ||||
| -rw-r--r-- | src/class.c | 4 | ||||
| -rw-r--r-- | src/codegen.c | 8 | ||||
| -rw-r--r-- | src/hash.c | 73 | ||||
| -rw-r--r-- | src/kernel.c | 4 | ||||
| -rw-r--r-- | src/sprintf.c | 2 | ||||
| -rw-r--r-- | src/struct.c | 6 | ||||
| -rw-r--r-- | src/symbol.c | 3 | ||||
| -rw-r--r-- | src/variable.c | 37 | ||||
| -rw-r--r-- | test/t/kernel.rb | 12 | ||||
| -rw-r--r-- | test/t/module.rb | 47 | ||||
| -rw-r--r-- | test/t/string.rb | 8 |
20 files changed, 294 insertions, 193 deletions
@@ -6,3 +6,4 @@ Original Authors "mruby developers" are: Daniel Bovensiepen Jon Maken Bjorn De Meyer + Yuichiro MASUI diff --git a/CMakeLists.txt b/CMakeLists.txt index ee97ad8b4..c8b252d2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,12 +33,13 @@ project(mruby C) # TODO stop polluting source tree with CMakeFiles/ and CMakeCache.txt # on build location check failure # Make sure we are not trying to generate in in-tree build unless building -# with a MSVC IDE where it's OK. -if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE) +# with an MSVC or Xcode IDE where it's OK. +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT (MSVC_IDE OR XCODE)) message(FATAL_ERROR "\nIn-source builds are not allowed as CMake would overwrite the " - "Makefiles distributed with mruby. Please change to the 'build' " - "subdirectory and run CMake from there.") + "Makefiles distributed with mruby. Delete any created 'CMakeFiles' " + "subdirectory and 'CMakeCache.txt' file from the current directory, " + "change to the 'build' subdirectory, and re-run CMake from there.") endif() if(COMMAND cmake_policy) diff --git a/include/mruby.h b/include/mruby.h index 3bbc6e6cc..70bbdf3c2 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -147,7 +147,7 @@ mrb_obj_value(void *p) } static inline mrb_value -mrb_false_value() +mrb_false_value(void) { mrb_value v; @@ -157,7 +157,7 @@ mrb_false_value() } static inline mrb_value -mrb_nil_value() +mrb_nil_value(void) { mrb_value v; @@ -167,7 +167,7 @@ mrb_nil_value() } static inline mrb_value -mrb_true_value() +mrb_true_value(void) { mrb_value v; @@ -177,7 +177,7 @@ mrb_true_value() } static inline mrb_value -mrb_undef_value() +mrb_undef_value(void) { mrb_value v; @@ -416,6 +416,9 @@ mrb_value mrb_check_funcall(mrb_state *mrb, mrb_value recv, mrb_sym mid, int arg #define ISXDIGIT(c) (ISASCII(c) && isxdigit((int)(unsigned char)(c))) #endif +mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, long len); +void mrb_exc_raise(mrb_state *mrb, mrb_value exc); + int mrb_block_given_p(void); void mrb_raise(mrb_state *mrb, struct RClass *c, const char *fmt, ...); void rb_raise(struct RClass *c, const char *fmt, ...); diff --git a/include/mruby/array.h b/include/mruby/array.h index e2acee535..21d50152f 100644 --- a/include/mruby/array.h +++ b/include/mruby/array.h @@ -13,8 +13,8 @@ extern "C" { struct RArray { MRUBY_OBJECT_HEADER; - size_t len; - size_t capa; + int len; + int capa; mrb_value *buf; }; @@ -25,27 +25,25 @@ struct RArray { #define RARRAY_LEN(a) (RARRAY(a)->len) #define RARRAY_PTR(a) (RARRAY(a)->buf) -mrb_value mrb_ary_new_capa(mrb_state*, size_t); +mrb_value mrb_ary_new_capa(mrb_state*, int); mrb_value mrb_ary_new(mrb_state *mrb); -mrb_value mrb_ary_new_elts(mrb_state *mrb, long n, const mrb_value *elts); +mrb_value mrb_ary_new_elts(mrb_state *mrb, int n, const mrb_value *elts); void mrb_ary_concat(mrb_state*, mrb_value, mrb_value); mrb_value mrb_ary_splat(mrb_state*, mrb_value); void mrb_ary_push(mrb_state*, mrb_value, mrb_value); mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary); -mrb_value mrb_ary_new_from_values(mrb_state *mrb, size_t size, mrb_value *vals); +mrb_value mrb_ary_new_from_values(mrb_state *mrb, int size, mrb_value *vals); mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self); mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n); void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val); int mrb_ary_len(mrb_state *mrb, mrb_value ary); -mrb_value mrb_ary_replace_m(mrb_state *mrb, mrb_value self); -void mrb_ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, size_t len); +void mrb_ary_replace(mrb_state *mrb, mrb_value a, mrb_value b); mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self); mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item); -mrb_value mrb_ary_new4(mrb_state *mrb, long n, const mrb_value *elts); +mrb_value mrb_ary_new4(mrb_state *mrb, int n, const mrb_value *elts); mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr); -mrb_value mrb_ary_entry(mrb_value ary, long offset); -void mrb_mem_clear(mrb_value *mem, long size); -mrb_value mrb_ary_tmp_new(mrb_state *mrb, long capa); +mrb_value mrb_ary_entry(mrb_value ary, int offset); +mrb_value mrb_ary_tmp_new(mrb_state *mrb, int capa); mrb_value mrb_ary_sort(mrb_state *mrb, mrb_value ary); mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self); diff --git a/include/mruby/hash.h b/include/mruby/hash.h index 1bbd9bd97..369decdb4 100644 --- a/include/mruby/hash.h +++ b/include/mruby/hash.h @@ -17,18 +17,15 @@ struct RHash { struct kh_ht *ht; }; -#define mrb_hash_end(h) st_hash_end(h) -#define mrb_hash_uint(h, i) st_hash_uint(h, i) - #define mrb_hash_ptr(v) ((struct RHash*)((v).value.p)) #define mrb_hash_value(p) mrb_obj_value((void*)(p)) -mrb_value mrb_hash_new_capa(mrb_state*, size_t); -mrb_value mrb_hash_new(mrb_state *mrb, int capa); +mrb_value mrb_hash_new_capa(mrb_state*, int); +mrb_value mrb_hash_new(mrb_state *mrb); void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val); mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key); -mrb_value mrb_hash_getWithDef(mrb_state *mrb, mrb_value hash, mrb_value vkey, mrb_value def); +mrb_value mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def); mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key); mrb_value mrb_hash(mrb_state *mrb, mrb_value obj); mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value self); @@ -36,8 +33,6 @@ mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value self); /* RHASH_TBL allocates st_table if not available. */ #define RHASH(obj) ((struct RHash*)((obj).value.p)) #define RHASH_TBL(h) (RHASH(h)->ht) -#define RHASH_SIZE(h) (RHASH_TBL(h)->size) -#define RHASH_EMPTY_P(h) (RHASH_SIZE(h) == 0) #define RHASH_IFNONE(h) mrb_iv_get(mrb, (h), mrb_intern(mrb, "ifnone")) #define RHASH_PROCDEFAULT(h) RHASH_IFNONE(h) struct kh_ht * mrb_hash_tbl(mrb_state *mrb, mrb_value hash); diff --git a/include/mruby/object.h b/include/mruby/object.h index 495c34083..4be060078 100644 --- a/include/mruby/object.h +++ b/include/mruby/object.h @@ -13,7 +13,7 @@ extern "C" { #define MRUBY_OBJECT_HEADER \ enum mrb_vtype tt:8; \ - int color:3;\ + unsigned int color:3;\ unsigned int flags:21;\ struct RClass *c;\ struct RBasic *gcnext diff --git a/include/mruby/variable.h b/include/mruby/variable.h index 880d6217e..400dd50b0 100644 --- a/include/mruby/variable.h +++ b/include/mruby/variable.h @@ -14,8 +14,8 @@ extern "C" { typedef struct global_variable { int counter; mrb_value *data; - mrb_value (*getter)(); - void (*setter)(); + mrb_value (*getter)(void); + void (*setter)(void); //void (*marker)(); //int block_trace; //struct trace_var *trace; diff --git a/mrblib/hash.rb b/mrblib/hash.rb index d6ad55e47..dc85c8f5e 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -80,6 +80,62 @@ class Hash end h end + + # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing. + def reject!(&b) + keys = [] + self.each_key{|k| + v = self[k] + if b.call(k, v) + keys.push(k) + end + } + return nil if keys.size == 0 + keys.each{|k| + self.delete(k) + } + self + end + + # 1.8/1.9 Hash#reject returns Hash; ISO says nothing. + def reject(&b) + h = {} + self.each_key{|k| + v = self[k] + unless b.call(k, v) + h[k] = v + end + } + h + end + + # 1.9 Hash#select! returns Hash; ISO says nothing. + def reject!(&b) + keys = [] + self.each_key{|k| + v = self[k] + unless b.call(k, v) + keys.push(k) + end + } + return nil if keys.size == 0 + keys.each{|k| + self.delete(k) + } + self + end + + # 1.9 Hash#select returns Hash; ISO says nothing. + def select(&b) + h = {} + self.each_key{|k| + v = self[k] + if b.call(k, v) + h[k] = v + end + } + h + end end ## diff --git a/src/array.c b/src/array.c index 187a8404d..ed207a2a2 100644 --- a/src/array.c +++ b/src/array.c @@ -10,7 +10,6 @@ #include "mruby/string.h" #include "mruby/class.h" -//#define ARY_DEFAULT_LEN 16 #define ARY_DEFAULT_LEN 4 #define ARY_SHRINK_RATIO 5 /* must be larger than 2 */ #ifdef LONG_MAX @@ -56,7 +55,7 @@ ary_new_capa(mrb_state *mrb, size_t capa) } mrb_value -mrb_ary_new_capa(mrb_state *mrb, size_t capa) +mrb_ary_new_capa(mrb_state *mrb, int capa) { struct RArray *a = ary_new_capa(mrb, capa); return mrb_obj_value(a); @@ -69,7 +68,7 @@ mrb_ary_new(mrb_state *mrb) } mrb_value -mrb_ary_new_from_values(mrb_state *mrb, size_t size, mrb_value *vals) +mrb_ary_new_from_values(mrb_state *mrb, int size, mrb_value *vals) { mrb_value ary; struct RArray *a; @@ -92,7 +91,7 @@ mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr) } void -ary_fill_with_nil(mrb_value *buf, size_t size) +ary_fill_with_nil(mrb_value *buf, int size) { mrb_value nil = mrb_nil_value(); @@ -102,9 +101,9 @@ ary_fill_with_nil(mrb_value *buf, size_t size) } void -mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, size_t len) +mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, int len) { - size_t capa = a->capa; + int capa = a->capa; #ifdef LONG_MAX if (len > ARY_MAX_SIZE) { @@ -134,7 +133,7 @@ mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, size_t len) void mrb_ary_shrink_capa(mrb_state *mrb, struct RArray *a) { - size_t capa = a->capa; + int capa = a->capa; if (capa < ARY_DEFAULT_LEN * 2) return; if (capa <= a->len * ARY_SHRINK_RATIO) return; @@ -160,29 +159,36 @@ mrb_ary_s_create(mrb_state *mrb, mrb_value self) int len; mrb_get_args(mrb, "*", &vals, &len); - return mrb_ary_new_from_values(mrb, (size_t)len, vals); + return mrb_ary_new_from_values(mrb, (int)len, vals); +} + +static void +ary_concat(mrb_state *mrb, struct RArray *a, mrb_value *buf, int blen) +{ + int len = a->len + blen; + + if (a->capa < len) mrb_ary_expand_capa(mrb, a, len); + memcpy(a->buf+a->len, buf, sizeof(mrb_value)*blen); + mrb_write_barrier(mrb, (struct RBasic*)a); + a->len = len; } void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other) { - struct RArray *a1 = mrb_ary_ptr(self); struct RArray *a2 = mrb_ary_ptr(other); - size_t len = a1->len + a2->len; - if (a1->capa < len) mrb_ary_expand_capa(mrb, a1, len); - memcpy(a1->buf+a1->len, a2->buf, sizeof(mrb_value)*a2->len); - mrb_write_barrier(mrb, (struct RBasic*)a1); - a1->len = len; + ary_concat(mrb, mrb_ary_ptr(self), a2->buf, a2->len); } mrb_value mrb_ary_concat_m(mrb_state *mrb, mrb_value self) { - mrb_value other; + mrb_value *buf; + int blen; - mrb_get_args(mrb, "A", &other); - mrb_ary_concat(mrb, self, other); + mrb_get_args(mrb, "a", &buf, &blen); + ary_concat(mrb, mrb_ary_ptr(self), buf, blen); return self; } @@ -191,15 +197,16 @@ mrb_ary_plus(mrb_state *mrb, mrb_value self) { struct RArray *a1 = mrb_ary_ptr(self); struct RArray *a2; - mrb_value other; mrb_value ary; + mrb_value *buf; + int blen; - mrb_get_args(mrb, "A", &other); - ary = mrb_ary_new_capa(mrb, a1->len + RARRAY_LEN(other)); + mrb_get_args(mrb, "a", &buf, &blen); + ary = mrb_ary_new_capa(mrb, a1->len + blen); a2 = mrb_ary_ptr(ary); memcpy(a2->buf, a1->buf, sizeof(mrb_value)*a1->len); - memcpy(a2->buf + a1->len, RARRAY_PTR(other), sizeof(mrb_value)*RARRAY_LEN(other)); - a2->len = a1->len + RARRAY_LEN(other); + memcpy(a2->buf + a1->len, buf, sizeof(mrb_value)*blen); + a2->len = a1->len + blen; return ary; } @@ -248,8 +255,8 @@ mrb_ary_cmp(mrb_state *mrb, mrb_value ary1) return mrb_fixnum_value((len == 0)? 0: (len > 0)? 1: -1); } -void -mrb_ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, size_t len) +static void +ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, int len) { if (a->capa < len) mrb_ary_expand_capa(mrb, a, len); memcpy(a->buf, argv, sizeof(mrb_value)*len); @@ -257,13 +264,21 @@ mrb_ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, size_t len) a->len = len; } +void +mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other) +{ + struct RArray *a2 = mrb_ary_ptr(other); + + ary_replace(mrb, mrb_ary_ptr(self), a2->buf, a2->len); +} + mrb_value mrb_ary_replace_m(mrb_state *mrb, mrb_value self) { mrb_value other; - mrb_get_args(mrb, "o", &other); - mrb_ary_replace(mrb, mrb_ary_ptr(self), RARRAY_PTR(other), RARRAY_LEN(other)); + mrb_get_args(mrb, "A", &other); + mrb_ary_replace(mrb, self, other); return self; } @@ -276,7 +291,6 @@ mrb_ary_times(mrb_state *mrb, mrb_value self) mrb_value ary; mrb_value *buf; mrb_int times; - //size_t len; mrb_get_args(mrb, "i", ×); if (times < 0) { @@ -296,28 +310,22 @@ mrb_ary_times(mrb_state *mrb, mrb_value self) return ary; } -static void -ary_reverse(struct RArray *a) -{ - mrb_value *p1, *p2; - - p1 = a->buf; - p2 = a->buf + a->len - 1; - - while(p1 < p2) { - mrb_value tmp = *p1; - *p1++ = *p2; - *p2-- = tmp; - } -} - mrb_value mrb_ary_reverse_bang(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); if (a->len > 1) { - ary_reverse(a); + mrb_value *p1, *p2; + + p1 = a->buf; + p2 = a->buf + a->len - 1; + + while(p1 < p2) { + mrb_value tmp = *p1; + *p1++ = *p2; + *p2-- = tmp; + } } return self; } @@ -325,33 +333,41 @@ mrb_ary_reverse_bang(mrb_state *mrb, mrb_value self) mrb_value mrb_ary_reverse(mrb_state *mrb, mrb_value self) { - struct RArray *a = mrb_ary_ptr(self); + struct RArray *a = mrb_ary_ptr(self), *b; mrb_value ary; ary = mrb_ary_new_capa(mrb, a->len); + b = mrb_ary_ptr(ary); if (a->len > 0) { - mrb_ary_replace(mrb, mrb_ary_ptr(ary), a->buf, a->len); - ary_reverse(mrb_ary_ptr(ary)); + mrb_value *p1, *p2, *e; + + p1 = a->buf; + e = p1 + a->len; + p2 = b->buf + a->len - 1; + while(p1 < e) { + *p2-- = *p1++; + } + b->len = a->len; } return ary; } mrb_value -mrb_ary_new4(mrb_state *mrb, long n, const mrb_value *elts) +mrb_ary_new4(mrb_state *mrb, int n, const mrb_value *elts) { mrb_value ary; - ary = mrb_ary_new_capa(mrb, n);//mrb_ary_new2(n); + ary = mrb_ary_new_capa(mrb, n); if (n > 0 && elts) { memcpy(RARRAY_PTR(ary), elts, sizeof(mrb_value)*n); - RARRAY_LEN(ary) = n; //ARY_SET_LEN(ary, n); + RARRAY_LEN(ary) = n; } return ary; } mrb_value -mrb_ary_new_elts(mrb_state *mrb, long n, const mrb_value *elts) +mrb_ary_new_elts(mrb_state *mrb, int n, const mrb_value *elts) { return mrb_ary_new4(mrb, n, elts); } @@ -403,7 +419,7 @@ mrb_ary_shift(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_value *buf = a->buf; - size_t size = a->len; + int size = a->len; mrb_value val; if (size == 0) return mrb_nil_value(); @@ -461,7 +477,7 @@ mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n) /* range check */ if (n < 0) n += a->len; - if (n < 0 || a->len <= (size_t)n) return mrb_nil_value(); + if (n < 0 || a->len <= (int)n) return mrb_nil_value(); return a->buf[n]; } @@ -476,8 +492,8 @@ mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val) /* rb_ary_s if (n < 0) { mrb_raise(mrb, E_INDEX_ERROR, "index %ld out of array", n - a->len); } - if (a->len <= (size_t)n) { - if (a->capa <= (size_t)n) mrb_ary_expand_capa(mrb, a, n + 1); + if (a->len <= (int)n) { + if (a->capa <= (int)n) mrb_ary_expand_capa(mrb, a, n + 1); ary_fill_with_nil(a->buf + a->len, n + 1 - a->len); a->len = n + 1; } @@ -491,7 +507,7 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val { struct RArray *a = mrb_ary_ptr(ary); mrb_int tail; - size_t size; + int size; mrb_value *argv; int i, argc; @@ -518,7 +534,7 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val if (size > a->capa) mrb_ary_expand_capa(mrb, a, size); if (head > a->len) { - ary_fill_with_nil(a->buf + a->len, (size_t)(head - a->len)); + ary_fill_with_nil(a->buf + a->len, (int)(head - a->len)); } else if (head < a->len) { memmove(a->buf + head + argc, a->buf + tail, sizeof(mrb_value)*(a->len - tail)); @@ -558,10 +574,10 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self) } len = mrb_fixnum(argv[0]); if (index < 0) index += a->len; - if (index < 0 || a->len < (size_t)index) return mrb_nil_value(); + if (index < 0 || a->len < (int)index) return mrb_nil_value(); if ((len = mrb_fixnum(argv[0])) < 0) return mrb_nil_value(); - if (a->len == (size_t)index) return mrb_ary_new(mrb); - if ((size_t)len > a->len - index) len = a->len - index; + if (a->len == (int)index) return mrb_ary_new(mrb); + if ((int)len > a->len - index) len = a->len - index; return mrb_ary_new_from_values(mrb, len, a->buf + index); default: @@ -607,11 +623,11 @@ mrb_ary_delete_at(mrb_state *mrb, mrb_value self) mrb_int index; mrb_value val; mrb_value *buf; - size_t len; + int len; mrb_get_args(mrb, "i", &index); if (index < 0) index += a->len; - if (index < 0 || a->len <= (size_t)index) return mrb_nil_value(); + if (index < 0 || a->len <= (int)index) return mrb_nil_value(); val = a->buf[index]; @@ -632,8 +648,7 @@ mrb_value mrb_ary_first(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); - //mrb_value ary; - size_t size; + int size; mrb_value *vals; int len; @@ -654,8 +669,7 @@ mrb_value mrb_ary_last(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); - //mrb_value ary; - size_t size; + int size; mrb_value *vals; int len; @@ -747,7 +761,7 @@ mrb_check_array_type(mrb_state *mrb, mrb_value ary) } mrb_value -mrb_ary_entry(mrb_value ary, long offset) +mrb_ary_entry(mrb_value ary, int offset) { if (offset < 0) { offset += RARRAY_LEN(ary); @@ -755,24 +769,16 @@ mrb_ary_entry(mrb_value ary, long offset) return ary_elt(ary, offset); } -void -mrb_mem_clear(mrb_value *mem, long size) -{ - while (size--) { - *mem++ = mrb_nil_value(); - } -} - mrb_value -mrb_ary_tmp_new(mrb_state *mrb, long capa) +mrb_ary_tmp_new(mrb_state *mrb, int capa) { - return mrb_ary_new_capa(mrb, capa);//ary_new(0, capa); + return mrb_ary_new_capa(mrb, capa); } static mrb_value inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) { - long i; + int i; mrb_value s, arystr; char *head = "["; char *sep = ", "; @@ -801,7 +807,6 @@ inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) } else { s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]); } - //mrb_str_buf_append(mrb, arystr, s); mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s)); mrb_gc_arena_restore(mrb, ai); } @@ -835,7 +840,7 @@ mrb_ary_inspect(mrb_state *mrb, mrb_value ary) static mrb_value join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list) { - long i; + int i; mrb_value result, val, tmp; /* check recursive */ @@ -851,7 +856,6 @@ join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list) for(i=0; i<RARRAY_LEN(ary); i++) { if (i > 0 && !mrb_nil_p(sep)) { - //mrb_str_buf_append(mrb, result, sep); // segv (encoding error?) mrb_str_buf_cat(mrb, result, RSTRING_PTR(sep), RSTRING_LEN(sep)); } @@ -864,7 +868,6 @@ join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list) case MRB_TT_STRING: str_join: - //mrb_str_buf_append(mrb, result, val); mrb_str_buf_cat(mrb, result, RSTRING_PTR(val), RSTRING_LEN(val)); break; @@ -951,7 +954,7 @@ mrb_ary_equal(mrb_state *mrb, mrb_value ary1) } if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value(); else { - long i; + int i; for (i=0; i<RARRAY_LEN(ary1); i++) { if (!mrb_equal(mrb, ary_elt(ary1, i), ary_elt(ary2, i))) @@ -980,7 +983,7 @@ mrb_ary_eql(mrb_state *mrb, mrb_value ary1) if (mrb_type(ary2) != MRB_TT_ARRAY) return mrb_false_value(); if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value(); else { - long i; + int i; for (i=0; i<RARRAY_LEN(ary1); i++) { if (!mrb_eql(mrb, ary_elt(ary1, i), ary_elt(ary2, i))) diff --git a/src/class.c b/src/class.c index b13ab2288..9424e8d84 100644 --- a/src/class.c +++ b/src/class.c @@ -17,8 +17,8 @@ #include "mruby/khash.h" -KHASH_MAP_INIT_INT(mt, struct RProc*); -KHASH_MAP_INIT_INT(iv, mrb_value); +KHASH_INIT(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal) +KHASH_INIT(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal) typedef struct fc_result { mrb_sym name; diff --git a/src/codegen.c b/src/codegen.c index 505f0ad5b..20799f9ae 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -208,23 +208,19 @@ genop_peep(codegen_scope *s, mrb_code i, int val) case OP_SETCV: case OP_SETCONST: case OP_SETMCNST: - switch (c0) { - case OP_MOVE: + if (c0 == OP_MOVE) { if (GETARG_A(i) == GETARG_A(i0)) { s->iseq[s->pc-1] = MKOP_ABx(c1, GETARG_B(i0), GETARG_Bx(i)); return; } - break; } break; case OP_SETUPVAR: - switch (c0) { - case OP_MOVE: + if (c0 == OP_MOVE) { if (GETARG_A(i) == GETARG_A(i0)) { s->iseq[s->pc-1] = MKOP_ABC(c1, GETARG_B(i0), GETARG_B(i), GETARG_C(i)); return; } - break; } break; case OP_EPOP: diff --git a/src/hash.c b/src/hash.c index a06becd91..4a85fcec3 100644 --- a/src/hash.c +++ b/src/hash.c @@ -60,6 +60,7 @@ mrb_gc_mark_ht(mrb_state *mrb, struct RHash *hash) khiter_t k; khash_t(ht) *h = hash->ht; + if (!h) return; for (k = kh_begin(h); k != kh_end(h); k++) if (kh_exist(h, k)) { mrb_gc_mark_value(mrb, kh_key(h, k)); @@ -70,43 +71,48 @@ mrb_gc_mark_ht(mrb_state *mrb, struct RHash *hash) size_t mrb_gc_mark_ht_size(mrb_state *mrb, struct RHash *hash) { + if (!hash->ht) return 0; return kh_size(hash->ht)*2; } void mrb_gc_free_ht(mrb_state *mrb, struct RHash *hash) { - kh_destroy(ht, hash->ht); + if (hash->ht) kh_destroy(ht, hash->ht); } mrb_value -mrb_hash_new_capa(mrb_state *mrb, size_t capa) +mrb_hash_new_capa(mrb_state *mrb, int capa) { struct RHash *h; h = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class); h->ht = kh_init(ht, mrb); - kh_resize(ht, h->ht, capa); + if (capa > 0) { + kh_resize(ht, h->ht, capa); + } h->iv = 0; return mrb_obj_value(h); } mrb_value -mrb_hash_new(mrb_state *mrb, int capa) +mrb_hash_new(mrb_state *mrb) { - return mrb_hash_new_capa(mrb, capa); + return mrb_hash_new_capa(mrb, 0); } mrb_value -mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key) /* mrb_hash_aref */ /* mrb_hash_lookup */ +mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key) { khash_t(ht) *h = RHASH_TBL(hash); khiter_t k; - k = kh_get(ht, h, key); - if (k != kh_end(h)) - return kh_value(h, k); + if (h) { + k = kh_get(ht, h, key); + if (k != kh_end(h)) + return kh_value(h, k); + } /* not found */ if (MRB_RHASH_PROCDEFAULT_P(hash)) { @@ -116,13 +122,13 @@ mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key) /* mrb_hash_aref */ } mrb_value -mrb_hash_getWithDef(mrb_state *mrb, mrb_value hash, mrb_value vkey, mrb_value def) /* mrb_hash_lookup2 */ +mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def) { khash_t(ht) *h = RHASH_TBL(hash); khiter_t k; if (h) { - k = kh_get(ht, h, vkey); + k = kh_get(ht, h, key); if (k != kh_end(h)) return kh_value(h, k); } @@ -140,6 +146,7 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val) /* mr mrb_hash_modify(mrb, hash); h = RHASH_TBL(hash); + if (!h) h = RHASH_TBL(hash) = kh_init(ht, mrb); k = kh_get(ht, h, key); if (k == kh_end(h)) { /* expand */ @@ -165,11 +172,11 @@ mrb_hash_dup(mrb_state *mrb, mrb_value hash) khash_t(ht) *h, *ret_h; khiter_t k, ret_k; + h = RHASH_TBL(hash); ret = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class); ret->ht = kh_init(ht, mrb); - if (!RHASH_EMPTY_P(hash)) { - h = RHASH_TBL(hash); + if (kh_size(h) > 0) { ret_h = ret->ht; for (k = kh_begin(h); k != kh_end(h); k++) { @@ -194,7 +201,7 @@ mrb_hash_tbl(mrb_state *mrb, mrb_value hash) { khash_t(ht) *h = RHASH_TBL(hash); - if (!RHASH_TBL(hash)) { + if (!h) { RHASH_TBL(hash) = kh_init(ht, mrb); } return h; @@ -331,15 +338,9 @@ mrb_hash_aget(mrb_state *mrb, mrb_value self) } mrb_value -mrb_hash_lookup2(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def) -{ - return mrb_hash_getWithDef(mrb, hash, key, def); -} - -mrb_value mrb_hash_lookup(mrb_state *mrb, mrb_value hash, mrb_value key) { - return mrb_hash_lookup2(mrb, hash, key, mrb_nil_value()); + return mrb_hash_get(mrb, hash, key); } /* @@ -700,7 +701,7 @@ mrb_hash_clear(mrb_state *mrb, mrb_value hash) { khash_t(ht) *h = RHASH_TBL(hash); - kh_clear(ht, h); + if (h) kh_clear(ht, h); return hash; } @@ -817,13 +818,12 @@ static mrb_value mrb_hash_empty_p(mrb_state *mrb, mrb_value self) { khash_t(ht) *h = RHASH_TBL(self); - khiter_t k; + if (h) { - for (k = kh_begin(h); k != kh_end(h); k++) - if (kh_exist(h, k)) - return mrb_false_value(); + if (kh_size(h) == 0) + return mrb_true_value(); } - return mrb_true_value(); + return mrb_false_value(); } /* 15.2.13.4.11 */ @@ -938,8 +938,10 @@ inspect_hash(mrb_state *mrb, mrb_value hash, int recur) static mrb_value mrb_hash_inspect(mrb_state *mrb, mrb_value hash) { - if (RHASH_EMPTY_P(hash)) - return mrb_str_new_cstr(mrb, "{}"); + khash_t(ht) *h = RHASH_TBL(hash); + + if (!h || kh_size(h) == 0) + return mrb_str_new_cstr(mrb, "{}"); return inspect_hash(mrb, hash, 0); } @@ -1106,6 +1108,8 @@ mrb_hash_has_value(mrb_state *mrb, mrb_value hash) static mrb_value hash_equal(mrb_state *mrb, mrb_value hash1, mrb_value hash2, int eql) { + khash_t(ht) *h1, *h2; + if (mrb_obj_equal(mrb, hash1, hash2)) return mrb_true_value(); if (mrb_type(hash2) != MRB_TT_HASH) { if (!mrb_respond_to(mrb, hash2, mrb_intern(mrb, "to_hash"))) { @@ -1116,10 +1120,15 @@ hash_equal(mrb_state *mrb, mrb_value hash1, mrb_value hash2, int eql) else return mrb_fixnum_value(mrb_equal(mrb, hash2, hash1)); } - if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2)) return mrb_false_value(); + h1 = RHASH_TBL(hash1); + h2 = RHASH_TBL(hash2); + if (!h2) { + if (!h2) return mrb_true_value(); + return mrb_false_value(); + } + if (!h2) return mrb_false_value(); + if (kh_size(h1) != kh_size(h2)) return mrb_false_value(); else { - khash_t(ht) *h1 = RHASH_TBL(hash1); - khash_t(ht) *h2 = RHASH_TBL(hash2); khiter_t k1, k2; mrb_value key; diff --git a/src/kernel.c b/src/kernel.c index 9092d239d..15a4158a4 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -39,8 +39,8 @@ typedef enum { #include "regint.h" #endif -KHASH_MAP_INIT_INT(mt, struct RProc*); -KHASH_MAP_INIT_INT(iv, mrb_value); +KHASH_INIT(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal) +KHASH_INIT(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal) #ifndef FALSE #define FALSE 0 diff --git a/src/sprintf.c b/src/sprintf.c index 79bd101ad..296a7c73e 100644 --- a/src/sprintf.c +++ b/src/sprintf.c @@ -165,7 +165,7 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base) (mrb_raise(mrb, E_ARGUMENT_ERROR, "named%.*s after unnumbered(%d)", (len), (name), posarg), mrb_undef_value()) : \ posarg == -1 ? \ (mrb_raise(mrb, E_ARGUMENT_ERROR, "named%.*s after numbered", (len), (name)), mrb_undef_value()) : \ - (posarg = -2, mrb_hash_getWithDef(mrb, get_hash(mrb, &hash, argc, argv), id, mrb_undef_value()))) + (posarg = -2, mrb_hash_fetch(mrb, get_hash(mrb, &hash, argc, argv), id, mrb_undef_value()))) #define GETNUM(n, val) \ for (; p < end && ISDIGIT(*p); p++) {\ diff --git a/src/struct.c b/src/struct.c index d06124b50..9f7010e0b 100644 --- a/src/struct.c +++ b/src/struct.c @@ -416,12 +416,10 @@ mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_val mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs"); } st = RSTRUCT(self); - st->ptr = malloc(sizeof(mrb_value)*argc); + st->ptr = mrb_malloc(mrb, sizeof(mrb_value)*argc); st->len = n; memcpy(st->ptr, argv, sizeof(mrb_value)*argc); - //if (n > argc) { - // mrb_mem_clear(RSTRUCT_PTR(self)+argc, n-argc); - //} + return self; } diff --git a/src/symbol.c b/src/symbol.c index aa5b659fa..d09833689 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -188,8 +188,7 @@ sym_to_sym(mrb_state *mrb, mrb_value sym) #define is_identchar(c) (SIGN_EXTEND_CHAR(c)!=-1&&(ISALNUM(c) || (c) == '_')) static int -is_special_global_name(m) - const char *m; +is_special_global_name(const char* m) { switch (*m) { case '~': case '*': case '$': case '?': case '!': case '@': diff --git a/src/variable.c b/src/variable.c index 82ab7bef3..33cd6ba14 100644 --- a/src/variable.c +++ b/src/variable.c @@ -19,7 +19,7 @@ #include "st.h" #endif -KHASH_MAP_INIT_INT(iv, mrb_value); +KHASH_INIT(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal) #ifndef FALSE #define FALSE 0 @@ -186,7 +186,7 @@ mrb_vm_cv_set(mrb_state *mrb, mrb_sym sym, mrb_value v) if (k != kh_end(h)) { k = kh_put(iv, h, sym); kh_value(h, k) = v; - return; + return; } } c = c->super; @@ -233,26 +233,8 @@ const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym) struct RClass *c = base; khash_t(iv) *h; khiter_t k; + mrb_sym cm = mrb_intern(mrb, "const_missing"); - if (c->iv) { - h = c->iv; - k = kh_get(iv, h, sym); - if (k != kh_end(h)) { - return kh_value(h, k); - } - } - for (;;) { - c = mrb_class_outer_module(mrb, c); - if (!c) break; - if (c->iv) { - h = c->iv; - k = kh_get(iv, h, sym); - if (k != kh_end(h)) { - return kh_value(h, k); - } - } - } - c = base->super; while (c) { if (c->iv) { h = c->iv; @@ -260,19 +242,14 @@ const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym) if (k != kh_end(h)) { return kh_value(h, k); } + if (mrb_respond_to(mrb, mrb_obj_value(c), cm)) { + mrb_value argv = mrb_symbol_value(sym); + return mrb_funcall_argv(mrb, mrb_obj_value(c), "const_missing", 1, &argv); + } } c = c->super; } - if (!c) { - c = mrb->object_class; - } - - if (mrb_respond_to(mrb, mrb_obj_value(c), mrb_intern(mrb, "const_missing"))) { - mrb_value argv = mrb_symbol_value(sym); - return mrb_funcall_argv(mrb, mrb_obj_value(c), "const_missing", 1, &argv); - } - mrb_raise(mrb, E_NAME_ERROR, "uninitialized constant %s", mrb_sym2name(mrb, sym)); /* not reached */ diff --git a/test/t/kernel.rb b/test/t/kernel.rb index cd1f2d99e..847f1baeb 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -112,7 +112,17 @@ assert('Kernel#respond_to?', '15.3.1.2.43') do respond_to? :nil? end -# TODO at the moment doesn't comply to ISO assert('Kernel#send', '15.3.1.2.44') do +assert('Kernel#send', '15.3.1.2.44') do + # test with block + l = send(:lambda) do + true + end + l.call and l.class == Proc and + # test with argument + send(:respond_to?, :nil?) and + # test without argument and without block + send(:public_methods).class == Array +end assert('Kernel#singleton_methods', '15.3.1.2.45') do singleton_methods.class == Array diff --git a/test/t/module.rb b/test/t/module.rb index 854be75a5..a5331e96d 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -5,6 +5,53 @@ assert('Module', '15.2.2') do Module.class == Class end +assert('Module#const_defined?', '15.2.2.4.20') do + module Test4ConstDefined + Const4Test4ConstDefined = true + end + + Test4ConstDefined.const_defined?(:Const4Test4ConstDefined) and + not Test4ConstDefined.const_defined?(:NotExisting) +end + +assert('Module#const_get', '15.2.2.4.21') do + module Test4ConstGet + Const4Test4ConstGet = 42 + end + + Test4ConstGet.const_get(:Const4Test4ConstGet) == 42 +end + +assert('Module.const_missing', '15.2.2.4.22') do + e1 = nil + + module Test4ConstMissing + def const_missing(sym) + # ATM this redirect doesn't work + puts "PLEASE GO TO TEST CASE Module.const_missing!" + puts "IT IS WORKING NOW!! PLEASE FINALIZE." + puts "Thanks :)" + end + end + + begin + Test4ConstMissing.const_get(:ConstDoesntExist) + rescue => e2 + e1 = e2 + end + + e1.class == NameError +end + +assert('Module#const_get', '15.2.2.4.23') do + module Test4ConstSet + Const4Test4ConstSet = 42 + end + + Test4ConstSet.const_set(:Const4Test4ConstSet, 23) + Test4ConstSet.const_get(:Const4Test4ConstSet) == 23 +end + # TODO not implemented ATM assert('Module.constants', '15.2.2') do # TODO not implemented ATM assert('Module.nesting', '15.2.2') do diff --git a/test/t/string.rb b/test/t/string.rb index f38790c17..ee969a696 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -319,3 +319,11 @@ assert('String#upcase!', '15.2.10.5.43') do a == 'ABC' end + +# Not ISO specified + +assert('String interpolation (mrb_str_concat for shared strings)') do + a = "A" * 32 + "#{a}:" == "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:" +end + |
