Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
mini_builtin.c
1#include "internal.h"
2#include "internal/array.h"
3#include "iseq.h"
4#include "vm_core.h"
5#include "builtin.h"
6
7#include "miniprelude.c"
8
9// included from miniinit.c
10
11#ifndef INCLUDED_BY_BUILTIN_C
12static struct st_table *loaded_builtin_table;
13#endif
14
15rb_ast_t *rb_builtin_ast(const char *feature_name, VALUE *name_str);
16
17static const rb_iseq_t *
18builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
19{
20 VALUE name_str = 0;
21 rb_ast_t *ast = rb_builtin_ast(feature_name, &name_str);
22 rb_vm_t *vm = GET_VM();
23
24 vm->builtin_function_table = table;
25 vm->builtin_inline_index = 0;
26 static const rb_compile_option_t optimization = {
27 TRUE, /* int inline_const_cache; */
28 TRUE, /* int peephole_optimization; */
29 FALSE,/* int tailcall_optimization; */
30 TRUE, /* int specialized_instruction; */
31 TRUE, /* int operands_unification; */
32 TRUE, /* int instructions_unification; */
33 TRUE, /* int stack_caching; */
34 TRUE, /* int frozen_string_literal; */
35 FALSE, /* int debug_frozen_string_literal; */
36 FALSE, /* unsigned int coverage_enabled; */
37 0, /* int debug_level; */
38 };
39 const rb_iseq_t *iseq = rb_iseq_new_with_opt(&ast->body, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization);
40 GET_VM()->builtin_function_table = NULL;
41
42 rb_ast_dispose(ast);
43
44 // for debug
45 if (0 && strcmp("prelude", feature_name) == 0) {
46 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
47 }
48
49#ifndef INCLUDED_BY_BUILTIN_C
50 st_insert(loaded_builtin_table, (st_data_t)feature_name, (st_data_t)iseq);
51 rb_gc_register_mark_object((VALUE)iseq);
52#endif
53
54 return iseq;
55}
56
57void
58rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
59{
60 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
61 rb_iseq_eval(iseq);
62}
63
64#ifndef INCLUDED_BY_BUILTIN_C
65
66static int
67each_builtin_i(st_data_t key, st_data_t val, st_data_t dmy)
68{
69 const char *feature = (const char *)key;
70 const rb_iseq_t *iseq = (const rb_iseq_t *)val;
71
72 rb_yield_values(2, rb_str_new2(feature), rb_iseqw_new(iseq));
73
74 return ST_CONTINUE;
75}
76
77static VALUE
78each_builtin(VALUE self)
79{
80 st_foreach(loaded_builtin_table, each_builtin_i, 0);
81 return Qnil;
82}
83
84void
85Init_builtin(void)
86{
87 rb_define_singleton_method(rb_cRubyVM, "each_builtin", each_builtin, 0);
88 loaded_builtin_table = st_init_strtable();
89}
90
91void
92Init_builtin_features(void)
93{
94 // register for ruby
95 builtin_iseq_load("gem_prelude", NULL);
96}
97#endif
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
#define Qnil
Old name of RUBY_Qnil.
VALUE rb_stdout
STDOUT constant.
Definition io.c:194
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_io_write(VALUE io, VALUE str)
Writes the given string to the given IO.
Definition io.c:2264
Definition st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40