Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/mruby/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,12 @@ mrb_value mrb_vm_cv_get(mrb_state*, mrb_sym);
void mrb_vm_cv_set(mrb_state*, mrb_sym, mrb_value);
mrb_value mrb_vm_const_get(mrb_state*, mrb_sym);
mrb_bool mrb_vm_const_defined_p(mrb_state *mrb, mrb_callinfo *ci, mrb_sym sym);
mrb_value mrb_vm_const_get_noraise(mrb_state *mrb, mrb_callinfo *ci, mrb_sym sym);
mrb_value mrb_const_get_noraise(mrb_state *mrb, struct RClass *mod, mrb_sym sym);
mrb_value mrb_vm_const_get_noraise(mrb_state *mrb, mrb_callinfo *ci, mrb_sym sym, mrb_bool load);
mrb_value mrb_const_get_noraise(mrb_state *mrb, struct RClass *mod, mrb_sym sym, mrb_bool load);
mrb_bool mrb_const_lookup_at(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value *vp, mrb_bool load);
void mrb_autoload_define(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value path);
mrb_value mrb_autoload_path(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_bool inherit);
mrb_bool mrb_autoload_p(mrb_value v);
mrb_bool mrb_vm_cv_defined_p(mrb_state *mrb, const struct RProc *proc, mrb_sym sym);
struct RClass *mrb_vm_cref_class(mrb_state *mrb, mrb_callinfo *ci);
struct RClass *mrb_vm_definee_class(mrb_state *mrb, mrb_callinfo *ci);
Expand Down
71 changes: 63 additions & 8 deletions src/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ struct RClass*
mrb_vm_define_module(mrb_state *mrb, mrb_value outer, mrb_sym id)
{
check_if_class_or_module(mrb, outer);
if (mrb_obj_iv_defined(mrb, mrb_obj_ptr(outer), id)) {
mrb_value old = mrb_const_get(mrb, outer, id);

/* `module X` with an autoload pending for X loads it first, as in CRuby */
mrb_value old;
if (mrb_const_lookup_at(mrb, mrb_class_ptr(outer), id, &old, TRUE)) {
if (!mrb_module_p(old)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%!v is not a module", old);
}
Expand Down Expand Up @@ -652,9 +652,8 @@ mrb_vm_define_class(mrb_state *mrb, mrb_value outer, mrb_value super, mrb_sym id
s = NULL;
}
check_if_class_or_module(mrb, outer);
if (mrb_obj_iv_defined(mrb, mrb_obj_ptr(outer), id)) {
mrb_value old = mrb_const_get(mrb, outer, id);

mrb_value old;
if (mrb_const_lookup_at(mrb, mrb_class_ptr(outer), id, &old, TRUE)) {
if (!mrb_class_p(old)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%!v is not a class", old);
}
Expand Down Expand Up @@ -4654,7 +4653,7 @@ static mrb_bool
const_defined_sym(mrb_state *mrb, mrb_value mod, mrb_sym id, mrb_bool inherit, mrb_bool top)
{
check_const_name_sym(mrb, id);
if (!inherit) return mrb_iv_defined(mrb, mod, id);
if (!inherit) return mrb_const_lookup_at(mrb, mrb_class_ptr(mod), id, NULL, FALSE);
return mrb_const_defined(mrb, mod, id) || const_in_object_p(mrb, mod, id, top);
}

Expand All @@ -4671,7 +4670,8 @@ const_get_sym(mrb_state *mrb, mrb_value mod, mrb_sym id, mrb_bool inherit, mrb_b
}
return mrb_const_get(mrb, mod, id);
}
if (mrb_iv_defined(mrb, mod, id)) return mrb_iv_get(mrb, mod, id);
mrb_value v;
if (mrb_const_lookup_at(mrb, mrb_class_ptr(mod), id, &v, TRUE)) return v;
mrb_value name = mrb_symbol_value(id);
return mrb_funcall_argv(mrb, mod, MRB_SYM(const_missing), 1, &name);
}
Expand Down Expand Up @@ -4786,9 +4786,62 @@ mrb_mod_remove_const(mrb_state *mrb, mrb_value mod)
if (mrb_undef_p(val)) {
mrb_name_error(mrb, id, "constant %n not defined", id);
}
/* a pending autoload goes with nothing to show for it */
if (mrb_autoload_p(val)) return mrb_nil_value();
return val;
}

/*
* call-seq:
* mod.autoload(const, path) -> nil
*
* Registers `path` to be required, with Kernel#require, the first time
* `const` is looked up in `mod`. A constant already defined is left
* alone.
*
* module A
* autoload :B, "a/b"
* end
* A::B # requires "a/b" first
*/
static mrb_value
mrb_mod_autoload(mrb_state *mrb, mrb_value mod)
{
mrb_sym id;
mrb_value path;

mrb_get_args(mrb, "nS", &id, &path);
mrb_int len;
const char *name = mrb_sym_name_len(mrb, id, &len);
if (!mrb_const_name_p(mrb, name, len)) {
mrb_name_error(mrb, id, "autoload must be constant name: %n", id);
}
if (RSTRING_LEN(path) == 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "empty feature name");
}
mrb_check_frozen(mrb, mrb_obj_ptr(mod));
mrb_autoload_define(mrb, mrb_class_ptr(mod), id, path);
return mrb_nil_value();
}

/*
* call-seq:
* mod.autoload?(const, inherit = true) -> String or nil
*
* Returns the path registered with Module#autoload for `const` in `mod`,
* or in its ancestors when `inherit`, while it is still to be loaded; nil
* otherwise.
*/
static mrb_value
mrb_mod_autoload_p(mrb_state *mrb, mrb_value mod)
{
mrb_sym id;
mrb_bool inherit = TRUE;

mrb_get_args(mrb, "n|b", &id, &inherit);
return mrb_autoload_path(mrb, mrb_class_ptr(mod), id, inherit);
}

mrb_value
mrb_const_missing(mrb_state *mrb, mrb_value mod, mrb_sym sym)
{
Expand Down Expand Up @@ -5455,6 +5508,8 @@ static const mrb_mt_entry mod_rom_entries[] = {
MRB_MT_ENTRY(mrb_mod_attr_accessor, MRB_SYM(attr_accessor), MRB_ARGS_ANY()), /* 15.2.2.4.12 */
MRB_MT_ENTRY(mrb_mod_attr_reader, MRB_SYM(attr_reader), MRB_ARGS_ANY()), /* 15.2.2.4.13 */
MRB_MT_ENTRY(mrb_mod_attr_writer, MRB_SYM(attr_writer), MRB_ARGS_ANY()), /* 15.2.2.4.14 */
MRB_MT_ENTRY(mrb_mod_autoload, MRB_SYM(autoload), MRB_ARGS_REQ(2)),
MRB_MT_ENTRY(mrb_mod_autoload_p, MRB_SYM_Q(autoload), MRB_ARGS_ARG(1,1)),
MRB_MT_ENTRY(mrb_mod_module_eval, MRB_SYM(class_eval), MRB_ARGS_ANY()), /* 15.2.2.4.15 */
MRB_MT_ENTRY(mrb_do_nothing, MRB_SYM(const_added), MRB_ARGS_REQ(1) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_mod_const_defined, MRB_SYM_Q(const_defined), MRB_ARGS_ARG(1,1)), /* 15.2.2.4.20 */
Expand Down
54 changes: 51 additions & 3 deletions src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,59 @@ mrb_f_defined_super(mrb_state *mrb, mrb_value self)
return mrb_nil_value();
}

/* The module Kernel#autoload registers on: the caller's cref, as in CRuby */
static struct RClass*
autoload_cref_class(mrb_state *mrb)
{
mrb_callinfo *ci = &mrb->c->ci[-1];
struct RClass *c = NULL;

if (ci >= mrb->c->cibase && ci->proc) c = mrb_vm_cref_class(mrb, ci);
if (!c || c->tt == MRB_TT_SCLASS) c = mrb->object_class;
return c;
}

/*
* call-seq:
* autoload(const, path) -> nil
*
* Module#autoload on the module the call is written in, Object at the
* top level.
*/
static mrb_value
mrb_f_autoload(mrb_state *mrb, mrb_value self)
{
mrb_value *argv;
mrb_int argc;

mrb_get_args(mrb, "*", &argv, &argc);
return mrb_funcall_argv(mrb, mrb_obj_value(autoload_cref_class(mrb)), MRB_SYM(autoload), argc, argv);
}

/*
* call-seq:
* autoload?(const, inherit = true) -> String or nil
*
* Module#autoload? on the module the call is written in.
*/
static mrb_value
mrb_f_autoload_p(mrb_state *mrb, mrb_value self)
{
mrb_value *argv;
mrb_int argc;

mrb_get_args(mrb, "*", &argv, &argc);
return mrb_funcall_argv(mrb, mrb_obj_value(autoload_cref_class(mrb)), MRB_SYM_Q(autoload), argc, argv);
}

/* `path` holds the names of a constant path from its root down, and `start`
says where to look the first one up: nil for the caller's lexical scope
(`A::B::C`), or the module the path is rooted at (`Object` for `::A::B`).
Each name below the root resolves the way reading `outer::NAME` resolves
it, so a constant Object holds is out of reach through any other module,
and `const_missing` is not asked. A name that is missing, or an outer
value that is not a module, ends the walk at nil rather than raising. */
value that is not a module, ends the walk at nil rather than raising.
A pending autoload is loaded for every name but the last, as in CRuby. */
static mrb_value
mrb_f_defined_const_path(mrb_state *mrb, mrb_value self)
{
Expand All @@ -881,7 +927,7 @@ mrb_f_defined_const_path(mrb_state *mrb, mrb_value self)
if (mrb_nil_p(start)) {
mrb_callinfo *ci = &mrb->c->ci[-1];
if (ci < mrb->c->cibase || ci->proc == NULL) return mrb_nil_value();
outer = mrb_vm_const_get_noraise(mrb, ci, mrb_symbol(RARRAY_PTR(path)[0]));
outer = mrb_vm_const_get_noraise(mrb, ci, mrb_symbol(RARRAY_PTR(path)[0]), len > 1);
if (mrb_undef_p(outer)) return mrb_nil_value();
i = 1;
}
Expand All @@ -894,7 +940,7 @@ mrb_f_defined_const_path(mrb_state *mrb, mrb_value self)
return mrb_nil_value();
}
if (!mrb_symbol_p(RARRAY_PTR(path)[i])) return mrb_nil_value();
outer = mrb_const_get_noraise(mrb, mrb_class_ptr(outer), mrb_symbol(RARRAY_PTR(path)[i]));
outer = mrb_const_get_noraise(mrb, mrb_class_ptr(outer), mrb_symbol(RARRAY_PTR(path)[i]), i < len - 1);
if (mrb_undef_p(outer)) return mrb_nil_value();
}
return mrb_str_new_lit_frozen(mrb, "constant");
Expand Down Expand Up @@ -967,6 +1013,8 @@ static const mrb_mt_entry kernel_rom_entries[] = {
MRB_MT_ENTRY(mrb_eqq_m, MRB_OPSYM(eqq), MRB_ARGS_REQ(1)), /* 15.3.1.3.2 */
MRB_MT_ENTRY(mrb_obj_not_match, MRB_OPSYM(nmatch), MRB_ARGS_REQ(1)), /* 11.4.4 c) */
MRB_MT_ENTRY(mrb_cmp_m, MRB_OPSYM(cmp), MRB_ARGS_REQ(1)),
MRB_MT_ENTRY(mrb_f_autoload, MRB_SYM(autoload), MRB_ARGS_REQ(2) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_f_autoload_p, MRB_SYM_Q(autoload), MRB_ARGS_ARG(1,1) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_f_block_given_p_m, MRB_SYM_Q(block_given), MRB_ARGS_NONE() | MRB_MT_PRIVATE), /* 15.3.1.3.6 */
MRB_MT_ENTRY(mrb_obj_class_m, MRB_SYM(class), MRB_ARGS_NONE()), /* 15.3.1.3.7 */
MRB_MT_ENTRY(mrb_obj_clone, MRB_SYM(clone), MRB_ARGS_NONE()), /* 15.3.1.3.8 */
Expand Down
Loading
Loading