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