Ruby  3.1.4p223 (2023-03-30 revision HEAD)
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
12 static struct st_table *loaded_builtin_table;
13 #endif
14 
15 rb_ast_t *rb_builtin_ast(const char *feature_name, VALUE *name_str);
16 
17 static const rb_iseq_t *
18 builtin_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, INT2FIX(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);
52 #endif
53 
54  return iseq;
55 }
56 
57 void
58 rb_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 
66 static int
67 each_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 
77 static VALUE
78 each_builtin(VALUE self)
79 {
80  st_foreach(loaded_builtin_table, each_builtin_i, 0);
81  return Qnil;
82 }
83 
84 void
85 Init_builtin(void)
86 {
87  rb_define_singleton_method(rb_cRubyVM, "each_builtin", each_builtin, 0);
88  loaded_builtin_table = st_init_strtable();
89 }
90 
91 void
92 Init_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.
Definition: cxxanyargs.hpp:685
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition: string.h:1738
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define Qnil
Old name of RUBY_Qnil.
VALUE rb_stdout
STDOUT constant.
Definition: io.c:198
void rb_gc_register_mark_object(VALUE object)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition: gc.c:8687
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_io_write(VALUE io, VALUE str)
Writes the given string to the given IO.
Definition: io.c:2063
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:1369
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
Definition: cxxanyargs.hpp:432
Definition: st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40