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"
21#include "ractor_core.h"
23static VALUE ruby_dln_libmap;
25#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
26#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
27#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
29#if SIZEOF_VALUE <= SIZEOF_LONG
30# define SVALUE2NUM(x) LONG2NUM((long)(x))
31# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LONG(x)
32#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
33# define SVALUE2NUM(x) LL2NUM((LONG_LONG)(x))
34# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LL(x)
36# error Need integer for VALUE
44static const char *
const loadable_ext[] = {
49static const char *
const ruby_ext[] = {
67rb_construct_expanded_load_path(rb_vm_t *vm,
enum expand_type
type,
int *has_relative,
int *has_non_cache)
69 VALUE load_path = vm->load_path;
70 VALUE expanded_load_path = vm->expanded_load_path;
74 ary = rb_ary_hidden_new(
RARRAY_LEN(load_path));
76 VALUE path, as_str, expanded_path;
77 int is_string, non_cache;
81 non_cache = !is_string ? 1 : 0;
82 as_str = rb_get_path_check_to_string(path);
83 as_cstr = RSTRING_PTR(as_str);
86 if ((
type == EXPAND_RELATIVE &&
87 rb_is_absolute_path(as_cstr)) ||
88 (
type == EXPAND_HOME &&
89 (!as_cstr[0] || as_cstr[0] !=
'~')) ||
90 (
type == EXPAND_NON_CACHE)) {
92 rb_ary_push(ary,
RARRAY_AREF(expanded_load_path, i));
96 if (!*has_relative && !rb_is_absolute_path(as_cstr))
98 if (!*has_non_cache && non_cache)
103 as_str = rb_get_path_check_convert(as_str);
104 expanded_path = rb_check_realpath(
Qnil, as_str, NULL);
105 if (
NIL_P(expanded_path)) expanded_path = as_str;
106 rb_ary_push(ary, rb_fstring(expanded_path));
109 vm->expanded_load_path = ary;
110 rb_ary_replace(vm->load_path_snapshot, vm->load_path);
114get_expanded_load_path(rb_vm_t *vm)
118 if (!rb_ary_shared_with_p(vm->load_path_snapshot, vm->load_path)) {
120 int has_relative = 0, has_non_cache = 0;
121 rb_construct_expanded_load_path(vm, EXPAND_ALL, &has_relative, &has_non_cache);
123 vm->load_path_check_cache = rb_dir_getwd_ospath();
125 else if (has_non_cache) {
127 vm->load_path_check_cache = non_cache;
130 vm->load_path_check_cache = 0;
133 else if (vm->load_path_check_cache == non_cache) {
134 int has_relative = 1, has_non_cache = 1;
136 rb_construct_expanded_load_path(vm, EXPAND_NON_CACHE,
137 &has_relative, &has_non_cache);
139 else if (vm->load_path_check_cache) {
140 int has_relative = 1, has_non_cache = 1;
141 VALUE cwd = rb_dir_getwd_ospath();
145 vm->load_path_check_cache = cwd;
146 rb_construct_expanded_load_path(vm, EXPAND_RELATIVE,
147 &has_relative, &has_non_cache);
151 rb_construct_expanded_load_path(vm, EXPAND_HOME,
152 &has_relative, &has_non_cache);
155 return vm->expanded_load_path;
159rb_get_expanded_load_path(
void)
161 return get_expanded_load_path(GET_VM());
165load_path_getter(
ID id,
VALUE * p)
167 rb_vm_t *vm = (
void *)p;
168 return vm->load_path;
172get_loaded_features(rb_vm_t *vm)
174 return vm->loaded_features;
178get_loaded_features_realpaths(rb_vm_t *vm)
180 return vm->loaded_features_realpaths;
184get_loaded_features_realpath_map(rb_vm_t *vm)
186 return vm->loaded_features_realpath_map;
190get_LOADED_FEATURES(
ID _x,
VALUE *_y)
192 return get_loaded_features(GET_VM());
196reset_loaded_features_snapshot(rb_vm_t *vm)
198 rb_ary_replace(vm->loaded_features_snapshot, vm->loaded_features);
201static struct st_table *
202get_loaded_features_index_raw(rb_vm_t *vm)
204 return vm->loaded_features_index;
208get_loading_table(rb_vm_t *vm)
210 return vm->loading_table;
214feature_key(
const char *str,
size_t len)
216 return st_hash(str,
len, 0xfea7009e);
220is_rbext_path(
VALUE feature_path)
222 long len = RSTRING_LEN(feature_path);
224 if (
len <= rbext_len)
return false;
225 return IS_RBEXT(RSTRING_PTR(feature_path) +
len - rbext_len);
228typedef rb_darray(
long) feature_indexes_t;
230struct features_index_add_single_args {
237features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args,
int existing)
239 struct features_index_add_single_args *args = (
struct features_index_add_single_args *)raw_args;
240 rb_vm_t *vm = args->vm;
241 VALUE offset = args->offset;
245 VALUE this_feature_index = *value;
248 VALUE loaded_features = get_loaded_features(vm);
251 feature_indexes_t feature_indexes;
252 rb_darray_make(&feature_indexes, 2);
253 int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0;
254 rb_darray_set(feature_indexes, top^0,
FIX2LONG(this_feature_index));
255 rb_darray_set(feature_indexes, top^1,
FIX2LONG(offset));
261 *value = (st_data_t)feature_indexes;
264 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
268 VALUE loaded_features = get_loaded_features(vm);
269 for (
size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
270 long idx = rb_darray_get(feature_indexes, i);
273 if (!is_rbext_path(this_feature_path)) {
280 rb_darray_append(&feature_indexes,
FIX2LONG(offset));
282 *value = (st_data_t)feature_indexes;
285 long *ptr = rb_darray_data_ptr(feature_indexes);
286 long len = rb_darray_size(feature_indexes);
287 MEMMOVE(ptr + pos, ptr + pos + 1,
long,
len - pos - 1);
300features_index_add_single(rb_vm_t *vm,
const char* str,
size_t len,
VALUE offset,
bool rb)
302 struct st_table *features_index;
303 st_data_t short_feature_key;
306 short_feature_key = feature_key(str,
len);
308 features_index = get_loaded_features_index_raw(vm);
310 struct features_index_add_single_args args = {
316 st_update(features_index, short_feature_key, features_index_add_single_callback, (st_data_t)&args);
328features_index_add(rb_vm_t *vm,
VALUE feature,
VALUE offset)
330 const char *feature_str, *feature_end, *ext, *p;
334 feature_end = feature_str + RSTRING_LEN(feature);
336 for (ext = feature_end; ext > feature_str; ext--)
337 if (*ext ==
'.' || *ext ==
'/')
346 p = ext ? ext : feature_end;
349 while (p >= feature_str && *p !=
'/')
354 features_index_add_single(vm, p + 1, feature_end - p - 1, offset,
false);
356 features_index_add_single(vm, p + 1, ext - p - 1, offset, rb);
359 features_index_add_single(vm, feature_str, feature_end - feature_str, offset,
false);
361 features_index_add_single(vm, feature_str, ext - feature_str, offset, rb);
366loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
370 rb_darray_free((
void *)obj);
376rb_free_loaded_features_index(rb_vm_t *vm)
378 st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
379 st_free_table(vm->loaded_features_index);
383get_loaded_features_index(rb_vm_t *vm)
388 if (!rb_ary_shared_with_p(vm->loaded_features_snapshot, vm->loaded_features)) {
391 st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
393 VALUE realpaths = vm->loaded_features_realpaths;
394 VALUE realpath_map = vm->loaded_features_realpath_map;
395 VALUE previous_realpath_map = rb_hash_dup(realpath_map);
396 rb_hash_clear(realpaths);
397 rb_hash_clear(realpath_map);
402 features = rb_ary_resurrect(vm->loaded_features);
405 as_str = entry = rb_ary_entry(features, i);
407 as_str = rb_fstring(as_str);
409 rb_ary_store(features, i, as_str);
410 features_index_add(vm, as_str,
INT2FIX(i));
413 if (!rb_ary_shared_with_p(features, vm->loaded_features)) {
414 rb_ary_replace(vm->loaded_features, features);
416 reset_loaded_features_snapshot(vm);
418 features = rb_ary_dup(vm->loaded_features_snapshot);
420 for (i = 0; i < j; i++) {
421 VALUE as_str = rb_ary_entry(features, i);
422 VALUE realpath = rb_hash_aref(previous_realpath_map, as_str);
423 if (
NIL_P(realpath)) {
424 realpath = rb_check_realpath(
Qnil, as_str, NULL);
425 if (
NIL_P(realpath)) realpath = as_str;
426 realpath = rb_fstring(realpath);
428 rb_hash_aset(realpaths, realpath,
Qtrue);
429 rb_hash_aset(realpath_map, as_str, realpath);
432 return vm->loaded_features_index;
447loaded_feature_path(
const char *name,
long vlen,
const char *feature,
long len,
454 if (vlen <
len+1)
return 0;
455 if (strchr(feature,
'.') && !strncmp(name+(vlen-
len), feature,
len)) {
459 for (e = name + vlen; name != e && *e !=
'.' && *e !=
'/'; --e);
462 strncmp(e-
len, feature,
len))
464 plen = e - name -
len;
466 if (plen > 0 && name[plen-1] !=
'/') {
469 if (
type ==
's' ? !IS_DLEXT(&name[plen+
len]) :
470 type ==
'r' ? !IS_RBEXT(&name[plen+
len]) :
477 if (plen > 0) --plen;
481 long n = RSTRING_LEN(p);
483 if (n != plen)
continue;
484 if (n && strncmp(name, s, n))
continue;
499loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
501 const char *s = (
const char *)v;
503 VALUE p = loaded_feature_path(s, strlen(s), fp->name, fp->len,
504 fp->type, fp->load_path);
505 if (!p)
return ST_CONTINUE;
517rb_feature_p(rb_vm_t *vm,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
519 VALUE features, this_feature_index =
Qnil, v, p, load_path = 0;
521 long i,
len, elen, n;
522 st_table *loading_tbl, *features_index;
530 len = strlen(feature) - elen;
531 type = rb ?
'r' :
's';
534 len = strlen(feature);
538 features = get_loaded_features(vm);
539 features_index = get_loaded_features_index(vm);
541 key = feature_key(feature, strlen(feature));
568 if (st_lookup(features_index, key, &data) && !
NIL_P(this_feature_index = (
VALUE)data)) {
569 for (
size_t i = 0; ; i++) {
573 index =
FIX2LONG(this_feature_index);
576 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
577 if (i >= rb_darray_size(feature_indexes))
break;
578 index = rb_darray_get(feature_indexes, i);
583 if ((n = RSTRING_LEN(v)) <
len)
continue;
584 if (strncmp(f, feature,
len) != 0) {
585 if (expanded)
continue;
586 if (!load_path) load_path = get_expanded_load_path(vm);
587 if (!(p = loaded_feature_path(f, n, feature,
len,
type, load_path)))
590 f += RSTRING_LEN(p) + 1;
592 if (!*(e = f +
len)) {
596 if (*e !=
'.')
continue;
597 if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
600 if ((rb || !ext) && (IS_RBEXT(e))) {
606 loading_tbl = get_loading_table(vm);
608 if (!expanded && !rb_is_absolute_path(feature)) {
613 fs.load_path = load_path ? load_path : get_expanded_load_path(vm);
615 st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
616 if ((f = fs.result) != 0) {
621 if (st_get_key(loading_tbl, (st_data_t)feature, &data)) {
622 if (fn) *fn = (
const char*)data;
628 static const char so_ext[][4] = {
632 if (ext && *ext)
return 0;
634 buf = RSTRING_PTR(bufstr);
636 for (i = 0; (e = loadable_ext[i]) != 0; i++) {
637 strlcpy(buf +
len, e, DLEXT_MAXLEN + 1);
638 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
639 rb_str_resize(bufstr, 0);
640 if (fn) *fn = (
const char*)data;
641 return i ?
's' :
'r';
644 for (i = 0; i < numberof(so_ext); i++) {
645 strlcpy(buf +
len, so_ext[i], DLEXT_MAXLEN + 1);
646 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
647 rb_str_resize(bufstr, 0);
648 if (fn) *fn = (
const char*)data;
652 rb_str_resize(bufstr, 0);
657 if (!ext)
return 'u';
658 return !IS_RBEXT(ext) ?
's' :
'r';
668feature_provided(rb_vm_t *vm,
const char *feature,
const char **loading)
670 const char *ext = strrchr(feature,
'.');
673 if (*feature ==
'.' &&
674 (feature[1] ==
'/' || strncmp(feature+1,
"./", 2) == 0)) {
675 fullpath = rb_file_expand_path_fast(rb_get_path(
rb_str_new2(feature)),
Qnil);
676 feature = RSTRING_PTR(fullpath);
678 if (ext && !strchr(ext,
'/')) {
680 if (rb_feature_p(vm, feature, ext, TRUE, FALSE, loading))
return TRUE;
683 else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
684 if (rb_feature_p(vm, feature, ext, FALSE, FALSE, loading))
return TRUE;
688 if (rb_feature_p(vm, feature, 0, TRUE, FALSE, loading))
697 return feature_provided(GET_VM(), feature, loading);
701rb_provide_feature(rb_vm_t *vm,
VALUE feature)
705 features = get_loaded_features(vm);
708 "$LOADED_FEATURES is frozen; cannot append feature");
710 feature = rb_fstring(feature);
712 get_loaded_features_index(vm);
716 rb_ary_clear(vm->loaded_features_snapshot);
717 rb_ary_push(features, feature);
719 reset_loaded_features_snapshot(vm);
725 rb_provide_feature(GET_VM(), rb_fstring_cstr(feature));
728NORETURN(
static void load_failed(
VALUE));
731realpath_internal_cached(
VALUE hash,
VALUE path)
733 VALUE ret = rb_hash_aref(hash, path);
738 VALUE realpath = rb_realpath_internal(
Qnil, path, 1);
739 rb_hash_aset(hash, rb_fstring(path), rb_fstring(realpath));
744load_iseq_eval(rb_execution_context_t *ec,
VALUE fname)
746 const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
749 rb_execution_context_t *ec = GET_EC();
750 VALUE v = rb_vm_push_frame_fname(ec, fname);
752 rb_thread_t *th = rb_ec_thread_ptr(ec);
753 VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
755 if (rb_ruby_prism_p()) {
758 result.
node.coverage_enabled = 1;
760 VALUE error = pm_load_parse_file(&result, fname, NULL);
764 iseq = pm_iseq_new_top(&result.
node, rb_fstring_lit(
"<top (required)>"), fname, realpath_internal_cached(realpath_map, fname), NULL, &error_state);
766 pm_parse_result_free(&result);
770 rb_jump_tag(error_state);
776 pm_parse_result_free(&result);
783 VALUE parser = rb_parser_new();
784 rb_parser_set_context(parser, NULL, FALSE);
785 ast_value = rb_parser_load_file(parser, fname);
786 ast = rb_ruby_ast_data_get(ast_value);
788 iseq = rb_iseq_new_top(ast_value, rb_fstring_lit(
"<top (required)>"),
789 fname, realpath_internal_cached(realpath_map, fname), NULL);
796 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
800static inline enum ruby_tag_type
801load_wrapping(rb_execution_context_t *ec,
VALUE fname,
VALUE load_wrapper)
803 enum ruby_tag_type state;
804 rb_thread_t *th = rb_ec_thread_ptr(ec);
805 volatile VALUE wrapper = th->top_wrapper;
806 volatile VALUE self = th->top_self;
808 rb_thread_t *
volatile th0 = th;
815 th->top_wrapper = load_wrapper;
819 state = EC_EXEC_TAG();
820 if (state == TAG_NONE) {
821 load_iseq_eval(ec, fname);
830 th->top_wrapper = wrapper;
835raise_load_if_failed(rb_execution_context_t *ec,
enum ruby_tag_type state)
838 rb_vm_jump_tag_but_local_jump(state);
841 if (!
NIL_P(ec->errinfo)) {
842 rb_exc_raise(ec->errinfo);
849 rb_execution_context_t *ec = GET_EC();
850 enum ruby_tag_type state = TAG_NONE;
855 state = load_wrapping(ec, fname, wrap);
858 load_iseq_eval(ec, fname);
860 raise_load_if_failed(ec, state);
867 if (!tmp) load_failed(fname);
868 rb_load_internal(tmp, RBOOL(wrap));
874 enum ruby_tag_type state;
876 EC_PUSH_TAG(GET_EC());
877 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
882 if (state != TAG_NONE) *pstate = state;
918 VALUE fname, wrap, path, orig_fname;
922 orig_fname = rb_get_path_check_to_string(fname);
923 fname = rb_str_encode_ospath(orig_fname);
924 RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
926 path = rb_find_file(fname);
928 if (!rb_file_load_ok(RSTRING_PTR(fname)))
929 load_failed(orig_fname);
932 rb_load_internal(path, wrap);
934 RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
940load_lock(rb_vm_t *vm,
const char *ftptr,
bool warn)
943 st_table *loading_tbl = get_loading_table(vm);
945 if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
948 data = (st_data_t)rb_thread_shield_new();
949 st_insert(loading_tbl, (st_data_t)ftptr, data);
950 return (
char *)ftptr;
953 if (warn && rb_thread_shield_owned((
VALUE)data)) {
954 VALUE warning = rb_warning_string(
"loading in progress, circular require considered harmful - %s", ftptr);
958 switch (rb_thread_shield_wait((
VALUE)data)) {
963 return (
char *)ftptr;
967release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done,
int existing)
970 if (!existing)
return ST_STOP;
972 rb_thread_shield_destroy(thread_shield);
976 else if (rb_thread_shield_release(thread_shield)) {
985load_unlock(rb_vm_t *vm,
const char *ftptr,
int done)
988 st_data_t key = (st_data_t)ftptr;
989 st_table *loading_tbl = get_loading_table(vm);
991 st_update(loading_tbl, key, release_thread_shield, done);
995static VALUE rb_require_string_internal(
VALUE fname,
bool resurrect);
1052 VALUE base = rb_current_realfilepath();
1056 base = rb_file_dirname(base);
1057 return rb_require_string_internal(rb_file_absolute_path(fname, base),
false);
1060typedef int (*feature_func)(rb_vm_t *vm,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn);
1063search_required(rb_vm_t *vm,
VALUE fname,
volatile VALUE *path, feature_func rb_feature_p)
1068 const char *loading;
1071 ext = strrchr(ftptr = RSTRING_PTR(fname),
'.');
1072 if (ext && !strchr(ext,
'/')) {
1073 if (IS_RBEXT(ext)) {
1074 if (rb_feature_p(vm, ftptr, ext, TRUE, FALSE, &loading)) {
1078 if ((tmp = rb_find_file(fname)) != 0) {
1079 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1080 if (!rb_feature_p(vm, ftptr, ext, TRUE, TRUE, &loading) || loading)
1086 else if (IS_SOEXT(ext)) {
1087 if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
1094 if ((tmp = rb_find_file(tmp)) != 0) {
1095 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1096 if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
1101 else if (IS_DLEXT(ext)) {
1102 if (rb_feature_p(vm, ftptr, ext, FALSE, FALSE, &loading)) {
1106 if ((tmp = rb_find_file(fname)) != 0) {
1107 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1108 if (!rb_feature_p(vm, ftptr, ext, FALSE, TRUE, &loading) || loading)
1114 else if ((ft = rb_feature_p(vm, ftptr, 0, FALSE, FALSE, &loading)) ==
'r') {
1119 const unsigned int type = rb_find_file_ext(&tmp, ft ==
's' ? ruby_ext : loadable_ext);
1123 if (!ft &&
type != loadable_ext_rb && vm->static_ext_inits) {
1124 VALUE lookup_name = tmp;
1132 ftptr = RSTRING_PTR(lookup_name);
1133 if (st_lookup(vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
1143 goto feature_present;
1144 ftptr = RSTRING_PTR(tmp);
1145 return rb_feature_p(vm, ftptr, 0, FALSE, TRUE, 0);
1149 goto feature_present;
1152 case loadable_ext_rb:
1153 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1154 if (rb_feature_p(vm, ftptr, ext,
type == loadable_ext_rb, TRUE, &loading) && !loading)
1158 return type > loadable_ext_rb ?
's' :
'r';
1166load_failed(
VALUE fname)
1168 rb_load_fail(fname,
"cannot load such file");
1174 rb_scope_visibility_set(METHOD_VISI_PUBLIC);
1175 return (
VALUE)dln_load(RSTRING_PTR(path));
1179run_static_ext_init(rb_vm_t *vm,
const char *feature)
1181 st_data_t key = (st_data_t)feature;
1182 st_data_t init_func;
1184 if (vm->static_ext_inits && st_delete(vm->static_ext_inits, &key, &init_func)) {
1185 ((void (*)(void))init_func)();
1192no_feature_p(rb_vm_t *vm,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
1199rb_resolve_feature_path(
VALUE klass,
VALUE fname)
1205 fname = rb_get_path(fname);
1206 path = rb_str_encode_ospath(fname);
1207 found = search_required(GET_VM(), path, &path, no_feature_p);
1211 sym =
ID2SYM(rb_intern(
"rb"));
1214 sym =
ID2SYM(rb_intern(
"so"));
1220 return rb_ary_new_from_args(2, sym, path);
1226 *prev = th->ext_config;
1233 th->ext_config = *prev;
1239 GET_THREAD()->ext_config.ractor_safe = flag;
1250require_internal(rb_execution_context_t *ec,
VALUE fname,
int exception,
bool warn)
1252 volatile int result = -1;
1253 rb_thread_t *th = rb_ec_thread_ptr(ec);
1254 volatile const struct {
1255 VALUE wrapper, self, errinfo;
1256 rb_execution_context_t *ec;
1258 th->top_wrapper, th->top_self, ec->errinfo,
1261 enum ruby_tag_type state;
1262 char *
volatile ftptr = 0;
1264 volatile VALUE saved_path;
1265 volatile VALUE realpath = 0;
1266 VALUE realpaths = get_loaded_features_realpaths(th->vm);
1267 VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
1268 volatile bool reset_ext_config =
false;
1271 path = rb_str_encode_ospath(fname);
1272 RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
1277 th->top_wrapper = 0;
1278 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1282 RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
1283 found = search_required(th->vm, path, &saved_path, rb_feature_p);
1284 RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
1288 if (!path || !(ftptr = load_lock(th->vm, RSTRING_PTR(path), warn))) {
1292 result = TAG_RETURN;
1294 else if (found ==
's' && run_static_ext_init(th->vm, RSTRING_PTR(path))) {
1295 result = TAG_RETURN;
1297 else if (
RTEST(rb_hash_aref(realpaths,
1298 realpath = realpath_internal_cached(realpath_map, path)))) {
1304 load_iseq_eval(saved.ec, path);
1308 reset_ext_config =
true;
1309 ext_config_push(th, &prev_ext_config);
1310 handle = rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
1311 path, VM_BLOCK_HANDLER_NONE, path);
1312 rb_hash_aset(ruby_dln_libmap, path, SVALUE2NUM((
SIGNED_VALUE)handle));
1315 result = TAG_RETURN;
1322 rb_thread_t *th2 = rb_ec_thread_ptr(ec);
1323 th2->top_self = saved.self;
1324 th2->top_wrapper = saved.wrapper;
1325 if (reset_ext_config) ext_config_pop(th2, &prev_ext_config);
1328 if (ftptr) load_unlock(th2->vm, RSTRING_PTR(path), !state);
1331 if (state == TAG_FATAL || state == TAG_THROW) {
1332 EC_JUMP_TAG(ec, state);
1334 else if (exception) {
1337 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
1338 if (!
NIL_P(exc)) ec->errinfo = exc;
1341 else if (state == TAG_RETURN) {
1348 if (!
NIL_P(ec->errinfo)) {
1349 if (!exception)
return TAG_RAISE;
1350 rb_exc_raise(ec->errinfo);
1353 if (result == TAG_RETURN) {
1354 rb_provide_feature(th2->vm, path);
1355 VALUE real = realpath;
1357 real = rb_fstring(real);
1358 rb_hash_aset(realpaths, real,
Qtrue);
1361 ec->errinfo = saved.errinfo;
1363 RUBY_DTRACE_HOOK(REQUIRE_RETURN, RSTRING_PTR(fname));
1369rb_require_internal_silent(
VALUE fname)
1371 rb_execution_context_t *ec = GET_EC();
1372 return require_internal(ec, fname, 1,
false);
1376rb_require_internal(
VALUE fname)
1378 rb_execution_context_t *ec = GET_EC();
1383ruby_require_internal(
const char *fname,
unsigned int len)
1386 VALUE str = rb_setup_fake_str(&fake, fname,
len, 0);
1387 rb_execution_context_t *ec = GET_EC();
1389 rb_set_errinfo(
Qnil);
1390 return result == TAG_RETURN ? 1 : result ? -1 : 0;
1396 return rb_require_string_internal(
FilePathValue(fname),
false);
1400rb_require_string_internal(
VALUE fname,
bool resurrect)
1402 rb_execution_context_t *ec = GET_EC();
1405 if (!rb_ractor_main_p()) {
1407 return rb_ractor_require(fname);
1412 if (result > TAG_RETURN) {
1413 EC_JUMP_TAG(ec, result);
1420 return RBOOL(result);
1428 VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
1429 return rb_require_string_internal(str,
true);
1433register_init_ext(st_data_t *key, st_data_t *value, st_data_t init,
int existing)
1435 const char *name = (
char *)*key;
1438 rb_warn(
"%s is already registered", name);
1441 *value = (st_data_t)init;
1450ruby_init_ext(
const char *name,
void (*init)(
void))
1452 st_table *inits_table;
1453 rb_vm_t *vm = GET_VM();
1455 if (feature_provided(vm, name, 0))
1458 inits_table = vm->static_ext_inits;
1460 inits_table = st_init_strtable();
1461 vm->static_ext_inits = inits_table;
1463 st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init);
1490 rb_autoload_str(mod,
id, file);
1521rb_mod_autoload_p(
int argc,
VALUE *argv,
VALUE mod)
1524 VALUE sym = argv[0];
1530 return rb_autoload_at_p(mod,
id, recur);
1553 rb_raise(
rb_eTypeError,
"Can not set autoload on singleton class");
1555 return rb_mod_autoload(klass, sym, file);
1582rb_f_autoload_p(
int argc,
VALUE *argv,
VALUE obj)
1585 VALUE klass = rb_vm_cbase();
1589 return rb_mod_autoload_p(argc, argv, klass);
1601 resolved = rb_resolve_feature_path((
VALUE)NULL, fname_str);
1602 if (
NIL_P(resolved)) {
1603 ext = strrchr(fname,
'.');
1604 if (!ext || !IS_SOEXT(ext)) {
1607 if (rb_feature_p(GET_VM(), fname, 0, FALSE, FALSE, 0)) {
1608 return dln_symbol(NULL, symbol);
1612 if (
RARRAY_LEN(resolved) != 2 || rb_ary_entry(resolved, 0) !=
ID2SYM(rb_intern(
"so"))) {
1615 path = rb_ary_entry(resolved, 1);
1616 handle = rb_hash_lookup(ruby_dln_libmap, path);
1617 if (
NIL_P(handle)) {
1620 return dln_symbol((
void *)NUM2SVALUE(handle), symbol);
1626 rb_vm_t *vm = GET_VM();
1627 static const char var_load_path[] =
"$:";
1628 ID id_load_path = rb_intern2(var_load_path,
sizeof(var_load_path)-1);
1634 vm->expanded_load_path = rb_ary_hidden_new(0);
1635 vm->load_path_snapshot = rb_ary_hidden_new(0);
1636 vm->load_path_check_cache = 0;
1642 vm->loaded_features_snapshot = rb_ary_hidden_new(0);
1643 vm->loaded_features_index = st_init_numtable();
1657 ruby_dln_libmap = rb_hash_new_with_size(0);
1658 rb_vm_register_global_object(ruby_dln_libmap);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
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.
VALUE rb_ary_new(void)
Allocates a new, empty array.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
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_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_dup(VALUE str)
Duplicates a 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_filesystem_str_new_cstr(const char *ptr)
Identical to rb_filesystem_str_new(), except it assumes the passed pointer is a pointer to a C string...
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.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
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.
int32_t line
The line within the file that the parse starts on.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_options_t options
The options that will be passed to the parser.
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.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.