6#include "eval_intern.h"
8#include "internal/dir.h"
9#include "internal/error.h"
10#include "internal/eval.h"
11#include "internal/file.h"
12#include "internal/hash.h"
13#include "internal/load.h"
14#include "internal/namespace.h"
15#include "internal/ruby_parser.h"
16#include "internal/thread.h"
17#include "internal/variable.h"
23#include "ractor_core.h"
26#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
27#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
28#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
30#if SIZEOF_VALUE <= SIZEOF_LONG
31# define SVALUE2NUM(x) LONG2NUM((long)(x))
32# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LONG(x)
33#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
34# define SVALUE2NUM(x) LL2NUM((LONG_LONG)(x))
35# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LL(x)
37# error Need integer for VALUE
45static const char *
const loadable_ext[] = {
50static const char *
const ruby_ext[] = {
68rb_construct_expanded_load_path(
rb_namespace_t *ns,
enum expand_type
type,
int *has_relative,
int *has_non_cache)
70 VALUE load_path = ns->load_path;
71 VALUE expanded_load_path = ns->expanded_load_path;
78 VALUE path, as_str, expanded_path;
79 int is_string, non_cache;
83 non_cache = !is_string ? 1 : 0;
84 as_str = rb_get_path_check_to_string(path);
85 as_cstr = RSTRING_PTR(as_str);
88 if ((
type == EXPAND_RELATIVE &&
89 rb_is_absolute_path(as_cstr)) ||
90 (
type == EXPAND_HOME &&
91 (!as_cstr[0] || as_cstr[0] !=
'~')) ||
92 (
type == EXPAND_NON_CACHE)) {
98 if (!*has_relative && !rb_is_absolute_path(as_cstr))
100 if (!*has_non_cache && non_cache)
105 as_str = rb_get_path_check_convert(as_str);
106 expanded_path = rb_check_realpath(
Qnil, as_str, NULL);
107 if (
NIL_P(expanded_path)) expanded_path = as_str;
111 ns->expanded_load_path = ary;
112 snapshot = ns->load_path_snapshot;
113 load_path = ns->load_path;
122 const VALUE load_path_snapshot = ns->load_path_snapshot;
123 const VALUE load_path = ns->load_path;
127 int has_relative = 0, has_non_cache = 0;
128 rb_construct_expanded_load_path(ns, EXPAND_ALL, &has_relative, &has_non_cache);
130 ns->load_path_check_cache = rb_dir_getwd_ospath();
132 else if (has_non_cache) {
134 ns->load_path_check_cache = non_cache;
137 ns->load_path_check_cache = 0;
140 else if ((check_cache = ns->load_path_check_cache) == non_cache) {
141 int has_relative = 1, has_non_cache = 1;
143 rb_construct_expanded_load_path(ns, EXPAND_NON_CACHE,
144 &has_relative, &has_non_cache);
146 else if (check_cache) {
147 int has_relative = 1, has_non_cache = 1;
148 VALUE cwd = rb_dir_getwd_ospath();
152 ns->load_path_check_cache = cwd;
153 rb_construct_expanded_load_path(ns, EXPAND_RELATIVE,
154 &has_relative, &has_non_cache);
158 rb_construct_expanded_load_path(ns, EXPAND_HOME,
159 &has_relative, &has_non_cache);
162 return ns->expanded_load_path;
166rb_get_expanded_load_path(
void)
168 return get_expanded_load_path((
rb_namespace_t *)rb_loading_namespace());
172load_path_getter(
ID _x,
VALUE * _y)
174 return rb_loading_namespace()->load_path;
178get_LOADED_FEATURES(
ID _x,
VALUE *_y)
180 return rb_loading_namespace()->loaded_features;
186 VALUE snapshot = ns->loaded_features_snapshot;
187 VALUE loaded_features = ns->loaded_features;
194 return ns->loaded_features_index;
198feature_key(
const char *str,
size_t len)
200 return st_hash(str,
len, 0xfea7009e);
204is_rbext_path(
VALUE feature_path)
206 long len = RSTRING_LEN(feature_path);
208 if (
len <= rbext_len)
return false;
209 return IS_RBEXT(RSTRING_PTR(feature_path) +
len - rbext_len);
212typedef rb_darray(
long) feature_indexes_t;
214struct features_index_add_single_args {
221features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args,
int existing)
223 struct features_index_add_single_args *args = (
struct features_index_add_single_args *)raw_args;
225 VALUE offset = args->offset;
229 VALUE this_feature_index = *value;
232 VALUE loaded_features = ns->loaded_features;
235 feature_indexes_t feature_indexes;
236 rb_darray_make(&feature_indexes, 2);
237 int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0;
238 rb_darray_set(feature_indexes, top^0,
FIX2LONG(this_feature_index));
239 rb_darray_set(feature_indexes, top^1,
FIX2LONG(offset));
245 *value = (st_data_t)feature_indexes;
248 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
252 VALUE loaded_features = ns->loaded_features;
253 for (
size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
254 long idx = rb_darray_get(feature_indexes, i);
257 if (!is_rbext_path(this_feature_path)) {
264 rb_darray_append(&feature_indexes,
FIX2LONG(offset));
266 *value = (st_data_t)feature_indexes;
269 long *ptr = rb_darray_data_ptr(feature_indexes);
270 long len = rb_darray_size(feature_indexes);
271 MEMMOVE(ptr + pos + 1, ptr + pos,
long,
len - pos - 1);
287 st_data_t short_feature_key;
290 short_feature_key = feature_key(str,
len);
292 features_index = get_loaded_features_index_raw(ns);
294 struct features_index_add_single_args args = {
300 st_update(features_index, short_feature_key, features_index_add_single_callback, (st_data_t)&args);
316 const char *feature_str, *feature_end, *ext, *p;
320 feature_end = feature_str + RSTRING_LEN(feature);
322 for (ext = feature_end; ext > feature_str; ext--)
323 if (*ext ==
'.' || *ext ==
'/')
332 p = ext ? ext : feature_end;
335 while (p >= feature_str && *p !=
'/')
340 features_index_add_single(ns, p + 1, feature_end - p - 1, offset,
false);
342 features_index_add_single(ns, p + 1, ext - p - 1, offset, rb);
345 features_index_add_single(ns, feature_str, feature_end - feature_str, offset,
false);
347 features_index_add_single(ns, feature_str, ext - feature_str, offset, rb);
352loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
356 rb_darray_free((
void *)obj);
365 VALUE features = ns->loaded_features;
366 const VALUE snapshot = ns->loaded_features_snapshot;
371 st_foreach(ns->loaded_features_index, loaded_features_index_clear_i, 0);
373 VALUE realpaths = ns->loaded_features_realpaths;
374 VALUE realpath_map = ns->loaded_features_realpath_map;
375 VALUE previous_realpath_map = rb_hash_dup(realpath_map);
376 rb_hash_clear(realpaths);
377 rb_hash_clear(realpath_map);
387 as_str = rb_fstring(as_str);
390 features_index_add(ns, as_str,
INT2FIX(i));
396 reset_loaded_features_snapshot(ns);
398 features = ns->loaded_features_snapshot;
400 for (i = 0; i < j; i++) {
402 VALUE realpath = rb_hash_aref(previous_realpath_map, as_str);
403 if (
NIL_P(realpath)) {
404 realpath = rb_check_realpath(
Qnil, as_str, NULL);
405 if (
NIL_P(realpath)) realpath = as_str;
406 realpath = rb_fstring(realpath);
408 rb_hash_aset(realpaths, realpath,
Qtrue);
409 rb_hash_aset(realpath_map, as_str, realpath);
412 return ns->loaded_features_index;
427loaded_feature_path(
const char *name,
long vlen,
const char *feature,
long len,
434 if (vlen <
len+1)
return 0;
435 if (strchr(feature,
'.') && !strncmp(name+(vlen-
len), feature,
len)) {
439 for (e = name + vlen; name != e && *e !=
'.' && *e !=
'/'; --e);
442 strncmp(e-
len, feature,
len))
444 plen = e - name -
len;
446 if (plen > 0 && name[plen-1] !=
'/') {
449 if (
type ==
's' ? !IS_DLEXT(&name[plen+
len]) :
450 type ==
'r' ? !IS_RBEXT(&name[plen+
len]) :
457 if (plen > 0) --plen;
461 long n = RSTRING_LEN(p);
463 if (n != plen)
continue;
464 if (n && strncmp(name, s, n))
continue;
479loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
481 const char *s = (
const char *)v;
483 VALUE p = loaded_feature_path(s, strlen(s), fp->name, fp->len,
484 fp->type, fp->load_path);
485 if (!p)
return ST_CONTINUE;
497rb_feature_p(
const rb_namespace_t *ns,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
499 VALUE features, this_feature_index =
Qnil, v, p, load_path = 0;
501 long i,
len, elen, n;
502 st_table *loading_tbl, *features_index;
510 len = strlen(feature) - elen;
511 type = rb ?
'r' :
's';
514 len = strlen(feature);
518 features = ns->loaded_features;
519 features_index = get_loaded_features_index(ns);
521 key = feature_key(feature, strlen(feature));
548 if (st_lookup(features_index, key, &data) && !
NIL_P(this_feature_index = (
VALUE)data)) {
549 for (
size_t i = 0; ; i++) {
553 index =
FIX2LONG(this_feature_index);
556 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
557 if (i >= rb_darray_size(feature_indexes))
break;
558 index = rb_darray_get(feature_indexes, i);
564 if ((n = RSTRING_LEN(v)) <
len)
continue;
565 if (strncmp(f, feature,
len) != 0) {
566 if (expanded)
continue;
567 if (!load_path) load_path = get_expanded_load_path((
rb_namespace_t *)ns);
568 if (!(p = loaded_feature_path(f, n, feature,
len,
type, load_path)))
571 f += RSTRING_LEN(p) + 1;
573 if (!*(e = f +
len)) {
577 if (*e !=
'.')
continue;
578 if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
581 if ((rb || !ext) && (IS_RBEXT(e))) {
587 loading_tbl = ns->loading_table;
589 if (!expanded && !rb_is_absolute_path(feature)) {
594 fs.load_path = load_path ? load_path : get_expanded_load_path((
rb_namespace_t *)ns);
596 st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
597 if ((f = fs.result) != 0) {
602 if (st_get_key(loading_tbl, (st_data_t)feature, &data)) {
603 if (fn) *fn = (
const char*)data;
609 static const char so_ext[][4] = {
613 if (ext && *ext)
return 0;
615 buf = RSTRING_PTR(bufstr);
617 for (i = 0; (e = loadable_ext[i]) != 0; i++) {
618 strlcpy(buf +
len, e, DLEXT_MAXLEN + 1);
619 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
620 rb_str_resize(bufstr, 0);
621 if (fn) *fn = (
const char*)data;
622 return i ?
's' :
'r';
625 for (i = 0; i < numberof(so_ext); i++) {
626 strlcpy(buf +
len, so_ext[i], DLEXT_MAXLEN + 1);
627 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
628 rb_str_resize(bufstr, 0);
629 if (fn) *fn = (
const char*)data;
633 rb_str_resize(bufstr, 0);
638 if (!ext)
return 'u';
639 return !IS_RBEXT(ext) ?
's' :
'r';
649feature_provided(
rb_namespace_t *ns,
const char *feature,
const char **loading)
651 const char *ext = strrchr(feature,
'.');
654 if (*feature ==
'.' &&
655 (feature[1] ==
'/' || strncmp(feature+1,
"./", 2) == 0)) {
656 fullpath = rb_file_expand_path_fast(rb_get_path(
rb_str_new2(feature)),
Qnil);
657 feature = RSTRING_PTR(fullpath);
659 if (ext && !strchr(ext,
'/')) {
661 if (rb_feature_p(ns, feature, ext, TRUE, FALSE, loading))
return TRUE;
664 else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
665 if (rb_feature_p(ns, feature, ext, FALSE, FALSE, loading))
return TRUE;
669 if (rb_feature_p(ns, feature, 0, TRUE, FALSE, loading))
679 return feature_provided(ns, feature, loading);
687 features = ns->loaded_features;
690 "$LOADED_FEATURES is frozen; cannot append feature");
692 feature = rb_fstring(feature);
694 get_loaded_features_index(ns);
701 reset_loaded_features_snapshot(ns);
711 rb_provide_feature(rb_current_namespace(), rb_fstring_cstr(feature));
714NORETURN(
static void load_failed(
VALUE));
717realpath_internal_cached(
VALUE hash,
VALUE path)
719 VALUE ret = rb_hash_aref(hash, path);
724 VALUE realpath = rb_realpath_internal(
Qnil, path, 1);
725 rb_hash_aset(hash, rb_fstring(path), rb_fstring(realpath));
733 const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
737 VALUE v = rb_vm_push_frame_fname(ec, fname);
739 VALUE realpath_map = ns->loaded_features_realpath_map;
741 if (rb_ruby_prism_p()) {
744 result.
node.coverage_enabled = 1;
746 VALUE error = pm_load_parse_file(&result, fname, NULL);
750 iseq = pm_iseq_new_top(&result.
node, rb_fstring_lit(
"<top (required)>"), fname, realpath_internal_cached(realpath_map, fname), NULL, &error_state);
752 pm_parse_result_free(&result);
756 rb_jump_tag(error_state);
762 pm_parse_result_free(&result);
769 VALUE parser = rb_parser_new();
770 rb_parser_set_context(parser, NULL, FALSE);
771 ast_value = rb_parser_load_file(parser, fname);
772 ast = rb_ruby_ast_data_get(ast_value);
774 iseq = rb_iseq_new_top(ast_value, rb_fstring_lit(
"<top (required)>"),
775 fname, realpath_internal_cached(realpath_map, fname), NULL);
782 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
784 rb_iseq_eval(iseq, ns);
787static inline enum ruby_tag_type
790 enum ruby_tag_type state;
793 volatile VALUE wrapper = th->top_wrapper;
794 volatile VALUE self = th->top_self;
802 if (NAMESPACE_OBJ_P(load_wrapper)) {
803 ns = rb_get_namespace_t(load_wrapper);
807 th->top_self = ns->top_self;
812 th->top_wrapper = load_wrapper;
816 state = EC_EXEC_TAG();
817 if (state == TAG_NONE) {
818 load_iseq_eval(ec, fname);
827 th->top_wrapper = wrapper;
835 rb_vm_jump_tag_but_local_jump(state);
838 if (!
NIL_P(ec->errinfo)) {
849 enum ruby_tag_type state = TAG_NONE;
854 state = load_wrapping(ec, fname, wrap);
856 else if (NAMESPACE_OPTIONAL_P(ns)) {
857 namespace = ns->ns_object;
858 state = load_wrapping(ec, fname,
namespace);
861 load_iseq_eval(ec, fname);
863 raise_load_if_failed(ec, state);
870 if (!tmp) load_failed(fname);
871 rb_load_internal(tmp, RBOOL(wrap));
877 enum ruby_tag_type state;
879 EC_PUSH_TAG(GET_EC());
880 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
885 if (state != TAG_NONE) *pstate = state;
889load_entrypoint_internal(
VALUE fname,
VALUE wrap)
891 VALUE path, orig_fname;
893 orig_fname = rb_get_path_check_to_string(fname);
894 fname = rb_str_encode_ospath(orig_fname);
895 RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
897 path = rb_find_file(fname);
899 if (!rb_file_load_ok(RSTRING_PTR(fname)))
900 load_failed(orig_fname);
903 rb_load_internal(path, wrap);
905 RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
911rb_load_entrypoint(
VALUE args)
915 rb_bug(
"invalid arguments: %ld",
RARRAY_LEN(args));
919 return load_entrypoint_internal(fname, wrap);
957 return load_entrypoint_internal(fname, wrap);
964 st_table *loading_tbl = ns->loading_table;
966 if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
969 data = (st_data_t)rb_thread_shield_new();
970 st_insert(loading_tbl, (st_data_t)ftptr, data);
971 return (
char *)ftptr;
974 if (warn && rb_thread_shield_owned((
VALUE)data)) {
975 VALUE warning = rb_warning_string(
"loading in progress, circular require considered harmful - %s", ftptr);
979 switch (rb_thread_shield_wait((
VALUE)data)) {
984 return (
char *)ftptr;
988release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done,
int existing)
991 if (!existing)
return ST_STOP;
993 rb_thread_shield_destroy(thread_shield);
997 else if (rb_thread_shield_release(thread_shield)) {
1001 xfree((
char *)*key);
1006load_unlock(
const rb_namespace_t *ns,
const char *ftptr,
int done)
1009 st_data_t key = (st_data_t)ftptr;
1010 st_table *loading_tbl = ns->loading_table;
1012 st_update(loading_tbl, key, release_thread_shield, done);
1016static VALUE rb_require_string_internal(
VALUE fname,
bool resurrect);
1064rb_require_relative_entrypoint(
VALUE fname)
1066 VALUE base = rb_current_realfilepath();
1070 base = rb_file_dirname(base);
1071 return rb_require_string_internal(rb_file_absolute_path(fname, base),
false);
1086 return rb_require_relative_entrypoint(fname);
1089typedef int (*feature_func)(
const rb_namespace_t *ns,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn);
1097 const char *loading;
1100 ext = strrchr(ftptr = RSTRING_PTR(fname),
'.');
1101 if (ext && !strchr(ext,
'/')) {
1102 if (IS_RBEXT(ext)) {
1103 if (rb_feature_p(ns, ftptr, ext, TRUE, FALSE, &loading)) {
1107 if ((tmp = rb_find_file(fname)) != 0) {
1108 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1109 if (!rb_feature_p(ns, ftptr, ext, TRUE, TRUE, &loading) || loading)
1115 else if (IS_SOEXT(ext)) {
1116 if (rb_feature_p(ns, ftptr, ext, FALSE, FALSE, &loading)) {
1123 if ((tmp = rb_find_file(tmp)) != 0) {
1124 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1125 if (!rb_feature_p(ns, ftptr, ext, FALSE, TRUE, &loading) || loading)
1130 else if (IS_DLEXT(ext)) {
1131 if (rb_feature_p(ns, ftptr, ext, FALSE, FALSE, &loading)) {
1135 if ((tmp = rb_find_file(fname)) != 0) {
1136 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1137 if (!rb_feature_p(ns, ftptr, ext, FALSE, TRUE, &loading) || loading)
1143 else if ((ft = rb_feature_p(ns, ftptr, 0, FALSE, FALSE, &loading)) ==
'r') {
1148 const unsigned int type = rb_find_file_ext(&tmp, ft ==
's' ? ruby_ext : loadable_ext);
1152 if (!ft &&
type != loadable_ext_rb) {
1154 if (vm->static_ext_inits) {
1155 VALUE lookup_name = tmp;
1163 ftptr = RSTRING_PTR(lookup_name);
1164 if (st_lookup(vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
1175 goto feature_present;
1176 ftptr = RSTRING_PTR(tmp);
1177 return rb_feature_p(ns, ftptr, 0, FALSE, TRUE, 0);
1181 goto feature_present;
1184 case loadable_ext_rb:
1185 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1186 if (rb_feature_p(ns, ftptr, ext,
type == loadable_ext_rb, TRUE, &loading) && !loading)
1190 return type > loadable_ext_rb ?
's' :
'r';
1198load_failed(
VALUE fname)
1200 rb_load_fail(fname,
"cannot load such file");
1206 VALUE loaded = path;
1208 if (NAMESPACE_USER_P(ns)) {
1209 loaded = rb_namespace_local_extension(ns->ns_object, fname, path);
1211 rb_scope_visibility_set(METHOD_VISI_PUBLIC);
1212 return (
VALUE)dln_load_feature(RSTRING_PTR(loaded), RSTRING_PTR(fname));
1216run_static_ext_init(
rb_vm_t *vm,
const char *feature)
1218 st_data_t key = (st_data_t)feature;
1219 st_data_t init_func;
1221 if (vm->static_ext_inits && st_delete(vm->static_ext_inits, &key, &init_func)) {
1222 ((void (*)(void))init_func)();
1229no_feature_p(
const rb_namespace_t *ns,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
1236rb_resolve_feature_path(
VALUE klass,
VALUE fname)
1243 fname = rb_get_path(fname);
1244 path = rb_str_encode_ospath(fname);
1245 found = search_required(ns, path, &path, no_feature_p);
1249 sym =
ID2SYM(rb_intern(
"rb"));
1252 sym =
ID2SYM(rb_intern(
"so"));
1258 return rb_ary_new_from_args(2, sym, path);
1264 *prev = th->ext_config;
1271 th->ext_config = *prev;
1277 GET_THREAD()->ext_config.ractor_safe = flag;
1290 volatile int result = -1;
1293 volatile const struct {
1294 VALUE wrapper, self, errinfo;
1298 th->top_wrapper, th->top_self, ec->errinfo,
1301 enum ruby_tag_type state;
1302 char *
volatile ftptr = 0;
1304 volatile VALUE saved_path;
1305 volatile VALUE realpath = 0;
1306 VALUE realpaths = ns->loaded_features_realpaths;
1307 VALUE realpath_map = ns->loaded_features_realpath_map;
1308 volatile bool reset_ext_config =
false;
1311 path = rb_str_encode_ospath(fname);
1312 RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
1317 th->top_wrapper = 0;
1318 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1322 RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
1323 found = search_required(ns, path, &saved_path, rb_feature_p);
1324 RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
1328 if (!path || !(ftptr = load_lock(ns, RSTRING_PTR(path), warn))) {
1332 result = TAG_RETURN;
1334 else if (found ==
's' && run_static_ext_init(th->vm, RSTRING_PTR(path))) {
1335 result = TAG_RETURN;
1337 else if (
RTEST(rb_hash_aref(realpaths,
1338 realpath = realpath_internal_cached(realpath_map, path)))) {
1345 if (NAMESPACE_OPTIONAL_P(ns)) {
1348 load_wrapping(saved.ec, path, ns->ns_object);
1351 load_iseq_eval(saved.ec, path);
1356 reset_ext_config =
true;
1357 ext_config_push(th, &prev_ext_config);
1358 handle = rb_vm_call_cfunc_in_namespace(ns->top_self, load_ext, path, fname, path, ns);
1359 rb_hash_aset(ns->ruby_dln_libmap, path, SVALUE2NUM((
SIGNED_VALUE)handle));
1362 result = TAG_RETURN;
1371 th2->top_self = saved.self;
1372 th2->top_wrapper = saved.wrapper;
1373 if (reset_ext_config) ext_config_pop(th2, &prev_ext_config);
1376 if (ftptr) load_unlock(ns, RSTRING_PTR(path), !state);
1379 if (state == TAG_FATAL || state == TAG_THROW) {
1380 EC_JUMP_TAG(ec, state);
1382 else if (exception) {
1385 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
1386 if (!
NIL_P(exc)) ec->errinfo = exc;
1389 else if (state == TAG_RETURN) {
1396 if (!
NIL_P(ec->errinfo)) {
1397 if (!exception)
return TAG_RAISE;
1401 if (result == TAG_RETURN) {
1402 rb_provide_feature(ns, path);
1403 VALUE real = realpath;
1405 real = rb_fstring(real);
1406 rb_hash_aset(realpaths, real,
Qtrue);
1409 ec->errinfo = saved.errinfo;
1411 RUBY_DTRACE_HOOK(REQUIRE_RETURN, RSTRING_PTR(fname));
1417rb_require_internal_silent(
VALUE fname)
1419 if (!rb_ractor_main_p()) {
1420 return NUM2INT(rb_ractor_require(fname,
true));
1424 return require_internal(ec, fname, 1,
false);
1428rb_require_internal(
VALUE fname)
1435ruby_require_internal(
const char *fname,
unsigned int len)
1437 struct RString fake = {RBASIC_INIT};
1438 VALUE str = rb_setup_fake_str(&fake, fname,
len, 0);
1441 rb_set_errinfo(
Qnil);
1442 return result == TAG_RETURN ? 1 : result ? -1 : 0;
1448 return rb_require_string_internal(
FilePathValue(fname),
false);
1452rb_require_string_internal(
VALUE fname,
bool resurrect)
1457 if (!rb_ractor_main_p()) {
1459 return rb_ractor_require(fname,
false);
1464 if (result > TAG_RETURN) {
1465 EC_JUMP_TAG(ec, result);
1472 return RBOOL(result);
1479 struct RString fake = {RBASIC_INIT};
1480 VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
1481 return rb_require_string_internal(str,
true);
1485register_init_ext(st_data_t *key, st_data_t *value, st_data_t init,
int existing)
1487 const char *name = (
char *)*key;
1490 rb_warn(
"%s is already registered", name);
1493 *value = (st_data_t)init;
1502ruby_init_ext(
const char *name,
void (*init)(
void))
1511 inits_table = vm->static_ext_inits;
1513 inits_table = st_init_strtable();
1514 vm->static_ext_inits = inits_table;
1516 st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init);
1546 rb_autoload_str(mod,
id, file);
1577rb_mod_autoload_p(
int argc,
VALUE *argv,
VALUE mod)
1580 VALUE sym = argv[0];
1586 return rb_autoload_at_p(mod,
id, recur);
1612 rb_raise(
rb_eTypeError,
"Can not set autoload on singleton class");
1614 return rb_mod_autoload(klass, sym, file);
1641rb_f_autoload_p(
int argc,
VALUE *argv,
VALUE obj)
1644 VALUE klass = rb_vm_cbase();
1648 return rb_mod_autoload_p(argc, argv, klass);
1661 resolved = rb_resolve_feature_path((
VALUE)NULL, fname_str);
1662 if (
NIL_P(resolved)) {
1663 ext = strrchr(fname,
'.');
1664 if (!ext || !IS_SOEXT(ext)) {
1667 if (rb_feature_p(ns, fname, 0, FALSE, FALSE, 0)) {
1668 return dln_symbol(NULL, symbol);
1676 handle = rb_hash_lookup(ns->ruby_dln_libmap, path);
1677 if (
NIL_P(handle)) {
1680 return dln_symbol((
void *)NUM2SVALUE(handle), symbol);
1686 static const char var_load_path[] =
"$:";
1687 ID id_load_path = rb_intern2(var_load_path,
sizeof(var_load_path)-1);
1690 rb_gvar_namespace_ready(var_load_path);
1695 rb_gvar_namespace_ready(
"$\"");
1697 rb_gvar_namespace_ready(
"$LOADED_FEATURES");
#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_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 NUM2INT
Old name of RB_NUM2INT.
#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.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
#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.
VALUE rb_ary_shared_with_p(VALUE lhs, VALUE rhs)
Queries if the passed two arrays share the same backend storage.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
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_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)
Like rb_str_dup(), but always create an instance of rb_cString regardless of the given object's class...
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.
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.
Internal header for Namespace.
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.