Ruby: mini_builtin.c Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

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 if (!ast) {
25 rb_fatal("builtin_iseq_load: can not find %s; "
26 "probably miniprelude.c is out of date",
27 feature_name);
28 }
29 vm->builtin_function_table = table;
30 vm->builtin_inline_index = 0;
31 static const rb_compile_option_t optimization = {
32 TRUE, /* unsigned int inline_const_cache; */
33 TRUE, /* unsigned int peephole_optimization; */
34 FALSE,/* unsigned int tailcall_optimization; */
35 TRUE, /* unsigned int specialized_instruction; */
36 TRUE, /* unsigned int operands_unification; */
37 TRUE, /* unsigned int instructions_unification; */
38 TRUE, /* unsigned int frozen_string_literal; */
39 FALSE, /* unsigned int debug_frozen_string_literal; */
40 FALSE, /* unsigned int coverage_enabled; */
41 0, /* int debug_level; */
42 };
43 const rb_iseq_t *iseq = rb_iseq_new_with_opt(&ast->body, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization);
44 GET_VM()->builtin_function_table = NULL;
45
46 rb_ast_dispose(ast);
47
48 // for debug
49 if (0 && strcmp("prelude", feature_name) == 0) {
50 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
51 }
52
53#ifndef INCLUDED_BY_BUILTIN_C
54 st_insert(loaded_builtin_table, (st_data_t)feature_name, (st_data_t)iseq);
55 rb_gc_register_mark_object((VALUE)iseq);
56#endif
57
58 return iseq;
59}
60
61void
62rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
63{
64 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
65 rb_iseq_eval(iseq);
66}
67
68#ifndef INCLUDED_BY_BUILTIN_C
69
70static int
71each_builtin_i(st_data_t key, st_data_t val, st_data_t dmy)
72{
73 const char *feature = (const char *)key;
74 const rb_iseq_t *iseq = (const rb_iseq_t *)val;
75
76 rb_yield_values(2, rb_str_new2(feature), rb_iseqw_new(iseq));
77
78 return ST_CONTINUE;
79}
80
81/* :nodoc: */
82static VALUE
83each_builtin(VALUE self)
84{
85 st_foreach(loaded_builtin_table, each_builtin_i, 0);
86 return Qnil;
87}
88
89void
90Init_builtin(void)
91{
92 rb_define_singleton_method(rb_cRubyVM, "each_builtin", each_builtin, 0);
93 loaded_builtin_table = st_init_strtable();
94}
95
96void
97Init_builtin_features(void)
98{
99 // register for ruby
100 builtin_iseq_load("gem_prelude", NULL);
101}
102#endif
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
rb_stdout
VALUE rb_stdout
STDOUT constant.
Definition io.c:190
rb_yield_values
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1388
rb_builtin_function
Definition builtin.h:6
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40