Ruby: builtin.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

builtin.c
1#include "internal.h"
2#include "vm_core.h"
3#include "iseq.h"
4#include "builtin.h"
5
6#ifdef CROSS_COMPILING
7
8#define INCLUDED_BY_BUILTIN_C 1
9#include "mini_builtin.c"
10
11#else
12
13#include "builtin_binary.inc"
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\n", 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 vm->builtin_function_table = NULL;
52
53 // exec
54 rb_iseq_eval(rb_iseq_check(iseq));
55}
56
57#endif
58
59void
60Init_builtin(void)
61{
62 // nothing
63}
64
65void
66Init_builtin_features(void)
67{
68 rb_load_with_builtin_functions("gem_prelude", NULL);
69}
rb_bug
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition error.c:794
builtin_binary
Definition builtin.h:115
rb_builtin_function
Definition builtin.h:6
rb_iseq_struct
Definition vm_core.h:526
rb_vm_struct
Definition vm_core.h:615