Commit 878fe38b authored by mattn's avatar mattn

Gemify

parent 261efd8e
...@@ -17,6 +17,9 @@ MRuby::Build.new do |conf| ...@@ -17,6 +17,9 @@ MRuby::Build.new do |conf|
# Use standard Time class # Use standard Time class
conf.gem 'mrbgems/mruby-time' conf.gem 'mrbgems/mruby-time'
# Use standard Struct class
conf.gem 'mrbgems/mruby-struct'
# Generate binaries # Generate binaries
# conf.bins = %w(mrbc mruby mirb) # conf.bins = %w(mrbc mruby mirb)
......
...@@ -69,7 +69,7 @@ mrb_value ...@@ -69,7 +69,7 @@ mrb_value
mrb_struct_members(mrb_state *mrb, mrb_value s) 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))); mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s)));
if (mrb_type(s) == MRB_TT_STRUCT) { if (!strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s)), "Struct")) {
if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)",
RARRAY_LEN(members), RSTRUCT_LEN(s)); RARRAY_LEN(members), RSTRUCT_LEN(s));
...@@ -140,7 +140,7 @@ mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) ...@@ -140,7 +140,7 @@ mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
return ptr[i]; return ptr[i];
} }
} }
mrb_name_error(mrb, id, "%s is not struct member", mrb_sym2name(mrb, id)); mrb_raisef(mrb, E_NAME_ERROR, "%s is not struct member", mrb_sym2name(mrb, id));
return mrb_nil_value(); /* not reached */ return mrb_nil_value(); /* not reached */
} }
...@@ -219,7 +219,7 @@ mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) ...@@ -219,7 +219,7 @@ mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
} }
} }
mrb_name_error(mrb, mid, "`%s' is not a struct member", mrb_raisef(mrb, E_NAME_ERROR, "`%s' is not a struct member",
mrb_sym2name(mrb, mid)); mrb_sym2name(mrb, mid));
return mrb_nil_value(); /* not reached */ return mrb_nil_value(); /* not reached */
} }
...@@ -264,7 +264,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k ...@@ -264,7 +264,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k
name = mrb_str_to_str(mrb, name); name = mrb_str_to_str(mrb, name);
id = mrb_to_id(mrb, name); id = mrb_to_id(mrb, name);
if (!mrb_is_const_id(id)) { if (!mrb_is_const_id(id)) {
mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); mrb_raisef(mrb, E_NAME_ERROR, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name));
} }
if (mrb_const_defined_at(mrb, klass, id)) { if (mrb_const_defined_at(mrb, klass, id)) {
mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name));
...@@ -272,7 +272,6 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k ...@@ -272,7 +272,6 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k
} }
c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
} }
MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT);
nstr = mrb_obj_value(c); nstr = mrb_obj_value(c);
mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members); mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members);
...@@ -552,7 +551,7 @@ mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id) ...@@ -552,7 +551,7 @@ mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id)
return ptr[i]; return ptr[i];
} }
} }
mrb_name_error(mrb, id, "no member '%s' in struct", mrb_sym2name(mrb, id)); mrb_raisef(mrb, E_NAME_ERROR, "no member '%s' in struct", mrb_sym2name(mrb, id));
return mrb_nil_value(); /* not reached */ return mrb_nil_value(); /* not reached */
} }
...@@ -624,7 +623,7 @@ mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) ...@@ -624,7 +623,7 @@ mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
return val; return val;
} }
} }
mrb_name_error(mrb, id, "no member '%s' in struct", mrb_sym2name(mrb, id)); mrb_raisef(mrb, E_NAME_ERROR, "no member '%s' in struct", mrb_sym2name(mrb, id));
return val; /* not reach */ return val; /* not reach */
} }
...@@ -703,7 +702,7 @@ mrb_struct_equal(mrb_state *mrb, mrb_value s) ...@@ -703,7 +702,7 @@ mrb_struct_equal(mrb_state *mrb, mrb_value s)
mrb_get_args(mrb, "o", &s2); mrb_get_args(mrb, "o", &s2);
if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value();
if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); if (!strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s)), "Struct")) return mrb_false_value();
if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) 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)) { if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
mrb_bug("inconsistent struct"); /* should never happen */ mrb_bug("inconsistent struct"); /* should never happen */
...@@ -734,7 +733,7 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s) ...@@ -734,7 +733,7 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s)
mrb_get_args(mrb, "o", &s2); mrb_get_args(mrb, "o", &s2);
if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value();
if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); if (strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s2)), "Struct")) return mrb_false_value();
if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) 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)) { if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
mrb_bug("inconsistent struct"); /* should never happen */ mrb_bug("inconsistent struct"); /* should never happen */
...@@ -765,7 +764,7 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s) ...@@ -765,7 +764,7 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s)
* <code>Symbol</code> (such as <code>:name</code>). * <code>Symbol</code> (such as <code>:name</code>).
*/ */
void void
mrb_init_struct(mrb_state *mrb) mrb_mruby_struct_gem_init(mrb_state* mrb)
{ {
struct RClass *st; struct RClass *st;
st = mrb_define_class(mrb, "Struct", mrb->object_class); st = mrb_define_class(mrb, "Struct", mrb->object_class);
...@@ -783,3 +782,8 @@ mrb_init_struct(mrb_state *mrb) ...@@ -783,3 +782,8 @@ mrb_init_struct(mrb_state *mrb)
mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */ mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */
} }
void
mrb_mruby_struct_gem_final(mrb_state* mrb)
{
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment