Ruby: box.c Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

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