6#include "eval_intern.h"
8#include "internal/box.h"
9#include "internal/dir.h"
10#include "internal/error.h"
11#include "internal/eval.h"
12#include "internal/file.h"
13#include "internal/hash.h"
14#include "internal/load.h"
15#include "internal/ruby_parser.h"
16#include "internal/thread.h"
17#include "internal/variable.h"
19#include "prism_compile.h"
24#include "ractor_core.h"
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)
37static const char *
const loadable_ext[] = {
42static const char *
const ruby_ext[] = {
59load_unstable_begin(
const rb_box_t *box)
62 ((rb_box_t *)box)->load_unstable_depth++;
67load_unstable_end(
VALUE boxp)
69 const rb_box_t *box = (
const rb_box_t *)boxp;
73 ((rb_box_t *)box)->load_unstable_depth--;
84rb_construct_expanded_load_path(rb_box_t *box,
enum expand_type
type,
int *has_relative,
int *has_non_cache,
rb_len_t *maxlen_out)
86 VALUE load_path = box->load_path;
87 VALUE expanded_load_path = box->expanded_load_path;
92 VM_ASSERT(box->load_unstable_depth > 0);
96 VALUE path, as_str, expanded_path;
97 int is_string, non_cache;
101 non_cache = !is_string ? 1 : 0;
102 as_str = rb_get_path_check_to_string(path);
103 as_cstr = RSTRING_PTR(as_str);
106 if ((
type == EXPAND_RELATIVE &&
107 rb_is_absolute_path(as_cstr)) ||
108 (
type == EXPAND_HOME &&
109 (!as_cstr[0] || as_cstr[0] !=
'~')) ||
110 (
type == EXPAND_NON_CACHE)) {
112 expanded_path =
RARRAY_AREF(expanded_load_path, i);
114 if (
len > maxlen) maxlen =
len;
119 if (!*has_relative && !rb_is_absolute_path(as_cstr))
121 if (!*has_non_cache && non_cache)
126 as_str = rb_get_path_check_convert(as_str);
127 expanded_path = rb_check_realpath(
Qnil, as_str, NULL);
128 if (
NIL_P(expanded_path)) expanded_path = as_str;
130 if (
len > maxlen) maxlen =
len;
134 box->expanded_load_path = ary;
135 snapshot = box->load_path_snapshot;
136 load_path = box->load_path;
137 *maxlen_out = maxlen;
142expanded_load_path_current_p(
const rb_box_t *box)
145 box->load_path_check_cache == 0;
149rebuild_expanded_load_path(
VALUE boxp)
151 rb_box_t *box = (rb_box_t *)boxp;
154 const VALUE load_path_snapshot = box->load_path_snapshot;
155 const VALUE load_path = box->load_path;
160 int has_relative = 0, has_non_cache = 0;
161 rb_construct_expanded_load_path(box, EXPAND_ALL, &has_relative, &has_non_cache, &maxlen);
163 box->load_path_check_cache = rb_dir_getwd_ospath();
165 else if (has_non_cache) {
167 box->load_path_check_cache = non_cache;
170 box->load_path_check_cache = 0;
173 else if ((check_cache = box->load_path_check_cache) == non_cache) {
174 int has_relative = 1, has_non_cache = 1;
176 rb_construct_expanded_load_path(box, EXPAND_NON_CACHE,
177 &has_relative, &has_non_cache, &maxlen);
179 else if (check_cache) {
180 int has_relative = 1, has_non_cache = 1;
181 VALUE cwd = rb_dir_getwd_ospath();
185 box->load_path_check_cache = cwd;
186 rb_construct_expanded_load_path(box, EXPAND_RELATIVE,
187 &has_relative, &has_non_cache, &maxlen);
191 rb_construct_expanded_load_path(box, EXPAND_HOME,
192 &has_relative, &has_non_cache, &maxlen);
196 box->expanded_load_path_maxlen = maxlen;
202get_expanded_load_path(rb_box_t *box)
204 if (!expanded_load_path_current_p(box)) {
205 load_unstable_begin(box);
208 return box->expanded_load_path;
212rb_get_expanded_load_path(
rb_len_t *maxlen)
214 rb_box_t *box = (rb_box_t *)rb_loading_box();
215 VALUE load_path = get_expanded_load_path((rb_box_t *)box);
217 *maxlen = box->expanded_load_path_maxlen;
223load_path_getter(
ID _x,
VALUE * _y)
225 return rb_loading_box()->load_path;
229get_LOADED_FEATURES(
ID _x,
VALUE *_y)
231 return rb_loading_box()->loaded_features;
235reset_loaded_features_snapshot(
const rb_box_t *box)
237 VALUE snapshot = box->loaded_features_snapshot;
238 VALUE loaded_features = box->loaded_features;
242static struct st_table *
243get_loaded_features_index_raw(
const rb_box_t *box)
245 return box->loaded_features_index;
249feature_key(
const char *str,
size_t len)
251 return st_hash(str,
len, 0xfea7009e);
255is_rbext_path(
VALUE feature_path)
259 if (
len <= rbext_len)
return false;
260 return IS_RBEXT(RSTRING_PTR(feature_path) +
len - rbext_len);
263typedef rb_darray(
long) feature_indexes_t;
265struct features_index_add_single_args {
272features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args,
int existing)
274 struct features_index_add_single_args *args = (
struct features_index_add_single_args *)raw_args;
275 const rb_box_t *box = args->box;
276 VALUE offset = args->offset;
280 VALUE this_feature_index = *value;
283 VALUE loaded_features = box->loaded_features;
286 feature_indexes_t feature_indexes;
287 rb_darray_make(&feature_indexes, 2);
288 int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0;
289 rb_darray_set(feature_indexes, top^0,
FIX2LONG(this_feature_index));
290 rb_darray_set(feature_indexes, top^1,
FIX2LONG(offset));
296 *value = (st_data_t)feature_indexes;
299 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
303 VALUE loaded_features = box->loaded_features;
304 for (
size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
305 long idx = rb_darray_get(feature_indexes, i);
308 if (!is_rbext_path(this_feature_path)) {
315 rb_darray_append(&feature_indexes,
FIX2LONG(offset));
317 *value = (st_data_t)feature_indexes;
320 long *ptr = rb_darray_data_ptr(feature_indexes);
321 long len = rb_darray_size(feature_indexes);
322 MEMMOVE(ptr + pos + 1, ptr + pos,
long,
len - pos - 1);
335features_index_add_single(
const rb_box_t *box,
const char* str,
size_t len,
VALUE offset,
bool rb)
337 struct st_table *features_index;
338 st_data_t short_feature_key;
341 short_feature_key = feature_key(str,
len);
343 features_index = get_loaded_features_index_raw(box);
345 struct features_index_add_single_args args = {
351 st_update(features_index, short_feature_key, features_index_add_single_callback, (st_data_t)&args);
363features_index_add(
const rb_box_t *box,
VALUE feature,
VALUE offset)
367 const char *feature_str, *feature_end, *ext, *p;
371 feature_end = feature_str + RSTRING_LEN(feature);
373 for (ext = feature_end; ext > feature_str; ext--)
374 if (*ext ==
'.' || *ext ==
'/')
383 p = ext ? ext : feature_end;
386 while (p >= feature_str && *p !=
'/')
391 features_index_add_single(box, p + 1, feature_end - p - 1, offset,
false);
393 features_index_add_single(box, p + 1, ext - p - 1, offset, rb);
396 features_index_add_single(box, feature_str, feature_end - feature_str, offset,
false);
398 features_index_add_single(box, feature_str, ext - feature_str, offset, rb);
403loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
407 rb_darray_free_sized((
void *)obj,
long);
413rebuild_loaded_features_index(
VALUE boxp)
415 const rb_box_t *box = (
const rb_box_t *)boxp;
417 VALUE features = box->loaded_features;
419 st_foreach(box->loaded_features_index, loaded_features_index_clear_i, 0);
421 VALUE realpaths = box->loaded_features_realpaths;
422 VALUE realpath_map = box->loaded_features_realpath_map;
423 VALUE previous_realpath_map = rb_hash_dup(realpath_map);
424 rb_hash_clear(realpaths);
425 rb_hash_clear(realpath_map);
435 as_str = rb_fstring(as_str);
438 features_index_add(box, as_str,
INT2FIX(i));
445 reset_loaded_features_snapshot(box);
447 features = box->loaded_features_snapshot;
449 for (i = 0; i < j; i++) {
451 VALUE realpath = rb_hash_aref(previous_realpath_map, as_str);
452 if (
NIL_P(realpath)) {
453 realpath = rb_check_realpath(
Qnil, as_str, NULL);
454 if (
NIL_P(realpath)) realpath = as_str;
455 realpath = rb_fstring(realpath);
457 rb_hash_aset(realpaths, realpath,
Qtrue);
458 rb_hash_aset(realpath_map, as_str, realpath);
464get_loaded_features_index(
const rb_box_t *box)
469 load_unstable_begin(box);
472 return box->loaded_features_index;
494 if (vlen <
len+1)
return 0;
495 if (strchr(feature,
'.') && !strncmp(name+(vlen-
len), feature,
len)) {
499 for (e = name + vlen; name != e && *e !=
'.' && *e !=
'/'; --e);
502 strncmp(e-
len, feature,
len))
504 plen = e - name -
len;
506 if (plen > 0 && name[plen-1] !=
'/') {
509 if (
type ==
's' ? !IS_DLEXT(&name[plen+
len]) :
510 type ==
'r' ? !IS_RBEXT(&name[plen+
len]) :
517 if (plen > 0) --plen;
523 if (n != plen)
continue;
524 if (n && strncmp(name, s, n))
continue;
539loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f)
541 const char *s = (
const char *)v;
543 VALUE p = loaded_feature_path(s, strlen(s), fp->name, fp->len,
544 fp->type, fp->load_path);
545 if (!p)
return ST_CONTINUE;
598 const char *
const feature = search->feature;
600 VALUE this_feature_index;
603 if (!st_lookup(search->features_index, feature_key(feature, strlen(feature)), &data))
return 0;
604 if (
NIL_P(this_feature_index = (
VALUE)data))
return 0;
606 for (
size_t i = 0; ; i++) {
614 index =
FIX2LONG(this_feature_index);
617 feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
618 if (i >= rb_darray_size(feature_indexes))
break;
619 index = rb_darray_get(feature_indexes, i);
622 if (index >=
RARRAY_LEN(search->features))
continue;
624 if (search->no_dispatch) {
632 if ((n = RSTRING_LEN(v)) <
len)
continue;
633 if (strncmp(f, feature,
len) != 0) {
634 if (search->expanded)
continue;
635 if (!search->load_path) {
637 if (search->no_dispatch)
return -1;
638 search->load_path = get_expanded_load_path((rb_box_t *)search->box);
640 if (!(p = loaded_feature_path(f, n, feature,
len, search->type, search->load_path)))
642 search->expanded = 1;
643 f += RSTRING_LEN(p) + 1;
645 if (!*(e = f +
len)) {
646 if (search->ext)
continue;
649 if (*e !=
'.')
continue;
650 if ((!search->rb || !search->ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
653 if ((search->rb || !search->ext) && (IS_RBEXT(e))) {
668rb_feature_p(
const rb_box_t *box,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
672 st_table *loading_tbl;
678 len = strlen(feature) - strlen(ext);
679 type = rb ?
'r' :
's';
682 len = strlen(feature);
686 VALUE features = box->loaded_features;
687 st_table *features_index = get_loaded_features_index(box);
690 .features_index = features_index,
691 .features = features,
698 .expanded = expanded,
699 .no_dispatch =
false,
701 if ((found = features_index_match(&search)) != 0)
return found;
703 loading_tbl = box->loading_table;
705 if (!search.expanded && !rb_is_absolute_path(feature)) {
710 fs.load_path = search.load_path ? search.load_path : get_expanded_load_path((rb_box_t *)box);
712 st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs);
713 if ((f = fs.result) != 0) {
718 if (st_get_key(loading_tbl, (st_data_t)feature, &data)) {
719 if (fn) *fn = (
const char*)data;
725 static const char so_ext[][4] = {
729 if (ext && *ext)
return 0;
731 buf = RSTRING_PTR(bufstr);
733 for (i = 0; (e = loadable_ext[i]) != 0; i++) {
734 strlcpy(buf +
len, e, DLEXT_MAXLEN + 1);
735 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
736 rb_str_resize(bufstr, 0);
737 if (fn) *fn = (
const char*)data;
738 return i ?
's' :
'r';
741 for (i = 0; i < numberof(so_ext); i++) {
742 strlcpy(buf +
len, so_ext[i], DLEXT_MAXLEN + 1);
743 if (st_get_key(loading_tbl, (st_data_t)buf, &data)) {
744 rb_str_resize(bufstr, 0);
745 if (fn) *fn = (
const char*)data;
749 rb_str_resize(bufstr, 0);
754 if (!ext)
return 'u';
755 return !IS_RBEXT(ext) ?
's' :
'r';
765feature_provided(rb_box_t *box,
const char *feature,
const char **loading)
767 const char *ext = strrchr(feature,
'.');
770 if (*feature ==
'.' &&
771 (feature[1] ==
'/' || strncmp(feature+1,
"./", 2) == 0)) {
772 fullpath = rb_file_expand_path_fast(rb_get_path(
rb_str_new2(feature)),
Qnil);
773 feature = RSTRING_PTR(fullpath);
775 if (ext && !strchr(ext,
'/')) {
777 if (rb_feature_p(box, feature, ext, TRUE, FALSE, loading))
return TRUE;
780 else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
781 if (rb_feature_p(box, feature, ext, FALSE, FALSE, loading))
return TRUE;
785 if (rb_feature_p(box, feature, 0, TRUE, FALSE, loading))
794 rb_box_t *box = (rb_box_t *)rb_current_box();
795 return feature_provided(box, feature, loading);
808feature_p_ractor_safe(
const rb_box_t *box,
VALUE features,
VALUE load_path,
809 const char *feature,
const char *ext,
int rb)
814 .features_index = get_loaded_features_index_raw(box),
815 .features = features,
816 .load_path = load_path,
819 .len = ext ? strlen(feature) - strlen(ext) : strlen(feature),
820 .
type = ext ? (rb ?
'r' :
's') : 0,
826 switch (features_index_match(&search)) {
845rb_feature_provided_ractor_safe(
VALUE fname)
847 const rb_box_t *box = rb_current_box();
852 if (rb_box_available() && box != rb_main_box())
return false;
857 fname = rb_str_encode_ospath(fname);
859 const char *feature = RSTRING_PTR(fname);
862 bool provided =
false;
864 if (flen == 0 || strlen(feature) != (
size_t)flen)
return false;
865 if (feature[0] ==
'.' &&
866 (feature[1] ==
'/' || strncmp(feature + 1,
"./", 2) == 0)) {
873 ext = strrchr(feature,
'.');
874 if (ext && strchr(ext,
'/')) {
878 else if (ext && !IS_RBEXT(ext) && !IS_SOEXT(ext) && !IS_DLEXT(ext)) {
890 if (box->load_unstable_depth == 0 &&
894 VALUE load_path = expanded_load_path_current_p(box) ?
895 box->expanded_load_path : 0;
896 provided = feature_p_ractor_safe(box, box->loaded_features_snapshot,
897 load_path, feature, ext,
898 ext ? IS_RBEXT(ext) : TRUE);
914provide_feature_locked(
VALUE argp)
917 const rb_box_t *box = args->box;
918 VALUE features = args->features;
926 reset_loaded_features_snapshot(box);
931provide_feature_unlock(
VALUE argp)
935 RB_VM_LOCK_LEAVE_LEV(&args->lev);
940rb_provide_feature(
const rb_box_t *box,
VALUE feature)
944 features = box->loaded_features;
947 "$LOADED_FEATURES is frozen; cannot append feature");
949 feature = rb_fstring(feature);
951 get_loaded_features_index(box);
955 .features = features,
958 RB_VM_LOCK_ENTER_LEV(&args.lev);
969 rb_provide_feature(rb_current_box(), rb_fstring_cstr(feature));
972NORETURN(
static void load_failed(
VALUE));
975realpath_internal_cached(
VALUE hash,
VALUE path)
977 VALUE ret = rb_hash_aref(hash, path);
982 VALUE realpath = rb_realpath_internal(
Qnil, path, 1);
983 rb_hash_aset(hash, rb_fstring(path), rb_fstring(realpath));
991 const rb_iseq_t *iseq;
996load_prism_parse(
VALUE args_ptr)
1000 VALUE fname = args->fname;
1002 VALUE error = pm_load_parse_file(result, fname, NULL);
1003 if (error !=
Qnil) {
1004 args->error = error;
1009 args->iseq = pm_iseq_new_top(&result->
node, rb_fstring_lit(
"<top (required)>"), fname,
1010 realpath_internal_cached(args->realpath_map, fname), NULL, &error_state);
1013 rb_jump_tag(error_state);
1020load_prism_free_result(
VALUE args_ptr)
1023 pm_parse_result_free(&args->result);
1028load_iseq_eval(rb_execution_context_t *ec,
VALUE fname)
1030 const rb_box_t *box = rb_loading_box();
1031 const rb_iseq_t *iseq = rb_iseq_load_iseq(fname);
1034 rb_execution_context_t *ec = GET_EC();
1035 VALUE v = rb_vm_push_frame_fname(ec, fname);
1037 VALUE realpath_map = box->loaded_features_realpath_map;
1039 if (rb_ruby_prism_p()) {
1042 .realpath_map = realpath_map,
1046 pm_parse_result_init(&args.result);
1047 args.result.
node.coverage_enabled = 1;
1054 if (args.error !=
Qnil) {
1055 rb_vm_pop_frame(ec);
1065 VALUE parser = rb_parser_new();
1066 rb_parser_set_context(parser, NULL, FALSE);
1067 ast_value = rb_parser_load_file(parser, fname);
1068 ast = rb_ruby_ast_data_get(ast_value);
1070 iseq = rb_iseq_new_top(ast_value, rb_fstring_lit(
"<top (required)>"),
1071 fname, realpath_internal_cached(realpath_map, fname), NULL);
1072 rb_ast_dispose(ast);
1075 rb_vm_pop_frame(ec);
1078 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
1080 rb_iseq_eval(iseq, box);
1083static inline enum ruby_tag_type
1084load_wrapping(rb_execution_context_t *ec,
VALUE fname,
VALUE load_wrapper)
1086 enum ruby_tag_type state;
1088 rb_thread_t *th = rb_ec_thread_ptr(ec);
1089 volatile VALUE wrapper = th->top_wrapper;
1090 volatile VALUE self = th->top_self;
1091#if !defined __GNUC__
1092 rb_thread_t *
volatile th0 = th;
1098 if (BOX_OBJ_P(load_wrapper)) {
1099 box = rb_get_box_t(load_wrapper);
1100 if (!box->top_self) {
1103 th->top_self = box->top_self;
1108 th->top_wrapper = load_wrapper;
1112 state = EC_EXEC_TAG();
1113 if (state == TAG_NONE) {
1114 load_iseq_eval(ec, fname);
1118#if !defined __GNUC__
1122 th->top_self = self;
1123 th->top_wrapper = wrapper;
1128raise_load_if_failed(rb_execution_context_t *ec,
enum ruby_tag_type state)
1131 rb_vm_jump_tag_but_local_jump(state);
1134 if (!
NIL_P(ec->errinfo)) {
1143 rb_execution_context_t *ec = GET_EC();
1144 const rb_box_t *box = rb_loading_box();
1145 enum ruby_tag_type state = TAG_NONE;
1150 state = load_wrapping(ec, fname, wrap);
1152 else if (BOX_OPTIONAL_P(box)) {
1153 box_value = box->box_object;
1154 state = load_wrapping(ec, fname, box_value);
1157 load_iseq_eval(ec, fname);
1159 raise_load_if_failed(ec, state);
1166 if (!tmp) load_failed(fname);
1167 rb_load_internal(tmp, RBOOL(wrap));
1173 enum ruby_tag_type state;
1175 EC_PUSH_TAG(GET_EC());
1176 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1181 if (state != TAG_NONE) *pstate = state;
1187 VALUE path, orig_fname;
1189 orig_fname = rb_get_path_check_to_string(fname);
1190 fname = rb_str_encode_ospath(orig_fname);
1191 RUBY_DTRACE_HOOK(LOAD_ENTRY, RSTRING_PTR(orig_fname));
1193 path = rb_find_file(fname);
1195 if (!rb_file_load_ok(RSTRING_PTR(fname)))
1196 load_failed(orig_fname);
1199 rb_load_internal(path, wrap);
1201 RUBY_DTRACE_HOOK(LOAD_RETURN, RSTRING_PTR(orig_fname));
1241 return rb_load_entrypoint(fname, wrap);
1245load_lock(
const rb_box_t *box,
const char *ftptr,
bool warn)
1248 st_table *loading_tbl = box->loading_table;
1250 if (!st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
1253 data = (st_data_t)rb_thread_shield_new();
1254 st_insert(loading_tbl, (st_data_t)ftptr, data);
1255 return (
char *)ftptr;
1258 if (warn && rb_thread_shield_owned((
VALUE)data)) {
1259 VALUE warning = rb_warning_string(
"loading in progress, circular require considered harmful - %s", ftptr);
1263 switch (rb_thread_shield_wait((
VALUE)data)) {
1268 return (
char *)ftptr;
1272release_thread_shield(st_data_t *key, st_data_t *value, st_data_t done,
int existing)
1275 if (!existing)
return ST_STOP;
1277 rb_thread_shield_destroy(thread_shield);
1281 else if (rb_thread_shield_release(thread_shield)) {
1285 xfree((
char *)*key);
1290load_unlock(
const rb_box_t *box,
const char *ftptr,
int done)
1293 st_data_t key = (st_data_t)ftptr;
1294 st_table *loading_tbl = box->loading_table;
1296 st_update(loading_tbl, key, release_thread_shield, done);
1300static VALUE rb_require_string_internal(
VALUE fname,
bool resurrect);
1346rb_require_relative_entrypoint(
VALUE fname)
1348 VALUE base = rb_current_realfilepath();
1352 base = rb_file_dirname(base);
1353 return rb_require_string_internal(rb_file_absolute_path(fname, base),
false);
1368 return rb_require_relative_entrypoint(fname);
1372find_ext(
VALUE str,
char **ptr,
const char **end)
1375 *ptr = RSTRING_PTR(str);
1377 return memrchr(*ptr,
'.',
len);
1381ext_equal(
const char *ext,
const char *end,
const char *suffix,
size_t len)
1383 return (
size_t)(end - ext) ==
len && memcmp(ext, suffix,
len) == 0;
1386#define EXT_RB_P(ext, end) ext_equal(ext, end, ".rb", rb_strlen_lit(".rb"))
1387#define EXT_SO_P(ext, end) (ext_equal(ext, end, ".so", rb_strlen_lit(".so")) || \
1388 ext_equal(ext, end, ".o", rb_strlen_lit(".o")))
1389#define EXT_DLEXT_P(ext, end) ext_equal(ext, end, DLEXT, rb_strlen_lit(DLEXT))
1391typedef int (*feature_func)(
const rb_box_t *box,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn);
1394search_required(
const rb_box_t *box,
VALUE fname,
volatile VALUE *path, feature_func rb_feature_p)
1399 const char *ftend, *loading;
1402 ext = find_ext(fname, &ftptr, &ftend);
1403 if (ext && !memchr(ext,
'/', ftend - ext)) {
1404 if (EXT_RB_P(ext, ftend)) {
1405 if (rb_feature_p(box, ftptr, ext, TRUE, FALSE, &loading)) {
1409 if ((tmp = rb_find_file(fname)) != 0) {
1410 ext = find_ext(tmp, &ftptr, &ftend);
1411 if (!rb_feature_p(box, ftptr, ext, TRUE, TRUE, &loading) || loading)
1417 else if (EXT_SO_P(ext, ftend)) {
1418 if (rb_feature_p(box, ftptr, ext, FALSE, FALSE, &loading)) {
1425 if ((tmp = rb_find_file(tmp)) != 0) {
1426 ext = find_ext(tmp, &ftptr, &ftend);
1427 if (!rb_feature_p(box, ftptr, ext, FALSE, TRUE, &loading) || loading)
1432 else if (EXT_DLEXT_P(ext, ftend)) {
1433 if (rb_feature_p(box, ftptr, ext, FALSE, FALSE, &loading)) {
1437 if ((tmp = rb_find_file(fname)) != 0) {
1438 ext = find_ext(tmp, &ftptr, &ftend);
1439 if (!rb_feature_p(box, ftptr, ext, FALSE, TRUE, &loading) || loading)
1445 else if ((ft = rb_feature_p(box, ftptr, 0, FALSE, FALSE, &loading)) ==
'r') {
1450 const unsigned int type = rb_find_file_ext(&tmp, ft ==
's' ? ruby_ext : loadable_ext);
1454 if (!ft &&
type != loadable_ext_rb) {
1455 rb_vm_t *vm = GET_VM();
1456 if (vm->static_ext_inits.num_entries) {
1457 VALUE lookup_name = tmp;
1465 ftptr = RSTRING_PTR(lookup_name);
1466 if (st_lookup(&vm->static_ext_inits, (st_data_t)ftptr, NULL)) {
1477 goto feature_present;
1478 ftptr = RSTRING_PTR(tmp);
1479 return rb_feature_p(box, ftptr, 0, FALSE, TRUE, 0);
1483 goto feature_present;
1486 case loadable_ext_rb:
1487 ext = find_ext(tmp, &ftptr, &ftend);
1488 if (rb_feature_p(box, ftptr, ext,
type == loadable_ext_rb, TRUE, &loading) && !loading)
1492 return type > loadable_ext_rb ?
's' :
'r';
1500load_failed(
VALUE fname)
1502 rb_load_fail(fname,
"cannot load such file");
1508 VALUE loaded = path;
1509 const rb_box_t *box = rb_loading_box();
1511 if (BOX_USER_P(box)) {
1512 loaded = rb_box_local_extension(box->box_object, path, &cleanup);
1514 rb_scope_visibility_set(METHOD_VISI_PUBLIC);
1515 void *handle = dln_load_feature(RSTRING_PTR(loaded), RSTRING_PTR(fname));
1517 rb_box_cleanup_local_extension(cleanup);
1518 rb_box_defer_unload_local_extension(handle);
1522 return (
VALUE)handle;
1526run_static_ext_init(
VALUE vm_ptr,
VALUE feature_value)
1528 rb_vm_t *vm = (rb_vm_t *)vm_ptr;
1529 const char *feature = RSTRING_PTR(feature_value);
1530 st_data_t key = (st_data_t)feature;
1531 st_data_t init_func;
1533 if (st_delete(&vm->static_ext_inits, &key, &init_func)) {
1534 ((void (*)(void))init_func)();
1541no_feature_p(
const rb_box_t *box,
const char *feature,
const char *ext,
int rb,
int expanded,
const char **fn)
1548rb_resolve_feature_path(
VALUE klass,
VALUE fname)
1553 const rb_box_t *box = rb_loading_box();
1555 fname = rb_get_path(fname);
1556 path = rb_str_encode_ospath(fname);
1557 found = search_required(box, path, &path, no_feature_p);
1561 sym =
ID2SYM(rb_intern(
"rb"));
1564 sym =
ID2SYM(rb_intern(
"so"));
1570 return rb_ary_new_from_args(2, sym, path);
1574ext_config_push(rb_thread_t *th,
volatile struct rb_ext_config *prev)
1576 *prev = th->ext_config;
1581ext_config_pop(rb_thread_t *th,
volatile struct rb_ext_config *prev)
1583 th->ext_config = *prev;
1589 GET_THREAD()->ext_config.ractor_safe = flag;
1600require_internal(rb_execution_context_t *ec,
VALUE fname,
int exception,
bool warn)
1602 volatile int result = -1;
1603 rb_thread_t *th = rb_ec_thread_ptr(ec);
1604 const rb_box_t *box = rb_loading_box();
1605 volatile const struct {
1606 VALUE wrapper, self, errinfo;
1607 rb_execution_context_t *ec;
1608 const rb_box_t *box;
1610 th->top_wrapper, th->top_self, ec->errinfo,
1613 enum ruby_tag_type state;
1614 char *
volatile ftptr = 0;
1616 volatile VALUE saved_path;
1617 volatile VALUE realpath = 0;
1618 VALUE realpaths = box->loaded_features_realpaths;
1619 VALUE realpath_map = box->loaded_features_realpath_map;
1620 volatile bool reset_ext_config =
false;
1623 path = rb_str_encode_ospath(fname);
1624 RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
1629 th->top_wrapper = 0;
1630 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1634 RUBY_DTRACE_HOOK(FIND_REQUIRE_ENTRY, RSTRING_PTR(fname));
1635 found = search_required(box, path, &saved_path, rb_feature_p);
1636 RUBY_DTRACE_HOOK(FIND_REQUIRE_RETURN, RSTRING_PTR(fname));
1640 if (!path || !(ftptr = load_lock(box, RSTRING_PTR(path), warn))) {
1644 result = TAG_RETURN;
1646 else if (found ==
's' &&
RTEST(rb_vm_call_cfunc_in_box(
Qnil, run_static_ext_init, (
VALUE)th->vm, path, path, box))) {
1647 result = TAG_RETURN;
1649 else if (
RTEST(rb_hash_aref(realpaths,
1650 realpath = realpath_internal_cached(realpath_map, path)))) {
1656 load_iseq_eval(saved.ec, path);
1660 reset_ext_config =
true;
1661 ext_config_push(th, &prev_ext_config);
1662 handle = rb_vm_call_cfunc_in_box(box->top_self, load_ext, path, fname, path, box);
1663 rb_hash_aset(box->ruby_dln_libmap, path, PTR2NUM(handle));
1666 result = TAG_RETURN;
1674 rb_thread_t *th2 = rb_ec_thread_ptr(ec);
1675 th2->top_self = saved.self;
1676 th2->top_wrapper = saved.wrapper;
1677 if (reset_ext_config) ext_config_pop(th2, &prev_ext_config);
1680 if (ftptr) load_unlock(box, RSTRING_PTR(path), !state);
1683 if (state == TAG_FATAL || state == TAG_THROW) {
1684 EC_JUMP_TAG(ec, state);
1686 else if (exception) {
1689 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
1690 if (!
NIL_P(exc)) ec->errinfo = exc;
1693 else if (state == TAG_RETURN) {
1700 if (!
NIL_P(ec->errinfo)) {
1701 if (!exception)
return TAG_RAISE;
1705 if (result == TAG_RETURN) {
1706 rb_provide_feature(box, path);
1707 VALUE real = realpath;
1709 real = rb_fstring(real);
1710 rb_hash_aset(realpaths, real,
Qtrue);
1713 ec->errinfo = saved.errinfo;
1715 RUBY_DTRACE_HOOK(REQUIRE_RETURN, RSTRING_PTR(fname));
1721rb_require_internal_silent(
VALUE fname)
1723 if (!rb_ractor_main_p()) {
1724 return NUM2INT(rb_ractor_require(fname,
true));
1727 rb_execution_context_t *ec = GET_EC();
1728 return require_internal(ec, fname, 1,
false);
1732rb_require_internal(
VALUE fname)
1734 rb_execution_context_t *ec = GET_EC();
1739ruby_require_internal(
const char *fname,
unsigned int len)
1741 struct RString fake = {RBASIC_INIT};
1742 VALUE str = rb_setup_fake_str(&fake, fname,
len, 0);
1743 rb_execution_context_t *ec = GET_EC();
1745 rb_set_errinfo(
Qnil);
1746 return result == TAG_RETURN ? 1 : result ? -1 : 0;
1752 return rb_require_string_internal(
FilePathValue(fname),
false);
1756rb_require_string_internal(
VALUE fname,
bool resurrect)
1758 rb_execution_context_t *ec = GET_EC();
1761 if (!rb_ractor_main_p()) {
1763 return rb_ractor_require(fname,
false);
1768 if (result > TAG_RETURN) {
1769 EC_JUMP_TAG(ec, result);
1776 return RBOOL(result);
1783 struct RString fake = {RBASIC_INIT};
1784 VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
1785 return rb_require_string_internal(str,
true);
1789register_init_ext(st_data_t *key, st_data_t *value, st_data_t init,
int existing)
1791 const char *name = (
char *)*key;
1794 rb_warn(
"%s is already registered", name);
1797 *value = (st_data_t)init;
1806ruby_init_ext(
const char *name,
void (*init)(
void))
1808 rb_vm_t *vm = GET_VM();
1809 const rb_box_t *box = rb_loading_box();
1811 if (feature_provided((rb_box_t *)box, name, 0))
1814 st_update(&vm->static_ext_inits, (st_data_t)name, register_init_ext, (st_data_t)init);
1844 rb_autoload_str(mod,
id, file);
1876 VALUE base, absolute_path;
1880 base = rb_current_realfilepath();
1882 rb_loaderror(
"cannot infer basepath (autoload_relative called without file context)");
1884 base = rb_file_dirname(base);
1885 absolute_path = rb_file_absolute_path(file, base);
1887 rb_autoload_str(mod,
id, absolute_path);
1918rb_mod_autoload_p(
int argc,
VALUE *argv,
VALUE mod)
1921 VALUE sym = argv[0];
1927 return rb_autoload_at_p(mod,
id, recur);
1953 rb_raise(
rb_eTypeError,
"Can not set autoload on singleton class");
1955 return rb_mod_autoload(klass, sym, file);
1985 rb_raise(
rb_eTypeError,
"Can not set autoload on singleton class");
1987 return rb_mod_autoload_relative(klass, sym, file);
2014rb_f_autoload_p(
int argc,
VALUE *argv,
VALUE obj)
2017 VALUE klass = rb_vm_cbase();
2021 return rb_mod_autoload_p(argc, argv, klass);
2032 const rb_box_t *box = rb_loading_box();
2034 resolved = rb_resolve_feature_path((
VALUE)NULL, fname_str);
2035 if (
NIL_P(resolved)) {
2036 ext = strrchr(fname,
'.');
2037 if (!ext || !IS_SOEXT(ext)) {
2040 if (rb_feature_p(box, fname, 0, FALSE, FALSE, 0)) {
2041 return dln_symbol(NULL, symbol);
2049 handle = rb_hash_lookup(box->ruby_dln_libmap, path);
2050 if (
NIL_P(handle)) {
2053 return dln_symbol(NUM2PTR(handle), symbol);
2059 static const char var_load_path[] =
"$:";
2060 ID id_load_path = rb_intern2(var_load_path,
sizeof(var_load_path)-1);
2063 rb_gvar_box_ready(var_load_path);
2068 rb_gvar_box_ready(
"$\"");
2070 rb_gvar_box_ready(
"$LOADED_FEATURES");
#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.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
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_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty 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.
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_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
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(rb_len_t len)
Allocates a "temporary" string.
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.
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
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.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#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.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
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.