* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / mini_builtin.c
blob8c8cf66263e59beeae616d579eecc07e4810907c
1 #include "internal.h"
2 #include "internal/array.h"
3 #include "iseq.h"
4 #include "vm_core.h"
5 #include "builtin.h"
7 #include "miniprelude.c"
9 // included from miniinit.c
11 #ifndef INCLUDED_BY_BUILTIN_C
12 static struct st_table *loaded_builtin_table;
13 #endif
15 rb_ast_t *rb_builtin_ast(const char *feature_name, VALUE *name_str);
17 static const rb_iseq_t *
18 builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
20 VALUE name_str = 0;
21 rb_ast_t *ast = rb_builtin_ast(feature_name, &name_str);
22 rb_vm_t *vm = GET_VM();
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; */
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;
42 rb_ast_dispose(ast);
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));
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
54 return iseq;
57 void
58 rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
60 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
61 rb_iseq_eval(iseq);
64 #ifndef INCLUDED_BY_BUILTIN_C
66 static int
67 each_builtin_i(st_data_t key, st_data_t val, st_data_t dmy)
69 const char *feature = (const char *)key;
70 const rb_iseq_t *iseq = (const rb_iseq_t *)val;
72 rb_yield_values(2, rb_str_new2(feature), rb_iseqw_new(iseq));
74 return ST_CONTINUE;
77 static VALUE
78 each_builtin(VALUE self)
80 st_foreach(loaded_builtin_table, each_builtin_i, 0);
81 return Qnil;
84 void
85 Init_builtin(void)
87 rb_define_singleton_method(rb_cRubyVM, "each_builtin", each_builtin, 0);
88 loaded_builtin_table = st_init_strtable();
91 void
92 Init_builtin_features(void)
94 // register for ruby
95 builtin_iseq_load("gem_prelude", NULL);
97 #endif