modules/mruby/src/class.c in webruby-0.2.2 vs modules/mruby/src/class.c in webruby-0.2.4
- old
+ new
@@ -51,11 +51,11 @@
void
mrb_name_class(mrb_state *mrb, struct RClass *c, mrb_sym name)
{
mrb_obj_iv_set(mrb, (struct RObject*)c,
- mrb_intern2(mrb, "__classid__", 11), mrb_symbol_value(name));
+ mrb_intern_lit(mrb, "__classid__"), mrb_symbol_value(name));
}
#define make_metaclass(mrb, c) prepare_singleton_class((mrb), (struct RBasic*)(c))
static void
@@ -87,11 +87,11 @@
sc->super = o->c;
}
o->c = sc;
mrb_field_write_barrier(mrb, (struct RBasic*)o, (struct RBasic*)sc);
mrb_field_write_barrier(mrb, (struct RBasic*)sc, (struct RBasic*)o);
- mrb_obj_iv_set(mrb, (struct RObject*)sc, mrb_intern2(mrb, "__attached__", 12), mrb_obj_value(o));
+ mrb_obj_iv_set(mrb, (struct RObject*)sc, mrb_intern_lit(mrb, "__attached__"), mrb_obj_value(o));
}
struct RClass*
mrb_define_module_id(mrb_state *mrb, mrb_sym name)
{
@@ -114,19 +114,19 @@
setup_class(mrb_state *mrb, mrb_value outer, struct RClass *c, mrb_sym id)
{
mrb_name_class(mrb, c, id);
mrb_const_set(mrb, outer, id, mrb_obj_value(c));
mrb_obj_iv_set(mrb, (struct RObject*)c,
- mrb_intern2(mrb, "__outer__", 9), outer);
+ mrb_intern_lit(mrb, "__outer__"), outer);
}
struct RClass*
mrb_class_outer_module(mrb_state *mrb, struct RClass *c)
{
mrb_value outer;
- outer = mrb_obj_iv_get(mrb, (struct RObject*)c, mrb_intern2(mrb, "__outer__", 9));
+ outer = mrb_obj_iv_get(mrb, (struct RObject*)c, mrb_intern_lit(mrb, "__outer__"));
if (mrb_nil_p(outer)) return 0;
return mrb_class_ptr(outer);
}
struct RClass*
@@ -378,10 +378,11 @@
format specifiers:
string mruby type C type note
----------------------------------------------------------------------------------------------
o: Object [mrb_value]
+ C: class/module [mrb_value]
S: String [mrb_value]
A: Array [mrb_value]
H: Hash [mrb_value]
s: String [char*,int] Receive two arguments.
z: String [char*] NUL terminated string.
@@ -432,10 +433,33 @@
*p = *sp++;
i++;
}
}
break;
+ case 'C':
+ {
+ mrb_value *p;
+
+ p = va_arg(ap, mrb_value*);
+ if (i < argc) {
+ mrb_value ss;
+
+ ss = *sp++;
+ switch (mrb_type(ss)) {
+ case MRB_TT_CLASS:
+ case MRB_TT_MODULE:
+ case MRB_TT_SCLASS:
+ break;
+ default:
+ mrb_raisef(mrb, E_TYPE_ERROR, "%S is not class/module", ss);
+ break;
+ }
+ *p = ss;
+ i++;
+ }
+ }
+ break;
case 'S':
{
mrb_value *p;
p = va_arg(ap, mrb_value*);
@@ -574,20 +598,12 @@
mrb_raise(mrb, E_RANGE_ERROR, "float too big for int");
}
*p = (mrb_int)f;
}
break;
- case MRB_TT_FALSE:
- *p = 0;
- break;
default:
- {
- mrb_value tmp;
-
- tmp = mrb_convert_type(mrb, *sp, MRB_TT_FIXNUM, "Integer", "to_int");
- *p = mrb_fixnum(tmp);
- }
+ *p = mrb_fixnum(mrb_Integer(mrb, *sp));
break;
}
sp++;
i++;
}
@@ -738,11 +754,11 @@
mrb_mod_append_features(mrb_state *mrb, mrb_value mod)
{
mrb_value klass;
mrb_check_type(mrb, mod, MRB_TT_MODULE);
- mrb_get_args(mrb, "o", &klass);
+ mrb_get_args(mrb, "C", &klass);
mrb_include_module(mrb, mrb_class_ptr(klass), mrb_class_ptr(mod));
return mod;
}
static mrb_value
@@ -786,11 +802,11 @@
mrb_mod_include_p(mrb_state *mrb, mrb_value mod)
{
mrb_value mod2;
struct RClass *c = mrb_class_ptr(mod);
- mrb_get_args(mrb, "o", &mod2);
+ mrb_get_args(mrb, "C", &mod2);
mrb_check_type(mrb, mod2, MRB_TT_MODULE);
while (c) {
if (c->tt == MRB_TT_ICLASS) {
if (c->c == mrb_class_ptr(mod2)) return mrb_true_value();
@@ -1043,38 +1059,41 @@
mrb_value *argv;
int argc;
obj = mrb_instance_alloc(mrb, cv);
mrb_get_args(mrb, "*&", &argv, &argc, &blk);
- mrb_funcall_with_block(mrb, obj, mrb_intern2(mrb, "initialize", 10), argc, argv, blk);
+ mrb_funcall_with_block(mrb, obj, mrb_intern_lit(mrb, "initialize"), argc, argv, blk);
return obj;
}
mrb_value
mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv)
{
mrb_value obj;
obj = mrb_instance_alloc(mrb, mrb_obj_value(c));
- mrb_funcall_argv(mrb, obj, mrb_intern2(mrb, "initialize", 10), argc, argv);
+ mrb_funcall_argv(mrb, obj, mrb_intern_lit(mrb, "initialize"), argc, argv);
return obj;
}
static mrb_value
mrb_class_new_class(mrb_state *mrb, mrb_value cv)
{
- mrb_value super;
- struct RClass *new_class;
+ mrb_value super, blk;
+ mrb_value new_class;
- if (mrb_get_args(mrb, "|o", &super) == 0) {
+ if (mrb_get_args(mrb, "|C&", &super, &blk) == 0) {
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);
+ new_class = mrb_obj_value(mrb_class_new(mrb, mrb_class_ptr(super)));
+ if (!mrb_nil_p(blk)) {
+ mrb_funcall_with_block(mrb, new_class, mrb_intern_cstr(mrb, "class_eval"), 0, NULL, blk);
+ }
+ mrb_funcall(mrb, super, "inherited", 1, new_class);
+ return new_class;
}
mrb_value
mrb_class_superclass(mrb_state *mrb, mrb_value klass)
{
@@ -1142,11 +1161,11 @@
int alen;
mrb_value inspect;
mrb_get_args(mrb, "n*", &name, &a, &alen);
- if (mrb_respond_to(mrb,mod,mrb_intern2(mrb, "inspect",7))){
+ if (mrb_respond_to(mrb,mod,mrb_intern_lit(mrb, "inspect"))){
inspect = mrb_funcall(mrb, mod, "inspect", 0);
if (RSTRING_LEN(inspect) > 64) {
inspect = mrb_any_to_s(mrb, mod);
}
}
@@ -1194,11 +1213,11 @@
mrb_class_path(mrb_state *mrb, struct RClass *c)
{
mrb_value path;
const char *name;
size_t len;
- mrb_sym classpath = mrb_intern2(mrb, "__classpath__", 13);
+ mrb_sym classpath = mrb_intern_lit(mrb, "__classpath__");
path = mrb_obj_iv_get(mrb, (struct RObject*)c, classpath);
if (mrb_nil_p(path)) {
struct RClass *outer = mrb_class_outer_module(mrb, c);
mrb_sym sym = mrb_class_sym(mrb, c, outer);
@@ -1355,11 +1374,11 @@
mrb_mod_to_s(mrb_state *mrb, mrb_value klass)
{
mrb_value str;
if (mrb_type(klass) == MRB_TT_SCLASS) {
- mrb_value v = mrb_iv_get(mrb, klass, mrb_intern2(mrb, "__attached__", 12));
+ mrb_value v = mrb_iv_get(mrb, klass, mrb_intern_lit(mrb, "__attached__"));
str = mrb_str_new(mrb, "#<Class:", 8);
switch (mrb_type(v)) {
case MRB_TT_CLASS:
@@ -1891,13 +1910,13 @@
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_intern2(mrb, "BasicObject", 11));
- mrb_name_class(mrb, obj, mrb_intern2(mrb, "Object", 6));
- mrb_name_class(mrb, mod, mrb_intern2(mrb, "Module", 6));
- mrb_name_class(mrb, cls, mrb_intern2(mrb, "Class", 5));
+ mrb_name_class(mrb, bob, mrb_intern_lit(mrb, "BasicObject"));
+ mrb_name_class(mrb, obj, mrb_intern_lit(mrb, "Object"));
+ mrb_name_class(mrb, mod, mrb_intern_lit(mrb, "Module"));
+ mrb_name_class(mrb, cls, mrb_intern_lit(mrb, "Class"));
MRB_SET_INSTANCE_TT(cls, MRB_TT_CLASS);
mrb_define_method(mrb, bob, "initialize", mrb_bob_init, MRB_ARGS_NONE());
mrb_define_method(mrb, bob, "!", mrb_bob_not, MRB_ARGS_NONE());
mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, MRB_ARGS_ANY()); /* 15.3.1.3.30 */