#include "ruby/ruby.h"
#include "ruby/util.h"
#include "internal.h"
#include "dln.h"
#include "eval_intern.h"
#include "probes.h"
#include "node.h"
VALUE ruby_dln_librefs;
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
#ifdef DLEXT2
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0 || strcmp((e), DLEXT2) == 0)
#else
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
#endif
static const char *const loadable_ext[] = {
".rb", DLEXT,
#ifdef DLEXT2
DLEXT2,
#endif
0
};
VALUE
rb_get_load_path(void)
{
VALUE load_path = GET_VM()->load_path;
return load_path;
}
enum expand_type {
EXPAND_ALL,
EXPAND_RELATIVE,
EXPAND_HOME,
EXPAND_NON_CACHE
};
static void
rb_construct_expanded_load_path(int type, int *has_relative, int *has_non_cache)
{
rb_vm_t *vm = GET_VM();
VALUE load_path = vm->load_path;
VALUE expanded_load_path = vm->expanded_load_path;
VALUE ary;
long i;
int level = rb_safe_level();
ary = rb_ary_tmp_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_PTR(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, level);
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)) {
rb_ary_push(ary, RARRAY_PTR(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;
if (is_string)
rb_str_freeze(path);
as_str = rb_get_path_check_convert(path, as_str, level);
expanded_path = rb_file_expand_path_fast(as_str, Qnil);
rb_str_freeze(expanded_path);
rb_ary_push(ary, expanded_path);
}
rb_obj_freeze(ary);
vm->expanded_load_path = ary;
rb_ary_replace(vm->load_path_snapshot, vm->load_path);
}
static VALUE
load_path_getcwd(void)
{
char *cwd = my_getcwd();
VALUE cwd_str = rb_filesystem_str_new_cstr(cwd);
xfree(cwd);
return cwd_str;
}
VALUE
rb_get_expanded_load_path(void)
{
rb_vm_t *vm = GET_VM();
const VALUE non_cache = Qtrue;
if (!rb_ary_shared_with_p(vm->load_path_snapshot, vm->load_path)) {
int has_relative = 0, has_non_cache = 0;
rb_construct_expanded_load_path(EXPAND_ALL, &has_relative, &has_non_cache);
if (has_relative) {
vm->load_path_check_cache = load_path_getcwd();
}
else if (has_non_cache) {
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;
rb_construct_expanded_load_path(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 = load_path_getcwd();
if (!rb_str_equal(vm->load_path_check_cache, cwd)) {
vm->load_path_check_cache = cwd;
rb_construct_expanded_load_path(EXPAND_RELATIVE,
&has_relative, &has_non_cache);
}
else {
rb_construct_expanded_load_path(EXPAND_HOME,
&has_relative, &has_non_cache);
}
}
return vm->expanded_load_path;
}
static VALUE
load_path_getter(ID id, rb_vm_t *vm)
{
return vm->load_path;
}
static VALUE
get_loaded_features(void)
{
return GET_VM()->loaded_features;
}
static void
reset_loaded_features_snapshot(void)
{
rb_vm_t *vm = GET_VM();
rb_ary_replace(vm->loaded_features_snapshot, vm->loaded_features);
}
static struct st_table *
get_loaded_features_index_raw(void)
{
return GET_VM()->loaded_features_index;
}
static st_table *
get_loading_table(void)
{
return GET_VM()->loading_table;
}
static void
features_index_add_single(VALUE short_feature, VALUE offset)
{
struct st_table *features_index;
VALUE this_feature_index = Qnil;
char *short_feature_cstr;
Check_Type(offset, T_FIXNUM);
Check_Type(short_feature, T_STRING);
short_feature_cstr = StringValueCStr(short_feature);
features_index = get_loaded_features_index_raw();
st_lookup(features_index, (st_data_t)short_feature_cstr, (st_data_t *)&this_feature_index);
if (NIL_P(this_feature_index)) {
st_insert(features_index, (st_data_t)ruby_strdup(short_feature_cstr), (st_data_t)offset);
}
else if (RB_TYPE_P(this_feature_index, T_FIXNUM)) {
VALUE feature_indexes[2];
feature_indexes[0] = this_feature_index;
feature_indexes[1] = offset;
this_feature_index = rb_ary_tmp_new(numberof(feature_indexes));
rb_ary_cat(this_feature_index, feature_indexes, numberof(feature_indexes));
st_insert(features_index, (st_data_t)short_feature_cstr, (st_data_t)this_feature_index);
}
else {
Check_Type(this_feature_index, T_ARRAY);
rb_ary_push(this_feature_index, offset);
}
}
static void
features_index_add(VALUE feature, VALUE offset)
{
VALUE short_feature;
const char *feature_str, *feature_end, *ext, *p;
feature_str = StringValuePtr(feature);
feature_end = feature_str + RSTRING_LEN(feature);
for (ext = feature_end; ext > feature_str; ext--)
if (*ext == '.' || *ext == '/')
break;
if (*ext != '.')
ext = NULL;
p = ext ? ext : feature_end;
while (1) {
p--;
while (p >= feature_str && *p != '/')
p--;
if (p < feature_str)
break;
short_feature = rb_str_subseq(feature, p + 1 - feature_str, feature_end - p - 1);
features_index_add_single(short_feature, offset);
if (ext) {
short_feature = rb_str_subseq(feature, p + 1 - feature_str, ext - p - 1);
features_index_add_single(short_feature, offset);
}
}
features_index_add_single(feature, offset);
if (ext) {
short_feature = rb_str_subseq(feature, 0, ext - feature_str);
features_index_add_single(short_feature, offset);
}
}
static int
loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
{
xfree((char *)key);
return ST_DELETE;
}
static st_table *
get_loaded_features_index(void)
{
VALUE features;
int i;
rb_vm_t *vm = GET_VM();
if (!rb_ary_shared_with_p(vm->loaded_features_snapshot, vm->loaded_features)) {
st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
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);
if (as_str != entry)
rb_ary_store(features, i, as_str);
rb_str_freeze(as_str);
features_index_add(as_str, INT2FIX(i));
}
reset_loaded_features_snapshot();
}
return vm->loaded_features_index;
}
static VALUE
loaded_feature_path(const char *name, long vlen, const char *feature, long len,
int type, VALUE load_path)
{
long i;
long plen;
const char *e;
if (vlen < len+1) return 0;
if (!strncmp(name+(vlen-len), feature, 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;
}
if (plen > 0 && name[plen-1] != '/') {
return 0;
}
if (type == 's' ? !IS_DLEXT(&name[plen+len]) :
type == 'r' ? !IS_RBEXT(&name[plen+len]) :
0) {
return 0;
}
if (plen > 0) --plen;
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
VALUE p = RARRAY_PTR(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;
}
return 0;
}
struct loaded_feature_searching {
const char *name;
long len;
int type;
VALUE load_path;
const char *result;
};
static int
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);
if (!p) return ST_CONTINUE;
fp->result = s;
return ST_STOP;
}
static int
rb_feature_p(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;
long i, len, elen, n;
st_table *loading_tbl, *features_index;
st_data_t data;
int type;
if (fn) *fn = 0;
if (ext) {
elen = strlen(ext);
len = strlen(feature) - elen;
type = rb ? 'r' : 's';
}
else {
len = strlen(feature);
elen = 0;
type = 0;
}
features = get_loaded_features();
features_index = get_loaded_features_index();
st_lookup(features_index, (st_data_t)feature, (st_data_t *)&this_feature_index);
for (i = 0; !NIL_P(this_feature_index); i++) {
VALUE entry;
long index;
if (RB_TYPE_P(this_feature_index, T_ARRAY)) {
if (i >= RARRAY_LEN(this_feature_index)) break;
entry = RARRAY_PTR(this_feature_index)[i];
}
else {
if (i > 0) break;
entry = this_feature_index;
}
index = FIX2LONG(entry);
v = RARRAY_PTR(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 = rb_get_expanded_load_path();
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();
if (loading_tbl) {
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 : rb_get_expanded_load_path();
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;
loading:
if (!ext) return 'u';
return !IS_RBEXT(ext) ? 's' : 'r';
}
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);
}
}
return 0;
}
int
rb_provided(const char *feature)
{
return rb_feature_provided(feature, 0);
}
int
rb_feature_provided(const char *feature, const char **loading)
{
const char *ext = strrchr(feature, '.');
volatile 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);
}
if (ext && !strchr(ext, '/')) {
if (IS_RBEXT(ext)) {
if (rb_feature_p(feature, ext, TRUE, FALSE, loading)) return TRUE;
return FALSE;
}
else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
if (rb_feature_p(feature, ext, FALSE, FALSE, loading)) return TRUE;
return FALSE;
}
}
if (rb_feature_p(feature, 0, TRUE, FALSE, loading))
return TRUE;
return FALSE;
}
static void
rb_provide_feature(VALUE feature)
{
VALUE features;
features = get_loaded_features();
if (OBJ_FROZEN(features)) {
rb_raise(rb_eRuntimeError,
"$LOADED_FEATURES is frozen; cannot append feature");
}
rb_str_freeze(feature);
rb_ary_push(features, feature);
features_index_add(feature, INT2FIX(RARRAY_LEN(features)-1));
reset_loaded_features_snapshot();
}
void
rb_provide(const char *feature)
{
rb_provide_feature(rb_usascii_str_new2(feature));
}
NORETURN(static void load_failed(VALUE));
static void
rb_load_internal(VALUE fname, int wrap)
{
int state;
rb_thread_t *th = GET_THREAD();
volatile VALUE wrapper = th->top_wrapper;
volatile VALUE self = th->top_self;
volatile int loaded = FALSE;
volatile int mild_compile_error;
#ifndef __GNUC__
rb_thread_t *volatile th0 = th;
#endif
th->errinfo = Qnil;
if (!wrap) {
rb_secure(4);
th->top_wrapper = 0;
}
else {
th->top_self = rb_obj_clone(rb_vm_top_self());
th->top_wrapper = rb_module_new();
rb_extend_object(th->top_self, th->top_wrapper);
}
mild_compile_error = th->mild_compile_error;
PUSH_TAG();
state = EXEC_TAG();
if (state == 0) {
NODE *node;
VALUE iseq;
th->mild_compile_error++;
node = (NODE *)rb_load_file(RSTRING_PTR(fname));
loaded = TRUE;
iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_realpath_internal(Qnil, fname, 1), Qfalse);
th->mild_compile_error--;
rb_iseq_eval(iseq);
}
POP_TAG();
#ifndef __GNUC__
th = th0;
fname = RB_GC_GUARD(fname);
#endif
th->mild_compile_error = mild_compile_error;
th->top_self = self;
th->top_wrapper = wrapper;
if (!loaded && !FIXNUM_P(GET_THREAD()->errinfo)) {
rb_exc_raise(GET_THREAD()->errinfo);
}
if (state) {
rb_vm_jump_tag_but_local_jump(state);
}
if (!NIL_P(GET_THREAD()->errinfo)) {
rb_exc_raise(th->errinfo);
}
}
void
rb_load(VALUE fname, int wrap)
{
VALUE tmp = rb_find_file(FilePathValue(fname));
if (!tmp) load_failed(fname);
rb_load_internal(tmp, wrap);
}
void
rb_load_protect(VALUE fname, int wrap, int *state)
{
int status;
PUSH_TAG();
if ((status = EXEC_TAG()) == 0) {
rb_load(fname, wrap);
}
POP_TAG();
if (state)
*state = status;
}
static VALUE
rb_f_load(int argc, VALUE *argv)
{
VALUE fname, wrap, path;
rb_scan_args(argc, argv, "11", &fname, &wrap);
if (RUBY_DTRACE_LOAD_ENTRY_ENABLED()) {
RUBY_DTRACE_LOAD_ENTRY(StringValuePtr(fname),
rb_sourcefile(),
rb_sourceline());
}
path = rb_find_file(FilePathValue(fname));
if (!path) {
if (!rb_file_load_ok(RSTRING_PTR(fname)))
load_failed(fname);
path = fname;
}
rb_load_internal(path, RTEST(wrap));
if (RUBY_DTRACE_LOAD_RETURN_ENABLED()) {
RUBY_DTRACE_LOAD_RETURN(StringValuePtr(fname),
rb_sourcefile(),
rb_sourceline());
}
return Qtrue;
}
static char *
load_lock(const char *ftptr)
{
st_data_t data;
st_table *loading_tbl = get_loading_table();
if (!loading_tbl || !st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
if (!loading_tbl) {
GET_VM()->loading_table = loading_tbl = st_init_strtable();
}
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 (RB_TYPE_P((VALUE)data, T_NODE) && nd_type((VALUE)data) == NODE_MEMO) {
NODE *memo = RNODE(data);
void (*init)(void) = (void (*)(void))memo->nd_cfnc;
data = (st_data_t)rb_thread_shield_new();
st_insert(loading_tbl, (st_data_t)ftptr, data);
(*init)();
return (char *)"";
}
if (RTEST(ruby_verbose)) {
rb_warning("loading in progress, circular require considered harmful - %s", ftptr);
rb_backtrace();
}
switch (rb_thread_shield_wait((VALUE)data)) {
case Qfalse:
data = (st_data_t)ftptr;
st_insert(loading_tbl, data, (st_data_t)rb_thread_shield_new());
return 0;
case Qnil:
return 0;
}
return (char *)ftptr;
}
static int
release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done, int existing)
{
VALUE thread_shield = (VALUE)*value;
if (!existing) return ST_STOP;
if (done ? rb_thread_shield_destroy(thread_shield) : rb_thread_shield_release(thread_shield)) {
return ST_CONTINUE;
}
xfree((char *)*key);
return ST_DELETE;
}
static void
load_unlock(const char *ftptr, int done)
{
if (ftptr) {
st_data_t key = (st_data_t)ftptr;
st_table *loading_tbl = get_loading_table();
st_update(loading_tbl, key, release_thread_shield, done);
}
}
VALUE
rb_f_require(VALUE obj, VALUE fname)
{
return rb_require_safe(fname, rb_safe_level());
}
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_safe(rb_file_absolute_path(fname, base), rb_safe_level());
}
static int
search_required(VALUE fname, volatile VALUE *path, int safe_level)
{
VALUE tmp;
char *ext, *ftptr;
int type, 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(ftptr, ext, TRUE, FALSE, &loading)) {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 'r';
}
if ((tmp = rb_find_file_safe(fname, safe_level)) != 0) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
if (!rb_feature_p(ftptr, ext, TRUE, TRUE, &loading) || loading)
*path = tmp;
return 'r';
}
return 0;
}
else if (IS_SOEXT(ext)) {
if (rb_feature_p(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));
#ifdef DLEXT2
OBJ_FREEZE(tmp);
if (rb_find_file_ext_safe(&tmp, loadable_ext + 1, safe_level)) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
if (!rb_feature_p(ftptr, ext, FALSE, TRUE, &loading) || loading)
*path = tmp;
return 's';
}
#else
rb_str_cat2(tmp, DLEXT);
OBJ_FREEZE(tmp);
if ((tmp = rb_find_file_safe(tmp, safe_level)) != 0) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
if (!rb_feature_p(ftptr, ext, FALSE, TRUE, &loading) || loading)
*path = tmp;
return 's';
}
#endif
}
else if (IS_DLEXT(ext)) {
if (rb_feature_p(ftptr, ext, FALSE, FALSE, &loading)) {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 's';
}
if ((tmp = rb_find_file_safe(fname, safe_level)) != 0) {
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
if (!rb_feature_p(ftptr, ext, FALSE, TRUE, &loading) || loading)
*path = tmp;
return 's';
}
}
}
else if ((ft = rb_feature_p(ftptr, 0, FALSE, FALSE, &loading)) == 'r') {
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return 'r';
}
tmp = fname;
type = rb_find_file_ext_safe(&tmp, loadable_ext, safe_level);
switch (type) {
case 0:
if (ft)
goto statically_linked;
ftptr = RSTRING_PTR(tmp);
return rb_feature_p(ftptr, 0, FALSE, TRUE, 0);
default:
if (ft) {
statically_linked:
if (loading) *path = rb_filesystem_str_new_cstr(loading);
return ft;
}
case 1:
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
if (rb_feature_p(ftptr, ext, !--type, TRUE, &loading) && !loading)
break;
*path = tmp;
}
return type ? 's' : 'r';
}
static void
load_failed(VALUE fname)
{
rb_load_fail(fname, "cannot load such file");
}
static VALUE
load_ext(VALUE path)
{
SCOPE_SET(NOEX_PUBLIC);
return (VALUE)dln_load(RSTRING_PTR(path));
}
VALUE
rb_require_safe(VALUE fname, int safe)
{
volatile VALUE result = Qnil;
rb_thread_t *th = GET_THREAD();
volatile VALUE errinfo = th->errinfo;
int state;
struct {
int safe;
} volatile saved;
char *volatile ftptr = 0;
if (RUBY_DTRACE_REQUIRE_ENTRY_ENABLED()) {
RUBY_DTRACE_REQUIRE_ENTRY(StringValuePtr(fname),
rb_sourcefile(),
rb_sourceline());
}
PUSH_TAG();
saved.safe = rb_safe_level();
if ((state = EXEC_TAG()) == 0) {
VALUE path;
long handle;
int found;
rb_set_safe_level_force(safe);
FilePathValue(fname);
rb_set_safe_level_force(0);
if (RUBY_DTRACE_FIND_REQUIRE_ENTRY_ENABLED()) {
RUBY_DTRACE_FIND_REQUIRE_ENTRY(StringValuePtr(fname),
rb_sourcefile(),
rb_sourceline());
}
found = search_required(fname, &path, safe);
if (RUBY_DTRACE_FIND_REQUIRE_RETURN_ENABLED()) {
RUBY_DTRACE_FIND_REQUIRE_RETURN(StringValuePtr(fname),
rb_sourcefile(),
rb_sourceline());
}
if (found) {
if (!path || !(ftptr = load_lock(RSTRING_PTR(path)))) {
result = Qfalse;
}
else if (!*ftptr) {
rb_provide_feature(path);
result = Qtrue;
}
else {
switch (found) {
case 'r':
rb_load_internal(path, 0);
break;
case 's':
handle = (long)rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
path, 0, path);
rb_ary_push(ruby_dln_librefs, LONG2NUM(handle));
break;
}
rb_provide_feature(path);
result = Qtrue;
}
}
}
POP_TAG();
load_unlock(ftptr, !state);
rb_set_safe_level_force(saved.safe);
if (state) {
JUMP_TAG(state);
}
if (NIL_P(result)) {
load_failed(fname);
}
th->errinfo = errinfo;
if (RUBY_DTRACE_REQUIRE_RETURN_ENABLED()) {
RUBY_DTRACE_REQUIRE_RETURN(StringValuePtr(fname),
rb_sourcefile(),
rb_sourceline());
}
return result;
}
VALUE
rb_require(const char *fname)
{
VALUE fn = rb_str_new2(fname);
OBJ_FREEZE(fn);
return rb_require_safe(fn, rb_safe_level());
}
static int
register_init_ext(st_data_t *key, st_data_t *value, st_data_t init, int existing)
{
const char *name = (char *)*key;
if (existing) {
rb_warn("%s is already registered", name);
}
else {
*value = (st_data_t)NEW_MEMO(init, 0, 0);
*key = (st_data_t)ruby_strdup(name);
}
return ST_CONTINUE;
}
RUBY_FUNC_EXPORTED void
ruby_init_ext(const char *name, void (*init)(void))
{
st_table *loading_tbl = get_loading_table();
if (!loading_tbl) {
GET_VM()->loading_table = loading_tbl = st_init_strtable();
}
st_update(loading_tbl, (st_data_t)name, register_init_ext, (st_data_t)init);
}
static VALUE
rb_mod_autoload(VALUE mod, VALUE sym, VALUE file)
{
ID id = rb_to_id(sym);
FilePathValue(file);
rb_autoload(mod, id, RSTRING_PTR(file));
return Qnil;
}
static VALUE
rb_mod_autoload_p(VALUE mod, VALUE sym)
{
ID id = rb_check_id(&sym);
if (!id) {
return Qnil;
}
return rb_autoload_p(mod, id);
}
static VALUE
rb_f_autoload(VALUE obj, VALUE sym, VALUE file)
{
VALUE klass = rb_class_real(rb_vm_cbase());
if (NIL_P(klass)) {
rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
}
return rb_mod_autoload(klass, sym, file);
}
static VALUE
rb_f_autoload_p(VALUE obj, VALUE sym)
{
VALUE klass = rb_vm_cbase();
if (NIL_P(klass)) {
return Qnil;
}
return rb_mod_autoload_p(klass, sym);
}
void
Init_load()
{
#undef rb_intern
#define rb_intern(str) rb_intern2((str), strlen(str))
rb_vm_t *vm = GET_VM();
static const char var_load_path[] = "$:";
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_alias_variable(rb_intern("$-I"), id_load_path);
rb_alias_variable(rb_intern("$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->load_path_check_cache = 0;
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_index = st_init_strtable();
rb_define_global_function("load", rb_f_load, -1);
rb_define_global_function("require", rb_f_require, 1);
rb_define_global_function("require_relative", rb_f_require_relative, 1);
rb_define_method(rb_cModule, "autoload", rb_mod_autoload, 2);
rb_define_method(rb_cModule, "autoload?", rb_mod_autoload_p, 1);
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);
}