Ruby: builtin.c Source File

Ruby 3.4.9p82 (2026-03-11 revision 76cca827ab52ab1d346a728f068d5b8da3e2952b)

builtin.c
1#include "internal.h"
2#include "vm_core.h"
3#include "iseq.h"
4#include "builtin.h"
5
6#include "builtin_binary.inc"
7
8#ifndef BUILTIN_BINARY_SIZE
9
10#define BUILTIN_LOADED(feature_name, iseq) ((void)0)
11#include "mini_builtin.c"
12
13#else
14
15static const unsigned char *
16bin4feature(const struct builtin_binary *bb, const char *feature, size_t *psize)
17{
18 *psize = bb->bin_size;
19 return strcmp(bb->feature, feature) ? NULL : bb->bin;
20}
21
22static const unsigned char*
23builtin_lookup(const char *feature, size_t *psize)
24{
25 static int index = 0;
26 const unsigned char *bin = bin4feature(&builtin_binary[index++], feature, psize);
27
28 // usually, `builtin_binary` order is loading order at miniruby.
29 for (const struct builtin_binary *bb = &builtin_binary[0]; bb->feature &&! bin; bb++) {
30 bin = bin4feature(bb++, feature, psize);
31 }
32 return bin;
33}
34
35void
36rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
37{
38 // search binary
39 size_t size;
40 const unsigned char *bin = builtin_lookup(feature_name, &size);
41 if (! bin) {
42 rb_bug("builtin_lookup: can not find %s", feature_name);
43 }
44
45 // load binary
46 rb_vm_t *vm = GET_VM();
47 if (vm->builtin_function_table != NULL) rb_bug("vm->builtin_function_table should be NULL.");
48 vm->builtin_function_table = table;
49 const rb_iseq_t *iseq = rb_iseq_ibf_load_bytes((const char *)bin, size);
50 ASSUME(iseq); // otherwise an exception should have raised
51 vm->builtin_function_table = NULL;
52
53 // exec
54 rb_iseq_eval(rb_iseq_check(iseq));
55}
56
57#endif
58
59void
60rb_free_loaded_builtin_table(void)
61{
62 // do nothing
63}
64
65void
66Init_builtin(void)
67{
68 // nothing
69}
70
71void
72Init_builtin_features(void)
73{
74 rb_load_with_builtin_functions("gem_prelude", NULL);
75}
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
builtin_binary
Definition builtin.h:119
rb_builtin_function
Definition builtin.h:6