define Module#const_missing

parent 480e3297
......@@ -1833,6 +1833,18 @@ mrb_mod_remove_const(mrb_state *mrb, mrb_value mod)
return val;
}
mrb_value
mrb_mod_const_missing(mrb_state *mrb, mrb_value mod)
{
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
mrb_name_error(mrb, sym, "uninitialized constant %S",
mrb_sym2str(mrb, sym));
/* not reached */
return mrb_nil_value();
}
static mrb_value
mrb_mod_s_constants(mrb_state *mrb, mrb_value mod)
{
......@@ -1926,6 +1938,7 @@ mrb_init_class(mrb_state *mrb)
mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, MRB_ARGS_REQ(2)); /* 15.2.2.4.23 */
mrb_define_method(mrb, mod, "constants", mrb_mod_constants, MRB_ARGS_NONE()); /* 15.2.2.4.24 */
mrb_define_method(mrb, mod, "remove_const", mrb_mod_remove_const, MRB_ARGS_REQ(1)); /* 15.2.2.4.40 */
mrb_define_method(mrb, mod, "const_missing", mrb_mod_const_missing, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mod, "define_method", mod_define_method, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, MRB_ARGS_NONE()); /* 15.2.2.4.19 */
mrb_define_method(mrb, mod, "===", mrb_mod_eqq, MRB_ARGS_REQ(1));
......
......@@ -867,7 +867,7 @@ const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym)
mrb_value v;
iv_tbl *t;
mrb_bool retry = 0;
mrb_sym cm;
mrb_value name;
L_RETRY:
while (c) {
......@@ -883,19 +883,8 @@ L_RETRY:
retry = 1;
goto L_RETRY;
}
c = base;
cm = mrb_intern2(mrb, "const_missing", 13);
while (c) {
if (mrb_respond_to(mrb, mrb_obj_value(c), cm)) {
mrb_value name = mrb_symbol_value(sym);
return mrb_funcall_argv(mrb, mrb_obj_value(c), cm, 1, &name);
}
c = c->super;
}
mrb_name_error(mrb, sym, "uninitialized constant %S",
mrb_sym2str(mrb, sym));
/* not reached */
return mrb_nil_value();
name = mrb_symbol_value(sym);
return mrb_funcall_argv(mrb, mrb_obj_value(base), mrb_intern2(mrb, "const_missing", 13), 1, &name);
}
mrb_value
......
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