6#include "eval_intern.h"
8#include "internal/dir.h"
9#include "internal/error.h"
10#include "internal/file.h"
11#include "internal/hash.h"
12#include "internal/load.h"
13#include "internal/ruby_parser.h"
14#include "internal/thread.h"
15#include "internal/variable.h"
22static VALUE ruby_dln_libmap;
24#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
25#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
26#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
28#if SIZEOF_VALUE <= SIZEOF_LONG
29# define SVALUE2NUM(x) LONG2NUM((long)(x))
30# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LONG(x)
31#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
32# define SVALUE2NUM(x) LL2NUM((LONG_LONG)(x))
33# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LL(x)
35# error Need integer for VALUE
43static const char *
const loadable_ext[] = {
48static const char *
const ruby_ext[] = {
66rb_construct_expanded_load_path(
rb_vm_t *vm,
enum expand_type
type,
int *has_relative,
int *has_non_cache)
68 VALUE load_path = vm->load_path;
69 VALUE expanded_load_path = vm->expanded_load_path;
73 ary = rb_ary_hidden_new(
RARRAY_LEN(load_path));
75 VALUE path, as_str, expanded_path;
76 int is_string, non_cache;
79 is_string = RB_TYPE_P(path,
T_STRING) ? 1 : 0;
80 non_cache = !is_string ? 1 : 0;
81 as_str = rb_get_path_check_to_string(path);
82 as_cstr = RSTRING_PTR(as_str);
85 if ((
type == EXPAND_RELATIVE &&
86 rb_is_absolute_path(as_cstr)) ||
87 (
type == EXPAND_HOME &&
88 (!as_cstr[0] || as_cstr[0] !=
'~')) ||
89 (
type == EXPAND_NON_CACHE)) {
91 rb_ary_push(ary,
RARRAY_AREF(expanded_load_path, i));
95 if (!*has_relative && !rb_is_absolute_path(as_cstr))
97 if (!*has_non_cache && non_cache)
102 as_str = rb_get_path_check_convert(as_str);
103 expanded_path = rb_check_realpath(
Qnil, as_str, NULL);
104 if (
NIL_P(expanded_path)) expanded_path = as_str;
105 rb_ary_push(ary, rb_fstring(expanded_path));
108 vm->expanded_load_path = ary;
109 rb_ary_replace(vm->load_path_snapshot, vm->load_path);
113get_expanded_load_path(
rb_vm_t *vm)
117 if (!rb_ary_shared_with_p(vm->load_path_snapshot, vm->load_path)) {
119 int has_relative = 0, has_non_cache = 0;
120 rb_construct_expanded_load_path(vm, EXPAND_ALL, &has_relative, &has_non_cache);
122 vm->load_path_check_cache = rb_dir_getwd_ospath();
124 else if (has_non_cache) {
126 vm->load_path_check_cache = non_cache;
129 vm->load_path_check_cache = 0;
132 else if (vm->load_path_check_cache == non_cache) {
133 int has_relative = 1, has_non_cache = 1;
135 rb_construct_expanded_load_path(vm, EXPAND_NON_CACHE,
136 &has_relative, &has_non_cache);
138 else if (vm->load_path_check_cache) {
139 int has_relative = 1, has_non_cache = 1;
140 VALUE cwd = rb_dir_getwd_ospath();
144 vm->load_path_check_cache = cwd;
145 rb_construct_expanded_load_path(vm, EXPAND_RELATIVE,
146 &has_relative, &has_non_cache);
150 rb_construct_expanded_load_path(vm, EXPAND_HOME,
151 &has_relative, &has_non_cache);
154 return vm->expanded_load_path;
158rb_get_expanded_load_path(
void)
160 return get_expanded_load_path(GET_VM());
164load_path_getter(
ID id,
VALUE * p)
167 return vm->load_path;
171get_loaded_features(
rb_vm_t *vm)
173 return vm->loaded_features;
177get_loaded_features_realpaths(
rb_vm_t *vm)
179 return vm->loaded_features_realpaths;
183get_loaded_features_realpath_map(
rb_vm_t *vm)
185 return vm->loaded_features_realpath_map;
189get_LOADED_FEATURES(
ID _x,
VALUE *_y)
191 return get_loaded_features(GET_VM());
195reset_loaded_features_snapshot(
rb_vm_t *vm)
197 rb_ary_replace(vm->loaded_features_snapshot, vm->loaded_features);
201get_loaded_features_index_raw(
rb_vm_t *vm)
203 return vm->loaded_features_index;
209 return vm->loading_table;
213feature_key(
const char *str,
size_t len)
215 return st_hash(str,
len, 0xfea7009e);
219is_rbext_path(
VALUE feature_path)
221 long len = RSTRING_LEN(feature_path);
223 if (
len <= rbext_len)
return false;
224 return IS_RBEXT(RSTRING_PTR(feature_path) +
len - rbext_len);
227typedef rb_darray(
long) feature_indexes_t;
229struct features_index_add_single_args {
236features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args,
int existing)
238 struct features_index_add_single_args *args = (
struct features_index_add_single_args *)raw_args;
240 VALUE offset = args->offset;
244 VALUE this_feature_index = *value;
247 VALUE loaded_features = get_loaded_features(vm);
250 feature_indexes_t feature_indexes;
251 rb_darray_make(&feature_indexes, 2);
252 int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0;
253 rb_darray_set(feature_indexes, top^0,
FIX2LONG(this_feature_index));
254 rb_darray_set(feature_indexes, top^1,
FIX2LONG(offset));
256 assert(rb_darray_size(feature_indexes) == 2);
260 *value = (st_data_t)feature_indexes;
263 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
267 VALUE loaded_features = get_loaded_features(vm);
268 for (
size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
269 long idx = rb_darray_get(feature_indexes, i);
272 if (!is_rbext_path(this_feature_path)) {
279 rb_darray_append(&feature_indexes,
FIX2LONG(offset));
281 *value = (st_data_t)feature_indexes;
284 long *ptr = rb_darray_data_ptr(feature_indexes);
285 long len = rb_darray_size(feature_indexes);
286 MEMMOVE(ptr + pos, ptr + pos + 1,
long,
len - pos - 1);
299features_index_add_single(
rb_vm_t *vm,
const char* str,
size_t len,
VALUE offset,
bool rb)
302 st_data_t short_feature_key;
305 short_feature_key = feature_key(str,
len);
307 features_index = get_loaded_features_index_raw(vm);
309 struct features_index_add_single_args args = {
315 st_update(features_index, short_feature_key, features_index_add_single_callback, (st_data_t)&args);
329 const char *feature_str, *feature_end, *ext, *p;
333 feature_end = feature_str + RSTRING_LEN(feature);
335 for (ext = feature_end; ext > feature_str; ext--)
336 if (*ext ==
'.' || *ext ==
'/')
345 p = ext ? ext : feature_end;
348 while (p >= feature_str && *p !=
'/')
353 features_index_add_single(vm, p + 1, feature_end - p - 1, offset,
false);
355 features_index_add_single(vm, p + 1, ext - p - 1, offset, rb);
358 features_index_add_single(vm, feature_str, feature_end - feature_str, offset,
false);
360 features_index_add_single(vm, feature_str, ext - feature_str, offset, rb);
365loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
369 rb_darray_free((
void *)obj);
375rb_free_loaded_features_index(
rb_vm_t *vm)
377 st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
378 st_free_table(vm->loaded_features_index);
382get_loaded_features_index(
rb_vm_t *vm)
387 if (!rb_ary_shared_with_p(vm->loaded_features_snapshot, vm->loaded_features)) {
390 st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
392 VALUE realpaths = vm->loaded_features_realpaths;
393 VALUE realpath_map = vm->loaded_features_realpath_map;
394 VALUE previous_realpath_map = rb_hash_dup(realpath_map);
395 rb_hash_clear(realpaths);
396 rb_hash_clear(realpath_map);
397 features = vm->loaded_features;
400 as_str = entry = rb_ary_entry(features, i);
402 as_str = rb_fstring(as_str);
404 rb_ary_store(features, i, as_str);
405 features_index_add(vm, as_str,
INT2FIX(i));
407 reset_loaded_features_snapshot(vm);
409 features = rb_ary_dup(vm->loaded_features_snapshot);
411 for (i = 0; i < j; i++) {
412 VALUE as_str = rb_ary_entry(features, i);
413 VALUE realpath = rb_hash_aref(previous_realpath_map, as_str);
414 if (
NIL_P(realpath)) {
415 realpath = rb_check_realpath(
Qnil, as_str, NULL);
416 if (
NIL_P(realpath)) realpath = as_str;
417 realpath = rb_fstring(realpath);
419 rb_hash_aset(realpaths, realpath,
Qtrue);
420 rb_hash_aset(realpath_map, as_str, realpath);
423 return vm->loaded_features_index;
438loaded_feature_path(
const char *name,
long vlen,
const char *feature,
long len,
445 if (vlen <
len+1)
return 0;
446 if (strchr(feature,
'.') && !strncmp(name+(vlen-
len), feature,
len)) {
450 for (e = name + vlen; name != e && *e !=
'.' && *e !=
'/'; --e);
453 strncmp(e-
len, feature,
len))
455 plen = e - name -
len;
457 if (plen > 0 && name[plen-1] !=
'/') {
460 if (
type ==
's' ? !IS_DLEXT(&name[plen+
len]) :
461 type ==
'r' ? !IS_RBEXT(&name[plen+
len]) :
468 if (plen > 0) --plen;
472 long n = RSTRING_LEN(p);
474 if (n != plen)
continue;
475 if (n && strncmp(name, s, n))
continue;
490loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
492 const char *s = (
const char *)v;
494 VALUE p = loaded_feature_path(s, strlen(s), fp->name, fp->len,
495 fp->type, fp->load_path);
496 if (!p)
return ST_CONTINUE;
508rb_feature_p(
rb_vm_t *vm,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
510 VALUE features, this_feature_index =
Qnil, v, p, load_path = 0;
512 long i,
len, elen, n;
513 st_table *loading_tbl, *features_index;
521 len = strlen(feature) - elen;
522 type = rb ?
'r' :
's';
525 len = strlen(feature);
529 features = get_loaded_features(vm);
530 features_index = get_loaded_features_index(vm);
532 key = feature_key(feature, strlen(feature));
559 if (st_lookup(features_index, key, &data) && !
NIL_P(this_feature_index = (
VALUE)data)) {
560 for (
size_t i = 0; ; i++) {
564 index =
FIX2LONG(this_feature_index);
567 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
568 if (i >= rb_darray_size(feature_indexes))
break;
569 index = rb_darray_get(feature_indexes, i);
574 if ((n = RSTRING_LEN(v)) <
len)
continue;
575 if (strncmp(f, feature,
len) != 0) {
576 if (expanded)
continue;
577 if (!load_path) load_path = get_expanded_load_path(vm);
578 if (!(p = loaded_feature_path(f, n, feature,
len,
type, load_path)))
581 f += RSTRING_LEN(p) + 1;
583 if (!*(e = f +
len)) {
587 if (*e !=
'.')
continue;
588 if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
591 if ((rb || !ext) && (IS_RBEXT(e))) {
597 loading_tbl = get_loading_table(vm);
604 fs.load_path = load_path ? load_path : get_expanded_load_path(vm);
606 st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
607 if ((f = fs.result) != 0) {
612 if (st_get_key(loading_tbl, (st_data_t)feature, &data)) {
613 if (fn) *fn = (
const char*)data;
619 static const char so_ext[][4] = {
623 if (ext && *ext)
return 0;
625 buf = RSTRING_PTR(bufstr);
627 for (i = 0; (e = loadable_ext[i]) != 0; i++) {
628 strlcpy(buf +
len, e, DLEXT_MAXLEN + 1);
629 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
630 rb_str_resize(bufstr, 0);
631 if (fn) *fn = (
const char*)data;
632 return i ?
's' :
'r';
635 for (i = 0; i < numberof(so_ext); i++) {
636 strlcpy(buf +
len, so_ext[i], DLEXT_MAXLEN + 1);
637 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
638 rb_str_resize(bufstr, 0);
639 if (fn) *fn = (
const char*)data;
643 rb_str_resize(bufstr, 0);
648 if (!ext)
return 'u';
649 return !IS_RBEXT(ext) ?
's' :
'r';
659feature_provided(
rb_vm_t *vm,
const char *feature,
const char **loading)
661 const char *ext = strrchr(feature,
'.');
664 if (*feature ==
'.' &&
665 (feature[1] ==
'/' || strncmp(feature+1,
"./", 2) == 0)) {
666 fullpath = rb_file_expand_path_fast(rb_get_path(
rb_str_new2(feature)),
Qnil);
667 feature = RSTRING_PTR(fullpath);
669 if (ext && !strchr(ext,
'/')) {
671 if (rb_feature_p(vm, feature, ext, TRUE, FALSE, loading))
return TRUE;
674 else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
675 if (rb_feature_p(vm, feature, ext, FALSE, FALSE, loading))
return TRUE;
679 if (rb_feature_p(vm, feature, 0, TRUE, FALSE, loading))
688 return feature_provided(GET_VM(), feature, loading);
696 features = get_loaded_features(vm);
699 "$LOADED_FEATURES is frozen; cannot append feature");
701 feature = rb_fstring(feature);
703 get_loaded_features_index(vm);
707 rb_ary_clear(vm->loaded_features_snapshot);
708 rb_ary_push(features, feature);
710 reset_loaded_features_snapshot(vm);
716 rb_provide_feature(GET_VM(), rb_fstring_cstr(feature));
719NORETURN(
static void load_failed(
VALUE));
722realpath_internal_cached(
VALUE hash,
VALUE path)
724 VALUE ret = rb_hash_aref(hash, path);
729 VALUE realpath = rb_realpath_internal(
Qnil, path, 1);
730 rb_hash_aset(hash, rb_fstring(path), rb_fstring(realpath));
737 const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
740 if (*rb_ruby_prism_ptr()) {
744 pm_string_mapped_init(&input, RSTRING_PTR(fname));
745 pm_options_filepath_set(&options, RSTRING_PTR(fname));
748 pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), &options);
750 iseq = rb_iseq_new_main_prism(&input, &options, fname);
752 pm_string_free(&input);
753 pm_options_free(&options);
757 VALUE v = rb_vm_push_frame_fname(ec, fname);
759 VALUE parser = rb_parser_new();
760 rb_parser_set_context(parser, NULL, FALSE);
761 ast = (
rb_ast_t *)rb_parser_load_file(parser, fname);
764 VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
766 iseq = rb_iseq_new_top(&ast->body, rb_fstring_lit(
"<top (required)>"),
767 fname, realpath_internal_cached(realpath_map, fname), NULL);
773 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
777static inline enum ruby_tag_type
780 enum ruby_tag_type state;
782 volatile VALUE wrapper = th->top_wrapper;
783 volatile VALUE self = th->top_self;
792 th->top_wrapper = load_wrapper;
796 state = EC_EXEC_TAG();
797 if (state == TAG_NONE) {
798 load_iseq_eval(ec, fname);
807 th->top_wrapper = wrapper;
815 rb_vm_jump_tag_but_local_jump(state);
818 if (!
NIL_P(ec->errinfo)) {
819 rb_exc_raise(ec->errinfo);
827 enum ruby_tag_type state = TAG_NONE;
832 state = load_wrapping(ec, fname, wrap);
835 load_iseq_eval(ec, fname);
837 raise_load_if_failed(ec, state);
844 if (!tmp) load_failed(fname);
845 rb_load_internal(tmp, RBOOL(wrap));
851 enum ruby_tag_type state;
853 EC_PUSH_TAG(GET_EC());
854 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
859 if (state != TAG_NONE) *pstate = state;
896 VALUE fname, wrap, path, orig_fname;
900 orig_fname = rb_get_path_check_to_string(fname);
901 fname = rb_str_encode_ospath(orig_fname);
902 RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
904 path = rb_find_file(fname);
906 if (!rb_file_load_ok(RSTRING_PTR(fname)))
907 load_failed(orig_fname);
910 rb_load_internal(path, wrap);
912 RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
918load_lock(
rb_vm_t *vm,
const char *ftptr,
bool warn)
921 st_table *loading_tbl = get_loading_table(vm);
923 if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
926 data = (st_data_t)rb_thread_shield_new();
927 st_insert(loading_tbl, (st_data_t)ftptr, data);
928 return (
char *)ftptr;
931 if (warn && rb_thread_shield_owned((
VALUE)data)) {
932 VALUE warning = rb_warning_string(
"loading in progress, circular require considered harmful - %s", ftptr);
936 switch (rb_thread_shield_wait((
VALUE)data)) {
941 return (
char *)ftptr;
945release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done,
int existing)
948 if (!existing)
return ST_STOP;
950 rb_thread_shield_destroy(thread_shield);
954 else if (rb_thread_shield_release(thread_shield)) {
963load_unlock(
rb_vm_t *vm,
const char *ftptr,
int done)
966 st_data_t key = (st_data_t)ftptr;
967 st_table *loading_tbl = get_loading_table(vm);
969 st_update(loading_tbl, key, release_thread_shield, done);
973static VALUE rb_require_string_internal(
VALUE fname,
bool resurrect);
1030 VALUE base = rb_current_realfilepath();
1034 base = rb_file_dirname(base);
1035 return rb_require_string_internal(rb_file_absolute_path(fname, base),
false);
1038typedef int (*feature_func)(
rb_vm_t *vm,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn);
1041search_required(
rb_vm_t *vm,
VALUE fname,
volatile VALUE *path, feature_func rb_feature_p)
1046 const char *loading;
1049 ext = strrchr(ftptr = RSTRING_PTR(fname),
'.');
1050 if (ext && !strchr(ext,
'/')) {
1051 if (IS_RBEXT(ext)) {
1052 if (rb_feature_p(vm, ftptr, ext, TRUE, FALSE, &loading)) {
1053 if (loading) *path = rb_filesystem_str_new_cstr(loading);
1056 if ((tmp = rb_find_file(fname)) != 0) {
1057 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1058 if (!rb_feature_p(vm, ftptr, ext, TRUE, TRUE, &loading) || loading)
1064 else if (IS_SOEXT(ext)) {
1065 if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
1066 if (loading) *path = rb_filesystem_str_new_cstr(loading);
1069 tmp = rb_str_subseq(fname, 0, ext - RSTRING_PTR(fname));
1072 if ((tmp = rb_find_file(tmp)) != 0) {
1073 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1074 if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
1079 else if (IS_DLEXT(ext)) {
1080 if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
1081 if (loading) *path = rb_filesystem_str_new_cstr(loading);
1084 if ((tmp = rb_find_file(fname)) != 0) {
1085 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1086 if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
1092 else if ((ft = rb_feature_p(vm, ftptr, 0, FALSE, FALSE, &loading)) ==
'r') {
1093 if (loading) *path = rb_filesystem_str_new_cstr(loading);
1097 const unsigned int type = rb_find_file_ext(&tmp, ft ==
's' ? ruby_ext : loadable_ext);
1101 if (!ft &&
type != loadable_ext_rb && vm->static_ext_inits) {
1102 VALUE lookup_name = tmp;
1107 lookup_name = rb_str_dup(lookup_name);
1110 ftptr = RSTRING_PTR(lookup_name);
1111 if (st_lookup(vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
1112 *path = rb_filesystem_str_new_cstr(ftptr);
1120 goto feature_present;
1121 ftptr = RSTRING_PTR(tmp);
1122 return rb_feature_p(vm, ftptr, 0, FALSE, TRUE, 0);
1126 goto feature_present;
1129 case loadable_ext_rb:
1130 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1131 if (rb_feature_p(vm, ftptr, ext,
type == loadable_ext_rb, TRUE, &loading) && !loading)
1135 return type > loadable_ext_rb ?
's' :
'r';
1138 if (loading) *path = rb_filesystem_str_new_cstr(loading);
1143load_failed(
VALUE fname)
1145 rb_load_fail(fname,
"cannot load such file");
1151 rb_scope_visibility_set(METHOD_VISI_PUBLIC);
1152 return (
VALUE)dln_load(RSTRING_PTR(path));
1156run_static_ext_init(
rb_vm_t *vm,
const char *feature)
1158 st_data_t key = (st_data_t)feature;
1159 st_data_t init_func;
1161 if (vm->static_ext_inits && st_delete(vm->static_ext_inits, &key, &init_func)) {
1162 ((void (*)(void))init_func)();
1169no_feature_p(
rb_vm_t *vm,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
1176rb_resolve_feature_path(
VALUE klass,
VALUE fname)
1182 fname = rb_get_path(fname);
1183 path = rb_str_encode_ospath(fname);
1184 found = search_required(GET_VM(), path, &path, no_feature_p);
1188 sym =
ID2SYM(rb_intern(
"rb"));
1191 sym =
ID2SYM(rb_intern(
"so"));
1197 return rb_ary_new_from_args(2, sym, path);
1203 *prev = th->ext_config;
1210 th->ext_config = *prev;
1216 GET_THREAD()->ext_config.ractor_safe = flag;
1229 volatile int result = -1;
1231 volatile const struct {
1232 VALUE wrapper, self, errinfo;
1235 th->top_wrapper, th->top_self, ec->errinfo,
1238 enum ruby_tag_type state;
1239 char *
volatile ftptr = 0;
1241 volatile VALUE saved_path;
1242 volatile VALUE realpath = 0;
1243 VALUE realpaths = get_loaded_features_realpaths(th->vm);
1244 VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
1245 volatile bool reset_ext_config =
false;
1248 path = rb_str_encode_ospath(fname);
1249 RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
1254 th->top_wrapper = 0;
1255 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1259 RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
1260 found = search_required(th->vm, path, &saved_path, rb_feature_p);
1261 RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
1265 if (!path || !(ftptr = load_lock(th->vm, RSTRING_PTR(path), warn))) {
1269 result = TAG_RETURN;
1271 else if (found ==
's' && run_static_ext_init(th->vm, RSTRING_PTR(path))) {
1272 result = TAG_RETURN;
1274 else if (
RTEST(rb_hash_aref(realpaths,
1275 realpath = realpath_internal_cached(realpath_map, path)))) {
1281 load_iseq_eval(ec, path);
1285 reset_ext_config =
true;
1286 ext_config_push(th, &prev_ext_config);
1287 handle = rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
1288 path, VM_BLOCK_HANDLER_NONE, path);
1289 rb_hash_aset(ruby_dln_libmap, path, SVALUE2NUM((
SIGNED_VALUE)handle));
1292 result = TAG_RETURN;
1300 th2->top_self = saved.self;
1301 th2->top_wrapper = saved.wrapper;
1302 if (reset_ext_config) ext_config_pop(th2, &prev_ext_config);
1305 if (ftptr) load_unlock(th2->vm, RSTRING_PTR(path), !state);
1308 if (state == TAG_FATAL || state == TAG_THROW) {
1309 EC_JUMP_TAG(ec, state);
1311 else if (exception) {
1314 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
1315 if (!
NIL_P(exc)) ec->errinfo = exc;
1318 else if (state == TAG_RETURN) {
1325 if (!
NIL_P(ec->errinfo)) {
1326 if (!exception)
return TAG_RAISE;
1327 rb_exc_raise(ec->errinfo);
1330 if (result == TAG_RETURN) {
1331 rb_provide_feature(th2->vm, path);
1332 VALUE real = realpath;
1334 real = rb_fstring(real);
1335 rb_hash_aset(realpaths, real,
Qtrue);
1338 ec->errinfo = saved.errinfo;
1340 RUBY_DTRACE_HOOK(REQUIRE_RETURN, RSTRING_PTR(fname));
1346rb_require_internal_silent(
VALUE fname)
1349 return require_internal(ec, fname, 1,
false);
1353rb_require_internal(
VALUE fname)
1360ruby_require_internal(
const char *fname,
unsigned int len)
1363 VALUE str = rb_setup_fake_str(&fake, fname,
len, 0);
1366 rb_set_errinfo(
Qnil);
1367 return result == TAG_RETURN ? 1 : result ? -1 : 0;
1373 return rb_require_string_internal(
FilePathValue(fname),
false);
1377rb_require_string_internal(
VALUE fname,
bool resurrect)
1382 if (result > TAG_RETURN) {
1383 EC_JUMP_TAG(ec, result);
1390 return RBOOL(result);
1397 VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
1398 return rb_require_string_internal(str,
true);
1402register_init_ext(st_data_t *key, st_data_t *value, st_data_t init,
int existing)
1404 const char *name = (
char *)*key;
1407 rb_warn(
"%s is already registered", name);
1410 *value = (st_data_t)init;
1419ruby_init_ext(
const char *name,
void (*init)(
void))
1424 if (feature_provided(vm, name, 0))
1427 inits_table = vm->static_ext_inits;
1429 inits_table = st_init_strtable();
1430 vm->static_ext_inits = inits_table;
1432 st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init);
1459 rb_autoload_str(mod,
id, file);
1490rb_mod_autoload_p(
int argc,
VALUE *argv,
VALUE mod)
1493 VALUE sym = argv[0];
1499 return rb_autoload_at_p(mod,
id, recur);
1522 rb_raise(
rb_eTypeError,
"Can not set autoload on singleton class");
1524 return rb_mod_autoload(klass, sym, file);
1539rb_f_autoload_p(
int argc,
VALUE *argv,
VALUE obj)
1542 VALUE klass = rb_vm_cbase();
1546 return rb_mod_autoload_p(argc, argv, klass);
1558 resolved = rb_resolve_feature_path((
VALUE)NULL, fname_str);
1559 if (
NIL_P(resolved)) {
1560 ext = strrchr(fname,
'.');
1561 if (!ext || !IS_SOEXT(ext)) {
1564 if (rb_feature_p(GET_VM(), fname, 0, FALSE, FALSE, 0)) {
1565 return dln_symbol(NULL, symbol);
1569 if (
RARRAY_LEN(resolved) != 2 || rb_ary_entry(resolved, 0) !=
ID2SYM(rb_intern(
"so"))) {
1572 path = rb_ary_entry(resolved, 1);
1573 handle = rb_hash_lookup(ruby_dln_libmap, path);
1574 if (
NIL_P(handle)) {
1577 return dln_symbol((
void *)NUM2SVALUE(handle), symbol);
1584 static const char var_load_path[] =
"$:";
1585 ID id_load_path = rb_intern2(var_load_path,
sizeof(var_load_path)-1);
1590 vm->load_path = rb_ary_new();
1591 vm->expanded_load_path = rb_ary_hidden_new(0);
1592 vm->load_path_snapshot = rb_ary_hidden_new(0);
1593 vm->load_path_check_cache = 0;
1598 vm->loaded_features = rb_ary_new();
1599 vm->loaded_features_snapshot = rb_ary_hidden_new(0);
1600 vm->loaded_features_index = st_init_numtable();
1601 vm->loaded_features_realpaths = rb_hash_new();
1602 rb_obj_hide(vm->loaded_features_realpaths);
1603 vm->loaded_features_realpath_map = rb_hash_new();
1604 rb_obj_hide(vm->loaded_features_realpath_map);
1614 ruby_dln_libmap = rb_hash_new_with_size(0);
1615 rb_gc_register_mark_object(ruby_dln_libmap);
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_module_new(void)
Creates a new, anonymous module.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
void rb_warning(const char *fmt,...)
Issues a warning.
VALUE rb_cModule
Module class.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
VALUE rb_f_require(VALUE self, VALUE feature)
Identical to rb_require_string(), except it ignores the first argument for no reason.
void rb_ext_ractor_safe(bool flag)
Asserts that the extension library that calls this function is aware of Ractor.
VALUE rb_require_string(VALUE feature)
Finds and loads the given feature, if absent.
int rb_feature_provided(const char *feature, const char **loading)
Identical to rb_provided(), except it additionally returns the "canonical" name of the loaded feature...
void rb_load_protect(VALUE path, int wrap, int *state)
Identical to rb_load(), except it avoids potential global escapes.
int rb_provided(const char *feature)
Queries if the given feature has already been loaded into the execution context.
void rb_load(VALUE path, int wrap)
Loads and executes the Ruby program in the given file.
void * rb_ext_resolve_symbol(const char *feature, const char *symbol)
Resolves and returns a symbol of a function in the native extension specified by the feature and symb...
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
VALUE rb_str_resurrect(VALUE str)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
#define rb_strlen_lit(str)
Length of a string literal.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
int len
Length of the buffer.
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
#define RARRAY_LEN
Just another name of rb_array_len
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
VALUE rb_require(const char *feature)
Identical to rb_require_string(), except it takes C's string instead of Ruby's.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
The options that can be passed to the parser.
This struct represents the overall parser.
A generic string type that can have various ownership semantics.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.