summaryrefslogtreecommitdiff
path: root/load.c
diff options
context:
space:
mode:
authorSatoshi Tagomori <[email protected]>2025-04-30 13:48:02 +0900
committerSatoshi Tagomori <[email protected]>2025-05-11 23:32:50 +0900
commit382645d440d5da66a0c04557f3ff2ca226de3a27 (patch)
treeb7453449930197237e739d0985561b664f51b0f3 /load.c
parent49742414f6444960838bb968bab43db27f5872c1 (diff)
namespace on read
Diffstat (limited to 'load.c')
-rw-r--r--load.c446
1 files changed, 304 insertions, 142 deletions
diff --git a/load.c b/load.c
index 4023ac03ba..d0a1880186 100644
--- a/load.c
+++ b/load.c
@@ -7,9 +7,11 @@
#include "internal.h"
#include "internal/dir.h"
#include "internal/error.h"
+#include "internal/eval.h"
#include "internal/file.h"
#include "internal/hash.h"
#include "internal/load.h"
+#include "internal/namespace.h"
#include "internal/ruby_parser.h"
#include "internal/thread.h"
#include "internal/variable.h"
@@ -19,6 +21,7 @@
#include "ruby/encoding.h"
#include "ruby/util.h"
#include "ractor_core.h"
+#include "vm_core.h"
static VALUE ruby_dln_libmap;
@@ -36,6 +39,38 @@ static VALUE ruby_dln_libmap;
# error Need integer for VALUE
#endif
+#define IS_NAMESPACE(obj) (CLASS_OF(obj) == rb_cNamespace)
+
+struct vm_and_namespace_struct {
+ rb_vm_t *vm;
+ rb_namespace_t *ns;
+};
+typedef struct vm_and_namespace_struct vm_ns_t;
+#define GET_vm_ns() vm_ns_t vm_ns_v = { .vm = GET_VM(), .ns = (rb_namespace_t *)rb_current_namespace(), }; vm_ns_t *vm_ns = &vm_ns_v;
+#define GET_loading_vm_ns() vm_ns_t vm_ns_v = { .vm = GET_VM(), .ns = (rb_namespace_t *)rb_loading_namespace(), }; vm_ns_t *vm_ns = &vm_ns_v;
+
+#define CURRENT_NS_attr(vm_ns, attr) (NAMESPACE_USER_P(vm_ns->ns) ? vm_ns->ns->attr : vm_ns->vm->attr)
+#define SET_NS_attr(vm_ns, attr, value) do { \
+ if (NAMESPACE_USER_P(vm_ns->ns)) { vm_ns->ns->attr = value; } \
+ else { vm_ns->vm->attr = value; } \
+} while (0)
+
+#define SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, value) SET_NS_attr(vm_ns, load_path_check_cache, value)
+#define SET_NS_EXPANDED_LOAD_PATH(vm_ns, value) SET_NS_attr(vm_ns, expanded_load_path, value)
+
+#define CURRENT_NS_LOAD_PATH(vm_ns) CURRENT_NS_attr(vm_ns, load_path)
+#define CURRENT_NS_LOAD_PATH_SNAPSHOT(vm_ns) CURRENT_NS_attr(vm_ns, load_path_snapshot)
+#define CURRENT_NS_LOAD_PATH_CHECK_CACHE(vm_ns) CURRENT_NS_attr(vm_ns, load_path_check_cache)
+#define CURRENT_NS_EXPANDED_LOAD_PATH(vm_ns) CURRENT_NS_attr(vm_ns, expanded_load_path)
+#define CURRENT_NS_LOADING_TABLE(vm_ns) CURRENT_NS_attr(vm_ns, loading_table)
+#define CURRENT_NS_LOADED_FEATURES(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features)
+#define CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_snapshot)
+#define CURRENT_NS_LOADED_FEATURES_REALPATHS(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_realpaths)
+#define CURRENT_NS_LOADED_FEATURES_REALPATH_MAP(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_realpath_map)
+#define CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_index)
+
+#define CURRENT_NS_RUBY_DLN_LIBMAP(vm_ns, map) (NAMESPACE_USER_P(vm_ns->ns) ? vm_ns->ns->ruby_dln_libmap : map)
+
enum {
loadable_ext_rb = (0+ /* .rb extension is the first in both tables */
1) /* offset by rb_find_file_ext() */
@@ -64,10 +99,11 @@ enum expand_type {
string objects in $LOAD_PATH are frozen.
*/
static void
-rb_construct_expanded_load_path(rb_vm_t *vm, enum expand_type type, int *has_relative, int *has_non_cache)
+rb_construct_expanded_load_path(vm_ns_t *vm_ns, enum expand_type type, int *has_relative, int *has_non_cache)
{
- VALUE load_path = vm->load_path;
- VALUE expanded_load_path = vm->expanded_load_path;
+ VALUE load_path = CURRENT_NS_LOAD_PATH(vm_ns);
+ VALUE expanded_load_path = CURRENT_NS_EXPANDED_LOAD_PATH(vm_ns);
+ VALUE snapshot;
VALUE ary;
long i;
@@ -106,108 +142,117 @@ rb_construct_expanded_load_path(rb_vm_t *vm, enum expand_type type, int *has_rel
rb_ary_push(ary, rb_fstring(expanded_path));
}
rb_ary_freeze(ary);
- vm->expanded_load_path = ary;
- rb_ary_replace(vm->load_path_snapshot, vm->load_path);
+ SET_NS_EXPANDED_LOAD_PATH(vm_ns, ary);
+ snapshot = CURRENT_NS_LOAD_PATH_SNAPSHOT(vm_ns);
+ load_path = CURRENT_NS_LOAD_PATH(vm_ns);
+ rb_ary_replace(snapshot, load_path);
}
static VALUE
-get_expanded_load_path(rb_vm_t *vm)
+get_expanded_load_path(vm_ns_t *vm_ns)
{
+ VALUE check_cache;
const VALUE non_cache = Qtrue;
+ const VALUE load_path_snapshot = CURRENT_NS_LOAD_PATH_SNAPSHOT(vm_ns);
+ const VALUE load_path = CURRENT_NS_LOAD_PATH(vm_ns);
- if (!rb_ary_shared_with_p(vm->load_path_snapshot, vm->load_path)) {
+ if (!rb_ary_shared_with_p(load_path_snapshot, load_path)) {
/* The load path was modified. Rebuild the expanded load path. */
int has_relative = 0, has_non_cache = 0;
- rb_construct_expanded_load_path(vm, EXPAND_ALL, &has_relative, &has_non_cache);
+ rb_construct_expanded_load_path(vm_ns, EXPAND_ALL, &has_relative, &has_non_cache);
if (has_relative) {
- vm->load_path_check_cache = rb_dir_getwd_ospath();
+ SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, rb_dir_getwd_ospath());
}
else if (has_non_cache) {
/* Non string object. */
- vm->load_path_check_cache = non_cache;
+ SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, non_cache);
}
else {
- vm->load_path_check_cache = 0;
+ SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, 0);
}
}
- else if (vm->load_path_check_cache == non_cache) {
+ else if ((check_cache = CURRENT_NS_LOAD_PATH_CHECK_CACHE(vm_ns)) == non_cache) {
int has_relative = 1, has_non_cache = 1;
/* Expand only non-cacheable objects. */
- rb_construct_expanded_load_path(vm, EXPAND_NON_CACHE,
+ rb_construct_expanded_load_path(vm_ns, EXPAND_NON_CACHE,
&has_relative, &has_non_cache);
}
- else if (vm->load_path_check_cache) {
+ else if (check_cache) {
int has_relative = 1, has_non_cache = 1;
VALUE cwd = rb_dir_getwd_ospath();
- if (!rb_str_equal(vm->load_path_check_cache, cwd)) {
+ if (!rb_str_equal(check_cache, cwd)) {
/* Current working directory or filesystem encoding was changed.
Expand relative load path and non-cacheable objects again. */
- vm->load_path_check_cache = cwd;
- rb_construct_expanded_load_path(vm, EXPAND_RELATIVE,
+ SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, cwd);
+ rb_construct_expanded_load_path(vm_ns, EXPAND_RELATIVE,
&has_relative, &has_non_cache);
}
else {
/* Expand only tilde (User HOME) and non-cacheable objects. */
- rb_construct_expanded_load_path(vm, EXPAND_HOME,
+ rb_construct_expanded_load_path(vm_ns, EXPAND_HOME,
&has_relative, &has_non_cache);
}
}
- return vm->expanded_load_path;
+ return CURRENT_NS_EXPANDED_LOAD_PATH(vm_ns);
}
VALUE
rb_get_expanded_load_path(void)
{
- return get_expanded_load_path(GET_VM());
+ GET_loading_vm_ns();
+ return get_expanded_load_path(vm_ns);
}
static VALUE
load_path_getter(ID id, VALUE * p)
{
- rb_vm_t *vm = (void *)p;
- return vm->load_path;
+ GET_loading_vm_ns();
+ return CURRENT_NS_LOAD_PATH(vm_ns);
}
static VALUE
-get_loaded_features(rb_vm_t *vm)
+get_loaded_features(vm_ns_t *vm_ns)
{
- return vm->loaded_features;
+ return CURRENT_NS_LOADED_FEATURES(vm_ns);
}
static VALUE
-get_loaded_features_realpaths(rb_vm_t *vm)
+get_loaded_features_realpaths(vm_ns_t *vm_ns)
{
- return vm->loaded_features_realpaths;
+ return CURRENT_NS_LOADED_FEATURES_REALPATHS(vm_ns);
}
static VALUE
-get_loaded_features_realpath_map(rb_vm_t *vm)
+get_loaded_features_realpath_map(vm_ns_t *vm_ns)
{
- return vm->loaded_features_realpath_map;
+ return CURRENT_NS_LOADED_FEATURES_REALPATH_MAP(vm_ns);
}
static VALUE
get_LOADED_FEATURES(ID _x, VALUE *_y)
{
- return get_loaded_features(GET_VM());
+ GET_loading_vm_ns();
+ return get_loaded_features(vm_ns);
}
static void
-reset_loaded_features_snapshot(rb_vm_t *vm)
+reset_loaded_features_snapshot(vm_ns_t *vm_ns)
{
- rb_ary_replace(vm->loaded_features_snapshot, vm->loaded_features);
+ VALUE snapshot = CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns);
+ VALUE loaded_features = CURRENT_NS_LOADED_FEATURES(vm_ns);
+ rb_ary_replace(snapshot, loaded_features);
}
static struct st_table *
-get_loaded_features_index_raw(rb_vm_t *vm)
+get_loaded_features_index_raw(vm_ns_t *vm_ns)
{
- return vm->loaded_features_index;
+ return CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns);
}
static st_table *
-get_loading_table(rb_vm_t *vm)
+get_loading_table(vm_ns_t *vm_ns)
{
- return vm->loading_table;
+ return CURRENT_NS_LOADING_TABLE(vm_ns);
}
static st_data_t
@@ -228,7 +273,7 @@ is_rbext_path(VALUE feature_path)
typedef rb_darray(long) feature_indexes_t;
struct features_index_add_single_args {
- rb_vm_t *vm;
+ vm_ns_t *vm_ns;
VALUE offset;
bool rb;
};
@@ -237,7 +282,7 @@ static int
features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args, int existing)
{
struct features_index_add_single_args *args = (struct features_index_add_single_args *)raw_args;
- rb_vm_t *vm = args->vm;
+ vm_ns_t *vm_ns = args->vm_ns;
VALUE offset = args->offset;
bool rb = args->rb;
@@ -245,7 +290,7 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r
VALUE this_feature_index = *value;
if (FIXNUM_P(this_feature_index)) {
- VALUE loaded_features = get_loaded_features(vm);
+ VALUE loaded_features = get_loaded_features(vm_ns);
VALUE this_feature_path = RARRAY_AREF(loaded_features, FIX2LONG(this_feature_index));
feature_indexes_t feature_indexes;
@@ -265,7 +310,7 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r
long pos = -1;
if (rb) {
- VALUE loaded_features = get_loaded_features(vm);
+ VALUE loaded_features = get_loaded_features(vm_ns);
for (size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
long idx = rb_darray_get(feature_indexes, i);
VALUE this_feature_path = RARRAY_AREF(loaded_features, idx);
@@ -297,7 +342,7 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r
}
static void
-features_index_add_single(rb_vm_t *vm, const char* str, size_t len, VALUE offset, bool rb)
+features_index_add_single(vm_ns_t *vm_ns, const char* str, size_t len, VALUE offset, bool rb)
{
struct st_table *features_index;
st_data_t short_feature_key;
@@ -305,10 +350,10 @@ features_index_add_single(rb_vm_t *vm, const char* str, size_t len, VALUE offset
Check_Type(offset, T_FIXNUM);
short_feature_key = feature_key(str, len);
- features_index = get_loaded_features_index_raw(vm);
+ features_index = get_loaded_features_index_raw(vm_ns);
struct features_index_add_single_args args = {
- .vm = vm,
+ .vm_ns = vm_ns,
.offset = offset,
.rb = rb,
};
@@ -325,7 +370,7 @@ features_index_add_single(rb_vm_t *vm, const char* str, size_t len, VALUE offset
relies on for its fast lookup.
*/
static void
-features_index_add(rb_vm_t *vm, VALUE feature, VALUE offset)
+features_index_add(vm_ns_t *vm_ns, VALUE feature, VALUE offset)
{
const char *feature_str, *feature_end, *ext, *p;
bool rb = false;
@@ -351,14 +396,14 @@ features_index_add(rb_vm_t *vm, VALUE feature, VALUE offset)
if (p < feature_str)
break;
/* Now *p == '/'. We reach this point for every '/' in `feature`. */
- features_index_add_single(vm, p + 1, feature_end - p - 1, offset, false);
+ features_index_add_single(vm_ns, p + 1, feature_end - p - 1, offset, false);
if (ext) {
- features_index_add_single(vm, p + 1, ext - p - 1, offset, rb);
+ features_index_add_single(vm_ns, p + 1, ext - p - 1, offset, rb);
}
}
- features_index_add_single(vm, feature_str, feature_end - feature_str, offset, false);
+ features_index_add_single(vm_ns, feature_str, feature_end - feature_str, offset, false);
if (ext) {
- features_index_add_single(vm, feature_str, ext - feature_str, offset, rb);
+ features_index_add_single(vm_ns, feature_str, ext - feature_str, offset, rb);
}
}
@@ -375,27 +420,31 @@ loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
void
rb_free_loaded_features_index(rb_vm_t *vm)
{
+ /* Destructs vm->loaded_features_index directly because this is only for
+ the VM destruction */
st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
st_free_table(vm->loaded_features_index);
}
+
+
static st_table *
-get_loaded_features_index(rb_vm_t *vm)
+get_loaded_features_index(vm_ns_t *vm_ns)
{
- VALUE features;
int i;
+ VALUE features = CURRENT_NS_LOADED_FEATURES(vm_ns);
+ const VALUE snapshot = CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns);
- if (!rb_ary_shared_with_p(vm->loaded_features_snapshot, vm->loaded_features)) {
+ if (!rb_ary_shared_with_p(snapshot, features)) {
/* The sharing was broken; something (other than us in rb_provide_feature())
modified loaded_features. Rebuild the index. */
- st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
+ st_foreach(CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns), loaded_features_index_clear_i, 0);
- VALUE realpaths = vm->loaded_features_realpaths;
- VALUE realpath_map = vm->loaded_features_realpath_map;
+ VALUE realpaths = CURRENT_NS_LOADED_FEATURES_REALPATHS(vm_ns);
+ VALUE realpath_map = CURRENT_NS_LOADED_FEATURES_REALPATH_MAP(vm_ns);
VALUE previous_realpath_map = rb_hash_dup(realpath_map);
rb_hash_clear(realpaths);
rb_hash_clear(realpath_map);
- features = vm->loaded_features;
for (i = 0; i < RARRAY_LEN(features); i++) {
VALUE entry, as_str;
as_str = entry = rb_ary_entry(features, i);
@@ -403,11 +452,11 @@ get_loaded_features_index(rb_vm_t *vm)
as_str = rb_fstring(as_str);
if (as_str != entry)
rb_ary_store(features, i, as_str);
- features_index_add(vm, as_str, INT2FIX(i));
+ features_index_add(vm_ns, as_str, INT2FIX(i));
}
- reset_loaded_features_snapshot(vm);
+ reset_loaded_features_snapshot(vm_ns);
- features = rb_ary_dup(vm->loaded_features_snapshot);
+ features = CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns);
long j = RARRAY_LEN(features);
for (i = 0; i < j; i++) {
VALUE as_str = rb_ary_entry(features, i);
@@ -421,7 +470,7 @@ get_loaded_features_index(rb_vm_t *vm)
rb_hash_aset(realpath_map, as_str, realpath);
}
}
- return vm->loaded_features_index;
+ return CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns);
}
/* This searches `load_path` for a value such that
@@ -506,7 +555,7 @@ loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
* 'u': unsuffixed
*/
static int
-rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn)
+rb_feature_p(vm_ns_t *vm_ns, const char *feature, const char *ext, int rb, int expanded, const char **fn)
{
VALUE features, this_feature_index = Qnil, v, p, load_path = 0;
const char *f, *e;
@@ -527,8 +576,8 @@ rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expa
elen = 0;
type = 0;
}
- features = get_loaded_features(vm);
- features_index = get_loaded_features_index(vm);
+ features = get_loaded_features(vm_ns);
+ features_index = get_loaded_features_index(vm_ns);
key = feature_key(feature, strlen(feature));
/* We search `features` for an entry such that either
@@ -575,7 +624,7 @@ rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expa
if ((n = RSTRING_LEN(v)) < len) continue;
if (strncmp(f, feature, len) != 0) {
if (expanded) continue;
- if (!load_path) load_path = get_expanded_load_path(vm);
+ if (!load_path) load_path = get_expanded_load_path(vm_ns);
if (!(p = loaded_feature_path(f, n, feature, len, type, load_path)))
continue;
expanded = 1;
@@ -595,14 +644,14 @@ rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expa
}
}
- loading_tbl = get_loading_table(vm);
+ loading_tbl = get_loading_table(vm_ns);
f = 0;
if (!expanded && !rb_is_absolute_path(feature)) {
struct loaded_feature_searching fs;
fs.name = feature;
fs.len = len;
fs.type = type;
- fs.load_path = load_path ? load_path : get_expanded_load_path(vm);
+ fs.load_path = load_path ? load_path : get_expanded_load_path(vm_ns);
fs.result = 0;
st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
if ((f = fs.result) != 0) {
@@ -657,7 +706,7 @@ rb_provided(const char *feature)
}
static int
-feature_provided(rb_vm_t *vm, const char *feature, const char **loading)
+feature_provided(vm_ns_t *vm_ns, const char *feature, const char **loading)
{
const char *ext = strrchr(feature, '.');
VALUE fullpath = 0;
@@ -669,15 +718,15 @@ feature_provided(rb_vm_t *vm, const char *feature, const char **loading)
}
if (ext && !strchr(ext, '/')) {
if (IS_RBEXT(ext)) {
- if (rb_feature_p(vm, feature, ext, TRUE, FALSE, loading)) return TRUE;
+ if (rb_feature_p(vm_ns, feature, ext, TRUE, FALSE, loading)) return TRUE;
return FALSE;
}
else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
- if (rb_feature_p(vm, feature, ext, FALSE, FALSE, loading)) return TRUE;
+ if (rb_feature_p(vm_ns, feature, ext, FALSE, FALSE, loading)) return TRUE;
return FALSE;
}
}
- if (rb_feature_p(vm, feature, 0, TRUE, FALSE, loading))
+ if (rb_feature_p(vm_ns, feature, 0, TRUE, FALSE, loading))
return TRUE;
RB_GC_GUARD(fullpath);
return FALSE;
@@ -686,35 +735,41 @@ feature_provided(rb_vm_t *vm, const char *feature, const char **loading)
int
rb_feature_provided(const char *feature, const char **loading)
{
- return feature_provided(GET_VM(), feature, loading);
+ GET_vm_ns();
+ return feature_provided(vm_ns, feature, loading);
}
static void
-rb_provide_feature(rb_vm_t *vm, VALUE feature)
+rb_provide_feature(vm_ns_t *vm_ns, VALUE feature)
{
VALUE features;
- features = get_loaded_features(vm);
+ features = get_loaded_features(vm_ns);
if (OBJ_FROZEN(features)) {
rb_raise(rb_eRuntimeError,
"$LOADED_FEATURES is frozen; cannot append feature");
}
feature = rb_fstring(feature);
- get_loaded_features_index(vm);
+ get_loaded_features_index(vm_ns);
// If loaded_features and loaded_features_snapshot share the same backing
// array, pushing into it would cause the whole array to be copied.
// To avoid this we first clear loaded_features_snapshot.
- rb_ary_clear(vm->loaded_features_snapshot);
+ rb_ary_clear(CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns));
rb_ary_push(features, feature);
- features_index_add(vm, feature, INT2FIX(RARRAY_LEN(features)-1));
- reset_loaded_features_snapshot(vm);
+ features_index_add(vm_ns, feature, INT2FIX(RARRAY_LEN(features)-1));
+ reset_loaded_features_snapshot(vm_ns);
}
void
rb_provide(const char *feature)
{
- rb_provide_feature(GET_VM(), rb_fstring_cstr(feature));
+ /*
+ * rb_provide() must use rb_current_namespace to store provided features
+ * in the current namespace's loaded_features, etc.
+ */
+ GET_vm_ns();
+ rb_provide_feature(vm_ns, rb_fstring_cstr(feature));
}
NORETURN(static void load_failed(VALUE));
@@ -732,17 +787,34 @@ realpath_internal_cached(VALUE hash, VALUE path)
return realpath;
}
+struct iseq_eval_in_namespace_data {
+ const rb_iseq_t *iseq;
+ bool in_builtin;
+};
+
+static VALUE
+iseq_eval_in_namespace(VALUE arg)
+{
+ struct iseq_eval_in_namespace_data *data = (struct iseq_eval_in_namespace_data *)arg;
+ if (rb_namespace_available() && data->in_builtin) {
+ return rb_iseq_eval_with_refinement(data->iseq, rb_mNamespaceRefiner);
+ } else {
+ return rb_iseq_eval(data->iseq);
+ }
+}
+
static inline void
load_iseq_eval(rb_execution_context_t *ec, VALUE fname)
{
+ GET_loading_vm_ns();
+ const rb_namespace_t *loading_ns = rb_loading_namespace();
const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
if (!iseq) {
rb_execution_context_t *ec = GET_EC();
VALUE v = rb_vm_push_frame_fname(ec, fname);
- rb_thread_t *th = rb_ec_thread_ptr(ec);
- VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
+ VALUE realpath_map = get_loaded_features_realpath_map(vm_ns);
if (rb_ruby_prism_p()) {
pm_parse_result_t result = { 0 };
@@ -786,13 +858,23 @@ load_iseq_eval(rb_execution_context_t *ec, VALUE fname)
RB_GC_GUARD(v);
}
rb_exec_event_hook_script_compiled(ec, iseq, Qnil);
- rb_iseq_eval(iseq);
+
+ if (loading_ns) {
+ struct iseq_eval_in_namespace_data arg = {
+ .iseq = iseq,
+ .in_builtin = NAMESPACE_BUILTIN_P(loading_ns),
+ };
+ rb_namespace_exec(loading_ns, iseq_eval_in_namespace, (VALUE)&arg);
+ } else {
+ rb_iseq_eval(iseq);
+ }
}
static inline enum ruby_tag_type
load_wrapping(rb_execution_context_t *ec, VALUE fname, VALUE load_wrapper)
{
enum ruby_tag_type state;
+ rb_namespace_t *ns;
rb_thread_t *th = rb_ec_thread_ptr(ec);
volatile VALUE wrapper = th->top_wrapper;
volatile VALUE self = th->top_self;
@@ -803,7 +885,15 @@ load_wrapping(rb_execution_context_t *ec, VALUE fname, VALUE load_wrapper)
ec->errinfo = Qnil; /* ensure */
/* load in module as toplevel */
- th->top_self = rb_obj_clone(rb_vm_top_self());
+ if (IS_NAMESPACE(load_wrapper)) {
+ ns = rb_get_namespace_t(load_wrapper);
+ if (!ns->top_self) {
+ ns->top_self = rb_obj_clone(rb_vm_top_self());
+ }
+ th->top_self = ns->top_self;
+ } else {
+ th->top_self = rb_obj_clone(rb_vm_top_self());
+ }
th->top_wrapper = load_wrapper;
rb_extend_object(th->top_self, th->top_wrapper);
@@ -838,7 +928,9 @@ raise_load_if_failed(rb_execution_context_t *ec, enum ruby_tag_type state)
static void
rb_load_internal(VALUE fname, VALUE wrap)
{
+ VALUE namespace;
rb_execution_context_t *ec = GET_EC();
+ const rb_namespace_t *ns = rb_loading_namespace();
enum ruby_tag_type state = TAG_NONE;
if (RTEST(wrap)) {
if (!RB_TYPE_P(wrap, T_MODULE)) {
@@ -846,6 +938,10 @@ rb_load_internal(VALUE fname, VALUE wrap)
}
state = load_wrapping(ec, fname, wrap);
}
+ else if (NAMESPACE_OPTIONAL_P(ns)) {
+ namespace = ns->ns_object;
+ state = load_wrapping(ec, fname, namespace);
+ }
else {
load_iseq_eval(ec, fname);
}
@@ -874,6 +970,40 @@ rb_load_protect(VALUE fname, int wrap, int *pstate)
if (state != TAG_NONE) *pstate = state;
}
+static VALUE
+load_entrypoint_internal(VALUE fname, VALUE wrap)
+{
+ VALUE path, orig_fname;
+
+ orig_fname = rb_get_path_check_to_string(fname);
+ fname = rb_str_encode_ospath(orig_fname);
+ RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
+
+ path = rb_find_file(fname);
+ if (!path) {
+ if (!rb_file_load_ok(RSTRING_PTR(fname)))
+ load_failed(orig_fname);
+ path = fname;
+ }
+ rb_load_internal(path, wrap);
+
+ RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
+
+ return Qtrue;
+}
+
+VALUE
+rb_load_entrypoint(VALUE args)
+{
+ VALUE fname, wrap;
+ if (RARRAY_LEN(args) != 2) {
+ rb_bug("invalid arguments: %ld", RARRAY_LEN(args));
+ }
+ fname = rb_ary_entry(args, 0);
+ wrap = rb_ary_entry(args, 1);
+ return load_entrypoint_internal(fname, wrap);
+}
+
/*
* call-seq:
* load(filename, wrap=false) -> true
@@ -907,32 +1037,16 @@ rb_load_protect(VALUE fname, int wrap, int *pstate)
static VALUE
rb_f_load(int argc, VALUE *argv, VALUE _)
{
- VALUE fname, wrap, path, orig_fname;
-
+ VALUE fname, wrap;
rb_scan_args(argc, argv, "11", &fname, &wrap);
-
- orig_fname = rb_get_path_check_to_string(fname);
- fname = rb_str_encode_ospath(orig_fname);
- RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
-
- path = rb_find_file(fname);
- if (!path) {
- if (!rb_file_load_ok(RSTRING_PTR(fname)))
- load_failed(orig_fname);
- path = fname;
- }
- rb_load_internal(path, wrap);
-
- RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
-
- return Qtrue;
+ return load_entrypoint_internal(fname, wrap);
}
static char *
-load_lock(rb_vm_t *vm, const char *ftptr, bool warn)
+load_lock(vm_ns_t *vm_ns, const char *ftptr, bool warn)
{
st_data_t data;
- st_table *loading_tbl = get_loading_table(vm);
+ st_table *loading_tbl = get_loading_table(vm_ns);
if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
/* partial state */
@@ -974,11 +1088,11 @@ release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done, int exis
}
static void
-load_unlock(rb_vm_t *vm, const char *ftptr, int done)
+load_unlock(vm_ns_t *vm_ns, const char *ftptr, int done)
{
if (ftptr) {
st_data_t key = (st_data_t)ftptr;
- st_table *loading_tbl = get_loading_table(vm);
+ st_table *loading_tbl = get_loading_table(vm_ns);
st_update(loading_tbl, key, release_thread_shield, done);
}
@@ -1026,9 +1140,22 @@ static VALUE rb_require_string_internal(VALUE fname, bool resurrect);
VALUE
rb_f_require(VALUE obj, VALUE fname)
{
+ // const rb_namespace_t *ns = rb_loading_namespace();
+ // printf("F:current loading ns: %ld\n", ns->ns_id);
return rb_require_string(fname);
}
+VALUE
+rb_require_relative_entrypoint(VALUE fname)
+{
+ VALUE base = rb_current_realfilepath();
+ if (NIL_P(base)) {
+ rb_loaderror("cannot infer basepath");
+ }
+ base = rb_file_dirname(base);
+ return rb_require_string_internal(rb_file_absolute_path(fname, base), false);
+}
+
/*
* call-seq:
* require_relative(string) -> true or false
@@ -1041,18 +1168,13 @@ rb_f_require(VALUE obj, VALUE fname)
VALUE
rb_f_require_relative(VALUE obj, VALUE fname)
{
- VALUE base = rb_current_realfilepath();
- if (NIL_P(base)) {
- rb_loaderror("cannot infer basepath");
- }
- base = rb_file_dirname(base);
- return rb_require_string_internal(rb_file_absolute_path(fname, base), false);
+ return rb_require_relative_entrypoint(fname);
}
-typedef int (*feature_func)(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn);
+typedef int (*feature_func)(vm_ns_t *vm_ns, const char *feature, const char *ext, int rb, int expanded, const char **fn);
static int
-search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_feature_p)
+search_required(vm_ns_t *vm_ns, VALUE fname, volatile VALUE *path, feature_func rb_feature_p)
{
VALUE tmp;
char *ext, *ftptr;
@@ -1063,20 +1185,20 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
ext = strrchr(ftptr = RSTRING_PTR(fname), '.');
if (ext && !strchr(ext, '/')) {
if (IS_RBEXT(ext)) {
- if (rb_feature_p(vm, ftptr, ext, TRUE, FALSE, &loading)) {
+ if (rb_feature_p(vm_ns, ftptr, ext, TRUE, FALSE, &loading)) {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 'r';
}
if ((tmp = rb_find_file(fname)) != 0) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
- if (!rb_feature_p(vm, ftptr, ext, TRUE, TRUE, &loading) || loading)
+ if (!rb_feature_p(vm_ns, ftptr, ext, TRUE, TRUE, &loading) || loading)
*path = tmp;
return 'r';
}
return 0;
}
else if (IS_SOEXT(ext)) {
- if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
+ if (rb_feature_p(vm_ns, ftptr, ext, FALSE, FALSE, &loading)) {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 's';
}
@@ -1085,25 +1207,25 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
OBJ_FREEZE(tmp);
if ((tmp = rb_find_file(tmp)) != 0) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
- if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
+ if (!rb_feature_p(vm_ns, ftptr, ext, FALSE, TRUE, &loading) || loading)
*path = tmp;
return 's';
}
}
else if (IS_DLEXT(ext)) {
- if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
+ if (rb_feature_p(vm_ns, ftptr, ext, FALSE, FALSE, &loading)) {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 's';
}
if ((tmp = rb_find_file(fname)) != 0) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
- if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
+ if (!rb_feature_p(vm_ns, ftptr, ext, FALSE, TRUE, &loading) || loading)
*path = tmp;
return 's';
}
}
}
- else if ((ft = rb_feature_p(vm, ftptr, 0, FALSE, FALSE, &loading)) == 'r') {
+ else if ((ft = rb_feature_p(vm_ns, ftptr, 0, FALSE, FALSE, &loading)) == 'r') {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 'r';
}
@@ -1112,7 +1234,7 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
// Check if it's a statically linked extension when
// not already a feature and not found as a dynamic library.
- if (!ft && type != loadable_ext_rb && vm->static_ext_inits) {
+ if (!ft && type != loadable_ext_rb && vm_ns->vm->static_ext_inits) {
VALUE lookup_name = tmp;
// Append ".so" if not already present so for example "etc" can find "etc.so".
// We always register statically linked extensions with a ".so" extension.
@@ -1122,7 +1244,7 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
rb_str_cat_cstr(lookup_name, ".so");
}
ftptr = RSTRING_PTR(lookup_name);
- if (st_lookup(vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
+ if (st_lookup(vm_ns->vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
*path = rb_filesystem_str_new_cstr(ftptr);
RB_GC_GUARD(lookup_name);
return 's';
@@ -1134,7 +1256,7 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
if (ft)
goto feature_present;
ftptr = RSTRING_PTR(tmp);
- return rb_feature_p(vm, ftptr, 0, FALSE, TRUE, 0);
+ return rb_feature_p(vm_ns, ftptr, 0, FALSE, TRUE, 0);
default:
if (ft) {
@@ -1143,7 +1265,7 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
/* fall through */
case loadable_ext_rb:
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
- if (rb_feature_p(vm, ftptr, ext, type == loadable_ext_rb, TRUE, &loading) && !loading)
+ if (rb_feature_p(vm_ns, ftptr, ext, type == loadable_ext_rb, TRUE, &loading) && !loading)
break;
*path = tmp;
}
@@ -1161,10 +1283,15 @@ load_failed(VALUE fname)
}
static VALUE
-load_ext(VALUE path)
+load_ext(VALUE path, VALUE fname)
{
+ VALUE loaded = path;
+ GET_loading_vm_ns();
+ if (NAMESPACE_USER_P(vm_ns->ns)) {
+ loaded = rb_namespace_local_extension(vm_ns->ns->ns_object, fname, path);
+ }
rb_scope_visibility_set(METHOD_VISI_PUBLIC);
- return (VALUE)dln_load(RSTRING_PTR(path));
+ return (VALUE)dln_load_feature(RSTRING_PTR(loaded), RSTRING_PTR(fname));
}
static bool
@@ -1181,7 +1308,7 @@ run_static_ext_init(rb_vm_t *vm, const char *feature)
}
static int
-no_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn)
+no_feature_p(vm_ns_t *vm_ns, const char *feature, const char *ext, int rb, int expanded, const char **fn)
{
return 0;
}
@@ -1193,10 +1320,11 @@ rb_resolve_feature_path(VALUE klass, VALUE fname)
VALUE path;
int found;
VALUE sym;
+ GET_loading_vm_ns();
fname = rb_get_path(fname);
path = rb_str_encode_ospath(fname);
- found = search_required(GET_VM(), path, &path, no_feature_p);
+ found = search_required(vm_ns, path, &path, no_feature_p);
switch (found) {
case 'r':
@@ -1231,6 +1359,20 @@ rb_ext_ractor_safe(bool flag)
GET_THREAD()->ext_config.ractor_safe = flag;
}
+struct rb_vm_call_cfunc2_data {
+ VALUE recv;
+ VALUE arg1;
+ VALUE arg2;
+ VALUE block_handler;
+ VALUE filename;
+};
+
+static VALUE
+call_load_ext_in_ns(VALUE data){
+ struct rb_vm_call_cfunc2_data *arg = (struct rb_vm_call_cfunc2_data *)data;
+ return rb_vm_call_cfunc2(arg->recv, load_ext, arg->arg1, arg->arg2, arg->block_handler, arg->filename);
+}
+
/*
* returns
* 0: if already loaded (false)
@@ -1250,13 +1392,14 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
th->top_wrapper, th->top_self, ec->errinfo,
ec,
};
+ GET_loading_vm_ns();
enum ruby_tag_type state;
char *volatile ftptr = 0;
VALUE path;
volatile VALUE saved_path;
volatile VALUE realpath = 0;
- VALUE realpaths = get_loaded_features_realpaths(th->vm);
- VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
+ VALUE realpaths = get_loaded_features_realpaths(vm_ns);
+ VALUE realpath_map = get_loaded_features_realpath_map(vm_ns);
volatile bool reset_ext_config = false;
struct rb_ext_config prev_ext_config;
@@ -1272,12 +1415,12 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
int found;
RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
- found = search_required(th->vm, path, &saved_path, rb_feature_p);
+ found = search_required(vm_ns, path, &saved_path, rb_feature_p);
RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
path = saved_path;
if (found) {
- if (!path || !(ftptr = load_lock(th->vm, RSTRING_PTR(path), warn))) {
+ if (!path || !(ftptr = load_lock(vm_ns, RSTRING_PTR(path), warn))) {
result = 0;
}
else if (!*ftptr) {
@@ -1293,15 +1436,30 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
else {
switch (found) {
case 'r':
- load_iseq_eval(saved.ec, path);
+ // iseq_eval_in_namespace will be called with the loading namespace eventually
+ if (NAMESPACE_OPTIONAL_P(vm_ns->ns)) {
+ // check with NAMESPACE_OPTIONAL_P (not NAMESPACE_USER_P) for NS1::xxx naming
+ // it is not expected for the main namespace
+ load_wrapping(saved.ec, path, vm_ns->ns->ns_object);
+ } else {
+ load_iseq_eval(saved.ec, path);
+ }
break;
case 's':
+ // the loading namespace must be set to the current namespace before calling load_ext
reset_ext_config = true;
ext_config_push(th, &prev_ext_config);
- handle = rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
- path, VM_BLOCK_HANDLER_NONE, path);
- rb_hash_aset(ruby_dln_libmap, path, SVALUE2NUM((SIGNED_VALUE)handle));
+ struct rb_vm_call_cfunc2_data arg = {
+ .recv = rb_vm_top_self(),
+ .arg1 = path,
+ .arg2 = fname,
+ .block_handler = VM_BLOCK_HANDLER_NONE,
+ .filename = path,
+ };
+ handle = rb_namespace_exec(vm_ns->ns, call_load_ext_in_ns, (VALUE)&arg);
+ rb_hash_aset(CURRENT_NS_RUBY_DLN_LIBMAP(vm_ns, ruby_dln_libmap), path,
+ SVALUE2NUM((SIGNED_VALUE)handle));
break;
}
result = TAG_RETURN;
@@ -1317,7 +1475,7 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
if (reset_ext_config) ext_config_pop(th2, &prev_ext_config);
path = saved_path;
- if (ftptr) load_unlock(th2->vm, RSTRING_PTR(path), !state);
+ if (ftptr) load_unlock(vm_ns, RSTRING_PTR(path), !state);
if (state) {
if (state == TAG_FATAL || state == TAG_THROW) {
@@ -1343,7 +1501,7 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
}
if (result == TAG_RETURN) {
- rb_provide_feature(th2->vm, path);
+ rb_provide_feature(vm_ns, path);
VALUE real = realpath;
if (real) {
real = rb_fstring(real);
@@ -1442,15 +1600,15 @@ void
ruby_init_ext(const char *name, void (*init)(void))
{
st_table *inits_table;
- rb_vm_t *vm = GET_VM();
+ GET_loading_vm_ns();
- if (feature_provided(vm, name, 0))
+ if (feature_provided(vm_ns, name, 0))
return;
- inits_table = vm->static_ext_inits;
+ inits_table = vm_ns->vm->static_ext_inits;
if (!inits_table) {
inits_table = st_init_strtable();
- vm->static_ext_inits = inits_table;
+ vm_ns->vm->static_ext_inits = inits_table;
}
st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init);
}
@@ -1595,6 +1753,7 @@ rb_ext_resolve_symbol(const char* fname, const char* symbol)
VALUE path;
char *ext;
VALUE fname_str = rb_str_new_cstr(fname);
+ GET_loading_vm_ns();
resolved = rb_resolve_feature_path((VALUE)NULL, fname_str);
if (NIL_P(resolved)) {
@@ -1602,7 +1761,7 @@ rb_ext_resolve_symbol(const char* fname, const char* symbol)
if (!ext || !IS_SOEXT(ext)) {
rb_str_cat_cstr(fname_str, ".so");
}
- if (rb_feature_p(GET_VM(), fname, 0, FALSE, FALSE, 0)) {
+ if (rb_feature_p(vm_ns, fname, 0, FALSE, FALSE, 0)) {
return dln_symbol(NULL, symbol);
}
return NULL;
@@ -1611,7 +1770,7 @@ rb_ext_resolve_symbol(const char* fname, const char* symbol)
return NULL;
}
path = rb_ary_entry(resolved, 1);
- handle = rb_hash_lookup(ruby_dln_libmap, path);
+ handle = rb_hash_lookup(CURRENT_NS_RUBY_DLN_LIBMAP(vm_ns, ruby_dln_libmap), path);
if (NIL_P(handle)) {
return NULL;
}
@@ -1626,6 +1785,7 @@ Init_load(void)
ID id_load_path = rb_intern2(var_load_path, sizeof(var_load_path)-1);
rb_define_hooked_variable(var_load_path, (VALUE*)vm, load_path_getter, rb_gvar_readonly_setter);
+ rb_gvar_namespace_ready(var_load_path);
rb_alias_variable(rb_intern_const("$-I"), id_load_path);
rb_alias_variable(rb_intern_const("$LOAD_PATH"), id_load_path);
vm->load_path = rb_ary_new();
@@ -1635,7 +1795,9 @@ Init_load(void)
rb_define_singleton_method(vm->load_path, "resolve_feature_path", rb_resolve_feature_path, 1);
rb_define_virtual_variable("$\"", get_LOADED_FEATURES, 0);
- rb_define_virtual_variable("$LOADED_FEATURES", get_LOADED_FEATURES, 0);
+ rb_gvar_namespace_ready("$\"");
+ rb_define_virtual_variable("$LOADED_FEATURES", get_LOADED_FEATURES, 0); // TODO: rb_alias_variable ?
+ rb_gvar_namespace_ready("$LOADED_FEATURES");
vm->loaded_features = rb_ary_new();
vm->loaded_features_snapshot = rb_ary_hidden_new(0);
vm->loaded_features_index = st_init_numtable();