Ruby: builtin.c Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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 INCLUDED_BY_BUILTIN_C 1
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 vm->builtin_inline_index = 0;
50 const rb_iseq_t *iseq = rb_iseq_ibf_load_bytes((const char *)bin, size);
51 ASSUME(iseq); // otherwise an exception should have raised
52 vm->builtin_function_table = NULL;
53
54 // exec
55 rb_iseq_eval(rb_iseq_check(iseq));
56}
57
58#endif
59
60void
61rb_free_loaded_builtin_table(void)
62{
63 // do nothing
64}
65
66void
67Init_builtin(void)
68{
69 // nothing
70}
71
72void
73Init_builtin_features(void)
74{
75 rb_load_with_builtin_functions("gem_prelude", NULL);
76}
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
builtin_binary
Definition builtin.h:111
rb_builtin_function
Definition builtin.h:6
rb_iseq_struct
Definition vm_core.h:534
rb_vm_struct
Definition vm_core.h:622