3#include "eval_intern.h"
5#include "internal/box.h"
6#include "internal/class.h"
7#include "internal/eval.h"
8#include "internal/error.h"
9#include "internal/file.h"
10#include "internal/gc.h"
11#include "internal/hash.h"
12#include "internal/io.h"
13#include "internal/load.h"
14#include "internal/random.h"
15#include "internal/st.h"
16#include "internal/variable.h"
31#ifdef HAVE_SYS_SENDFILE_H
32# include <sys/sendfile.h>
39VALUE rb_cBoxEntry = 0;
40VALUE rb_mBoxLoader = 0;
42static rb_box_t master_box[1];
43static rb_box_t *root_box;
44static rb_box_t *main_box;
46static rb_box_gem_flags_t box_gem_flags[1];
49static bool tmp_dir_has_dirsep;
52# define MAXPATHLEN 1024
56static char box_ext_tmp_dir[MAXPATHLEN];
57static rb_pid_t box_ext_tmp_dir_owner;
58static unsigned long box_ext_seq;
60#define BOX_TMP_PREFIX "_ruby_box_"
68bool ruby_box_enabled =
false;
69bool ruby_box_init_done =
false;
70bool ruby_box_crashed =
false;
76rb_box_set_gem_flags(rb_box_gem_flags_t *flags)
79 box_gem_flags->gem = flags->gem;
80 box_gem_flags->error_highlight = flags->error_highlight;
81 box_gem_flags->did_you_mean = flags->did_you_mean;
82 box_gem_flags->syntax_suggest = flags->syntax_suggest;
88 ruby_box_init_done =
true;
127 return rb_vm_current_box(GET_EC());
139 return rb_vm_loading_box(GET_EC());
144rb_current_box_in_crash_report(
void)
146 if (ruby_box_crashed)
148 return rb_current_box();
151static long box_id_counter = 0;
158 id = ++box_id_counter;
164box_main_to_s(
VALUE obj)
170box_entry_initialize(rb_box_t *box)
172 const rb_box_t *master = rb_master_box();
181 box->load_path =
rb_ary_dup(master->load_path);
182 box->expanded_load_path =
rb_ary_dup(master->expanded_load_path);
184 box->load_path_check_cache = 0;
185 box->loaded_features =
rb_ary_dup(master->loaded_features);
187 box->loaded_features_index = st_init_numtable();
188 box->load_unstable_depth = 0;
189 box->loaded_features_realpaths = rb_hash_dup(master->loaded_features_realpaths);
190 box->loaded_features_realpath_map = rb_hash_dup(master->loaded_features_realpath_map);
191 box->loading_table = st_init_strtable();
194 box->classext_cow_classes = st_init_numtable();
197 box->is_optional =
true;
201rb_box_gc_update_references(
void *ptr)
203 rb_box_t *box = (rb_box_t *)ptr;
207 rb_gc_update_moved(&box->box_object);
209 rb_gc_update_moved(&box->top_self);
210 rb_gc_update_moved(&box->load_path);
211 rb_gc_update_moved(&box->expanded_load_path);
212 rb_gc_update_moved(&box->load_path_snapshot);
213 if (box->load_path_check_cache) {
214 rb_gc_update_moved(&box->load_path_check_cache);
216 rb_gc_update_moved(&box->loaded_features);
217 rb_gc_update_moved(&box->loaded_features_snapshot);
218 rb_gc_update_moved(&box->loaded_features_realpaths);
219 rb_gc_update_moved(&box->loaded_features_realpath_map);
220 rb_gc_update_moved(&box->ruby_dln_libmap);
221 rb_gc_update_moved(&box->gvar_tbl);
225rb_box_entry_mark(
void *ptr)
227 const rb_box_t *box = (rb_box_t *)ptr;
230 rb_gc_mark(box->box_object);
231 rb_gc_mark(box->top_self);
232 rb_gc_mark(box->load_path);
233 rb_gc_mark(box->expanded_load_path);
234 rb_gc_mark(box->load_path_snapshot);
235 rb_gc_mark(box->load_path_check_cache);
236 rb_gc_mark(box->loaded_features);
237 rb_gc_mark(box->loaded_features_snapshot);
238 rb_gc_mark(box->loaded_features_realpaths);
239 rb_gc_mark(box->loaded_features_realpath_map);
240 if (box->loading_table) {
241 rb_mark_tbl(box->loading_table);
243 rb_gc_mark(box->ruby_dln_libmap);
244 rb_gc_mark(box->gvar_tbl);
245 if (box->classext_cow_classes) {
246 rb_mark_set(box->classext_cow_classes);
251free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
258free_loaded_feature_index_i(st_data_t key, st_data_t value, st_data_t arg)
261 rb_darray_free_sized((
void *)value,
long);
267free_box_st_tables(
void *ptr)
269 rb_box_t *box = (rb_box_t *)ptr;
270 if (box->loading_table) {
271 st_foreach(box->loading_table, free_loading_table_entry, 0);
272 st_free_table(box->loading_table);
273 box->loading_table = 0;
276 if (box->loaded_features_index) {
277 st_foreach(box->loaded_features_index, free_loaded_feature_index_i, 0);
278 st_free_table(box->loaded_features_index);
283free_classext_for_box(st_data_t key, st_data_t _value, st_data_t box_arg)
287 const rb_box_t *box = (
const rb_box_t *)box_arg;
296 ext = rb_class_unlink_classext(obj, box);
297 rb_class_classext_free(obj, ext,
false);
300 ext = rb_class_unlink_classext(obj, box);
301 rb_iclass_classext_free(obj, ext,
false);
304 rb_bug(
"Invalid type of object in classext_cow_classes: %s", rb_type_str(
BUILTIN_TYPE(obj)));
310box_entry_free(
void *ptr)
312 const rb_box_t *box = (
const rb_box_t *)ptr;
314 if (box->classext_cow_classes) {
315 st_foreach(box->classext_cow_classes, free_classext_for_box, (st_data_t)box);
318 free_box_st_tables(ptr);
323box_entry_memsize(
const void *ptr)
325 size_t size =
sizeof(rb_box_t);
326 const rb_box_t *box = (
const rb_box_t *)ptr;
327 if (box->loaded_features_index) {
328 size += rb_st_memsize(box->loaded_features_index);
330 if (box->loading_table) {
331 size += rb_st_memsize(box->loading_table);
342 rb_box_gc_update_references,
353 rb_box_gc_update_references,
355 &rb_box_data_type, 0, RUBY_TYPED_THREAD_SAFE_FREE
359rb_box_entry_alloc(
VALUE klass)
363 box_entry_initialize(entry);
368get_box_struct_internal(
VALUE entry)
376rb_get_box_t(
VALUE box)
384 return (rb_box_t *)rb_root_box();
386 VM_ASSERT(BOX_OBJ_P(box));
388 CONST_ID(id_box_entry,
"__box_entry__");
389 entry = rb_attr_get(box, id_box_entry);
390 return get_box_struct_internal(entry);
394rb_get_box_object(rb_box_t *box)
396 VM_ASSERT(box && box->box_object);
397 return box->box_object;
407box_initialize(
VALUE box_value)
410 rb_classext_t *object_classext;
413 CONST_ID(id_box_entry,
"__box_entry__");
415 if (!rb_box_available()) {
416 rb_raise(
rb_eRuntimeError,
"Ruby Box is disabled. Set RUBY_BOX=1 environment variable to use Ruby::Box.");
420 box = get_box_struct_internal(entry);
422 box->box_object = box_value;
423 box->box_id = box_generate_id();
429 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(box_value,
true);
433 object_classext = RCLASS_EXT_WRITABLE_IN_BOX(
rb_cObject, box);
434 RCLASS_SET_CONST_TBL(box_value, RCLASSEXT_CONST_TBL(object_classext),
true);
438 if (ruby_box_init_done) {
439 if (box_gem_flags->gem) {
440 rb_vm_call_cfunc_in_box(
Qnil, rb_define_gem_modules, (
VALUE)box_gem_flags,
Qnil,
442 rb_load_gem_prelude((
VALUE)box);
447 rb_zjit_invalidate_root_box();
459rb_box_s_getenabled(
VALUE recv)
461 return RBOOL(rb_box_available());
472rb_box_s_current(
VALUE recv)
476 if (!rb_box_available())
479 box = rb_vm_current_box(GET_EC());
480 VM_ASSERT(box && box->box_object);
481 return box->box_object;
491rb_box_load_path(
VALUE box)
493 VM_ASSERT(BOX_OBJ_P(box));
494 return rb_get_box_t(box)->load_path;
498UINT rb_w32_system_tmpdir(WCHAR *path, UINT
len);
503system_default_tmpdir(
void)
507 WCHAR tmppath[_MAX_PATH];
508 UINT
len = rb_w32_system_tmpdir(tmppath, numberof(tmppath));
510 int blen = WideCharToMultiByte(CP_UTF8, 0, tmppath,
len, NULL, 0, NULL, NULL);
511 char *tmpdir =
xmalloc(blen + 1);
512 WideCharToMultiByte(CP_UTF8, 0, tmppath,
len, tmpdir, blen, NULL, NULL);
516#elif defined _CS_DARWIN_USER_TEMP_DIR
517 char path[MAXPATHLEN];
518 size_t len = confstr(_CS_DARWIN_USER_TEMP_DIR, path,
sizeof(path));
521 if (
len >
sizeof(path)) {
522 confstr(_CS_DARWIN_USER_TEMP_DIR, tmpdir,
len);
525 memcpy(tmpdir, path,
len);
534check_tmpdir(
const char *dir)
538 if (!dir)
return FALSE;
539 if (stat(dir, &st))
return FALSE;
541# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
543 if (!S_ISDIR(st.st_mode))
return FALSE;
548 if (st.st_mode & S_IWOTH) {
550 if (!(st.st_mode & S_ISVTX))
return FALSE;
555 if (access(dir, W_OK))
return FALSE;
564# define RETURN_ENV(name) \
565 if (check_tmpdir(tmpdir = getenv(name))) return ruby_strdup(tmpdir)
566 RETURN_ENV(
"TMPDIR");
568 tmpdir = system_default_tmpdir();
569 if (check_tmpdir(tmpdir))
return tmpdir;
583ensure_box_ext_tmp_dir(
void)
588 if (box_ext_tmp_dir[0] && box_ext_tmp_dir_owner != getpid()) {
589 box_ext_tmp_dir[0] =
'\0';
592 if (box_ext_tmp_dir[0])
return;
595 for (
int retry = 0; retry < 10; retry++) {
596 char path[MAXPATHLEN];
598 if (ruby_fill_random_bytes(&suffix,
sizeof(suffix), FALSE) != 0) {
600 suffix = (uint64_t)(uintptr_t)&suffix ^ (uint64_t)retry;
602 int wrote = snprintf(path,
sizeof(path),
"%s%s%sp%"PRI_PIDT_PREFIX"u_%.16"PRIx64,
603 tmp_dir, tmp_dir_has_dirsep ?
"" : DIRSEP,
604 BOX_TMP_PREFIX, getpid(), suffix);
605 if (wrote >= (
int)
sizeof(path)) {
606 rb_raise(
rb_eLoadError,
"TMPDIR for Ruby Box extensions is too long: %s", tmp_dir);
608 if (mkdir(path, 0700) == 0) {
609 strlcpy(box_ext_tmp_dir, path,
sizeof(box_ext_tmp_dir));
610 box_ext_tmp_dir_owner = getpid();
614 if (last_errno != EEXIST)
break;
616 rb_raise(
rb_eLoadError,
"can't create the temporary directory for Ruby Box extensions under %s: %s",
617 tmp_dir, strerror(last_errno));
620enum copy_error_type {
625 COPY_ERROR_DST_WRITE,
627 COPY_ERROR_DST_CHMOD,
632copy_ext_file_error(
char *message,
size_t size,
int copy_retvalue)
635 int error = GetLastError();
637 size_t len = snprintf(message, size,
"%d: ", error);
639#define format_message(sublang) FormatMessage(\
640 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
641 NULL, error, MAKELANGID(LANG_NEUTRAL, (sublang)), \
642 message + len, size - len, NULL)
643 if (format_message(SUBLANG_ENGLISH_US) == 0)
644 format_message(SUBLANG_DEFAULT);
645 for (p = message +
len; *p; p++) {
646 if (*p ==
'\n' || *p ==
'\r')
650 switch (copy_retvalue) {
651 case COPY_ERROR_SRC_OPEN:
652 strlcpy(message,
"can't open the extension path", size);
654 case COPY_ERROR_DST_OPEN:
655 strlcpy(message,
"can't open the file to write", size);
657 case COPY_ERROR_SRC_READ:
658 strlcpy(message,
"failed to read the extension path", size);
660 case COPY_ERROR_DST_WRITE:
661 strlcpy(message,
"failed to write the extension path", size);
663 case COPY_ERROR_SRC_STAT:
664 strlcpy(message,
"failed to stat the extension path to copy permissions", size);
666 case COPY_ERROR_DST_CHMOD:
667 strlcpy(message,
"failed to set permissions to the copied extension path", size);
669 case COPY_ERROR_SYSERR:
670 strlcpy(message, strerror(
errno), size);
672 case COPY_ERROR_NONE:
674 rb_bug(
"unknown return value of copy_ext_file: %d", copy_retvalue);
681static enum copy_error_type
682copy_stream(
int src_fd,
int dst_fd)
687 while ((rsize = read(src_fd, buffer,
sizeof(buffer))) != 0) {
688 if (rsize < 0)
return COPY_ERROR_SRC_READ;
689 for (
size_t written = 0; written < (size_t)rsize;) {
690 ssize_t wsize = write(dst_fd, buffer+written, rsize-written);
691 if (wsize < 0)
return COPY_ERROR_DST_WRITE;
692 written += (size_t)wsize;
695 return COPY_ERROR_NONE;
699static enum copy_error_type
700copy_ext_file(
const char *src_path,
const char *dst_path)
703 WCHAR *w_src = rb_w32_mbstr_to_wstr(CP_UTF8, src_path, -1, NULL);
704 WCHAR *w_dst = rb_w32_mbstr_to_wstr(CP_UTF8, dst_path, -1, NULL);
705 if (!w_src || !w_dst) {
711 enum copy_error_type rvalue = CopyFileW(w_src, w_dst, TRUE) ?
712 COPY_ERROR_NONE : COPY_ERROR_SYSERR;
718 const int bin = O_BINARY;
723 const int cloexec = O_CLOEXEC;
725 const int cloexec = 0;
727 const int src_fd = open(src_path, O_RDONLY|cloexec|bin);
728 if (src_fd < 0)
return COPY_ERROR_SRC_OPEN;
729 if (!cloexec) rb_maygvl_fd_fix_cloexec(src_fd);
732 if (fstat(src_fd, &src_st)) {
734 return COPY_ERROR_SRC_STAT;
737 const int dst_fd = open(dst_path, O_WRONLY|O_CREAT|O_EXCL|cloexec|bin, S_IRWXU);
740 return COPY_ERROR_DST_OPEN;
742 if (!cloexec) rb_maygvl_fd_fix_cloexec(dst_fd);
744 enum copy_error_type ret = COPY_ERROR_NONE;
746 if (fchmod(dst_fd, src_st.st_mode & 0777)) {
747 ret = COPY_ERROR_DST_CHMOD;
751 const size_t count_max = (SIZE_MAX >> 1) + 1;
754# ifdef HAVE_COPY_FILE_RANGE
756 ssize_t written = copy_file_range(src_fd, NULL, dst_fd, NULL, count_max, 0);
757 if (written == 0)
goto done;
758 if (written < 0)
break;
761# ifdef HAVE_FCOPYFILE
762 if (fcopyfile(src_fd, dst_fd, NULL, COPYFILE_DATA) == 0) {
768 ssize_t written = sendfile(src_fd, dst_fd, NULL count_max);
769 if (written == 0)
goto done;
770 if (written < 0)
break;
773 ret = copy_stream(src_fd, dst_fd);
777 if (dst_fd >= 0) close(dst_fd);
778 if (ret != COPY_ERROR_NONE) unlink(dst_path);
783#if defined __CYGWIN__ || defined DOSISH
784#define isdirsep(x) ((x) == '/' || (x) == '\\')
786#define isdirsep(x) ((x) == '/')
790ext_basename(
const char *path)
792 const char *base = path;
793 for (
const char *pos = path; *pos; pos++) {
794 if (isdirsep(*pos)) base = pos + 1;
800box_ext_cleanup_mark(
void *p)
802 rb_gc_mark((
VALUE)p);
806box_ext_cleanup_free(
void *p)
809 unlink(RSTRING_PTR(path));
814 {box_ext_cleanup_mark, box_ext_cleanup_free},
819rb_box_cleanup_local_extension(
VALUE cleanup)
824 if (p) box_ext_cleanup_free(p);
830struct box_local_ext_list {
831 struct box_local_ext_list *next;
834static struct box_local_ext_list *box_local_exts;
838rb_box_defer_unload_local_extension(
void *handle)
846 struct box_local_ext_list *ext = malloc(
sizeof(
struct box_local_ext_list));
848 ext->handle = (HMODULE)handle;
849 ext->next = box_local_exts;
850 box_local_exts = ext;
855rb_box_unload_local_extensions(
void)
858 struct box_local_ext_list *ext = box_local_exts;
859 box_local_exts = NULL;
861 struct box_local_ext_list *next = ext->next;
862 WCHAR module_path[MAXPATHLEN];
863 DWORD
len = GetModuleFileNameW(ext->handle, module_path, numberof(module_path));
865 FreeLibrary(ext->handle);
866 if (
len > 0 &&
len < numberof(module_path)) DeleteFileW(module_path);
871 if (box_ext_tmp_dir[0] && box_ext_tmp_dir_owner == getpid()) {
872 rmdir(box_ext_tmp_dir);
873 box_ext_tmp_dir[0] =
'\0';
880 char ext_path[MAXPATHLEN];
882 const char *src_path = RSTRING_PTR(path);
883 rb_box_t *box = rb_get_box_t(box_value);
885 ensure_box_ext_tmp_dir();
886 wrote = snprintf(ext_path,
sizeof(ext_path),
"%s%s%ld_%lu_%s",
887 box_ext_tmp_dir, DIRSEP, box->box_id, box_ext_seq++, ext_basename(src_path));
888 if (wrote >= (
int)
sizeof(ext_path)) {
889 rb_raise(
rb_eLoadError,
"extension file path in the box is too long: %"PRIsVALUE, path);
893 enum copy_error_type copy_error = copy_ext_file(src_path, ext_path);
896 copy_ext_file_error(message,
sizeof(message), copy_error);
897 rb_raise(
rb_eLoadError,
"can't prepare the extension file for Ruby Box (%s from %"PRIsVALUE
"): %s", ext_path, path, message);
899 DATA_PTR(*cleanup) = (
void *)new_path;
909 rb_vm_frame_flag_set_box_require(GET_EC());
911 return rb_load_entrypoint(fname, wrap);
917 rb_vm_frame_flag_set_box_require(GET_EC());
923rb_box_require_relative(
VALUE box,
VALUE fname)
925 rb_vm_frame_flag_set_box_require(GET_EC());
927 return rb_require_relative_entrypoint(fname);
931initialize_master_box(
void)
933 rb_vm_t *vm = GET_VM();
934 rb_box_t *master = (rb_box_t *)rb_master_box();
939 master->load_path_check_cache = 0;
944 master->loaded_features_index = st_init_numtable();
945 master->load_unstable_depth = 0;
948 master->loaded_features_realpath_map =
rb_hash_new();
953 master->classext_cow_classes = NULL;
955 vm->master_box = master;
957 if (rb_box_available()) {
958 VALUE master_box, entry;
960 CONST_ID(id_box_entry,
"__box_entry__");
963 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(master_box,
true);
964 RCLASS_SET_CONST_TBL(master_box, RCLASSEXT_CONST_TBL(RCLASS_EXT_PRIME(
rb_cObject)),
true);
966 master->box_id = box_generate_id();
967 master->box_object = master_box;
972 rb_gc_register_mark_object(master_box);
973 rb_gc_register_mark_object(entry);
977 master->box_object =
Qnil;
984 const rb_iseq_t *iseq;
992 box = (
const rb_box_t *)rb_get_box_t(box_value);
994 return rb_iseq_eval(iseq, box);
997static int box_experimental_warned = 0;
1002box_value_initialize(
bool root,
bool user,
bool optional)
1007 VM_ASSERT(BOX_OBJ_P(box_value));
1009 box = rb_get_box_t(box_value);
1010 box->box_object = box_value;
1011 box->is_root = root;
1012 box->is_user = user;
1013 box->is_optional = optional;
1018rb_initialize_mandatory_boxes(
void)
1020 VALUE root_box_value, main_box_value;
1021 rb_vm_t *vm = GET_VM();
1023 VM_ASSERT(rb_box_available());
1025 if (!box_experimental_warned) {
1027 "Ruby::Box is experimental, and the behavior may change in the future!\n"
1028 "See https://docs.ruby-lang.org/en/%s/Ruby/Box.html for known issues, etc.",
1029 ruby_api_version_name);
1030 box_experimental_warned = 1;
1033 root_box_value = box_value_initialize(
true,
false,
false);
1034 main_box_value = box_value_initialize(
false,
true,
false);
1039 vm->root_box = root_box = rb_get_box_t(root_box_value);
1040 vm->main_box = main_box = rb_get_box_t(main_box_value);
1043 RCLASS_EXT_WRITABLE_IN_BOX(
rb_cObject, root_box);
1044 RCLASS_EXT_WRITABLE_IN_BOX(
rb_cObject, main_box);
1048rb_box_inspect(
VALUE obj)
1056 box = rb_get_box_t(obj);
1057 r = rb_sprintf(
"#<Ruby::Box:%ld", box->box_id);
1058 if (BOX_MASTER_P(box)) {
1061 if (BOX_ROOT_P(box)) {
1064 if (BOX_USER_P(box)) {
1067 if (BOX_MAIN_P(box)) {
1070 else if (BOX_OPTIONAL_P(box)) {
1078rb_box_loading_func(
int argc,
VALUE *argv,
VALUE _self)
1080 rb_vm_frame_flag_set_box_require(GET_EC());
1085box_define_loader_method(
const char *name)
1092Init_master_box(
void)
1094 master_box->loading_table = st_init_strtable();
1098Init_enable_box(
void)
1100 const char *env = getenv(
"RUBY_BOX");
1101 if (env && strlen(env) == 1 && env[0] ==
'1') {
1102 ruby_box_enabled =
true;
1105 ruby_box_init_done =
true;
1111rb_box_s_master(
VALUE recv)
1113 return master_box->box_object;
1118rb_box_s_root(
VALUE recv)
1120 return root_box->box_object;
1125rb_box_s_main(
VALUE recv)
1127 return main_box->box_object;
1132rb_box_master_p(
VALUE box_value)
1134 const rb_box_t *box = (
const rb_box_t *)rb_get_box_t(box_value);
1135 return RBOOL(BOX_MASTER_P(box));
1140rb_box_root_p(
VALUE box_value)
1142 const rb_box_t *box = (
const rb_box_t *)rb_get_box_t(box_value);
1143 return RBOOL(BOX_ROOT_P(box));
1148rb_box_main_p(
VALUE box_value)
1150 const rb_box_t *box = (
const rb_box_t *)rb_get_box_t(box_value);
1151 return RBOOL(BOX_MAIN_P(box));
1157classname(
VALUE klass)
1163 p = RCLASSEXT_CLASSPATH(RCLASS_EXT_PRIME(klass));
1165 return RSTRING_PTR(p);
1167 return "AnyClassValue";
1168 return "NonClassValue";
1171static enum rb_id_table_iterator_result
1172dump_classext_methods_i(
ID mid,
VALUE _val,
void *data)
1176 return ID_TABLE_CONTINUE;
1179static enum rb_id_table_iterator_result
1180dump_classext_constants_i(
ID mid,
VALUE _val,
void *data)
1184 return ID_TABLE_CONTINUE;
1188dump_classext_i(rb_classext_t *ext,
bool is_prime,
VALUE _recv,
void *data)
1194 snprintf(buf, 4096,
"Ruby::Box %ld:%s classext %p\n",
1195 RCLASSEXT_BOX(ext)->box_id, is_prime ?
" prime" :
"", (
void *)ext);
1198 snprintf(buf, 2048,
" Super: %s\n", classname(RCLASSEXT_SUPER(ext)));
1201 tbl = RCLASSEXT_M_TBL(ext);
1204 rb_id_table_foreach(RCLASSEXT_M_TBL(ext), dump_classext_methods_i, (
void *)ary);
1206 snprintf(buf, 4096,
" Methods(%"PRIdLEN
"): ",
RARRAY_LEN(ary));
1215 tbl = RCLASSEXT_CONST_TBL(ext);
1218 rb_id_table_foreach(tbl, dump_classext_constants_i, (
void *)ary);
1220 snprintf(buf, 4096,
" Constants(%"PRIdLEN
"): ",
RARRAY_LEN(ary));
1250 const rb_classext_t *ext;
1251 const rb_box_t *box;
1252 st_table *classext_tbl;
1255 snprintf(buf, 2048,
"Non-class/module value: %p (%s)\n", (
void *)klass, rb_type_str(
BUILTIN_TYPE(klass)));
1260 snprintf(buf, 2048,
"Class: %p (%s)%s\n",
1261 (
void *)klass, classname(klass), RCLASS_SINGLETON_P(klass) ?
" [singleton]" :
"");
1264 snprintf(buf, 2048,
"Module: %p (%s)\n", (
void *)klass, classname(klass));
1268 ext = RCLASS_EXT_PRIME(klass);
1269 box = RCLASSEXT_BOX(ext);
1270 snprintf(buf, 2048,
"Prime classext box(%ld,%s), readable(%s), writable(%s)\n",
1272 BOX_MASTER_P(box) ?
"master" : (BOX_ROOT_P(box) ?
"root" : (BOX_MAIN_P(box) ?
"main" :
"optional")),
1273 RCLASS_PRIME_CLASSEXT_READABLE_P(klass) ?
"t" :
"f",
1274 RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass) ?
"t" :
"f");
1277 classext_tbl = RCLASS_CLASSEXT_TBL(klass);
1278 if (!classext_tbl) {
1282 snprintf(buf, 2048,
"Non-prime classexts: %zu\n", st_table_size(classext_tbl));
1286 rb_class_classext_foreach(klass, dump_classext_i, (
void *)res);
1658 tmp_dir = system_tmpdir();
1659 tmp_dir_has_dirsep = (strcmp(tmp_dir + (strlen(tmp_dir) - strlen(DIRSEP)), DIRSEP) == 0);
1661 VALUE mRuby = rb_define_module(
"Ruby");
1670 initialize_master_box();
1673 rb_mBoxLoader = rb_define_module_under(
rb_cBox,
"Loader");
1674 box_define_loader_method(
"require");
1675 box_define_loader_method(
"require_relative");
1676 box_define_loader_method(
"load");
1678 if (rb_box_available()) {
#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_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
Ruby-level global variables / constants, visible from C.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
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 xfree
Old name of ruby_xfree.
#define T_NONE
Old name of RUBY_T_NONE.
#define xmalloc
Old name of ruby_xmalloc.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eLoadError
LoadError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
VALUE rb_cObject
Object class.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_cBox
Ruby::Box class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
VALUE rb_cModule
Module class.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
void rb_memerror(void)
Triggers out-of-memory error.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_new(void)
Allocates a new, empty 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_sort_bang(VALUE ary)
Destructively sorts the passed array in-place, according to each elements' <=> result.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_require_string(VALUE feature)
Finds and loads the given feature, if absent.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
#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.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
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 ...
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define PRI_PIDT_PREFIX
A rb_sprintf() format prefix to be used for a pid_t parameter.
#define RARRAY_LEN
Just another name of rb_array_len.
#define StringValue(v)
Ensures that the parameter object is a String.
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
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 bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.