Ruby: mini_builtin.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

mini_builtin.c
Go to the documentation of this file.
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
15 rb_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 const rb_iseq_t *iseq = rb_iseq_new(&ast->body, name_str, name_str, Qnil, NULL, ISEQ_TYPE_TOP);
27 GET_VM()->builtin_function_table = NULL;
28
29 rb_ast_dispose(ast);
30
31 // for debug
32 if (0 && strcmp("prelude", feature_name) == 0) {
33 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
34 }
35
36#ifndef INCLUDED_BY_BUILTIN_C
37 st_insert(loaded_builtin_table, (st_data_t)feature_name, (st_data_t)iseq);
38 rb_gc_register_mark_object((VALUE)iseq);
39#endif
40
41 return iseq;
42}
43
44void
45rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
46{
47 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
48 rb_iseq_eval(iseq);
49}
50
51#ifndef INCLUDED_BY_BUILTIN_C
52
53static int
54each_builtin_i(st_data_t key, st_data_t val, st_data_t dmy)
55{
56 const char *feature = (const char *)key;
57 const rb_iseq_t *iseq = (const rb_iseq_t *)val;
58
59 rb_yield_values(2, rb_str_new2(feature), rb_iseqw_new(iseq));
60
61 return ST_CONTINUE;
62}
63
64static VALUE
65each_builtin(VALUE self)
66{
67 st_foreach(loaded_builtin_table, each_builtin_i, 0);
68 return Qnil;
69}
70
71void
72Init_builtin(void)
73{
74 rb_define_singleton_method(rb_cRubyVM, "each_builtin", each_builtin, 0);
75 loaded_builtin_table = st_init_strtable();
76}
77
78void
79Init_builtin_features(void)
80{
81 // register for ruby
82 builtin_iseq_load("gem_prelude", NULL);
83}
84#endif
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:668
rb_gc_register_mark_object
void rb_gc_register_mark_object(VALUE obj)
Inform the garbage collector that object is a live Ruby object that should not be moved.
Definition: gc.c:8022
rb_stdout
VALUE rb_stdout
Definition: globals.h:118
rb_iseqw_new
VALUE rb_iseqw_new(const rb_iseq_t *)
Definition: iseq.c:1217
builtin.h
Defines RBIMPL_HAS_BUILTIN.
rb_io_write
VALUE rb_io_write(VALUE, VALUE)
Definition: io.c:1953
rb_str_new2
#define rb_str_new2
Definition: string.h:276
array.h
Internal header for Array.
internal.h
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:809
rb_iseq_disasm
VALUE rb_iseq_disasm(const rb_iseq_t *iseq)
Definition: iseq.c:2335
iseq.h
rb_yield_values
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:1353
Init_builtin_features
void Init_builtin_features(void)
Definition: mini_builtin.c:79
rb_builtin_ast
rb_ast_t * rb_builtin_ast(const char *feature_name, VALUE *name_str)
Init_builtin
void Init_builtin(void)
Definition: mini_builtin.c:72
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:45
rb_ast_dispose
void rb_ast_dispose(rb_ast_t *ast)
Definition: node.c:1448
key
key
Definition: openssl_missing.h:185
NULL
#define NULL
Definition: regenc.h:69
Qnil
#define Qnil
Definition: special_consts.h:51
ST_CONTINUE
@ ST_CONTINUE
Definition: st.h:99
st_data_t
unsigned long st_data_t
Definition: st.h:22
st_foreach
#define st_foreach
Definition: st.h:142
st_insert
#define st_insert
Definition: st.h:124
st_init_strtable
#define st_init_strtable
Definition: st.h:110
rb_ast_struct
Definition: node.h:403
rb_ast_struct::body
rb_ast_body_t body
Definition: node.h:406
rb_builtin_function
Definition: builtin.h:6
rb_iseq_struct
Definition: vm_core.h:444
rb_vm_struct
Definition: vm_core.h:564
rb_vm_struct::builtin_inline_index
int builtin_inline_index
Definition: vm_core.h:657
rb_vm_struct::builtin_function_table
const struct rb_builtin_function * builtin_function_table
Definition: vm_core.h:656
st_table
Definition: st.h:79
VALUE
unsigned long VALUE
Definition: value.h:38
rb_iseq_eval
VALUE rb_iseq_eval(const rb_iseq_t *iseq)
Definition: vm.c:2403
rb_cRubyVM
VALUE rb_cRubyVM
Definition: vm.c:373
vm_core.h