Ruby: mini_builtin.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

mini_builtin.c
1#include "internal.h"
2#include "internal/array.h"
3#include "internal/eval.h"
4#include "iseq.h"
5#include "vm_core.h"
6#include "builtin.h"
7
8#include "miniprelude.c"
9
10static VALUE
11prelude_ast_value(VALUE name, VALUE code, int line)
12{
13 rb_ast_t *ast;
14 VALUE ast_value = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
15 ast = rb_ruby_ast_data_get(ast_value);
16 if (!ast || !ast->body.root) {
17 if (ast) rb_ast_dispose(ast);
18 rb_exc_raise(rb_errinfo());
19 }
20 return ast_value;
21}
22
23static void
24pm_prelude_load(pm_parse_result_t *result, VALUE name, VALUE code, int line)
25{
26 pm_options_line_set(&result->options, line);
27 VALUE error = pm_parse_string(result, code, name, NULL);
28
29 if (!NIL_P(error)) {
30 pm_parse_result_free(result);
31 rb_exc_raise(error);
32 }
33}
34
35static const rb_iseq_t *
36builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
37{
38 VALUE name_str = 0;
39 int start_line;
40 const rb_iseq_t *iseq;
41 VALUE code = rb_builtin_find(feature_name, &name_str, &start_line);
42 if (NIL_P(code)) {
43 rb_fatal("builtin_iseq_load: can not find %s; "
44 "probably miniprelude.c is out of date",
45 feature_name);
46 }
47
48 rb_vm_t *vm = GET_VM();
49 static const rb_compile_option_t optimization = {
50 .inline_const_cache = TRUE,
51 .peephole_optimization = TRUE,
52 .tailcall_optimization = FALSE,
53 .specialized_instruction = TRUE,
54 .operands_unification = TRUE,
55 .instructions_unification = TRUE,
56 .frozen_string_literal = TRUE,
57 .debug_frozen_string_literal = FALSE,
58 .coverage_enabled = FALSE,
59 .debug_level = 0,
60 };
61
62 if (rb_ruby_prism_p()) {
63 pm_parse_result_t result = { 0 };
64 pm_prelude_load(&result, name_str, code, start_line);
65
66 vm->builtin_function_table = table;
67 int error_state;
68 iseq = pm_iseq_new_with_opt(&result.node, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, &error_state);
69
70 vm->builtin_function_table = NULL;
71 pm_parse_result_free(&result);
72
73 if (error_state) {
74 RUBY_ASSERT(iseq == NULL);
75 rb_jump_tag(error_state);
76 }
77 }
78 else {
79 VALUE ast_value = prelude_ast_value(name_str, code, start_line);
80 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
81
82 vm->builtin_function_table = table;
83 iseq = rb_iseq_new_with_opt(ast_value, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, Qnil);
84
85 vm->builtin_function_table = NULL;
86 rb_ast_dispose(ast);
87 }
88
89 // for debug
90 if (0 && strcmp("prelude", feature_name) == 0) {
91 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
92 }
93
94 BUILTIN_LOADED(feature_name, iseq);
95
96 return iseq;
97}
98
99void
100rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
101{
102 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
103 rb_iseq_eval(iseq, rb_root_box());
104}
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:653
rb_errinfo
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
Definition eval.c:2045
rb_stdout
VALUE rb_stdout
STDOUT constant.
Definition io.c:201
pm_parse_result_t
Definition prism_compile.h:69
pm_parse_result_t::node
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
Definition prism_compile.h:80
pm_parse_result_t::options
pm_options_t options
The options that will be passed to the parser.
Definition prism_compile.h:74
rb_builtin_function
Definition builtin.h:6
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40