Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Namespace management using control frame #13454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 1 addition & 10 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,8 @@ load_with_builtin_functions(const char *feature_name, const struct rb_builtin_fu
ASSUME(iseq); // otherwise an exception should have raised
vm->builtin_function_table = NULL;

rb_namespace_enable_builtin();

// exec
if (rb_namespace_available() && rb_mNamespaceRefiner) {
rb_iseq_eval_with_refinement(rb_iseq_check(iseq), rb_mNamespaceRefiner);
}
else {
rb_iseq_eval(rb_iseq_check(iseq));
}

rb_namespace_disable_builtin();
rb_iseq_eval(rb_iseq_check(iseq), rb_root_namespace()); // builtin functions are loaded in the root namespace
}

void
Expand Down
2 changes: 1 addition & 1 deletion class.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ class_alloc0(enum ruby_value_type type, VALUE klass, bool namespaceable)
{
rb_ns_subclasses_t *ns_subclasses;
rb_subclass_anchor_t *anchor;
const rb_namespace_t *ns = rb_definition_namespace();
const rb_namespace_t *ns = rb_current_namespace();

if (!ruby_namespace_init_done) {
namespaceable = true;
Expand Down
1 change: 1 addition & 0 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ruby_setup(void)
rb_vm_encoded_insn_data_table_init();
Init_enable_namespace();
Init_vm_objects();
Init_root_namespace();
Init_fstring_table();

EC_PUSH_TAG(GET_EC());
Expand Down
6 changes: 5 additions & 1 deletion eval_intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ VALUE rb_vm_make_jump_tag_but_local_jump(enum ruby_tag_type state, VALUE val);
rb_cref_t *rb_vm_cref(void);
rb_cref_t *rb_vm_cref_replace_with_duplicated_cref(void);
VALUE rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg, VALUE block_handler, VALUE filename);
VALUE rb_vm_call_cfunc2(VALUE recv, VALUE (*func)(VALUE, VALUE), VALUE arg1, VALUE arg2, VALUE block_handler, VALUE filename);
VALUE rb_vm_call_cfunc_in_namespace(VALUE recv, VALUE (*func)(VALUE, VALUE), VALUE arg1, VALUE arg2, VALUE filename, const rb_namespace_t *ns);
void rb_vm_frame_flag_set_ns_require(const rb_execution_context_t *ec);
const rb_namespace_t *rb_vm_current_namespace(const rb_execution_context_t *ec);
const rb_namespace_t *rb_vm_caller_namespace(const rb_execution_context_t *ec);
const rb_namespace_t *rb_vm_loading_namespace(const rb_execution_context_t *ec);
void rb_vm_set_progname(VALUE filename);
VALUE rb_vm_cbase(void);

Expand Down
2 changes: 1 addition & 1 deletion inits.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ rb_call_inits(void)
CALL(Time);
CALL(Random);
CALL(load);
CALL(Namespace);
CALL(Proc);
CALL(Binding);
CALL(Math);
Expand All @@ -77,7 +78,6 @@ rb_call_inits(void)
CALL(Prism);
CALL(unicode_version);
CALL(Set);
CALL(Namespace);

// enable builtin loading
CALL(builtin);
Expand Down
3 changes: 2 additions & 1 deletion insns.def
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,13 @@ defineclass
(VALUE val)
{
VALUE klass = vm_find_or_create_class_by_id(id, flags, cbase, super);
const rb_namespace_t *ns = rb_current_namespace();

rb_iseq_check(class_iseq);

/* enter scope */
vm_push_frame(ec, class_iseq, VM_FRAME_MAGIC_CLASS | VM_ENV_FLAG_LOCAL, klass,
GET_BLOCK_HANDLER(),
GC_GUARDED_PTR(ns),
(VALUE)vm_cref_push(ec, klass, NULL, FALSE, FALSE),
ISEQ_BODY(class_iseq)->iseq_encoded, GET_SP(),
ISEQ_BODY(class_iseq)->local_table_size,
Expand Down
16 changes: 6 additions & 10 deletions internal/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,7 @@ RCLASS_EXT_READABLE_LOOKUP(VALUE obj, const rb_namespace_t *ns)
static inline rb_classext_t *
RCLASS_EXT_READABLE_IN_NS(VALUE obj, const rb_namespace_t *ns)
{
if (!ns
|| NAMESPACE_BUILTIN_P(ns)
if (NAMESPACE_ROOT_P(ns)
|| RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
return RCLASS_EXT_PRIME(obj);
}
Expand All @@ -408,9 +407,9 @@ RCLASS_EXT_READABLE(VALUE obj)
if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
return RCLASS_EXT_PRIME(obj);
}
// delay namespace loading to optimize for unmodified classes
// delay determining the current namespace to optimize for unmodified classes
ns = rb_current_namespace();
if (!ns || NAMESPACE_BUILTIN_P(ns)) {
if (NAMESPACE_ROOT_P(ns)) {
return RCLASS_EXT_PRIME(obj);
}
return RCLASS_EXT_READABLE_LOOKUP(obj, ns);
Expand Down Expand Up @@ -443,8 +442,7 @@ RCLASS_EXT_WRITABLE_LOOKUP(VALUE obj, const rb_namespace_t *ns)
static inline rb_classext_t *
RCLASS_EXT_WRITABLE_IN_NS(VALUE obj, const rb_namespace_t *ns)
{
if (!ns
|| NAMESPACE_BUILTIN_P(ns)
if (NAMESPACE_ROOT_P(ns)
|| RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
return RCLASS_EXT_PRIME(obj);
}
Expand All @@ -458,11 +456,9 @@ RCLASS_EXT_WRITABLE(VALUE obj)
if (LIKELY(RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj))) {
return RCLASS_EXT_PRIME(obj);
}
// delay namespace loading to optimize for unmodified classes
// delay determining the current namespace to optimize for unmodified classes
ns = rb_current_namespace();
if (!ns || NAMESPACE_BUILTIN_P(ns)) {
// If no namespace is specified, Ruby VM is in bootstrap
// and the clean class definition is under construction.
if (NAMESPACE_ROOT_P(ns)) {
return RCLASS_EXT_PRIME(obj);
}
return RCLASS_EXT_WRITABLE_LOOKUP(obj, ns);
Expand Down
3 changes: 3 additions & 0 deletions internal/inits.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void Init_enable_namespace(void);
void Init_BareVM(void);
void Init_vm_objects(void);

/* namespace.c */
void Init_root_namespace(void);

/* vm_backtrace.c */
void Init_vm_backtrace(void);

Expand Down
21 changes: 7 additions & 14 deletions internal/namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ struct rb_namespace_struct {

VALUE gvar_tbl;

bool is_builtin;
bool is_user;
bool is_optional;
};
typedef struct rb_namespace_struct rb_namespace_t;

#define NAMESPACE_BUILTIN_P(ns) (ns && ns->is_builtin)
#define NAMESPACE_OBJ_P(obj) (rb_obj_class(obj) == rb_cNamespace)

#define NAMESPACE_ROOT_P(ns) (ns && !ns->is_user)
#define NAMESPACE_USER_P(ns) (ns && ns->is_user)
#define NAMESPACE_OPTIONAL_P(ns) (ns && ns->is_optional)
#define NAMESPACE_MAIN_P(ns) (ns && ns->is_user && !ns->is_optional)
Expand All @@ -60,24 +61,16 @@ rb_namespace_available(void)
return ruby_namespace_enabled;
}

void rb_namespace_enable_builtin(void);
void rb_namespace_disable_builtin(void);
void rb_namespace_push_loading_namespace(const rb_namespace_t *);
void rb_namespace_pop_loading_namespace(const rb_namespace_t *);
rb_namespace_t * rb_root_namespace(void);
const rb_namespace_t *rb_builtin_namespace(void);
rb_namespace_t * rb_main_namespace(void);
const rb_namespace_t * rb_definition_namespace(void);
const rb_namespace_t * rb_loading_namespace(void);
const rb_namespace_t * rb_root_namespace(void);
const rb_namespace_t * rb_main_namespace(void);
const rb_namespace_t * rb_current_namespace(void);
VALUE rb_current_namespace_details(VALUE);
const rb_namespace_t * rb_loading_namespace(void);

void rb_namespace_entry_mark(void *);
void rb_namespace_gc_update_references(void *ptr);

rb_namespace_t * rb_get_namespace_t(VALUE ns);
VALUE rb_get_namespace_object(rb_namespace_t *ns);
typedef VALUE namespace_exec_func(VALUE arg);
VALUE rb_namespace_exec(const rb_namespace_t *ns, namespace_exec_func *func, VALUE arg);

VALUE rb_namespace_local_extension(VALUE namespace, VALUE fname, VALUE path);

Expand Down
2 changes: 1 addition & 1 deletion iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,7 @@ iseqw_eval(VALUE self)
if (0 == ISEQ_BODY(iseq)->iseq_size) {
rb_raise(rb_eTypeError, "attempt to evaluate dummy InstructionSequence");
}
return rb_iseq_eval(iseq);
return rb_iseq_eval(iseq, rb_current_namespace());
}

/*
Expand Down
Loading
Loading