summaryrefslogtreecommitdiff
path: root/load.c
diff options
context:
space:
mode:
Diffstat (limited to 'load.c')
-rw-r--r--load.c893
1 files changed, 534 insertions, 359 deletions
diff --git a/load.c b/load.c
index f2f55fde2b..a9c08c62a8 100644
--- a/load.c
+++ b/load.c
@@ -8,8 +8,9 @@
#include "internal/dir.h"
#include "internal/error.h"
#include "internal/file.h"
+#include "internal/hash.h"
#include "internal/load.h"
-#include "internal/parse.h"
+#include "internal/ruby_parser.h"
#include "internal/thread.h"
#include "internal/variable.h"
#include "iseq.h"
@@ -18,12 +19,27 @@
#include "ruby/encoding.h"
#include "ruby/util.h"
-static VALUE ruby_dln_librefs;
+static VALUE ruby_dln_libmap;
#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
+#if SIZEOF_VALUE <= SIZEOF_LONG
+# define SVALUE2NUM(x) LONG2NUM((long)(x))
+# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LONG(x)
+#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
+# define SVALUE2NUM(x) LL2NUM((LONG_LONG)(x))
+# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LL(x)
+#else
+# error Need integer for VALUE
+#endif
+
+enum {
+ loadable_ext_rb = (0+ /* .rb extension is the first in both tables */
+ 1) /* offset by rb_find_file_ext() */
+};
+
static const char *const loadable_ext[] = {
".rb", DLEXT,
0
@@ -54,39 +70,39 @@ rb_construct_expanded_load_path(rb_vm_t *vm, enum expand_type type, int *has_rel
VALUE ary;
long i;
- ary = rb_ary_tmp_new(RARRAY_LEN(load_path));
+ ary = rb_ary_hidden_new(RARRAY_LEN(load_path));
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
- VALUE path, as_str, expanded_path;
- int is_string, non_cache;
- char *as_cstr;
- as_str = path = RARRAY_AREF(load_path, i);
- is_string = RB_TYPE_P(path, T_STRING) ? 1 : 0;
- non_cache = !is_string ? 1 : 0;
+ VALUE path, as_str, expanded_path;
+ int is_string, non_cache;
+ char *as_cstr;
+ as_str = path = RARRAY_AREF(load_path, i);
+ is_string = RB_TYPE_P(path, T_STRING) ? 1 : 0;
+ non_cache = !is_string ? 1 : 0;
as_str = rb_get_path_check_to_string(path);
- as_cstr = RSTRING_PTR(as_str);
-
- if (!non_cache) {
- if ((type == EXPAND_RELATIVE &&
- rb_is_absolute_path(as_cstr)) ||
- (type == EXPAND_HOME &&
- (!as_cstr[0] || as_cstr[0] != '~')) ||
- (type == EXPAND_NON_CACHE)) {
- /* Use cached expanded path. */
- rb_ary_push(ary, RARRAY_AREF(expanded_load_path, i));
- continue;
- }
- }
- if (!*has_relative && !rb_is_absolute_path(as_cstr))
- *has_relative = 1;
- if (!*has_non_cache && non_cache)
- *has_non_cache = 1;
- /* Freeze only string object. We expand other objects every time. */
- if (is_string)
- rb_str_freeze(path);
+ as_cstr = RSTRING_PTR(as_str);
+
+ if (!non_cache) {
+ if ((type == EXPAND_RELATIVE &&
+ rb_is_absolute_path(as_cstr)) ||
+ (type == EXPAND_HOME &&
+ (!as_cstr[0] || as_cstr[0] != '~')) ||
+ (type == EXPAND_NON_CACHE)) {
+ /* Use cached expanded path. */
+ rb_ary_push(ary, RARRAY_AREF(expanded_load_path, i));
+ continue;
+ }
+ }
+ if (!*has_relative && !rb_is_absolute_path(as_cstr))
+ *has_relative = 1;
+ if (!*has_non_cache && non_cache)
+ *has_non_cache = 1;
+ /* Freeze only string object. We expand other objects every time. */
+ if (is_string)
+ rb_str_freeze(path);
as_str = rb_get_path_check_convert(as_str);
- expanded_path = rb_check_realpath(Qnil, as_str, NULL);
- if (NIL_P(expanded_path)) expanded_path = as_str;
- rb_ary_push(ary, rb_fstring(expanded_path));
+ expanded_path = rb_check_realpath(Qnil, as_str, NULL);
+ if (NIL_P(expanded_path)) expanded_path = as_str;
+ rb_ary_push(ary, rb_fstring(expanded_path));
}
rb_obj_freeze(ary);
vm->expanded_load_path = ary;
@@ -99,41 +115,41 @@ get_expanded_load_path(rb_vm_t *vm)
const VALUE non_cache = Qtrue;
if (!rb_ary_shared_with_p(vm->load_path_snapshot, vm->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);
- if (has_relative) {
- vm->load_path_check_cache = rb_dir_getwd_ospath();
- }
- else if (has_non_cache) {
- /* Non string object. */
- vm->load_path_check_cache = non_cache;
- }
- else {
- vm->load_path_check_cache = 0;
- }
+ /* 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);
+ if (has_relative) {
+ vm->load_path_check_cache = rb_dir_getwd_ospath();
+ }
+ else if (has_non_cache) {
+ /* Non string object. */
+ vm->load_path_check_cache = non_cache;
+ }
+ else {
+ vm->load_path_check_cache = 0;
+ }
}
else if (vm->load_path_check_cache == non_cache) {
- int has_relative = 1, has_non_cache = 1;
- /* Expand only non-cacheable objects. */
- rb_construct_expanded_load_path(vm, EXPAND_NON_CACHE,
- &has_relative, &has_non_cache);
+ int has_relative = 1, has_non_cache = 1;
+ /* Expand only non-cacheable objects. */
+ rb_construct_expanded_load_path(vm, EXPAND_NON_CACHE,
+ &has_relative, &has_non_cache);
}
else if (vm->load_path_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)) {
- /* 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,
- &has_relative, &has_non_cache);
- }
- else {
- /* Expand only tilde (User HOME) and non-cacheable objects. */
- rb_construct_expanded_load_path(vm, EXPAND_HOME,
- &has_relative, &has_non_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)) {
+ /* 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,
+ &has_relative, &has_non_cache);
+ }
+ else {
+ /* Expand only tilde (User HOME) and non-cacheable objects. */
+ rb_construct_expanded_load_path(vm, EXPAND_HOME,
+ &has_relative, &has_non_cache);
+ }
}
return vm->expanded_load_path;
}
@@ -164,6 +180,12 @@ get_loaded_features_realpaths(rb_vm_t *vm)
}
static VALUE
+get_loaded_features_realpath_map(rb_vm_t *vm)
+{
+ return vm->loaded_features_realpath_map;
+}
+
+static VALUE
get_LOADED_FEATURES(ID _x, VALUE *_y)
{
return get_loaded_features(GET_VM());
@@ -231,9 +253,9 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r
rb_darray_set(feature_indexes, top^0, FIX2LONG(this_feature_index));
rb_darray_set(feature_indexes, top^1, FIX2LONG(offset));
- assert(rb_darray_size(feature_indexes) == 2);
+ RUBY_ASSERT(rb_darray_size(feature_indexes) == 2);
// assert feature_indexes does not look like a special const
- assert(!SPECIAL_CONST_P((VALUE)feature_indexes));
+ RUBY_ASSERT(!SPECIAL_CONST_P((VALUE)feature_indexes));
*value = (st_data_t)feature_indexes;
}
@@ -311,10 +333,10 @@ features_index_add(rb_vm_t *vm, VALUE feature, VALUE offset)
feature_end = feature_str + RSTRING_LEN(feature);
for (ext = feature_end; ext > feature_str; ext--)
- if (*ext == '.' || *ext == '/')
- break;
+ if (*ext == '.' || *ext == '/')
+ break;
if (*ext != '.')
- ext = NULL;
+ ext = NULL;
else
rb = IS_RBEXT(ext);
/* Now `ext` points to the only string matching %r{^\.[^./]*$} that is
@@ -322,20 +344,20 @@ features_index_add(rb_vm_t *vm, VALUE feature, VALUE offset)
p = ext ? ext : feature_end;
while (1) {
- p--;
- while (p >= feature_str && *p != '/')
- p--;
- 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);
- if (ext) {
- features_index_add_single(vm, p + 1, ext - p - 1, offset, rb);
- }
+ p--;
+ while (p >= feature_str && *p != '/')
+ p--;
+ 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);
+ if (ext) {
+ features_index_add_single(vm, p + 1, ext - p - 1, offset, rb);
+ }
}
features_index_add_single(vm, 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, feature_str, ext - feature_str, offset, rb);
}
}
@@ -349,6 +371,13 @@ loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
return ST_DELETE;
}
+void
+rb_free_loaded_features_index(rb_vm_t *vm)
+{
+ 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)
{
@@ -356,31 +385,39 @@ get_loaded_features_index(rb_vm_t *vm)
int i;
if (!rb_ary_shared_with_p(vm->loaded_features_snapshot, vm->loaded_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);
+ /* 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);
VALUE realpaths = vm->loaded_features_realpaths;
+ VALUE realpath_map = vm->loaded_features_realpath_map;
+ VALUE previous_realpath_map = rb_hash_dup(realpath_map);
rb_hash_clear(realpaths);
- features = vm->loaded_features;
- for (i = 0; i < RARRAY_LEN(features); i++) {
- VALUE entry, as_str;
- as_str = entry = rb_ary_entry(features, i);
- StringValue(as_str);
- as_str = rb_fstring(rb_str_freeze(as_str));
- if (as_str != entry)
- rb_ary_store(features, i, as_str);
- features_index_add(vm, as_str, INT2FIX(i));
- }
- reset_loaded_features_snapshot(vm);
+ 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);
+ StringValue(as_str);
+ 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));
+ }
+ reset_loaded_features_snapshot(vm);
features = rb_ary_dup(vm->loaded_features_snapshot);
long j = RARRAY_LEN(features);
for (i = 0; i < j; i++) {
VALUE as_str = rb_ary_entry(features, i);
- VALUE realpath = rb_check_realpath(Qnil, as_str, NULL);
- if (NIL_P(realpath)) realpath = as_str;
- rb_hash_aset(realpaths, rb_fstring(realpath), Qtrue);
+ VALUE realpath = rb_hash_aref(previous_realpath_map, as_str);
+ if (NIL_P(realpath)) {
+ realpath = rb_check_realpath(Qnil, as_str, NULL);
+ if (NIL_P(realpath)) realpath = as_str;
+ realpath = rb_fstring(realpath);
+ }
+ rb_hash_aset(realpaths, realpath, Qtrue);
+ rb_hash_aset(realpath_map, as_str, realpath);
}
}
return vm->loaded_features_index;
@@ -399,7 +436,7 @@ get_loaded_features_index(rb_vm_t *vm)
*/
static VALUE
loaded_feature_path(const char *name, long vlen, const char *feature, long len,
- int type, VALUE load_path)
+ int type, VALUE load_path)
{
long i;
long plen;
@@ -407,36 +444,36 @@ loaded_feature_path(const char *name, long vlen, const char *feature, long len,
if (vlen < len+1) return 0;
if (strchr(feature, '.') && !strncmp(name+(vlen-len), feature, len)) {
- plen = vlen - len;
+ plen = vlen - len;
}
else {
- for (e = name + vlen; name != e && *e != '.' && *e != '/'; --e);
- if (*e != '.' ||
- e-name < len ||
- strncmp(e-len, feature, len))
- return 0;
- plen = e - name - len;
+ for (e = name + vlen; name != e && *e != '.' && *e != '/'; --e);
+ if (*e != '.' ||
+ e-name < len ||
+ strncmp(e-len, feature, len))
+ return 0;
+ plen = e - name - len;
}
if (plen > 0 && name[plen-1] != '/') {
- return 0;
+ return 0;
}
if (type == 's' ? !IS_DLEXT(&name[plen+len]) :
- type == 'r' ? !IS_RBEXT(&name[plen+len]) :
- 0) {
- return 0;
+ type == 'r' ? !IS_RBEXT(&name[plen+len]) :
+ 0) {
+ return 0;
}
/* Now name == "#{prefix}/#{feature}#{ext}" where ext is acceptable
(possibly empty) and prefix is some string of length plen. */
if (plen > 0) --plen; /* exclude '.' */
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
- VALUE p = RARRAY_AREF(load_path, i);
- const char *s = StringValuePtr(p);
- long n = RSTRING_LEN(p);
+ VALUE p = RARRAY_AREF(load_path, i);
+ const char *s = StringValuePtr(p);
+ long n = RSTRING_LEN(p);
- if (n != plen) continue;
- if (n && strncmp(name, s, n)) continue;
- return p;
+ if (n != plen) continue;
+ if (n && strncmp(name, s, n)) continue;
+ return p;
}
return 0;
}
@@ -455,12 +492,18 @@ loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
const char *s = (const char *)v;
struct loaded_feature_searching *fp = (struct loaded_feature_searching *)f;
VALUE p = loaded_feature_path(s, strlen(s), fp->name, fp->len,
- fp->type, fp->load_path);
+ fp->type, fp->load_path);
if (!p) return ST_CONTINUE;
fp->result = s;
return ST_STOP;
}
+/*
+ * Returns the type of already provided feature.
+ * 'r': ruby script (".rb")
+ * 's': shared object (".so"/"."DLEXT)
+ * 'u': unsuffixed
+ */
static int
rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn)
{
@@ -474,14 +517,14 @@ rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expa
if (fn) *fn = 0;
if (ext) {
- elen = strlen(ext);
- len = strlen(feature) - elen;
- type = rb ? 'r' : 's';
+ elen = strlen(ext);
+ len = strlen(feature) - elen;
+ type = rb ? 'r' : 's';
}
else {
- len = strlen(feature);
- elen = 0;
- type = 0;
+ len = strlen(feature);
+ elen = 0;
+ type = 0;
}
features = get_loaded_features(vm);
features_index = get_loaded_features_index(vm);
@@ -515,89 +558,89 @@ rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expa
*/
if (st_lookup(features_index, key, &data) && !NIL_P(this_feature_index = (VALUE)data)) {
for (size_t i = 0; ; i++) {
- long index;
+ long index;
if (FIXNUM_P(this_feature_index)) {
- if (i > 0) break;
+ if (i > 0) break;
index = FIX2LONG(this_feature_index);
- }
+ }
else {
feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
if (i >= rb_darray_size(feature_indexes)) break;
index = rb_darray_get(feature_indexes, i);
}
- v = RARRAY_AREF(features, index);
- f = StringValuePtr(v);
- 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 (!(p = loaded_feature_path(f, n, feature, len, type, load_path)))
- continue;
- expanded = 1;
- f += RSTRING_LEN(p) + 1;
- }
- if (!*(e = f + len)) {
- if (ext) continue;
- return 'u';
- }
- if (*e != '.') continue;
- if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
- return 's';
- }
- if ((rb || !ext) && (IS_RBEXT(e))) {
- return 'r';
- }
- }
+ v = RARRAY_AREF(features, index);
+ f = StringValuePtr(v);
+ 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 (!(p = loaded_feature_path(f, n, feature, len, type, load_path)))
+ continue;
+ expanded = 1;
+ f += RSTRING_LEN(p) + 1;
+ }
+ if (!*(e = f + len)) {
+ if (ext) continue;
+ return 'u';
+ }
+ if (*e != '.') continue;
+ if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
+ return 's';
+ }
+ if ((rb || !ext) && (IS_RBEXT(e))) {
+ return 'r';
+ }
+ }
}
loading_tbl = get_loading_table(vm);
f = 0;
if (!expanded) {
- 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.result = 0;
- st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
- if ((f = fs.result) != 0) {
- if (fn) *fn = f;
- goto loading;
- }
+ 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.result = 0;
+ st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
+ if ((f = fs.result) != 0) {
+ if (fn) *fn = f;
+ goto loading;
+ }
}
if (st_get_key(loading_tbl, (st_data_t)feature, &data)) {
- if (fn) *fn = (const char*)data;
+ if (fn) *fn = (const char*)data;
goto loading;
}
else {
- VALUE bufstr;
- char *buf;
- static const char so_ext[][4] = {
- ".so", ".o",
- };
-
- if (ext && *ext) return 0;
- bufstr = rb_str_tmp_new(len + DLEXT_MAXLEN);
- buf = RSTRING_PTR(bufstr);
- MEMCPY(buf, feature, char, len);
- for (i = 0; (e = loadable_ext[i]) != 0; i++) {
- strlcpy(buf + len, e, DLEXT_MAXLEN + 1);
- if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
- rb_str_resize(bufstr, 0);
- if (fn) *fn = (const char*)data;
- return i ? 's' : 'r';
- }
- }
- for (i = 0; i < numberof(so_ext); i++) {
- strlcpy(buf + len, so_ext[i], DLEXT_MAXLEN + 1);
- if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
- rb_str_resize(bufstr, 0);
- if (fn) *fn = (const char*)data;
- return 's';
- }
- }
- rb_str_resize(bufstr, 0);
+ VALUE bufstr;
+ char *buf;
+ static const char so_ext[][4] = {
+ ".so", ".o",
+ };
+
+ if (ext && *ext) return 0;
+ bufstr = rb_str_tmp_new(len + DLEXT_MAXLEN);
+ buf = RSTRING_PTR(bufstr);
+ MEMCPY(buf, feature, char, len);
+ for (i = 0; (e = loadable_ext[i]) != 0; i++) {
+ strlcpy(buf + len, e, DLEXT_MAXLEN + 1);
+ if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
+ rb_str_resize(bufstr, 0);
+ if (fn) *fn = (const char*)data;
+ return i ? 's' : 'r';
+ }
+ }
+ for (i = 0; i < numberof(so_ext); i++) {
+ strlcpy(buf + len, so_ext[i], DLEXT_MAXLEN + 1);
+ if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
+ rb_str_resize(bufstr, 0);
+ if (fn) *fn = (const char*)data;
+ return 's';
+ }
+ }
+ rb_str_resize(bufstr, 0);
}
return 0;
@@ -619,22 +662,22 @@ feature_provided(rb_vm_t *vm, const char *feature, const char **loading)
VALUE fullpath = 0;
if (*feature == '.' &&
- (feature[1] == '/' || strncmp(feature+1, "./", 2) == 0)) {
- fullpath = rb_file_expand_path_fast(rb_get_path(rb_str_new2(feature)), Qnil);
- feature = RSTRING_PTR(fullpath);
+ (feature[1] == '/' || strncmp(feature+1, "./", 2) == 0)) {
+ fullpath = rb_file_expand_path_fast(rb_get_path(rb_str_new2(feature)), Qnil);
+ feature = RSTRING_PTR(fullpath);
}
if (ext && !strchr(ext, '/')) {
- if (IS_RBEXT(ext)) {
- if (rb_feature_p(vm, 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;
- return FALSE;
- }
+ if (IS_RBEXT(ext)) {
+ if (rb_feature_p(vm, 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;
+ return FALSE;
+ }
}
if (rb_feature_p(vm, feature, 0, TRUE, FALSE, loading))
- return TRUE;
+ return TRUE;
RB_GC_GUARD(fullpath);
return FALSE;
}
@@ -652,17 +695,17 @@ rb_provide_feature(rb_vm_t *vm, VALUE feature)
features = get_loaded_features(vm);
if (OBJ_FROZEN(features)) {
- rb_raise(rb_eRuntimeError,
- "$LOADED_FEATURES is frozen; cannot append feature");
+ rb_raise(rb_eRuntimeError,
+ "$LOADED_FEATURES is frozen; cannot append feature");
}
- rb_str_freeze(feature);
+ feature = rb_fstring(feature);
get_loaded_features_index(vm);
// 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_push(features, rb_fstring(feature));
+ rb_ary_push(features, feature);
features_index_add(vm, feature, INT2FIX(RARRAY_LEN(features)-1));
reset_loaded_features_snapshot(vm);
}
@@ -675,19 +718,61 @@ rb_provide(const char *feature)
NORETURN(static void load_failed(VALUE));
+static inline VALUE
+realpath_internal_cached(VALUE hash, VALUE path)
+{
+ VALUE ret = rb_hash_aref(hash, path);
+ if(RTEST(ret)) {
+ return ret;
+ }
+
+ VALUE realpath = rb_realpath_internal(Qnil, path, 1);
+ rb_hash_aset(hash, rb_fstring(path), rb_fstring(realpath));
+ return realpath;
+}
+
static inline void
load_iseq_eval(rb_execution_context_t *ec, VALUE fname)
{
const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
if (!iseq) {
- rb_ast_t *ast;
- VALUE parser = rb_parser_new();
- rb_parser_set_context(parser, NULL, FALSE);
- ast = (rb_ast_t *)rb_parser_load_file(parser, fname);
- iseq = rb_iseq_new_top(&ast->body, rb_fstring_lit("<top (required)>"),
- fname, rb_realpath_internal(Qnil, fname, 1), NULL);
- rb_ast_dispose(ast);
+ 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);
+
+ if (*rb_ruby_prism_ptr()) {
+ pm_parse_result_t result = { 0 };
+ result.options.line = 1;
+
+ VALUE error = pm_load_parse_file(&result, fname);
+
+ if (error == Qnil) {
+ iseq = pm_iseq_new_top(&result.node, rb_fstring_lit("<top (required)>"), fname, realpath_internal_cached(realpath_map, fname), NULL);
+ pm_parse_result_free(&result);
+ }
+ else {
+ rb_vm_pop_frame(ec);
+ RB_GC_GUARD(v);
+ pm_parse_result_free(&result);
+ rb_exc_raise(error);
+ }
+ }
+ else {
+ rb_ast_t *ast;
+ VALUE parser = rb_parser_new();
+ rb_parser_set_context(parser, NULL, FALSE);
+ ast = (rb_ast_t *)rb_parser_load_file(parser, fname);
+
+ iseq = rb_iseq_new_top(&ast->body, rb_fstring_lit("<top (required)>"),
+ fname, realpath_internal_cached(realpath_map, fname), NULL);
+ rb_ast_dispose(ast);
+ }
+
+ rb_vm_pop_frame(ec);
+ RB_GC_GUARD(v);
}
rb_exec_event_hook_script_compiled(ec, iseq, Qnil);
rb_iseq_eval(iseq);
@@ -802,9 +887,8 @@ rb_load_protect(VALUE fname, int wrap, int *pstate)
* LoadError will be raised.
*
* If the optional _wrap_ parameter is +true+, the loaded script will
- * be executed under an anonymous module, protecting the calling
- * program's global namespace. If the optional _wrap_ parameter is a
- * module, the loaded script will be executed under the given module.
+ * be executed under an anonymous module. If the optional _wrap_ parameter
+ * is a module, the loaded script will be executed under the given module.
* In no circumstance will any local variables in the loaded file be
* propagated to the loading environment.
*/
@@ -822,9 +906,9 @@ rb_f_load(int argc, VALUE *argv, VALUE _)
path = rb_find_file(fname);
if (!path) {
- if (!rb_file_load_ok(RSTRING_PTR(fname)))
- load_failed(orig_fname);
- path = fname;
+ if (!rb_file_load_ok(RSTRING_PTR(fname)))
+ load_failed(orig_fname);
+ path = fname;
}
rb_load_internal(path, wrap);
@@ -840,29 +924,22 @@ load_lock(rb_vm_t *vm, const char *ftptr, bool warn)
st_table *loading_tbl = get_loading_table(vm);
if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
- /* partial state */
- ftptr = ruby_strdup(ftptr);
- data = (st_data_t)rb_thread_shield_new();
- st_insert(loading_tbl, (st_data_t)ftptr, data);
- return (char *)ftptr;
+ /* partial state */
+ ftptr = ruby_strdup(ftptr);
+ data = (st_data_t)rb_thread_shield_new();
+ st_insert(loading_tbl, (st_data_t)ftptr, data);
+ return (char *)ftptr;
}
- else if (imemo_type_p(data, imemo_memo)) {
- struct MEMO *memo = MEMO_CAST(data);
- void (*init)(void) = memo->u3.func;
- data = (st_data_t)rb_thread_shield_new();
- st_insert(loading_tbl, (st_data_t)ftptr, data);
- (*init)();
- return (char *)"";
- }
- if (warn) {
- VALUE warning = rb_warning_string("loading in progress, circular require considered harmful - %s", ftptr);
- rb_backtrace_each(rb_str_append, warning);
- rb_warning("%"PRIsVALUE, warning);
+
+ if (warn && rb_thread_shield_owned((VALUE)data)) {
+ VALUE warning = rb_warning_string("loading in progress, circular require considered harmful - %s", ftptr);
+ rb_backtrace_each(rb_str_append, warning);
+ rb_warning("%"PRIsVALUE, warning);
}
switch (rb_thread_shield_wait((VALUE)data)) {
case Qfalse:
case Qnil:
- return 0;
+ return 0;
}
return (char *)ftptr;
}
@@ -873,13 +950,13 @@ release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done, int exis
VALUE thread_shield = (VALUE)*value;
if (!existing) return ST_STOP;
if (done) {
- rb_thread_shield_destroy(thread_shield);
- /* Delete the entry even if there are waiting threads, because they
- * won't load the file and won't delete the entry. */
+ rb_thread_shield_destroy(thread_shield);
+ /* Delete the entry even if there are waiting threads, because they
+ * won't load the file and won't delete the entry. */
}
else if (rb_thread_shield_release(thread_shield)) {
- /* still in-use */
- return ST_CONTINUE;
+ /* still in-use */
+ return ST_CONTINUE;
}
xfree((char *)*key);
return ST_DELETE;
@@ -889,13 +966,14 @@ static void
load_unlock(rb_vm_t *vm, const char *ftptr, int done)
{
if (ftptr) {
- st_data_t key = (st_data_t)ftptr;
- st_table *loading_tbl = get_loading_table(vm);
+ st_data_t key = (st_data_t)ftptr;
+ st_table *loading_tbl = get_loading_table(vm);
- st_update(loading_tbl, key, release_thread_shield, done);
+ st_update(loading_tbl, key, release_thread_shield, done);
}
}
+static VALUE rb_require_string_internal(VALUE fname, bool resurrect);
/*
* call-seq:
@@ -910,15 +988,14 @@ load_unlock(rb_vm_t *vm, const char *ftptr, int done)
* If the filename starts with './' or '../', resolution is based on Dir.pwd.
*
* If the filename has the extension ".rb", it is loaded as a source file; if
- * the extension is ".so", ".o", or ".dll", or the default shared library
- * extension on the current platform, Ruby loads the shared library as a
- * Ruby extension. Otherwise, Ruby tries adding ".rb", ".so", and so on
- * to the name until found. If the file named cannot be found, a LoadError
- * will be raised.
+ * the extension is ".so", ".o", or the default shared library extension on
+ * the current platform, Ruby loads the shared library as a Ruby extension.
+ * Otherwise, Ruby tries adding ".rb", ".so", and so on to the name until
+ * found. If the file named cannot be found, a LoadError will be raised.
*
- * For Ruby extensions the filename given may use any shared library
- * extension. For example, on Linux the socket extension is "socket.so" and
- * <code>require 'socket.dll'</code> will load the socket extension.
+ * For Ruby extensions the filename given may use ".so" or ".o". For example,
+ * on macOS the socket extension is "socket.bundle" and
+ * <code>require 'socket.so'</code> will load the socket extension.
*
* The absolute path of the loaded file is added to
* <code>$LOADED_FEATURES</code> (<code>$"</code>). A file will not be
@@ -945,19 +1022,20 @@ rb_f_require(VALUE obj, VALUE fname)
* call-seq:
* require_relative(string) -> true or false
*
- * Ruby tries to load the library named _string_ relative to the requiring
- * file's path. If the file's path cannot be determined a LoadError is raised.
- * If a file is loaded +true+ is returned and false otherwise.
+ * Ruby tries to load the library named _string_ relative to the directory
+ * containing the requiring file. If the file does not exist a LoadError is
+ * raised. Returns +true+ if the file was loaded and +false+ if the file was
+ * already loaded before.
*/
VALUE
rb_f_require_relative(VALUE obj, VALUE fname)
{
VALUE base = rb_current_realfilepath();
if (NIL_P(base)) {
- rb_loaderror("cannot infer basepath");
+ rb_loaderror("cannot infer basepath");
}
base = rb_file_dirname(base);
- return rb_require_string(rb_file_absolute_path(fname, base));
+ return rb_require_string_internal(rb_file_absolute_path(fname, base), false);
}
typedef int (*feature_func)(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn);
@@ -967,80 +1045,99 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_
{
VALUE tmp;
char *ext, *ftptr;
- int type, ft = 0;
+ int ft = 0;
const char *loading;
*path = 0;
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 (loading) *path = rb_filesystem_str_new_cstr(loading);
- return 'r';
- }
+ if (IS_RBEXT(ext)) {
+ if (rb_feature_p(vm, 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)
- *path = tmp;
- return 'r';
- }
- return 0;
- }
- else if (IS_SOEXT(ext)) {
- if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
- if (loading) *path = rb_filesystem_str_new_cstr(loading);
- return 's';
- }
- tmp = rb_str_subseq(fname, 0, ext - RSTRING_PTR(fname));
- rb_str_cat2(tmp, DLEXT);
- OBJ_FREEZE(tmp);
+ ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
+ if (!rb_feature_p(vm, 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 (loading) *path = rb_filesystem_str_new_cstr(loading);
+ return 's';
+ }
+ tmp = rb_str_subseq(fname, 0, ext - RSTRING_PTR(fname));
+ rb_str_cat2(tmp, DLEXT);
+ 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)
- *path = tmp;
- return 's';
- }
- }
- else if (IS_DLEXT(ext)) {
- if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
- if (loading) *path = rb_filesystem_str_new_cstr(loading);
- return 's';
- }
+ ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
+ if (!rb_feature_p(vm, 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 (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)
- *path = tmp;
- return 's';
- }
- }
+ ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
+ if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
+ *path = tmp;
+ return 's';
+ }
+ }
}
else if ((ft = rb_feature_p(vm, ftptr, 0, FALSE, FALSE, &loading)) == 'r') {
- if (loading) *path = rb_filesystem_str_new_cstr(loading);
- return 'r';
+ if (loading) *path = rb_filesystem_str_new_cstr(loading);
+ return 'r';
}
tmp = fname;
- type = rb_find_file_ext(&tmp, ft == 's' ? ruby_ext : loadable_ext);
+ const unsigned int type = rb_find_file_ext(&tmp, ft == 's' ? ruby_ext : loadable_ext);
+
+ // 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) {
+ 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.
+ // See encinit.c and extinit.c (generated at build-time).
+ if (!ext) {
+ lookup_name = rb_str_dup(lookup_name);
+ rb_str_cat_cstr(lookup_name, ".so");
+ }
+ ftptr = RSTRING_PTR(lookup_name);
+ if (st_lookup(vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
+ *path = rb_filesystem_str_new_cstr(ftptr);
+ return 's';
+ }
+ }
+
switch (type) {
case 0:
- if (ft)
- goto statically_linked;
- ftptr = RSTRING_PTR(tmp);
- return rb_feature_p(vm, ftptr, 0, FALSE, TRUE, 0);
+ if (ft)
+ goto feature_present;
+ ftptr = RSTRING_PTR(tmp);
+ return rb_feature_p(vm, ftptr, 0, FALSE, TRUE, 0);
default:
- if (ft) {
- goto statically_linked;
- }
+ if (ft) {
+ goto feature_present;
+ }
/* fall through */
- case 1:
- ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
- if (rb_feature_p(vm, ftptr, ext, !--type, TRUE, &loading) && !loading)
- break;
- *path = tmp;
+ case loadable_ext_rb:
+ ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
+ if (rb_feature_p(vm, ftptr, ext, type == loadable_ext_rb, TRUE, &loading) && !loading)
+ break;
+ *path = tmp;
}
- return type ? 's' : 'r';
+ return type > loadable_ext_rb ? 's' : 'r';
- statically_linked:
+ feature_present:
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return ft;
}
@@ -1058,6 +1155,19 @@ load_ext(VALUE path)
return (VALUE)dln_load(RSTRING_PTR(path));
}
+static bool
+run_static_ext_init(rb_vm_t *vm, const char *feature)
+{
+ st_data_t key = (st_data_t)feature;
+ st_data_t init_func;
+
+ if (vm->static_ext_inits && st_delete(vm->static_ext_inits, &key, &init_func)) {
+ ((void (*)(void))init_func)();
+ return true;
+ }
+ return false;
+}
+
static int
no_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn)
{
@@ -1123,8 +1233,10 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
rb_thread_t *th = rb_ec_thread_ptr(ec);
volatile const struct {
VALUE wrapper, self, errinfo;
+ rb_execution_context_t *ec;
} saved = {
th->top_wrapper, th->top_self, ec->errinfo,
+ ec,
};
enum ruby_tag_type state;
char *volatile ftptr = 0;
@@ -1132,10 +1244,10 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
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);
volatile bool reset_ext_config = false;
struct rb_ext_config prev_ext_config;
- fname = rb_get_path(fname);
path = rb_str_encode_ospath(fname);
RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
saved_path = path;
@@ -1144,45 +1256,49 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
ec->errinfo = Qnil; /* ensure */
th->top_wrapper = 0;
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
- long handle;
- int found;
+ VALUE handle;
+ int found;
- RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
+ RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
found = search_required(th->vm, path, &saved_path, rb_feature_p);
- RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
+ RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
path = saved_path;
- if (found) {
+ if (found) {
if (!path || !(ftptr = load_lock(th->vm, RSTRING_PTR(path), warn))) {
- result = 0;
- }
- else if (!*ftptr) {
- result = TAG_RETURN;
- }
+ result = 0;
+ }
+ else if (!*ftptr) {
+ result = TAG_RETURN;
+ }
+ else if (found == 's' && run_static_ext_init(th->vm, RSTRING_PTR(path))) {
+ result = TAG_RETURN;
+ }
else if (RTEST(rb_hash_aref(realpaths,
- realpath = rb_realpath_internal(Qnil, path, 1)))) {
+ realpath = realpath_internal_cached(realpath_map, path)))) {
result = 0;
}
- else {
- switch (found) {
- case 'r':
+ else {
+ switch (found) {
+ case 'r':
load_iseq_eval(ec, path);
- break;
+ break;
- case 's':
+ case 's':
reset_ext_config = true;
ext_config_push(th, &prev_ext_config);
- handle = (long)rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
- path, VM_BLOCK_HANDLER_NONE, path);
- rb_ary_push(ruby_dln_librefs, LONG2NUM(handle));
- break;
- }
+ 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));
+ break;
+ }
result = TAG_RETURN;
- }
- }
+ }
+ }
}
EC_POP_TAG();
+ ec = saved.ec;
rb_thread_t *th2 = rb_ec_thread_ptr(ec);
th2->top_self = saved.self;
th2->top_wrapper = saved.wrapper;
@@ -1205,9 +1321,9 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
else if (state == TAG_RETURN) {
return TAG_RAISE;
}
- RB_GC_GUARD(fname);
- /* never TAG_RETURN */
- return state;
+ RB_GC_GUARD(fname);
+ /* never TAG_RETURN */
+ return state;
}
if (!NIL_P(ec->errinfo)) {
if (!exception) return TAG_RAISE;
@@ -1218,7 +1334,8 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
rb_provide_feature(th2->vm, path);
VALUE real = realpath;
if (real) {
- rb_hash_aset(realpaths, rb_fstring(real), Qtrue);
+ real = rb_fstring(real);
+ rb_hash_aset(realpaths, real, Qtrue);
}
}
ec->errinfo = saved.errinfo;
@@ -1256,6 +1373,12 @@ ruby_require_internal(const char *fname, unsigned int len)
VALUE
rb_require_string(VALUE fname)
{
+ return rb_require_string_internal(FilePathValue(fname), false);
+}
+
+static VALUE
+rb_require_string_internal(VALUE fname, bool resurrect)
+{
rb_execution_context_t *ec = GET_EC();
int result = require_internal(ec, fname, 1, RTEST(ruby_verbose));
@@ -1263,7 +1386,8 @@ rb_require_string(VALUE fname)
EC_JUMP_TAG(ec, result);
}
if (result < 0) {
- load_failed(fname);
+ if (resurrect) fname = rb_str_resurrect(fname);
+ load_failed(fname);
}
return RBOOL(result);
@@ -1272,7 +1396,9 @@ rb_require_string(VALUE fname)
VALUE
rb_require(const char *fname)
{
- return rb_require_string(rb_str_new_cstr(fname));
+ struct RString fake;
+ VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
+ return rb_require_string_internal(str, true);
}
static int
@@ -1280,39 +1406,51 @@ register_init_ext(st_data_t *key, st_data_t *value, st_data_t init, int existing
{
const char *name = (char *)*key;
if (existing) {
- /* already registered */
- rb_warn("%s is already registered", name);
+ /* already registered */
+ rb_warn("%s is already registered", name);
}
else {
- *value = (st_data_t)MEMO_NEW(0, 0, init);
- *key = (st_data_t)ruby_strdup(name);
+ *value = (st_data_t)init;
}
return ST_CONTINUE;
}
-RUBY_FUNC_EXPORTED void
+// Private API for statically linked extensions.
+// Used with the ext/Setup file, the --with-setup and
+// --with-static-linked-ext configuration option, etc.
+void
ruby_init_ext(const char *name, void (*init)(void))
{
+ st_table *inits_table;
rb_vm_t *vm = GET_VM();
- st_table *loading_tbl = get_loading_table(vm);
if (feature_provided(vm, name, 0))
- return;
- st_update(loading_tbl, (st_data_t)name, register_init_ext, (st_data_t)init);
+ return;
+
+ inits_table = vm->static_ext_inits;
+ if (!inits_table) {
+ inits_table = st_init_strtable();
+ vm->static_ext_inits = inits_table;
+ }
+ st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init);
}
/*
* call-seq:
- * mod.autoload(module, filename) -> nil
+ * mod.autoload(const, filename) -> nil
*
* Registers _filename_ to be loaded (using Kernel::require)
- * the first time that _module_ (which may be a String or
+ * the first time that _const_ (which may be a String or
* a symbol) is accessed in the namespace of _mod_.
*
* module A
* end
* A.autoload(:B, "b")
* A::B.doit # autoloads "b"
+ *
+ * If _const_ in _mod_ is defined as autoload, the file name to be
+ * loaded is replaced with _filename_. If _const_ is defined but not
+ * as autoload, does nothing.
*/
static VALUE
@@ -1359,20 +1497,24 @@ rb_mod_autoload_p(int argc, VALUE *argv, VALUE mod)
ID id = rb_check_id(&sym);
if (!id) {
- return Qnil;
+ return Qnil;
}
return rb_autoload_at_p(mod, id, recur);
}
/*
* call-seq:
- * autoload(module, filename) -> nil
+ * autoload(const, filename) -> nil
*
* Registers _filename_ to be loaded (using Kernel::require)
- * the first time that _module_ (which may be a String or
+ * the first time that _const_ (which may be a String or
* a symbol) is accessed.
*
* autoload(:MyModule, "/usr/local/lib/modules/my_module.rb")
+ *
+ * If _const_ is defined as autoload, the file name to be loaded is
+ * replaced with _filename_. If _const_ is defined but not as
+ * autoload, does nothing.
*/
static VALUE
@@ -1380,7 +1522,7 @@ rb_f_autoload(VALUE obj, VALUE sym, VALUE file)
{
VALUE klass = rb_class_real(rb_vm_cbase());
if (!klass) {
- rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
+ rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
}
return rb_mod_autoload(klass, sym, file);
}
@@ -1402,11 +1544,42 @@ rb_f_autoload_p(int argc, VALUE *argv, VALUE obj)
/* use rb_vm_cbase() as same as rb_f_autoload. */
VALUE klass = rb_vm_cbase();
if (NIL_P(klass)) {
- return Qnil;
+ return Qnil;
}
return rb_mod_autoload_p(argc, argv, klass);
}
+void *
+rb_ext_resolve_symbol(const char* fname, const char* symbol)
+{
+ VALUE handle;
+ VALUE resolved;
+ VALUE path;
+ char *ext;
+ VALUE fname_str = rb_str_new_cstr(fname);
+
+ resolved = rb_resolve_feature_path((VALUE)NULL, fname_str);
+ if (NIL_P(resolved)) {
+ ext = strrchr(fname, '.');
+ if (!ext || !IS_SOEXT(ext)) {
+ rb_str_cat_cstr(fname_str, ".so");
+ }
+ if (rb_feature_p(GET_VM(), fname, 0, FALSE, FALSE, 0)) {
+ return dln_symbol(NULL, symbol);
+ }
+ return NULL;
+ }
+ if (RARRAY_LEN(resolved) != 2 || rb_ary_entry(resolved, 0) != ID2SYM(rb_intern("so"))) {
+ return NULL;
+ }
+ path = rb_ary_entry(resolved, 1);
+ handle = rb_hash_lookup(ruby_dln_libmap, path);
+ if (NIL_P(handle)) {
+ return NULL;
+ }
+ return dln_symbol((void *)NUM2SVALUE(handle), symbol);
+}
+
void
Init_load(void)
{
@@ -1418,18 +1591,20 @@ Init_load(void)
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();
- vm->expanded_load_path = rb_ary_tmp_new(0);
- vm->load_path_snapshot = rb_ary_tmp_new(0);
+ vm->expanded_load_path = rb_ary_hidden_new(0);
+ vm->load_path_snapshot = rb_ary_hidden_new(0);
vm->load_path_check_cache = 0;
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);
vm->loaded_features = rb_ary_new();
- vm->loaded_features_snapshot = rb_ary_tmp_new(0);
+ vm->loaded_features_snapshot = rb_ary_hidden_new(0);
vm->loaded_features_index = st_init_numtable();
vm->loaded_features_realpaths = rb_hash_new();
rb_obj_hide(vm->loaded_features_realpaths);
+ vm->loaded_features_realpath_map = rb_hash_new();
+ rb_obj_hide(vm->loaded_features_realpath_map);
rb_define_global_function("load", rb_f_load, -1);
rb_define_global_function("require", rb_f_require, 1);
@@ -1439,6 +1614,6 @@ Init_load(void)
rb_define_global_function("autoload", rb_f_autoload, 2);
rb_define_global_function("autoload?", rb_f_autoload_p, -1);
- ruby_dln_librefs = rb_ary_tmp_new(0);
- rb_gc_register_mark_object(ruby_dln_librefs);
+ ruby_dln_libmap = rb_hash_new_with_size(0);
+ rb_vm_register_global_object(ruby_dln_libmap);
}