Ruby: builtin.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

builtin.c
Go to the documentation of this file.
1#include "internal.h"
2#include "vm_core.h"
3#include "iseq.h"
4#include "builtin.h"
5
6#if 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}
Init_builtin_features
void Init_builtin_features(void)
Definition: builtin.c:66
Init_builtin
void Init_builtin(void)
Definition: builtin.c:60
rb_load_with_builtin_functions
void rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
Definition: builtin.c:36
rb_iseq_ibf_load_bytes
const rb_iseq_t * rb_iseq_ibf_load_bytes(const char *bytes, size_t size)
Definition: compile.c:12150
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:768
builtin.h
Defines RBIMPL_HAS_BUILTIN.
internal.h
size
voidpf void uLong size
Definition: ioapi.h:138
iseq.h
mini_builtin.c
NULL
#define NULL
Definition: regenc.h:69
bin
Definition: gzjoin.c:78
builtin_binary
Definition: builtin.h:77
builtin_binary::bin
const unsigned char * bin
Definition: builtin.h:79
builtin_binary::feature
const char * feature
Definition: builtin.h:78
builtin_binary::bin_size
size_t bin_size
Definition: builtin.h:80
rb_builtin_function
Definition: builtin.h:6
rb_iseq_struct
Definition: vm_core.h:444
rb_vm_struct
Definition: vm_core.h:564
rb_vm_struct::builtin_inline_index
int builtin_inline_index
Definition: vm_core.h:657
rb_vm_struct::builtin_function_table
const struct rb_builtin_function * builtin_function_table
Definition: vm_core.h:656
rb_iseq_eval
VALUE rb_iseq_eval(const rb_iseq_t *iseq)
Definition: vm.c:2403
vm_core.h