diff --git a/load.c b/load.c index d4d6afaddb..da81becfc1 100644 --- a/load.c +++ b/load.c @@ -1032,8 +1032,10 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_ } tmp = fname; type = rb_find_file_ext(&tmp, ft == 's' ? ruby_ext : loadable_ext); -#if EXTSTATIC - if (!ft && type != 1) { // not already a feature and not found as a dynamic library + + // Check if it's a statically linked extension when + // not already a feature and not found as a dynamic library. + if (!ft && type != 1 && vm->static_ext_inits) { VALUE lookup_name = tmp; // Append ".so" if not already present so for example "etc" can find "etc.so". // We always register statically linked extensions with a ".so" extension. @@ -1048,7 +1050,7 @@ search_required(rb_vm_t *vm, VALUE fname, volatile VALUE *path, feature_func rb_ return 's'; } } -#endif + switch (type) { case 0: if (ft) @@ -1087,19 +1089,18 @@ load_ext(VALUE path) return (VALUE)dln_load(RSTRING_PTR(path)); } -#if EXTSTATIC static bool run_static_ext_init(rb_vm_t *vm, const char *feature) { st_data_t key = (st_data_t)feature; st_data_t init_func; - if (st_delete(vm->static_ext_inits, &key, &init_func)) { + + if (vm->static_ext_inits && st_delete(vm->static_ext_inits, &key, &init_func)) { ((void (*)(void))init_func)(); return true; } return false; } -#endif static int no_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn) @@ -1203,11 +1204,9 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa else if (!*ftptr) { result = TAG_RETURN; } -#if EXTSTATIC else if (found == 's' && run_static_ext_init(th->vm, RSTRING_PTR(path))) { result = TAG_RETURN; } -#endif else if (RTEST(rb_hash_aref(realpaths, realpath = rb_realpath_internal(Qnil, path, 1)))) { result = 0; @@ -1326,7 +1325,6 @@ rb_require(const char *fname) return rb_require_string(rb_str_new_cstr(fname)); } -#if EXTSTATIC static int register_init_ext(st_data_t *key, st_data_t *value, st_data_t init, int existing) { @@ -1341,17 +1339,25 @@ register_init_ext(st_data_t *key, st_data_t *value, st_data_t init, int existing return ST_CONTINUE; } +// Private API for statically linked extensions. +// Used with the ext/Setup file, the --with-setup and +// --with-static-linked-ext configuration option, etc. void ruby_init_ext(const char *name, void (*init)(void)) { + st_table *inits_table; rb_vm_t *vm = GET_VM(); - st_table *inits_table = vm->static_ext_inits; if (feature_provided(vm, name, 0)) return; + + inits_table = vm->static_ext_inits; + if (!inits_table) { + inits_table = st_init_strtable(); + vm->static_ext_inits = inits_table; + } st_update(inits_table, (st_data_t)name, register_init_ext, (st_data_t)init); } -#endif /* * call-seq: diff --git a/vm.c b/vm.c index e1e8e78053..20cd8edd7c 100644 --- a/vm.c +++ b/vm.c @@ -4037,9 +4037,6 @@ Init_vm_objects(void) vm->mark_object_ary = rb_ary_hidden_new(128); vm->loading_table = st_init_strtable(); vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000); -#if EXTSTATIC - vm->static_ext_inits = st_init_strtable(); -#endif } /* Stub for builtin function when not building YJIT units*/ diff --git a/vm_core.h b/vm_core.h index f2aa4f63ca..7cdd40671d 100644 --- a/vm_core.h +++ b/vm_core.h @@ -557,10 +557,6 @@ struct rb_iseq_struct { #define ISEQ_BODY(iseq) ((iseq)->body) -#ifndef EXTSTATIC -#define EXTSTATIC 0 -#endif - #ifndef USE_LAZY_LOAD #define USE_LAZY_LOAD 0 #endif @@ -686,11 +682,9 @@ typedef struct rb_vm_struct { VALUE loaded_features_realpath_map; struct st_table *loaded_features_index; struct st_table *loading_table; -#if EXTSTATIC // For running the init function of statically linked // extensions when they are loaded struct st_table *static_ext_inits; -#endif /* signal */ struct {