iij/mruby

iij/mrubyとmruby/mrubyとの差分解消 (src編)

Closed this issue · 0 comments

git diff master src してみると

diff --git a/src/class.c b/src/class.c
index 0d71886..228549b 100644
--- a/src/class.c
+++ b/src/class.c
@@ -1075,6 +1075,7 @@ mrb_class_new_class(mrb_state *mrb, mrb_value cv)
     super = mrb_obj_value(mrb->object_class);
   }
   new_class = mrb_class_new(mrb, mrb_class_ptr(super));
+  mrb_funcall(mrb, super, "inherited", 1, mrb_obj_value(new_class));
   return mrb_obj_value(new_class);
 }

diff --git a/src/hash.c b/src/hash.c
index 3684b3b..33e49d0 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -721,9 +721,11 @@ mrb_hash_replace(mrb_state *mrb, mrb_value hash)

   h2 = RHASH_TBL(hash2);
   if (h2) {
+    int hi = mrb_gc_arena_save(mrb);
     for (k = kh_begin(h2); k != kh_end(h2); k++) {
       if (kh_exist(h2, k))
         mrb_hash_set(mrb, hash, kh_key(h2, k), kh_value(h2, k));
+      mrb_gc_arena_restore(mrb, hi);
     }
   }

diff --git a/src/object.c b/src/object.c
index aafd2d8..a5190f7 100644
--- a/src/object.c
+++ b/src/object.c
@@ -375,7 +375,6 @@ static const struct types {
   {MRB_TT_STRING, "String"},
   {MRB_TT_RANGE,  "Range"},
 //    {MRB_TT_BIGNUM,  "Bignum"},
-  {MRB_TT_FILE,   "File"},
   {MRB_TT_DATA,   "Data"},  /* internal use: wrapped C pointers */
 //    {MRB_TT_VARMAP,  "Varmap"},  /* internal use: dynamic variables */
 //    {MRB_TT_NODE,  "Node"},  /* internal use: syntax tree node */
diff --git a/src/string.c b/src/string.c
index 328266d..6b4030e 100644
--- a/src/string.c
+++ b/src/string.c
@@ -19,6 +19,7 @@
 #include "mruby/class.h"
 #include "mruby/range.h"
 #include "mruby/string.h"
+#include "mruby/variable.h"
 #include "re.h"

 const char mrb_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz";
@@ -1758,7 +1759,7 @@ static const char isspacetable[256] = {
  *  <code>$;</code> is <code>nil</code> (which is the default), <i>str</i> is
  *  split on whitespace as if ` ' were specified.
  *
- *  If the <i>limit</i> parameter is omitted, trailing null fields are
+ *  If the <i>limit</i> parameter is omitted or zero, trailing null fields are
  *  suppressed. If <i>limit</i> is a positive number, at most that number of
  *  fields will be returned (if <i>limit</i> is <code>1</code>, the entire
  *  string is returned as the only entry in an array). If negative, there is no
@@ -2577,4 +2578,6 @@ mrb_init_string(mrb_state *mrb)
   mrb_define_method(mrb, s, "upcase!",         mrb_str_upcase_bang,     MRB_ARGS_REQ(1)); /* 15.2.10.5.43 */
   mrb_define_method(mrb, s, "inspect",         mrb_str_inspect,         MRB_ARGS_NONE()); /* 15.2.10.5.46(x) */
   mrb_define_method(mrb, s, "bytes",           mrb_str_bytes,           MRB_ARGS_NONE());
+
+  mrb_gv_set(mrb, mrb_intern(mrb, "$;"), mrb_nil_value());
 }
diff --git a/src/variable.c b/src/variable.c
index 2610c5d..90fbd1d 100644
--- a/src/variable.c
+++ b/src/variable.c
@@ -13,6 +13,12 @@
 #include "error.h"
 #include <ctype.h>

+static const char *const mrb_gv_alias_names[] = {
+  "$LOAD_PATH=$:",
+  "$CHILD_STATUS=$?",
+  NULL
+};
+
 typedef int (iv_foreach_func)(mrb_state*,mrb_sym,mrb_value,void*);

 #ifdef MRB_USE_IV_SEGLIST
@@ -925,6 +931,29 @@ mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val)
   mrb_define_const(mrb, mrb->object_class, name, val);
 }

+static mrb_sym
+mrb_gv_alias_name_check(mrb_state *mrb, mrb_sym sym)
+{
+  int ai = mrb_gc_arena_save(mrb);
+  const char *const *ptr = mrb_gv_alias_names;
+  mrb_value sym0 = mrb_str_new_cstr(mrb, mrb_sym2name(mrb, sym));
+  int len = RSTRING_LEN(sym0);
+
+  for (;*ptr != NULL; ptr++) {
+    char *str = strchr(*ptr, '=');
+    if (str != NULL) {
+      if (len == (str - *ptr) && strncmp(RSTRING_PTR(sym0), *ptr, len) == 0) {
+        str++;
+        sym = mrb_intern(mrb, str);
+        break;
+      }
+    }
+  }
+
+  mrb_gc_arena_restore(mrb, ai);
+  return sym;
+}
+
 static int
 const_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p)
 {
@@ -972,6 +1001,7 @@ mrb_gv_get(mrb_state *mrb, mrb_sym sym)
   if (!mrb->globals) {
     return mrb_nil_value();
   }
+  sym = mrb_gv_alias_name_check(mrb, sym);
   if (iv_get(mrb, mrb->globals, sym, &v))
     return v;
   return mrb_nil_value();
@@ -982,6 +1012,7 @@ mrb_gv_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
 {
   iv_tbl *t;

+  sym = mrb_gv_alias_name_check(mrb, sym);
   if (!mrb->globals) {
     t = mrb->globals = iv_new(mrb);
   }