Ruby 4.1.0dev (2026-09-07 revision b57404b461ba8bf34e802d86b0db78388216e182)
mini_builtin.c (b57404b461ba8bf34e802d86b0db78388216e182)
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_parse_result_init(result);
27 pm_options_line_set(result->options, line);
28 VALUE error = pm_parse_string(result, code, name, NULL);
29
30 if (!NIL_P(error)) {
31 pm_parse_result_free(result);
32 rb_exc_raise(error);
33 }
34}
35
36static const rb_iseq_t *
37builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
38{
39 VALUE name_str = 0;
40 int start_line;
41 const rb_iseq_t *iseq;
42 VALUE code = rb_builtin_find(feature_name, &name_str, &start_line);
43 if (NIL_P(code)) {
44 rb_fatal("builtin_iseq_load: can not find %s; "
45 "probably miniprelude.c is out of date",
46 feature_name);
47 }
48
49 rb_vm_t *vm = GET_VM();
50 static const rb_compile_option_t optimization = {
51 .inline_const_cache = TRUE,
52 .peephole_optimization = TRUE,
53 .tailcall_optimization = FALSE,
54 .specialized_instruction = TRUE,
55 .operands_unification = TRUE,
56 .instructions_unification = TRUE,
57 .frozen_string_literal = TRUE,
58 .debug_frozen_string_literal = FALSE,
59 .coverage_enabled = FALSE,
60 .debug_level = 0,
61 };
62
63 if (rb_ruby_prism_p()) {
64 pm_parse_result_t result;
65 pm_prelude_load(&result, name_str, code, start_line);
66
67 vm->builtin_function_table = table;
68 int error_state;
69 iseq = pm_iseq_new_with_opt(&result.node, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, &error_state);
70
71 vm->builtin_function_table = NULL;
72 pm_parse_result_free(&result);
73
74 if (error_state) {
75 RUBY_ASSERT(iseq == NULL);
76 rb_jump_tag(error_state);
77 }
78 }
79 else {
80 VALUE ast_value = prelude_ast_value(name_str, code, start_line);
81 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
82
83 vm->builtin_function_table = table;
84 iseq = rb_iseq_new_with_opt(ast_value, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, Qnil);
85
86 vm->builtin_function_table = NULL;
87 rb_ast_dispose(ast);
88 }
89
90 // for debug
91 if (0 && strcmp("prelude", feature_name) == 0) {
92 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
93 }
94
95 BUILTIN_LOADED(feature_name, iseq);
96
97 return iseq;
98}
99
100void
101rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
102{
103 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
104 rb_iseq_eval(iseq, rb_root_box());
105}
106
107VALUE
108rb_define_gem_modules(VALUE _a, VALUE _b)
109{
110 // do nothing - moniruby doesn't load gem_prelude.rb.
111 return Qnil;
112}
113
114void
115rb_load_gem_prelude(VALUE _)
116{
117 // do nothing - miniruby doesn't support loading RubyGems.
118}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define Qnil
Old name of RUBY_Qnil.
#define NIL_P
Old name of RB_NIL_P.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:676
VALUE rb_stdout
STDOUT constant.
Definition io.c:203
Defines RBIMPL_HAS_BUILTIN.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_options_t * options
The options that will be passed to the parser.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40