Ruby: builtin.c Source File

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

builtin.c
1#include "internal.h"
2#include "internal/box.h"
3#include "vm_core.h"
4#include "iseq.h"
5#include "builtin.h"
6
7#include "builtin_binary.rbbin"
8
9#ifndef BUILTIN_BINARY_SIZE
10
11#define BUILTIN_LOADED(feature_name, iseq) ((void)0)
12#include "mini_builtin.c"
13
14#else
15
16static const unsigned char *
17bin4feature(const struct builtin_binary *bb, const char *feature, size_t *psize)
18{
19 *psize = bb->bin_size;
20 return strcmp(bb->feature, feature) ? NULL : bb->bin;
21}
22
23static const unsigned char*
24builtin_lookup(const char *feature, size_t *psize)
25{
26 static size_t index = 0;
27 const unsigned char *bin = NULL;
28
29 /*
30 * Fast path:
31 * builtin_binary is usually arranged in the same order
32 * as features are looked up in miniruby, so try the next entry first.
33 */
34 if (builtin_binary[index].feature) {
35 bin = bin4feature(&builtin_binary[index], feature, psize);
36 index++;
37 }
38 if (bin) {
39 return bin;
40 }
41
42 /*
43 * Fallback:
44 * In case the lookup order does not match the array order,
45 * scan the entire table to find the feature.
46 */
47 for (const struct builtin_binary *bb = &builtin_binary[0];
48 bb->feature;
49 bb++) {
50 bin = bin4feature(bb, feature, psize);
51 if (bin) {
52 break;
53 }
54 }
55
56 return bin;
57}
58
59static void
60load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table, const rb_box_t *target_box)
61{
62 // search binary
63 size_t size;
64 const unsigned char *bin = builtin_lookup(feature_name, &size);
65 if (! bin) {
66 rb_bug("builtin_lookup: can not find %s", feature_name);
67 }
68
69 // load binary
70 rb_vm_t *vm = GET_VM();
71 if (vm->builtin_function_table != NULL) rb_bug("vm->builtin_function_table should be NULL.");
72 vm->builtin_function_table = table;
73 const rb_iseq_t *iseq = rb_iseq_ibf_load_bytes((const char *)bin, size);
74 ASSUME(iseq); // otherwise an exception should have raised
75 vm->builtin_function_table = NULL;
76
77 // exec
78 rb_iseq_eval(rb_iseq_check(iseq), target_box);
79}
80
81void
82rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
83{
84 load_with_builtin_functions(feature_name, table, rb_root_box());
85}
86
87 VALUE
88rb_define_gem_modules(VALUE flags_value, VALUE _)
89{
90 rb_box_gem_flags_t *flags = (rb_box_gem_flags_t *)flags_value;
91
92 if (flags->gem) {
93 rb_define_module("Gem");
94 if (flags->error_highlight) {
95 rb_define_module("ErrorHighlight");
96 }
97 if (flags->did_you_mean) {
98 rb_define_module("DidYouMean");
99 }
100 if (flags->syntax_suggest) {
101 rb_define_module("SyntaxSuggest");
102 }
103 }
104
105 return Qnil;
106}
107
108void
109rb_load_gem_prelude(VALUE box)
110{
111 load_with_builtin_functions("gem_prelude", NULL, (const rb_box_t *)box);
112}
113
114#endif
115
116void
117rb_free_loaded_builtin_table(void)
118{
119 // do nothing
120}
121
122void
123Init_builtin(void)
124{
125 // nothing
126}
127
128void
129Init_builtin_features(void)
130{
131
132#ifdef BUILTIN_BINARY_SIZE
133
134 rb_load_gem_prelude((VALUE)rb_root_box());
135
136 rb_load_gem_prelude((VALUE)rb_main_box());
137
138#endif
139
140}
rb_define_module
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1590
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
builtin_binary
Definition builtin.h:121
rb_builtin_function
Definition builtin.h:6
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40