Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

mini_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 #include "miniprelude.c"
7
8 // included from miniinit.c
9
10 #ifndef INCLUDED_BY_BUILTIN_C
11 static struct st_table *loaded_builtin_table;
12 #endif
13
14 rb_ast_t *rb_builtin_ast(const char *feature_name, VALUE *name_str);
15
16 static const rb_iseq_t *
17 builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
18 {
19 VALUE name_str = 0;
20 rb_ast_t *ast = rb_builtin_ast(feature_name, &name_str);
21 rb_vm_t *vm = GET_VM();
22
23 vm->builtin_function_table = table;
24 vm->builtin_inline_index = 0;
25 const rb_iseq_t *iseq = rb_iseq_new(&ast->body, name_str, name_str, Qnil, NULL, ISEQ_TYPE_TOP);
26 GET_VM()->builtin_function_table = NULL;
27
28 rb_ast_dispose(ast);
29
30 // for debug
31 if (0 && strcmp("prelude", feature_name) == 0) {
32 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
33 }
34
35 #ifndef INCLUDED_BY_BUILTIN_C
36 st_insert(loaded_builtin_table, (st_data_t)feature_name, (st_data_t)iseq);
37 rb_gc_register_mark_object((VALUE)iseq);
38 #endif
39
40 return iseq;
41 }
42
43 void
44 rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
45 {
46 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
47 rb_iseq_eval(iseq);
48 }
49
50 #ifndef INCLUDED_BY_BUILTIN_C
51
52 static int
53 each_builtin_i(st_data_t key, st_data_t val, st_data_t dmy)
54 {
55 const char *feature = (const char *)key;
56 const rb_iseq_t *iseq = (const rb_iseq_t *)val;
57
58 rb_yield_values(2, rb_str_new2(feature), rb_iseqw_new(iseq));
59
60 return ST_CONTINUE;
61 }
62
63 static VALUE
64 each_builtin(VALUE self)
65 {
66 st_foreach(loaded_builtin_table, each_builtin_i, 0);
67 return Qnil;
68 }
69
70 void
71 Init_builtin(void)
72 {
73 rb_define_singleton_method(rb_cRubyVM, "each_builtin", each_builtin, 0);
74 loaded_builtin_table = st_init_strtable();
75 }
76
77 void
78 Init_builtin_features(void)
79 {
80 // register for ruby
81 builtin_iseq_load("gem_prelude", NULL);
82 }
83 #endif
rb_yield_values
#define rb_yield_values(argc,...)
Definition: rb_mjit_min_header-2.7.0.h:6584
rb_iseq_eval
VALUE rb_iseq_eval(const rb_iseq_t *iseq)
Definition: vm.c:2163
rb_str_new2
#define rb_str_new2
Definition: intern.h:903
rb_ast_struct::body
rb_ast_body_t body
Definition: node.h:402
rb_builtin_function
Definition: builtin.h:6
rb_iseq_struct
Definition: vm_core.h:456
rb_gc_register_mark_object
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:7063
rb_iseq_disasm
VALUE rb_iseq_disasm(const rb_iseq_t *iseq)
Definition: iseq.c:2278
rb_ast_dispose
void rb_ast_dispose(rb_ast_t *ast)
Definition: node.c:1389
VALUE
unsigned long VALUE
Definition: ruby.h:102
GET_VM
#define GET_VM()
Definition: vm_core.h:1764
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1755
rb_load_with_builtin_functions
void rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
Definition: mini_builtin.c:44
rb_stdout
RUBY_EXTERN VALUE rb_stdout
Definition: ruby.h:2090
rb_iseq_new
rb_iseq_t * rb_iseq_new(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum iseq_type type)
Definition: iseq.c:761
NULL
#define NULL
Definition: _sdbm.c:101
st_insert
int st_insert(st_table *tab, st_data_t key, st_data_t value)
Definition: st.c:1171
rb_ast_struct
Definition: node.h:399
rb_cRubyVM
VALUE rb_cRubyVM
Definition: vm.c:365
rb_iseqw_new
VALUE rb_iseqw_new(const rb_iseq_t *)
Definition: iseq.c:1157
iseq.h
st_init_strtable
st_table * st_init_strtable(void)
Definition: st.c:668
Init_builtin_features
void Init_builtin_features(void)
Definition: mini_builtin.c:78
vm_core.h
rb_vm_struct::builtin_inline_index
int builtin_inline_index
Definition: vm_core.h:659
rb_vm_struct::builtin_function_table
const struct rb_builtin_function * builtin_function_table
Definition: vm_core.h:658
Init_builtin
void Init_builtin(void)
Definition: mini_builtin.c:71
key
key
Definition: openssl_missing.h:181
strcmp
int strcmp(const char *, const char *)
st_foreach
int st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
Definition: st.c:1718
internal.h
ST_CONTINUE
@ ST_CONTINUE
Definition: st.h:99
st_data_t
unsigned long st_data_t
Definition: rb_mjit_min_header-2.7.0.h:5363
rb_io_write
VALUE rb_io_write(VALUE, VALUE)
Definition: io.c:1804
rb_vm_struct
Definition: vm_core.h:576
iseq
const rb_iseq_t * iseq
Definition: rb_mjit_min_header-2.7.0.h:13504
builtin.h
Qnil
#define Qnil
Definition: ruby.h:469
st_table
Definition: st.h:79
rb_builtin_ast
rb_ast_t * rb_builtin_ast(const char *feature_name, VALUE *name_str)