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"
26static VALUE ruby_dln_libmap;
28#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
29#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
30#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
32#if SIZEOF_VALUE <= SIZEOF_LONG
33# define SVALUE2NUM(x) LONG2NUM((long)(x))
34# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LONG(x)
35#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
36# define SVALUE2NUM(x) LL2NUM((LONG_LONG)(x))
37# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LL(x)
39# error Need integer for VALUE
42#define IS_NAMESPACE(obj) (CLASS_OF(obj) == rb_cNamespace)
49#define GET_vm_ns() vm_ns_t vm_ns_v = { .vm = GET_VM(), .ns = (rb_namespace_t *)rb_current_namespace(), }; vm_ns_t *vm_ns = &vm_ns_v;
50#define GET_loading_vm_ns() vm_ns_t vm_ns_v = { .vm = GET_VM(), .ns = (rb_namespace_t *)rb_loading_namespace(), }; vm_ns_t *vm_ns = &vm_ns_v;
52#define CURRENT_NS_attr(vm_ns, attr) (NAMESPACE_USER_P(vm_ns->ns) ? vm_ns->ns->attr : vm_ns->vm->attr)
53#define SET_NS_attr(vm_ns, attr, value) do { \
54 if (NAMESPACE_USER_P(vm_ns->ns)) { vm_ns->ns->attr = value; } \
55 else { vm_ns->vm->attr = value; } \
58#define SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, value) SET_NS_attr(vm_ns, load_path_check_cache, value)
59#define SET_NS_EXPANDED_LOAD_PATH(vm_ns, value) SET_NS_attr(vm_ns, expanded_load_path, value)
61#define CURRENT_NS_LOAD_PATH(vm_ns) CURRENT_NS_attr(vm_ns, load_path)
62#define CURRENT_NS_LOAD_PATH_SNAPSHOT(vm_ns) CURRENT_NS_attr(vm_ns, load_path_snapshot)
63#define CURRENT_NS_LOAD_PATH_CHECK_CACHE(vm_ns) CURRENT_NS_attr(vm_ns, load_path_check_cache)
64#define CURRENT_NS_EXPANDED_LOAD_PATH(vm_ns) CURRENT_NS_attr(vm_ns, expanded_load_path)
65#define CURRENT_NS_LOADING_TABLE(vm_ns) CURRENT_NS_attr(vm_ns, loading_table)
66#define CURRENT_NS_LOADED_FEATURES(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features)
67#define CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_snapshot)
68#define CURRENT_NS_LOADED_FEATURES_REALPATHS(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_realpaths)
69#define CURRENT_NS_LOADED_FEATURES_REALPATH_MAP(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_realpath_map)
70#define CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns) CURRENT_NS_attr(vm_ns, loaded_features_index)
72#define CURRENT_NS_RUBY_DLN_LIBMAP(vm_ns, map) (NAMESPACE_USER_P(vm_ns->ns) ? vm_ns->ns->ruby_dln_libmap : map)
79static const char *
const loadable_ext[] = {
84static const char *
const ruby_ext[] = {
102rb_construct_expanded_load_path(
vm_ns_t *vm_ns,
enum expand_type
type,
int *has_relative,
int *has_non_cache)
104 VALUE load_path = CURRENT_NS_LOAD_PATH(vm_ns);
105 VALUE expanded_load_path = CURRENT_NS_EXPANDED_LOAD_PATH(vm_ns);
112 VALUE path, as_str, expanded_path;
113 int is_string, non_cache;
117 non_cache = !is_string ? 1 : 0;
118 as_str = rb_get_path_check_to_string(path);
119 as_cstr = RSTRING_PTR(as_str);
122 if ((
type == EXPAND_RELATIVE &&
123 rb_is_absolute_path(as_cstr)) ||
124 (
type == EXPAND_HOME &&
125 (!as_cstr[0] || as_cstr[0] !=
'~')) ||
126 (
type == EXPAND_NON_CACHE)) {
132 if (!*has_relative && !rb_is_absolute_path(as_cstr))
134 if (!*has_non_cache && non_cache)
139 as_str = rb_get_path_check_convert(as_str);
140 expanded_path = rb_check_realpath(
Qnil, as_str, NULL);
141 if (
NIL_P(expanded_path)) expanded_path = as_str;
145 SET_NS_EXPANDED_LOAD_PATH(vm_ns, ary);
146 snapshot = CURRENT_NS_LOAD_PATH_SNAPSHOT(vm_ns);
147 load_path = CURRENT_NS_LOAD_PATH(vm_ns);
152get_expanded_load_path(
vm_ns_t *vm_ns)
156 const VALUE load_path_snapshot = CURRENT_NS_LOAD_PATH_SNAPSHOT(vm_ns);
157 const VALUE load_path = CURRENT_NS_LOAD_PATH(vm_ns);
161 int has_relative = 0, has_non_cache = 0;
162 rb_construct_expanded_load_path(vm_ns, EXPAND_ALL, &has_relative, &has_non_cache);
164 SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, rb_dir_getwd_ospath());
166 else if (has_non_cache) {
168 SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, non_cache);
171 SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, 0);
174 else if ((check_cache = CURRENT_NS_LOAD_PATH_CHECK_CACHE(vm_ns)) == non_cache) {
175 int has_relative = 1, has_non_cache = 1;
177 rb_construct_expanded_load_path(vm_ns, EXPAND_NON_CACHE,
178 &has_relative, &has_non_cache);
180 else if (check_cache) {
181 int has_relative = 1, has_non_cache = 1;
182 VALUE cwd = rb_dir_getwd_ospath();
186 SET_NS_LOAD_PATH_CHECK_CACHE(vm_ns, cwd);
187 rb_construct_expanded_load_path(vm_ns, EXPAND_RELATIVE,
188 &has_relative, &has_non_cache);
192 rb_construct_expanded_load_path(vm_ns, EXPAND_HOME,
193 &has_relative, &has_non_cache);
196 return CURRENT_NS_EXPANDED_LOAD_PATH(vm_ns);
200rb_get_expanded_load_path(
void)
203 return get_expanded_load_path(vm_ns);
207load_path_getter(
ID id,
VALUE * p)
210 return CURRENT_NS_LOAD_PATH(vm_ns);
214get_loaded_features(
vm_ns_t *vm_ns)
216 return CURRENT_NS_LOADED_FEATURES(vm_ns);
220get_loaded_features_realpaths(
vm_ns_t *vm_ns)
222 return CURRENT_NS_LOADED_FEATURES_REALPATHS(vm_ns);
226get_loaded_features_realpath_map(
vm_ns_t *vm_ns)
228 return CURRENT_NS_LOADED_FEATURES_REALPATH_MAP(vm_ns);
232get_LOADED_FEATURES(
ID _x,
VALUE *_y)
235 return get_loaded_features(vm_ns);
239reset_loaded_features_snapshot(
vm_ns_t *vm_ns)
241 VALUE snapshot = CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns);
242 VALUE loaded_features = CURRENT_NS_LOADED_FEATURES(vm_ns);
247get_loaded_features_index_raw(
vm_ns_t *vm_ns)
249 return CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns);
253get_loading_table(
vm_ns_t *vm_ns)
255 return CURRENT_NS_LOADING_TABLE(vm_ns);
259feature_key(
const char *str,
size_t len)
261 return st_hash(str,
len, 0xfea7009e);
265is_rbext_path(
VALUE feature_path)
267 long len = RSTRING_LEN(feature_path);
269 if (
len <= rbext_len)
return false;
270 return IS_RBEXT(RSTRING_PTR(feature_path) +
len - rbext_len);
273typedef rb_darray(
long) feature_indexes_t;
275struct features_index_add_single_args {
282features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args,
int existing)
284 struct features_index_add_single_args *args = (
struct features_index_add_single_args *)raw_args;
286 VALUE offset = args->offset;
290 VALUE this_feature_index = *value;
293 VALUE loaded_features = get_loaded_features(vm_ns);
296 feature_indexes_t feature_indexes;
297 rb_darray_make(&feature_indexes, 2);
298 int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0;
299 rb_darray_set(feature_indexes, top^0,
FIX2LONG(this_feature_index));
300 rb_darray_set(feature_indexes, top^1,
FIX2LONG(offset));
306 *value = (st_data_t)feature_indexes;
309 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
313 VALUE loaded_features = get_loaded_features(vm_ns);
314 for (
size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
315 long idx = rb_darray_get(feature_indexes, i);
318 if (!is_rbext_path(this_feature_path)) {
325 rb_darray_append(&feature_indexes,
FIX2LONG(offset));
327 *value = (st_data_t)feature_indexes;
330 long *ptr = rb_darray_data_ptr(feature_indexes);
331 long len = rb_darray_size(feature_indexes);
332 MEMMOVE(ptr + pos, ptr + pos + 1,
long,
len - pos - 1);
345features_index_add_single(
vm_ns_t *vm_ns,
const char* str,
size_t len,
VALUE offset,
bool rb)
348 st_data_t short_feature_key;
351 short_feature_key = feature_key(str,
len);
353 features_index = get_loaded_features_index_raw(vm_ns);
355 struct features_index_add_single_args args = {
361 st_update(features_index, short_feature_key, features_index_add_single_callback, (st_data_t)&args);
377 const char *feature_str, *feature_end, *ext, *p;
381 feature_end = feature_str + RSTRING_LEN(feature);
383 for (ext = feature_end; ext > feature_str; ext--)
384 if (*ext ==
'.' || *ext ==
'/')
393 p = ext ? ext : feature_end;
396 while (p >= feature_str && *p !=
'/')
401 features_index_add_single(vm_ns, p + 1, feature_end - p - 1, offset,
false);
403 features_index_add_single(vm_ns, p + 1, ext - p - 1, offset, rb);
406 features_index_add_single(vm_ns, feature_str, feature_end - feature_str, offset,
false);
408 features_index_add_single(vm_ns, feature_str, ext - feature_str, offset, rb);
413loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
417 rb_darray_free((
void *)obj);
423rb_free_loaded_features_index(
rb_vm_t *vm)
427 st_foreach(vm->loaded_features_index, loaded_features_index_clear_i, 0);
428 st_free_table(vm->loaded_features_index);
434get_loaded_features_index(
vm_ns_t *vm_ns)
437 VALUE features = CURRENT_NS_LOADED_FEATURES(vm_ns);
438 const VALUE snapshot = CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns);
443 st_foreach(CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns), loaded_features_index_clear_i, 0);
445 VALUE realpaths = CURRENT_NS_LOADED_FEATURES_REALPATHS(vm_ns);
446 VALUE realpath_map = CURRENT_NS_LOADED_FEATURES_REALPATH_MAP(vm_ns);
447 VALUE previous_realpath_map = rb_hash_dup(realpath_map);
448 rb_hash_clear(realpaths);
449 rb_hash_clear(realpath_map);
454 as_str = rb_fstring(as_str);
457 features_index_add(vm_ns, as_str,
INT2FIX(i));
459 reset_loaded_features_snapshot(vm_ns);
461 features = CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns);
463 for (i = 0; i < j; i++) {
465 VALUE realpath = rb_hash_aref(previous_realpath_map, as_str);
466 if (
NIL_P(realpath)) {
467 realpath = rb_check_realpath(
Qnil, as_str, NULL);
468 if (
NIL_P(realpath)) realpath = as_str;
469 realpath = rb_fstring(realpath);
471 rb_hash_aset(realpaths, realpath,
Qtrue);
472 rb_hash_aset(realpath_map, as_str, realpath);
475 return CURRENT_NS_LOADED_FEATURES_INDEX(vm_ns);
490loaded_feature_path(
const char *name,
long vlen,
const char *feature,
long len,
497 if (vlen <
len+1)
return 0;
498 if (strchr(feature,
'.') && !strncmp(name+(vlen-
len), feature,
len)) {
502 for (e = name + vlen; name != e && *e !=
'.' && *e !=
'/'; --e);
505 strncmp(e-
len, feature,
len))
507 plen = e - name -
len;
509 if (plen > 0 && name[plen-1] !=
'/') {
512 if (
type ==
's' ? !IS_DLEXT(&name[plen+
len]) :
513 type ==
'r' ? !IS_RBEXT(&name[plen+
len]) :
520 if (plen > 0) --plen;
524 long n = RSTRING_LEN(p);
526 if (n != plen)
continue;
527 if (n && strncmp(name, s, n))
continue;
542loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
544 const char *s = (
const char *)v;
546 VALUE p = loaded_feature_path(s, strlen(s), fp->name, fp->len,
547 fp->type, fp->load_path);
548 if (!p)
return ST_CONTINUE;
560rb_feature_p(
vm_ns_t *vm_ns,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
562 VALUE features, this_feature_index =
Qnil, v, p, load_path = 0;
564 long i,
len, elen, n;
565 st_table *loading_tbl, *features_index;
573 len = strlen(feature) - elen;
574 type = rb ?
'r' :
's';
577 len = strlen(feature);
581 features = get_loaded_features(vm_ns);
582 features_index = get_loaded_features_index(vm_ns);
584 key = feature_key(feature, strlen(feature));
611 if (st_lookup(features_index, key, &data) && !
NIL_P(this_feature_index = (
VALUE)data)) {
612 for (
size_t i = 0; ; i++) {
616 index =
FIX2LONG(this_feature_index);
619 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
620 if (i >= rb_darray_size(feature_indexes))
break;
621 index = rb_darray_get(feature_indexes, i);
626 if ((n = RSTRING_LEN(v)) <
len)
continue;
627 if (strncmp(f, feature,
len) != 0) {
628 if (expanded)
continue;
629 if (!load_path) load_path = get_expanded_load_path(vm_ns);
630 if (!(p = loaded_feature_path(f, n, feature,
len,
type, load_path)))
633 f += RSTRING_LEN(p) + 1;
635 if (!*(e = f +
len)) {
639 if (*e !=
'.')
continue;
640 if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
643 if ((rb || !ext) && (IS_RBEXT(e))) {
649 loading_tbl = get_loading_table(vm_ns);
651 if (!expanded && !rb_is_absolute_path(feature)) {
656 fs.load_path = load_path ? load_path : get_expanded_load_path(vm_ns);
658 st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
659 if ((f = fs.result) != 0) {
664 if (st_get_key(loading_tbl, (st_data_t)feature, &data)) {
665 if (fn) *fn = (
const char*)data;
671 static const char so_ext[][4] = {
675 if (ext && *ext)
return 0;
677 buf = RSTRING_PTR(bufstr);
679 for (i = 0; (e = loadable_ext[i]) != 0; i++) {
680 strlcpy(buf +
len, e, DLEXT_MAXLEN + 1);
681 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
682 rb_str_resize(bufstr, 0);
683 if (fn) *fn = (
const char*)data;
684 return i ?
's' :
'r';
687 for (i = 0; i < numberof(so_ext); i++) {
688 strlcpy(buf +
len, so_ext[i], DLEXT_MAXLEN + 1);
689 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
690 rb_str_resize(bufstr, 0);
691 if (fn) *fn = (
const char*)data;
695 rb_str_resize(bufstr, 0);
700 if (!ext)
return 'u';
701 return !IS_RBEXT(ext) ?
's' :
'r';
711feature_provided(
vm_ns_t *vm_ns,
const char *feature,
const char **loading)
713 const char *ext = strrchr(feature,
'.');
716 if (*feature ==
'.' &&
717 (feature[1] ==
'/' || strncmp(feature+1,
"./", 2) == 0)) {
718 fullpath = rb_file_expand_path_fast(rb_get_path(
rb_str_new2(feature)),
Qnil);
719 feature = RSTRING_PTR(fullpath);
721 if (ext && !strchr(ext,
'/')) {
723 if (rb_feature_p(vm_ns, feature, ext, TRUE, FALSE, loading))
return TRUE;
726 else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
727 if (rb_feature_p(vm_ns, feature, ext, FALSE, FALSE, loading))
return TRUE;
731 if (rb_feature_p(vm_ns, feature, 0, TRUE, FALSE, loading))
741 return feature_provided(vm_ns, feature, loading);
749 features = get_loaded_features(vm_ns);
752 "$LOADED_FEATURES is frozen; cannot append feature");
754 feature = rb_fstring(feature);
756 get_loaded_features_index(vm_ns);
760 rb_ary_clear(CURRENT_NS_LOADED_FEATURES_SNAPSHOT(vm_ns));
763 reset_loaded_features_snapshot(vm_ns);
774 rb_provide_feature(vm_ns, rb_fstring_cstr(feature));
777NORETURN(
static void load_failed(
VALUE));
780realpath_internal_cached(
VALUE hash,
VALUE path)
782 VALUE ret = rb_hash_aref(hash, path);
787 VALUE realpath = rb_realpath_internal(
Qnil, path, 1);
788 rb_hash_aset(hash, rb_fstring(path), rb_fstring(realpath));
798iseq_eval_in_namespace(
VALUE arg)
801 if (rb_namespace_available() && data->in_builtin) {
805 return rb_iseq_eval(data->iseq);
814 const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
818 VALUE v = rb_vm_push_frame_fname(ec, fname);
820 VALUE realpath_map = get_loaded_features_realpath_map(vm_ns);
822 if (rb_ruby_prism_p()) {
825 result.
node.coverage_enabled = 1;
827 VALUE error = pm_load_parse_file(&result, fname, NULL);
831 iseq = pm_iseq_new_top(&result.
node, rb_fstring_lit(
"<top (required)>"), fname, realpath_internal_cached(realpath_map, fname), NULL, &error_state);
833 pm_parse_result_free(&result);
837 rb_jump_tag(error_state);
843 pm_parse_result_free(&result);
850 VALUE parser = rb_parser_new();
851 rb_parser_set_context(parser, NULL, FALSE);
852 ast_value = rb_parser_load_file(parser, fname);
853 ast = rb_ruby_ast_data_get(ast_value);
855 iseq = rb_iseq_new_top(ast_value, rb_fstring_lit(
"<top (required)>"),
856 fname, realpath_internal_cached(realpath_map, fname), NULL);
863 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
868 .in_builtin = NAMESPACE_BUILTIN_P(loading_ns),
870 rb_namespace_exec(loading_ns, iseq_eval_in_namespace, (
VALUE)&arg);
877static inline enum ruby_tag_type
880 enum ruby_tag_type state;
883 volatile VALUE wrapper = th->top_wrapper;
884 volatile VALUE self = th->top_self;
892 if (IS_NAMESPACE(load_wrapper)) {
893 ns = rb_get_namespace_t(load_wrapper);
897 th->top_self = ns->top_self;
902 th->top_wrapper = load_wrapper;
906 state = EC_EXEC_TAG();
907 if (state == TAG_NONE) {
908 load_iseq_eval(ec, fname);
917 th->top_wrapper = wrapper;
925 rb_vm_jump_tag_but_local_jump(state);
928 if (!
NIL_P(ec->errinfo)) {
939 enum ruby_tag_type state = TAG_NONE;
944 state = load_wrapping(ec, fname, wrap);
946 else if (NAMESPACE_OPTIONAL_P(ns)) {
947 namespace = ns->ns_object;
948 state = load_wrapping(ec, fname,
namespace);
951 load_iseq_eval(ec, fname);
953 raise_load_if_failed(ec, state);
960 if (!tmp) load_failed(fname);
961 rb_load_internal(tmp, RBOOL(wrap));
967 enum ruby_tag_type state;
969 EC_PUSH_TAG(GET_EC());
970 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
975 if (state != TAG_NONE) *pstate = state;
979load_entrypoint_internal(
VALUE fname,
VALUE wrap)
981 VALUE path, orig_fname;
983 orig_fname = rb_get_path_check_to_string(fname);
984 fname = rb_str_encode_ospath(orig_fname);
985 RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
987 path = rb_find_file(fname);
989 if (!rb_file_load_ok(RSTRING_PTR(fname)))
990 load_failed(orig_fname);
993 rb_load_internal(path, wrap);
995 RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
1001rb_load_entrypoint(
VALUE args)
1005 rb_bug(
"invalid arguments: %ld",
RARRAY_LEN(args));
1009 return load_entrypoint_internal(fname, wrap);
1047 return load_entrypoint_internal(fname, wrap);
1051load_lock(
vm_ns_t *vm_ns,
const char *ftptr,
bool warn)
1054 st_table *loading_tbl = get_loading_table(vm_ns);
1056 if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
1059 data = (st_data_t)rb_thread_shield_new();
1060 st_insert(loading_tbl, (st_data_t)ftptr, data);
1061 return (
char *)ftptr;
1064 if (warn && rb_thread_shield_owned((
VALUE)data)) {
1065 VALUE warning = rb_warning_string(
"loading in progress, circular require considered harmful - %s", ftptr);
1069 switch (rb_thread_shield_wait((
VALUE)data)) {
1074 return (
char *)ftptr;
1078release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done,
int existing)
1081 if (!existing)
return ST_STOP;
1083 rb_thread_shield_destroy(thread_shield);
1087 else if (rb_thread_shield_release(thread_shield)) {
1091 xfree((
char *)*key);
1096load_unlock(
vm_ns_t *vm_ns,
const char *ftptr,
int done)
1099 st_data_t key = (st_data_t)ftptr;
1100 st_table *loading_tbl = get_loading_table(vm_ns);
1102 st_update(loading_tbl, key, release_thread_shield, done);
1106static VALUE rb_require_string_internal(
VALUE fname,
bool resurrect);
1154rb_require_relative_entrypoint(
VALUE fname)
1156 VALUE base = rb_current_realfilepath();
1160 base = rb_file_dirname(base);
1161 return rb_require_string_internal(rb_file_absolute_path(fname, base),
false);
1176 return rb_require_relative_entrypoint(fname);
1179typedef int (*feature_func)(
vm_ns_t *vm_ns,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn);
1182search_required(
vm_ns_t *vm_ns,
VALUE fname,
volatile VALUE *path, feature_func rb_feature_p)
1187 const char *loading;
1190 ext = strrchr(ftptr = RSTRING_PTR(fname),
'.');
1191 if (ext && !strchr(ext,
'/')) {
1192 if (IS_RBEXT(ext)) {
1193 if (rb_feature_p(vm_ns, ftptr, ext, TRUE, FALSE, &loading)) {
1197 if ((tmp = rb_find_file(fname)) != 0) {
1198 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1199 if (!rb_feature_p(vm_ns, ftptr, ext, TRUE, TRUE, &loading) || loading)
1205 else if (IS_SOEXT(ext)) {
1206 if (rb_feature_p(vm_ns, ftptr, ext, FALSE, FALSE, &loading)) {
1213 if ((tmp = rb_find_file(tmp)) != 0) {
1214 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1215 if (!rb_feature_p(vm_ns, ftptr, ext, FALSE, TRUE, &loading) || loading)
1220 else if (IS_DLEXT(ext)) {
1221 if (rb_feature_p(vm_ns, ftptr, ext, FALSE, FALSE, &loading)) {
1225 if ((tmp = rb_find_file(fname)) != 0) {
1226 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1227 if (!rb_feature_p(vm_ns, ftptr, ext, FALSE, TRUE, &loading) || loading)
1233 else if ((ft = rb_feature_p(vm_ns, ftptr, 0, FALSE, FALSE, &loading)) ==
'r') {
1238 const unsigned int type = rb_find_file_ext(&tmp, ft ==
's' ? ruby_ext : loadable_ext);
1242 if (!ft &&
type != loadable_ext_rb && vm_ns->vm->static_ext_inits) {
1243 VALUE lookup_name = tmp;
1251 ftptr = RSTRING_PTR(lookup_name);
1252 if (st_lookup(vm_ns->vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
1262 goto feature_present;
1263 ftptr = RSTRING_PTR(tmp);
1264 return rb_feature_p(vm_ns, ftptr, 0, FALSE, TRUE, 0);
1268 goto feature_present;
1271 case loadable_ext_rb:
1272 ext = strrchr(ftptr = RSTRING_PTR(tmp),
'.');
1273 if (rb_feature_p(vm_ns, ftptr, ext,
type == loadable_ext_rb, TRUE, &loading) && !loading)
1277 return type > loadable_ext_rb ?
's' :
'r';
1285load_failed(
VALUE fname)
1287 rb_load_fail(fname,
"cannot load such file");
1293 VALUE loaded = path;
1294 GET_loading_vm_ns();
1295 if (NAMESPACE_USER_P(vm_ns->ns)) {
1296 loaded = rb_namespace_local_extension(vm_ns->ns->ns_object, fname, path);
1298 rb_scope_visibility_set(METHOD_VISI_PUBLIC);
1299 return (
VALUE)dln_load_feature(RSTRING_PTR(loaded), RSTRING_PTR(fname));
1303run_static_ext_init(
rb_vm_t *vm,
const char *feature)
1305 st_data_t key = (st_data_t)feature;
1306 st_data_t init_func;
1308 if (vm->static_ext_inits && st_delete(vm->static_ext_inits, &key, &init_func)) {
1309 ((void (*)(void))init_func)();
1316no_feature_p(
vm_ns_t *vm_ns,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
1323rb_resolve_feature_path(
VALUE klass,
VALUE fname)
1328 GET_loading_vm_ns();
1330 fname = rb_get_path(fname);
1331 path = rb_str_encode_ospath(fname);
1332 found = search_required(vm_ns, path, &path, no_feature_p);
1336 sym =
ID2SYM(rb_intern(
"rb"));
1339 sym =
ID2SYM(rb_intern(
"so"));
1345 return rb_ary_new_from_args(2, sym, path);
1351 *prev = th->ext_config;
1358 th->ext_config = *prev;
1364 GET_THREAD()->ext_config.ractor_safe = flag;
1371 VALUE block_handler;
1376call_load_ext_in_ns(
VALUE data)
1379 return rb_vm_call_cfunc2(arg->recv, load_ext, arg->arg1, arg->arg2, arg->block_handler, arg->filename);
1392 volatile int result = -1;
1394 volatile const struct {
1395 VALUE wrapper, self, errinfo;
1398 th->top_wrapper, th->top_self, ec->errinfo,
1401 GET_loading_vm_ns();
1402 enum ruby_tag_type state;
1403 char *
volatile ftptr = 0;
1405 volatile VALUE saved_path;
1406 volatile VALUE realpath = 0;
1407 VALUE realpaths = get_loaded_features_realpaths(vm_ns);
1408 VALUE realpath_map = get_loaded_features_realpath_map(vm_ns);
1409 volatile bool reset_ext_config =
false;
1412 path = rb_str_encode_ospath(fname);
1413 RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
1418 th->top_wrapper = 0;
1419 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1423 RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
1424 found = search_required(vm_ns, path, &saved_path, rb_feature_p);
1425 RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
1429 if (!path || !(ftptr = load_lock(vm_ns, RSTRING_PTR(path), warn))) {
1433 result = TAG_RETURN;
1435 else if (found ==
's' && run_static_ext_init(th->vm, RSTRING_PTR(path))) {
1436 result = TAG_RETURN;
1438 else if (
RTEST(rb_hash_aref(realpaths,
1439 realpath = realpath_internal_cached(realpath_map, path)))) {
1446 if (NAMESPACE_OPTIONAL_P(vm_ns->ns)) {
1449 load_wrapping(saved.ec, path, vm_ns->ns->ns_object);
1452 load_iseq_eval(saved.ec, path);
1458 reset_ext_config =
true;
1459 ext_config_push(th, &prev_ext_config);
1461 .recv = rb_vm_top_self(),
1464 .block_handler = VM_BLOCK_HANDLER_NONE,
1467 handle = rb_namespace_exec(vm_ns->ns, call_load_ext_in_ns, (
VALUE)&arg);
1468 rb_hash_aset(CURRENT_NS_RUBY_DLN_LIBMAP(vm_ns, ruby_dln_libmap), path,
1472 result = TAG_RETURN;
1480 th2->top_self = saved.self;
1481 th2->top_wrapper = saved.wrapper;
1482 if (reset_ext_config) ext_config_pop(th2, &prev_ext_config);
1485 if (ftptr) load_unlock(vm_ns, RSTRING_PTR(path), !state);
1488 if (state == TAG_FATAL || state == TAG_THROW) {
1489 EC_JUMP_TAG(ec, state);
1491 else if (exception) {
1494 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
1495 if (!
NIL_P(exc)) ec->errinfo = exc;
1498 else if (state == TAG_RETURN) {
1505 if (!
NIL_P(ec->errinfo)) {
1506 if (!exception)
return TAG_RAISE;
1510 if (result == TAG_RETURN) {
1511 rb_provide_feature(vm_ns, path);
1512 VALUE real = realpath;
1514 real = rb_fstring(real);
1515 rb_hash_aset(realpaths, real,
Qtrue);
1518 ec->errinfo = saved.errinfo;
1520 RUBY_DTRACE_HOOK(REQUIRE_RETURN, RSTRING_PTR(fname));
1526rb_require_internal_silent(
VALUE fname)
1528 if (!rb_ractor_main_p()) {
1529 return NUM2INT(rb_ractor_require(fname,
true));
1533 return require_internal(ec, fname, 1,
false);
1537rb_require_internal(
VALUE fname)
1544ruby_require_internal(
const char *fname,
unsigned int len)
1547 VALUE str = rb_setup_fake_str(&fake, fname,
len, 0);
1550 rb_set_errinfo(
Qnil);
1551 return result == TAG_RETURN ? 1 : result ? -1 : 0;
1557 return rb_require_string_internal(
FilePathValue(fname),
false);
1561rb_require_string_internal(
VALUE fname,
bool resurrect)
1566 if (!rb_ractor_main_p()) {
1568 return rb_ractor_require(fname,
false);
1573 if (result > TAG_RETURN) {
1574 EC_JUMP_TAG(ec, result);
1581 return RBOOL(result);
1589 VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
1590 return rb_require_string_internal(str,
true);
1594register_init_ext(st_data_t *key, st_data_t *value, st_data_t init,
int existing)
1596 const char *name = (
char *)*key;
1599 rb_warn(
"%s is already registered", name);
1602 *value = (st_data_t)init;
1611ruby_init_ext(
const char *name,
void (*init)(
void))
1614 GET_loading_vm_ns();
1616 if (feature_provided(vm_ns, name, 0))
1619 inits_table = vm_ns->vm->static_ext_inits;
1621 inits_table = st_init_strtable();
1622 vm_ns->vm->static_ext_inits = inits_table;
1624 st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init);
1654 rb_autoload_str(mod,
id, file);
1685rb_mod_autoload_p(
int argc,
VALUE *argv,
VALUE mod)
1688 VALUE sym = argv[0];
1694 return rb_autoload_at_p(mod,
id, recur);
1720 rb_raise(
rb_eTypeError,
"Can not set autoload on singleton class");
1722 return rb_mod_autoload(klass, sym, file);
1749rb_f_autoload_p(
int argc,
VALUE *argv,
VALUE obj)
1752 VALUE klass = rb_vm_cbase();
1756 return rb_mod_autoload_p(argc, argv, klass);
1767 GET_loading_vm_ns();
1769 resolved = rb_resolve_feature_path((
VALUE)NULL, fname_str);
1770 if (
NIL_P(resolved)) {
1771 ext = strrchr(fname,
'.');
1772 if (!ext || !IS_SOEXT(ext)) {
1775 if (rb_feature_p(vm_ns, fname, 0, FALSE, FALSE, 0)) {
1776 return dln_symbol(NULL, symbol);
1784 handle = rb_hash_lookup(CURRENT_NS_RUBY_DLN_LIBMAP(vm_ns, ruby_dln_libmap), path);
1785 if (
NIL_P(handle)) {
1788 return dln_symbol((
void *)NUM2SVALUE(handle), symbol);
1795 static const char var_load_path[] =
"$:";
1796 ID id_load_path = rb_intern2(var_load_path,
sizeof(var_load_path)-1);
1799 rb_gvar_namespace_ready(var_load_path);
1805 vm->load_path_check_cache = 0;
1809 rb_gvar_namespace_ready(
"$\"");
1811 rb_gvar_namespace_ready(
"$LOADED_FEATURES");
1814 vm->loaded_features_index = st_init_numtable();
1815 vm->loaded_features_realpaths = rb_hash_new();
1817 vm->loaded_features_realpath_map = rb_hash_new();
1828 ruby_dln_libmap = rb_hash_new_with_size(0);
1829 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 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_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cModule
Module class.
VALUE rb_mNamespaceRefiner
Namespace::Refiner module.
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_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_ary_new(void)
Allocates a new, empty array.
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)
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.
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.