Ruby 4.1.0dev (2026-03-06 revision 9aca729140424bbf465c11ab8ab53e5cc6602c01)
box.c (9aca729140424bbf465c11ab8ab53e5cc6602c01)
1/* indent-tabs-mode: nil */
2
3#include "eval_intern.h"
4#include "internal.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/st.h"
15#include "internal/variable.h"
16#include "iseq.h"
18#include "ruby/util.h"
19#include "vm_core.h"
20#include "darray.h"
21#include "zjit.h"
22
23#include <stdio.h>
24
25#ifdef HAVE_SYS_SENDFILE_H
26# include <sys/sendfile.h>
27#endif
28#ifdef HAVE_COPYFILE_H
29#include <copyfile.h>
30#endif
31
33VALUE rb_cBoxEntry = 0;
34VALUE rb_mBoxLoader = 0;
35
36static rb_box_t root_box[1]; /* Initialize in initialize_root_box() */
37static rb_box_t *main_box;
38static char *tmp_dir;
39static bool tmp_dir_has_dirsep;
40
41#define BOX_TMP_PREFIX "_ruby_box_"
42
43#ifndef MAXPATHLEN
44# define MAXPATHLEN 1024
45#endif
46
47#if defined(_WIN32)
48# define DIRSEP "\\"
49#else
50# define DIRSEP "/"
51#endif
52
53bool ruby_box_enabled = false; // extern
54bool ruby_box_init_done = false; // extern
55bool ruby_box_crashed = false; // extern, changed only in vm.c
56
57VALUE rb_resolve_feature_path(VALUE klass, VALUE fname);
58static VALUE rb_box_inspect(VALUE obj);
59static void cleanup_all_local_extensions(VALUE libmap);
60
61void
62rb_box_init_done(void)
63{
64 ruby_box_init_done = true;
65}
66
67const rb_box_t *
68rb_root_box(void)
69{
70 return root_box;
71}
72
73const rb_box_t *
74rb_main_box(void)
75{
76 return main_box;
77}
78
79const rb_box_t *
80rb_current_box(void)
81{
82 /*
83 * If RUBY_BOX is not set, the root box is the only available one.
84 *
85 * Until the main_box is not initialized, the root box is
86 * the only valid box.
87 * This early return is to avoid accessing EC before its setup.
88 */
89 if (!main_box)
90 return root_box;
91
92 return rb_vm_current_box(GET_EC());
93}
94
95const rb_box_t *
96rb_loading_box(void)
97{
98 if (!main_box)
99 return root_box;
100
101 return rb_vm_loading_box(GET_EC());
102}
103
104const rb_box_t *
105rb_current_box_in_crash_report(void)
106{
107 if (ruby_box_crashed)
108 return NULL;
109 return rb_current_box();
110}
111
112static long box_id_counter = 0;
113
114static long
115box_generate_id(void)
116{
117 long id;
118 RB_VM_LOCKING() {
119 id = ++box_id_counter;
120 }
121 return id;
122}
123
124static VALUE
125box_main_to_s(VALUE obj)
126{
127 return rb_str_new2("main");
128}
129
130static void
131box_entry_initialize(rb_box_t *box)
132{
133 const rb_box_t *root = rb_root_box();
134
135 // These will be updated immediately
136 box->box_object = 0;
137 box->box_id = 0;
138
139 box->top_self = rb_obj_alloc(rb_cObject);
140 rb_define_singleton_method(box->top_self, "to_s", box_main_to_s, 0);
141 rb_define_alias(rb_singleton_class(box->top_self), "inspect", "to_s");
142 box->load_path = rb_ary_dup(root->load_path);
143 box->expanded_load_path = rb_ary_dup(root->expanded_load_path);
144 box->load_path_snapshot = rb_ary_new();
145 box->load_path_check_cache = 0;
146 box->loaded_features = rb_ary_dup(root->loaded_features);
147 box->loaded_features_snapshot = rb_ary_new();
148 box->loaded_features_index = st_init_numtable();
149 box->loaded_features_realpaths = rb_hash_dup(root->loaded_features_realpaths);
150 box->loaded_features_realpath_map = rb_hash_dup(root->loaded_features_realpath_map);
151 box->loading_table = st_init_strtable();
152 box->ruby_dln_libmap = rb_hash_new_with_size(0);
153 box->gvar_tbl = rb_hash_new_with_size(0);
154 box->classext_cow_classes = st_init_numtable();
155
156 box->is_user = true;
157 box->is_optional = true;
158}
159
160void
161rb_box_gc_update_references(void *ptr)
162{
163 rb_box_t *box = (rb_box_t *)ptr;
164 if (!box) return;
165
166 if (box->box_object)
167 box->box_object = rb_gc_location(box->box_object);
168 if (box->top_self)
169 box->top_self = rb_gc_location(box->top_self);
170 box->load_path = rb_gc_location(box->load_path);
171 box->expanded_load_path = rb_gc_location(box->expanded_load_path);
172 box->load_path_snapshot = rb_gc_location(box->load_path_snapshot);
173 if (box->load_path_check_cache) {
174 box->load_path_check_cache = rb_gc_location(box->load_path_check_cache);
175 }
176 box->loaded_features = rb_gc_location(box->loaded_features);
177 box->loaded_features_snapshot = rb_gc_location(box->loaded_features_snapshot);
178 box->loaded_features_realpaths = rb_gc_location(box->loaded_features_realpaths);
179 box->loaded_features_realpath_map = rb_gc_location(box->loaded_features_realpath_map);
180 box->ruby_dln_libmap = rb_gc_location(box->ruby_dln_libmap);
181 box->gvar_tbl = rb_gc_location(box->gvar_tbl);
182}
183
184void
185rb_box_entry_mark(void *ptr)
186{
187 const rb_box_t *box = (rb_box_t *)ptr;
188 if (!box) return;
189
190 rb_gc_mark(box->box_object);
191 rb_gc_mark(box->top_self);
192 rb_gc_mark(box->load_path);
193 rb_gc_mark(box->expanded_load_path);
194 rb_gc_mark(box->load_path_snapshot);
195 rb_gc_mark(box->load_path_check_cache);
196 rb_gc_mark(box->loaded_features);
197 rb_gc_mark(box->loaded_features_snapshot);
198 rb_gc_mark(box->loaded_features_realpaths);
199 rb_gc_mark(box->loaded_features_realpath_map);
200 if (box->loading_table) {
201 rb_mark_tbl(box->loading_table);
202 }
203 rb_gc_mark(box->ruby_dln_libmap);
204 rb_gc_mark(box->gvar_tbl);
205 if (box->classext_cow_classes) {
206 rb_mark_tbl(box->classext_cow_classes);
207 }
208}
209
210static int
211free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
212{
213 xfree((char *)key);
214 return ST_DELETE;
215}
216
217static int
218free_loaded_feature_index_i(st_data_t key, st_data_t value, st_data_t arg)
219{
220 if (!FIXNUM_P(value)) {
221 rb_darray_free_sized((void *)value, long);
222 }
223 return ST_CONTINUE;
224}
225
226static void
227box_root_free(void *ptr)
228{
229 rb_box_t *box = (rb_box_t *)ptr;
230 if (box->loading_table) {
231 st_foreach(box->loading_table, free_loading_table_entry, 0);
232 st_free_table(box->loading_table);
233 box->loading_table = 0;
234 }
235
236 if (box->loaded_features_index) {
237 st_foreach(box->loaded_features_index, free_loaded_feature_index_i, 0);
238 st_free_table(box->loaded_features_index);
239 }
240}
241
242static int
243free_classext_for_box(st_data_t _key, st_data_t obj_value, st_data_t box_arg)
244{
245 rb_classext_t *ext;
246 VALUE obj = (VALUE)obj_value;
247 const rb_box_t *box = (const rb_box_t *)box_arg;
248
249 if (RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE)) {
250 ext = rb_class_unlink_classext(obj, box);
251 rb_class_classext_free(obj, ext, false);
252 }
253 else if (RB_TYPE_P(obj, T_ICLASS)) {
254 ext = rb_class_unlink_classext(obj, box);
255 rb_iclass_classext_free(obj, ext, false);
256 }
257 else {
258 rb_bug("Invalid type of object in classext_cow_classes: %s", rb_type_str(BUILTIN_TYPE(obj)));
259 }
260 return ST_CONTINUE;
261}
262
263static void
264box_entry_free(void *ptr)
265{
266 const rb_box_t *box = (const rb_box_t *)ptr;
267
268 if (box->classext_cow_classes) {
269 st_foreach(box->classext_cow_classes, free_classext_for_box, (st_data_t)box);
270 }
271
272 cleanup_all_local_extensions(box->ruby_dln_libmap);
273
274 box_root_free(ptr);
275 SIZED_FREE(box);
276}
277
278static size_t
279box_entry_memsize(const void *ptr)
280{
281 size_t size = sizeof(rb_box_t);
282 const rb_box_t *box = (const rb_box_t *)ptr;
283 if (box->loaded_features_index) {
284 size += rb_st_memsize(box->loaded_features_index);
285 }
286 if (box->loading_table) {
287 size += rb_st_memsize(box->loading_table);
288 }
289 return size;
290}
291
292static const rb_data_type_t rb_box_data_type = {
293 "Ruby::Box::Entry",
294 {
295 rb_box_entry_mark,
296 box_entry_free,
297 box_entry_memsize,
298 rb_box_gc_update_references,
299 },
300 0, 0, RUBY_TYPED_FREE_IMMEDIATELY // TODO: enable RUBY_TYPED_WB_PROTECTED when inserting write barriers
301};
302
303static const rb_data_type_t rb_root_box_data_type = {
304 "Ruby::Box::Root",
305 {
306 rb_box_entry_mark,
307 box_root_free,
308 box_entry_memsize,
309 rb_box_gc_update_references,
310 },
311 &rb_box_data_type, 0, RUBY_TYPED_FREE_IMMEDIATELY // TODO: enable RUBY_TYPED_WB_PROTECTED when inserting write barriers
312};
313
314VALUE
315rb_box_entry_alloc(VALUE klass)
316{
317 rb_box_t *entry;
318 VALUE obj = TypedData_Make_Struct(klass, rb_box_t, &rb_box_data_type, entry);
319 box_entry_initialize(entry);
320 return obj;
321}
322
323static rb_box_t *
324get_box_struct_internal(VALUE entry)
325{
326 rb_box_t *sval;
327 TypedData_Get_Struct(entry, rb_box_t, &rb_box_data_type, sval);
328 return sval;
329}
330
331rb_box_t *
332rb_get_box_t(VALUE box)
333{
334 VALUE entry;
335 ID id_box_entry;
336
337 VM_ASSERT(box);
338
339 if (NIL_P(box))
340 return root_box;
341
342 VM_ASSERT(BOX_OBJ_P(box));
343
344 CONST_ID(id_box_entry, "__box_entry__");
345 entry = rb_attr_get(box, id_box_entry);
346 return get_box_struct_internal(entry);
347}
348
349VALUE
350rb_get_box_object(rb_box_t *box)
351{
352 VM_ASSERT(box && box->box_object);
353 return box->box_object;
354}
355
356/*
357 * call-seq:
358 * Ruby::Box.new -> new_box
359 *
360 * Returns a new Ruby::Box object.
361 */
362static VALUE
363box_initialize(VALUE box_value)
364{
365 rb_box_t *box;
366 rb_classext_t *object_classext;
367 VALUE entry;
368 ID id_box_entry;
369 CONST_ID(id_box_entry, "__box_entry__");
370
371 if (!rb_box_available()) {
372 rb_raise(rb_eRuntimeError, "Ruby Box is disabled. Set RUBY_BOX=1 environment variable to use Ruby::Box.");
373 }
374
375 entry = rb_class_new_instance_pass_kw(0, NULL, rb_cBoxEntry);
376 box = get_box_struct_internal(entry);
377
378 box->box_object = box_value;
379 box->box_id = box_generate_id();
380 rb_define_singleton_method(box->load_path, "resolve_feature_path", rb_resolve_feature_path, 1);
381
382 // Set the Ruby::Box object unique/consistent from any boxes to have just single
383 // constant table from any view of every (including main) box.
384 // If a code in the box adds a constant, the constant will be visible even from root/main.
385 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(box_value, true);
386
387 // Get a clean constant table of Object even by writable one
388 // because ns was just created, so it has not touched any constants yet.
389 object_classext = RCLASS_EXT_WRITABLE_IN_BOX(rb_cObject, box);
390 RCLASS_SET_CONST_TBL(box_value, RCLASSEXT_CONST_TBL(object_classext), true);
391
392 rb_ivar_set(box_value, id_box_entry, entry);
393
394 // Invalidate ZJIT code that assumes only the root box is active
395 rb_zjit_invalidate_root_box();
396
397 return box_value;
398}
399
400/*
401 * call-seq:
402 * Ruby::Box.enabled? -> true or false
403 *
404 * Returns +true+ if Ruby::Box is enabled.
405 */
406static VALUE
407rb_box_s_getenabled(VALUE recv)
408{
409 return RBOOL(rb_box_available());
410}
411
412/*
413 * call-seq:
414 * Ruby::Box.current -> box, nil or false
415 *
416 * Returns the current box.
417 * Returns +nil+ if Ruby Box is not enabled.
418 */
419static VALUE
420rb_box_s_current(VALUE recv)
421{
422 const rb_box_t *box;
423
424 if (!rb_box_available())
425 return Qnil;
426
427 box = rb_vm_current_box(GET_EC());
428 VM_ASSERT(box && box->box_object);
429 return box->box_object;
430}
431
432/*
433 * call-seq:
434 * load_path -> array
435 *
436 * Returns box local load path.
437 */
438static VALUE
439rb_box_load_path(VALUE box)
440{
441 VM_ASSERT(BOX_OBJ_P(box));
442 return rb_get_box_t(box)->load_path;
443}
444
445#ifdef _WIN32
446UINT rb_w32_system_tmpdir(WCHAR *path, UINT len);
447#endif
448
449/* Copied from mjit.c Ruby 3.0.3 */
450static char *
451system_default_tmpdir(void)
452{
453 // c.f. ext/etc/etc.c:etc_systmpdir()
454#ifdef _WIN32
455 WCHAR tmppath[_MAX_PATH];
456 UINT len = rb_w32_system_tmpdir(tmppath, numberof(tmppath));
457 if (len) {
458 int blen = WideCharToMultiByte(CP_UTF8, 0, tmppath, len, NULL, 0, NULL, NULL);
459 char *tmpdir = xmalloc(blen + 1);
460 WideCharToMultiByte(CP_UTF8, 0, tmppath, len, tmpdir, blen, NULL, NULL);
461 tmpdir[blen] = '\0';
462 return tmpdir;
463 }
464#elif defined _CS_DARWIN_USER_TEMP_DIR
465 char path[MAXPATHLEN];
466 size_t len = confstr(_CS_DARWIN_USER_TEMP_DIR, path, sizeof(path));
467 if (len > 0) {
468 char *tmpdir = xmalloc(len);
469 if (len > sizeof(path)) {
470 confstr(_CS_DARWIN_USER_TEMP_DIR, tmpdir, len);
471 }
472 else {
473 memcpy(tmpdir, path, len);
474 }
475 return tmpdir;
476 }
477#endif
478 return 0;
479}
480
481static int
482check_tmpdir(const char *dir)
483{
484 struct stat st;
485
486 if (!dir) return FALSE;
487 if (stat(dir, &st)) return FALSE;
488#ifndef S_ISDIR
489# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
490#endif
491 if (!S_ISDIR(st.st_mode)) return FALSE;
492#ifndef _WIN32
493# ifndef S_IWOTH
494# define S_IWOTH 002
495# endif
496 if (st.st_mode & S_IWOTH) {
497# ifdef S_ISVTX
498 if (!(st.st_mode & S_ISVTX)) return FALSE;
499# else
500 return FALSE;
501# endif
502 }
503 if (access(dir, W_OK)) return FALSE;
504#endif
505 return TRUE;
506}
507
508static char *
509system_tmpdir(void)
510{
511 char *tmpdir;
512# define RETURN_ENV(name) \
513 if (check_tmpdir(tmpdir = getenv(name))) return ruby_strdup(tmpdir)
514 RETURN_ENV("TMPDIR");
515 RETURN_ENV("TMP");
516 tmpdir = system_default_tmpdir();
517 if (check_tmpdir(tmpdir)) return tmpdir;
518 return ruby_strdup("/tmp");
519# undef RETURN_ENV
520}
521
522/* end of copy */
523
524static int
525sprint_ext_filename(char *str, size_t size, long box_id, const char *prefix, const char *basename)
526{
527 if (tmp_dir_has_dirsep) {
528 return snprintf(str, size, "%s%sp%"PRI_PIDT_PREFIX"u_%ld_%s", tmp_dir, prefix, getpid(), box_id, basename);
529 }
530 return snprintf(str, size, "%s%s%sp%"PRI_PIDT_PREFIX"u_%ld_%s", tmp_dir, DIRSEP, prefix, getpid(), box_id, basename);
531}
532
533enum copy_error_type {
534 COPY_ERROR_NONE,
535 COPY_ERROR_SRC_OPEN,
536 COPY_ERROR_DST_OPEN,
537 COPY_ERROR_SRC_READ,
538 COPY_ERROR_DST_WRITE,
539 COPY_ERROR_SRC_STAT,
540 COPY_ERROR_DST_CHMOD,
541 COPY_ERROR_SYSERR
542};
543
544static const char *
545copy_ext_file_error(char *message, size_t size, int copy_retvalue)
546{
547#ifdef _WIN32
548 int error = GetLastError();
549 char *p = message;
550 size_t len = snprintf(message, size, "%d: ", error);
551
552#define format_message(sublang) FormatMessage(\
553 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
554 NULL, error, MAKELANGID(LANG_NEUTRAL, (sublang)), \
555 message + len, size - len, NULL)
556 if (format_message(SUBLANG_ENGLISH_US) == 0)
557 format_message(SUBLANG_DEFAULT);
558 for (p = message + len; *p; p++) {
559 if (*p == '\n' || *p == '\r')
560 *p = ' ';
561 }
562#else
563 switch (copy_retvalue) {
564 case COPY_ERROR_SRC_OPEN:
565 strlcpy(message, "can't open the extension path", size);
566 break;
567 case COPY_ERROR_DST_OPEN:
568 strlcpy(message, "can't open the file to write", size);
569 break;
570 case COPY_ERROR_SRC_READ:
571 strlcpy(message, "failed to read the extension path", size);
572 break;
573 case COPY_ERROR_DST_WRITE:
574 strlcpy(message, "failed to write the extension path", size);
575 break;
576 case COPY_ERROR_SRC_STAT:
577 strlcpy(message, "failed to stat the extension path to copy permissions", size);
578 break;
579 case COPY_ERROR_DST_CHMOD:
580 strlcpy(message, "failed to set permissions to the copied extension path", size);
581 break;
582 case COPY_ERROR_SYSERR:
583 strlcpy(message, strerror(errno), size);
584 break;
585 case COPY_ERROR_NONE: /* shouldn't be called */
586 default:
587 rb_bug("unknown return value of copy_ext_file: %d", copy_retvalue);
588 }
589#endif
590 return message;
591}
592
593#ifndef _WIN32
594static enum copy_error_type
595copy_stream(int src_fd, int dst_fd)
596{
597 char buffer[1024];
598 ssize_t rsize;
599
600 while ((rsize = read(src_fd, buffer, sizeof(buffer))) != 0) {
601 if (rsize < 0) return COPY_ERROR_SRC_READ;
602 for (size_t written = 0; written < (size_t)rsize;) {
603 ssize_t wsize = write(dst_fd, buffer+written, rsize-written);
604 if (wsize < 0) return COPY_ERROR_DST_WRITE;
605 written += (size_t)wsize;
606 }
607 }
608 return COPY_ERROR_NONE;
609}
610#endif
611
612static enum copy_error_type
613copy_ext_file(const char *src_path, const char *dst_path)
614{
615#if defined(_WIN32)
616 WCHAR *w_src = rb_w32_mbstr_to_wstr(CP_UTF8, src_path, -1, NULL);
617 WCHAR *w_dst = rb_w32_mbstr_to_wstr(CP_UTF8, dst_path, -1, NULL);
618 if (!w_src || !w_dst) {
619 free(w_src);
620 free(w_dst);
621 rb_memerror();
622 }
623
624 enum copy_error_type rvalue = CopyFileW(w_src, w_dst, TRUE) ?
625 COPY_ERROR_NONE : COPY_ERROR_SYSERR;
626 free(w_src);
627 free(w_dst);
628 return rvalue;
629#else
630# ifdef O_BINARY
631 const int bin = O_BINARY;
632# else
633 const int bin = 0;
634# endif
635# ifdef O_CLOEXEC
636 const int cloexec = O_CLOEXEC;
637# else
638 const int cloexec = 0;
639# endif
640 const int src_fd = open(src_path, O_RDONLY|cloexec|bin);
641 if (src_fd < 0) return COPY_ERROR_SRC_OPEN;
642 if (!cloexec) rb_maygvl_fd_fix_cloexec(src_fd);
643
644 struct stat src_st;
645 if (fstat(src_fd, &src_st)) {
646 close(src_fd);
647 return COPY_ERROR_SRC_STAT;
648 }
649
650 const int dst_fd = open(dst_path, O_WRONLY|O_CREAT|O_EXCL|cloexec|bin, S_IRWXU);
651 if (dst_fd < 0) {
652 close(src_fd);
653 return COPY_ERROR_DST_OPEN;
654 }
655 if (!cloexec) rb_maygvl_fd_fix_cloexec(dst_fd);
656
657 enum copy_error_type ret = COPY_ERROR_NONE;
658
659 if (fchmod(dst_fd, src_st.st_mode & 0777)) {
660 ret = COPY_ERROR_DST_CHMOD;
661 goto done;
662 }
663
664 const size_t count_max = (SIZE_MAX >> 1) + 1;
665 (void)count_max;
666
667# ifdef HAVE_COPY_FILE_RANGE
668 for (;;) {
669 ssize_t written = copy_file_range(src_fd, NULL, dst_fd, NULL, count_max, 0);
670 if (written == 0) goto done;
671 if (written < 0) break;
672 }
673# endif
674# ifdef HAVE_FCOPYFILE
675 if (fcopyfile(src_fd, dst_fd, NULL, COPYFILE_DATA) == 0) {
676 goto done;
677 }
678# endif
679# ifdef USE_SENDFILE
680 for (;;) {
681 ssize_t written = sendfile(src_fd, dst_fd, NULL count_max);
682 if (written == 0) goto done;
683 if (written < 0) break;
684 }
685# endif
686 ret = copy_stream(src_fd, dst_fd);
687
688 done:
689 close(src_fd);
690 if (dst_fd >= 0) close(dst_fd);
691 if (ret != COPY_ERROR_NONE) unlink(dst_path);
692 return ret;
693#endif
694}
695
696#if defined __CYGWIN__ || defined DOSISH
697#define isdirsep(x) ((x) == '/' || (x) == '\\')
698#else
699#define isdirsep(x) ((x) == '/')
700#endif
701
702#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
703#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
704
705static void
706fname_without_suffix(const char *fname, char *rvalue, size_t rsize)
707{
708 size_t len = strlen(fname);
709 const char *pos;
710 for (pos = fname + len; pos > fname; pos--) {
711 if (IS_SOEXT(pos) || IS_DLEXT(pos)) {
712 len = pos - fname;
713 break;
714 }
715 if (fname + len - pos > DLEXT_MAXLEN) break;
716 }
717 if (len > rsize - 1) len = rsize - 1;
718 memcpy(rvalue, fname, len);
719 rvalue[len] = '\0';
720}
721
722static void
723escaped_basename(const char *path, const char *fname, char *rvalue, size_t rsize)
724{
725 char *pos;
726 const char *leaf = path, *found;
727 // `leaf + 1` looks uncomfortable (when leaf == path), but fname must not be the top-dir itself
728 while ((found = strstr(leaf + 1, fname)) != NULL) {
729 leaf = found; // find the last occurrence for the path like /etc/my-crazy-lib-dir/etc.so
730 }
731 strlcpy(rvalue, leaf, rsize);
732 for (pos = rvalue; *pos; pos++) {
733 if (isdirsep(*pos)) {
734 *pos = '+';
735 }
736 }
737}
738
739static void
740box_ext_cleanup_mark(void *p)
741{
742 rb_gc_mark((VALUE)p);
743}
744
745static void
746box_ext_cleanup_free(void *p)
747{
748 VALUE path = (VALUE)p;
749 unlink(RSTRING_PTR(path));
750}
751
752static const rb_data_type_t box_ext_cleanup_type = {
753 "box_ext_cleanup",
754 {box_ext_cleanup_mark, box_ext_cleanup_free},
756};
757
758void
759rb_box_cleanup_local_extension(VALUE cleanup)
760{
761 void *p = DATA_PTR(cleanup);
762 DATA_PTR(cleanup) = NULL;
763#ifndef _WIN32
764 if (p) box_ext_cleanup_free(p);
765#endif
766 (void)p;
767}
768
769static int
770cleanup_local_extension_i(VALUE key, VALUE value, VALUE arg)
771{
772#if defined(_WIN32)
773 HMODULE h = (HMODULE)NUM2PTR(value);
774 WCHAR module_path[MAXPATHLEN];
775 DWORD len = GetModuleFileNameW(h, module_path, numberof(module_path));
776
777 FreeLibrary(h);
778 if (len > 0 && len < numberof(module_path)) DeleteFileW(module_path);
779#endif
780 return ST_DELETE;
781}
782
783static void
784cleanup_all_local_extensions(VALUE libmap)
785{
786 rb_hash_foreach(libmap, cleanup_local_extension_i, 0);
787}
788
789VALUE
790rb_box_local_extension(VALUE box_value, VALUE fname, VALUE path, VALUE *cleanup)
791{
792 char ext_path[MAXPATHLEN], fname2[MAXPATHLEN], basename[MAXPATHLEN];
793 int wrote;
794 const char *src_path = RSTRING_PTR(path), *fname_ptr = RSTRING_PTR(fname);
795 rb_box_t *box = rb_get_box_t(box_value);
796
797 fname_without_suffix(fname_ptr, fname2, sizeof(fname2));
798 escaped_basename(src_path, fname2, basename, sizeof(basename));
799
800 wrote = sprint_ext_filename(ext_path, sizeof(ext_path), box->box_id, BOX_TMP_PREFIX, basename);
801 if (wrote >= (int)sizeof(ext_path)) {
802 rb_bug("Extension file path in the box was too long");
803 }
804 VALUE new_path = rb_str_new_cstr(ext_path);
805 *cleanup = TypedData_Wrap_Struct(0, &box_ext_cleanup_type, NULL);
806 enum copy_error_type copy_error = copy_ext_file(src_path, ext_path);
807 if (copy_error) {
808 char message[1024];
809 copy_ext_file_error(message, sizeof(message), copy_error);
810 rb_raise(rb_eLoadError, "can't prepare the extension file for Ruby Box (%s from %"PRIsVALUE"): %s", ext_path, path, message);
811 }
812 DATA_PTR(*cleanup) = (void *)new_path;
813 return new_path;
814}
815
816static VALUE
817rb_box_load(int argc, VALUE *argv, VALUE box)
818{
819 VALUE fname, wrap;
820 rb_scan_args(argc, argv, "11", &fname, &wrap);
821
822 rb_vm_frame_flag_set_box_require(GET_EC());
823
824 VALUE args = rb_ary_new_from_args(2, fname, wrap);
825 return rb_load_entrypoint(args);
826}
827
828static VALUE
829rb_box_require(VALUE box, VALUE fname)
830{
831 rb_vm_frame_flag_set_box_require(GET_EC());
832
833 return rb_require_string(fname);
834}
835
836static VALUE
837rb_box_require_relative(VALUE box, VALUE fname)
838{
839 rb_vm_frame_flag_set_box_require(GET_EC());
840
841 return rb_require_relative_entrypoint(fname);
842}
843
844static void
845initialize_root_box(void)
846{
847 rb_vm_t *vm = GET_VM();
848 rb_box_t *root = (rb_box_t *)rb_root_box();
849
850 root->load_path = rb_ary_new();
851 root->expanded_load_path = rb_ary_hidden_new(0);
852 root->load_path_snapshot = rb_ary_hidden_new(0);
853 root->load_path_check_cache = 0;
854 rb_define_singleton_method(root->load_path, "resolve_feature_path", rb_resolve_feature_path, 1);
855
856 root->loaded_features = rb_ary_new();
857 root->loaded_features_snapshot = rb_ary_hidden_new(0);
858 root->loaded_features_index = st_init_numtable();
859 root->loaded_features_realpaths = rb_hash_new();
860 rb_obj_hide(root->loaded_features_realpaths);
861 root->loaded_features_realpath_map = rb_hash_new();
862 rb_obj_hide(root->loaded_features_realpath_map);
863
864 root->ruby_dln_libmap = rb_hash_new_with_size(0);
865 root->gvar_tbl = rb_hash_new_with_size(0);
866 root->classext_cow_classes = NULL; // classext CoW never happen on the root box
867
868 vm->root_box = root;
869
870 if (rb_box_available()) {
871 VALUE root_box, entry;
872 ID id_box_entry;
873 CONST_ID(id_box_entry, "__box_entry__");
874
875 root_box = rb_obj_alloc(rb_cBox);
876 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(root_box, true);
877 RCLASS_SET_CONST_TBL(root_box, RCLASSEXT_CONST_TBL(RCLASS_EXT_PRIME(rb_cObject)), true);
878
879 root->box_id = box_generate_id();
880 root->box_object = root_box;
881
882 entry = TypedData_Wrap_Struct(rb_cBoxEntry, &rb_root_box_data_type, root);
883 rb_ivar_set(root_box, id_box_entry, entry);
884 }
885 else {
886 root->box_id = 1;
887 root->box_object = Qnil;
888 }
889}
890
891static VALUE
892rb_box_eval(VALUE box_value, VALUE str)
893{
894 const rb_iseq_t *iseq;
895 const rb_box_t *box;
896
897 StringValue(str);
898
899 iseq = rb_iseq_compile_iseq(str, rb_str_new_cstr("eval"));
900 VM_ASSERT(iseq);
901
902 box = (const rb_box_t *)rb_get_box_t(box_value);
903
904 return rb_iseq_eval(iseq, box);
905}
906
907static int box_experimental_warned = 0;
908
909RUBY_EXTERN const char ruby_api_version_name[];
910
911void
912rb_initialize_main_box(void)
913{
914 rb_box_t *box;
915 VALUE main_box_value;
916 rb_vm_t *vm = GET_VM();
917
918 VM_ASSERT(rb_box_available());
919
920 if (!box_experimental_warned) {
922 "Ruby::Box is experimental, and the behavior may change in the future!\n"
923 "See https://docs.ruby-lang.org/en/%s/Ruby/Box.html for known issues, etc.",
924 ruby_api_version_name);
925 box_experimental_warned = 1;
926 }
927
928 main_box_value = rb_class_new_instance(0, NULL, rb_cBox);
929 VM_ASSERT(BOX_OBJ_P(main_box_value));
930 box = rb_get_box_t(main_box_value);
931 box->box_object = main_box_value;
932 box->is_user = true;
933 box->is_optional = false;
934
935 rb_const_set(rb_cBox, rb_intern("MAIN"), main_box_value);
936
937 vm->main_box = main_box = box;
938
939 // create the writable classext of ::Object explicitly to finalize the set of visible top-level constants
940 RCLASS_EXT_WRITABLE_IN_BOX(rb_cObject, box);
941}
942
943static VALUE
944rb_box_inspect(VALUE obj)
945{
946 rb_box_t *box;
947 VALUE r;
948 if (obj == Qfalse) {
949 r = rb_str_new_cstr("#<Ruby::Box:root>");
950 return r;
951 }
952 box = rb_get_box_t(obj);
953 r = rb_str_new_cstr("#<Ruby::Box:");
954 rb_str_concat(r, rb_funcall(LONG2NUM(box->box_id), rb_intern("to_s"), 0));
955 if (BOX_ROOT_P(box)) {
956 rb_str_cat_cstr(r, ",root");
957 }
958 if (BOX_USER_P(box)) {
959 rb_str_cat_cstr(r, ",user");
960 }
961 if (BOX_MAIN_P(box)) {
962 rb_str_cat_cstr(r, ",main");
963 }
964 else if (BOX_OPTIONAL_P(box)) {
965 rb_str_cat_cstr(r, ",optional");
966 }
967 rb_str_cat_cstr(r, ">");
968 return r;
969}
970
971static VALUE
972rb_box_loading_func(int argc, VALUE *argv, VALUE _self)
973{
974 rb_vm_frame_flag_set_box_require(GET_EC());
975 return rb_call_super(argc, argv);
976}
977
978static void
979box_define_loader_method(const char *name)
980{
981 rb_define_private_method(rb_mBoxLoader, name, rb_box_loading_func, -1);
982 rb_define_singleton_method(rb_mBoxLoader, name, rb_box_loading_func, -1);
983}
984
985void
986Init_root_box(void)
987{
988 root_box->loading_table = st_init_strtable();
989}
990
991void
992Init_enable_box(void)
993{
994 const char *env = getenv("RUBY_BOX");
995 if (env && strlen(env) == 1 && env[0] == '1') {
996 ruby_box_enabled = true;
997 }
998 else {
999 ruby_box_init_done = true;
1000 }
1001}
1002
1003/* :nodoc: */
1004static VALUE
1005rb_box_s_root(VALUE recv)
1006{
1007 return root_box->box_object;
1008}
1009
1010/* :nodoc: */
1011static VALUE
1012rb_box_s_main(VALUE recv)
1013{
1014 return main_box->box_object;
1015}
1016
1017/* :nodoc: */
1018static VALUE
1019rb_box_root_p(VALUE box_value)
1020{
1021 const rb_box_t *box = (const rb_box_t *)rb_get_box_t(box_value);
1022 return RBOOL(BOX_ROOT_P(box));
1023}
1024
1025/* :nodoc: */
1026static VALUE
1027rb_box_main_p(VALUE box_value)
1028{
1029 const rb_box_t *box = (const rb_box_t *)rb_get_box_t(box_value);
1030 return RBOOL(BOX_MAIN_P(box));
1031}
1032
1033#if RUBY_DEBUG
1034
1035static const char *
1036classname(VALUE klass)
1037{
1038 VALUE p;
1039 if (!klass) {
1040 return "Qfalse";
1041 }
1042 p = RCLASSEXT_CLASSPATH(RCLASS_EXT_PRIME(klass));
1043 if (RTEST(p))
1044 return RSTRING_PTR(p);
1045 if (RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS))
1046 return "AnyClassValue";
1047 return "NonClassValue";
1048}
1049
1050static enum rb_id_table_iterator_result
1051dump_classext_methods_i(ID mid, VALUE _val, void *data)
1052{
1053 VALUE ary = (VALUE)data;
1054 rb_ary_push(ary, rb_id2str(mid));
1055 return ID_TABLE_CONTINUE;
1056}
1057
1058static enum rb_id_table_iterator_result
1059dump_classext_constants_i(ID mid, VALUE _val, void *data)
1060{
1061 VALUE ary = (VALUE)data;
1062 rb_ary_push(ary, rb_id2str(mid));
1063 return ID_TABLE_CONTINUE;
1064}
1065
1066static void
1067dump_classext_i(rb_classext_t *ext, bool is_prime, VALUE _recv, void *data)
1068{
1069 char buf[4096];
1070 struct rb_id_table *tbl;
1071 VALUE ary, res = (VALUE)data;
1072
1073 snprintf(buf, 4096, "Ruby::Box %ld:%s classext %p\n",
1074 RCLASSEXT_BOX(ext)->box_id, is_prime ? " prime" : "", (void *)ext);
1075 rb_str_cat_cstr(res, buf);
1076
1077 snprintf(buf, 2048, " Super: %s\n", classname(RCLASSEXT_SUPER(ext)));
1078 rb_str_cat_cstr(res, buf);
1079
1080 tbl = RCLASSEXT_M_TBL(ext);
1081 if (tbl) {
1082 ary = rb_ary_new_capa((long)rb_id_table_size(tbl));
1083 rb_id_table_foreach(RCLASSEXT_M_TBL(ext), dump_classext_methods_i, (void *)ary);
1084 rb_ary_sort_bang(ary);
1085 snprintf(buf, 4096, " Methods(%ld): ", RARRAY_LEN(ary));
1086 rb_str_cat_cstr(res, buf);
1087 rb_str_concat(res, rb_ary_join(ary, rb_str_new_cstr(",")));
1088 rb_str_cat_cstr(res, "\n");
1089 }
1090 else {
1091 rb_str_cat_cstr(res, " Methods(0): .\n");
1092 }
1093
1094 tbl = RCLASSEXT_CONST_TBL(ext);
1095 if (tbl) {
1096 ary = rb_ary_new_capa((long)rb_id_table_size(tbl));
1097 rb_id_table_foreach(tbl, dump_classext_constants_i, (void *)ary);
1098 rb_ary_sort_bang(ary);
1099 snprintf(buf, 4096, " Constants(%ld): ", RARRAY_LEN(ary));
1100 rb_str_cat_cstr(res, buf);
1101 rb_str_concat(res, rb_ary_join(ary, rb_str_new_cstr(",")));
1102 rb_str_cat_cstr(res, "\n");
1103 }
1104 else {
1105 rb_str_cat_cstr(res, " Constants(0): .\n");
1106 }
1107}
1108
1109/* :nodoc: */
1110static VALUE
1111rb_f_dump_classext(VALUE recv, VALUE klass)
1112{
1113 /*
1114 * The desired output String value is:
1115 * Class: 0x88800932 (String) [singleton]
1116 * Prime classext box(2,main), readable(t), writable(f)
1117 * Non-prime classexts: 3
1118 * Box 2: prime classext 0x88800933
1119 * Super: Object
1120 * Methods(43): aaaaa, bbbb, cccc, dddd, eeeee, ffff, gggg, hhhhh, ...
1121 * Constants(12): FOO, Bar, ...
1122 * Box 5: classext 0x88800934
1123 * Super: Object
1124 * Methods(43): aaaaa, bbbb, cccc, dddd, eeeee, ffff, gggg, hhhhh, ...
1125 * Constants(12): FOO, Bar, ...
1126 */
1127 char buf[2048];
1128 VALUE res;
1129 const rb_classext_t *ext;
1130 const rb_box_t *box;
1131 st_table *classext_tbl;
1132
1133 if (!(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE))) {
1134 snprintf(buf, 2048, "Non-class/module value: %p (%s)\n", (void *)klass, rb_type_str(BUILTIN_TYPE(klass)));
1135 return rb_str_new_cstr(buf);
1136 }
1137
1138 if (RB_TYPE_P(klass, T_CLASS)) {
1139 snprintf(buf, 2048, "Class: %p (%s)%s\n",
1140 (void *)klass, classname(klass), RCLASS_SINGLETON_P(klass) ? " [singleton]" : "");
1141 }
1142 else {
1143 snprintf(buf, 2048, "Module: %p (%s)\n", (void *)klass, classname(klass));
1144 }
1145 res = rb_str_new_cstr(buf);
1146
1147 ext = RCLASS_EXT_PRIME(klass);
1148 box = RCLASSEXT_BOX(ext);
1149 snprintf(buf, 2048, "Prime classext box(%ld,%s), readable(%s), writable(%s)\n",
1150 box->box_id,
1151 BOX_ROOT_P(box) ? "root" : (BOX_MAIN_P(box) ? "main" : "optional"),
1152 RCLASS_PRIME_CLASSEXT_READABLE_P(klass) ? "t" : "f",
1153 RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass) ? "t" : "f");
1154 rb_str_cat_cstr(res, buf);
1155
1156 classext_tbl = RCLASS_CLASSEXT_TBL(klass);
1157 if (!classext_tbl) {
1158 rb_str_cat_cstr(res, "Non-prime classexts: 0\n");
1159 }
1160 else {
1161 snprintf(buf, 2048, "Non-prime classexts: %zu\n", st_table_size(classext_tbl));
1162 rb_str_cat_cstr(res, buf);
1163 }
1164
1165 rb_class_classext_foreach(klass, dump_classext_i, (void *)res);
1166
1167 return res;
1168}
1169
1170#endif /* RUBY_DEBUG */
1171
1172/*
1173 * Document-class: Ruby::Box
1174 *
1175 * :markup: markdown
1176 * :include: doc/language/box.md
1177 */
1178void
1179Init_Box(void)
1180{
1181 tmp_dir = system_tmpdir();
1182 tmp_dir_has_dirsep = (strcmp(tmp_dir + (strlen(tmp_dir) - strlen(DIRSEP)), DIRSEP) == 0);
1183
1184 VALUE mRuby = rb_define_module("Ruby");
1185
1186 rb_cBox = rb_define_class_under(mRuby, "Box", rb_cModule);
1187 rb_define_method(rb_cBox, "initialize", box_initialize, 0);
1188
1189 /* :nodoc: */
1190 rb_cBoxEntry = rb_define_class_under(rb_cBox, "Entry", rb_cObject);
1191 rb_define_alloc_func(rb_cBoxEntry, rb_box_entry_alloc);
1192
1193 initialize_root_box();
1194
1195 /* :nodoc: */
1196 rb_mBoxLoader = rb_define_module_under(rb_cBox, "Loader");
1197 box_define_loader_method("require");
1198 box_define_loader_method("require_relative");
1199 box_define_loader_method("load");
1200
1201 if (rb_box_available()) {
1202 rb_include_module(rb_cObject, rb_mBoxLoader);
1203
1204 rb_define_singleton_method(rb_cBox, "root", rb_box_s_root, 0);
1205 rb_define_singleton_method(rb_cBox, "main", rb_box_s_main, 0);
1206 rb_define_method(rb_cBox, "root?", rb_box_root_p, 0);
1207 rb_define_method(rb_cBox, "main?", rb_box_main_p, 0);
1208
1209#if RUBY_DEBUG
1210 rb_define_global_function("dump_classext", rb_f_dump_classext, 1);
1211#endif
1212 }
1213
1214 rb_define_singleton_method(rb_cBox, "enabled?", rb_box_s_getenabled, 0);
1215 rb_define_singleton_method(rb_cBox, "current", rb_box_s_current, 0);
1216
1217 rb_define_method(rb_cBox, "load_path", rb_box_load_path, 0);
1218 rb_define_method(rb_cBox, "load", rb_box_load, -1);
1219 rb_define_method(rb_cBox, "require", rb_box_require, 1);
1220 rb_define_method(rb_cBox, "require_relative", rb_box_require_relative, 1);
1221 rb_define_method(rb_cBox, "eval", rb_box_eval, 1);
1222
1223 rb_define_method(rb_cBox, "inspect", rb_box_inspect, 0);
1224}
#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.
Definition dllexport.h:45
Ruby-level global variables / constants, visible from C.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1803
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2922
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1627
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1709
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1732
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2965
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.
Definition class.c:3255
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#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.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
VALUE rb_eLoadError
LoadError exception.
Definition error.c:1436
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1416
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
Definition error.h:51
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2285
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2326
VALUE rb_cBox
Ruby::Box class.
Definition box.c:32
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:100
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 ...
Definition object.c:2303
VALUE rb_cModule
Module class.
Definition object.c:62
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Definition vm_eval.c:362
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long 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_require_string(VALUE feature)
Finds and loads the given feature, if absent.
Definition load.c:1435
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:4055
#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.
Definition string.h:1657
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
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.
Definition variable.c:2024
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
Definition variable.c:3933
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.
Definition io.h:8
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
Definition util.c:515
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define PRI_PIDT_PREFIX
A rb_sprintf() format prefix to be used for a pid_t parameter.
Definition pid_t.h:38
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:119
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:736
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:514
#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...
Definition rtypeddata.h:561
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define RTEST
This is an old name of RB_TEST.
Internal header for Ruby Box.
Definition box.h:14
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:211
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376