Ruby 4.1.0dev (2026-10-28 revision 1b06b950bb460d20a9dcecb8b97d363adbf4a358)
mini_builtin.c (1b06b950bb460d20a9dcecb8b97d363adbf4a358)
1#include "internal.h"
2#include "internal/array.h"
3#include "internal/eval.h"
4#include "iseq.h"
5#include "prism_compile.h"
6#include "vm_core.h"
7#include "builtin.h"
8
9#include "miniprelude.c"
10
11static VALUE
12prelude_ast_value(VALUE name, VALUE code, int line)
13{
14 rb_ast_t *ast;
15 VALUE ast_value = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
16 ast = rb_ruby_ast_data_get(ast_value);
17 if (!ast || !ast->body.root) {
18 if (ast) rb_ast_dispose(ast);
19 rb_exc_raise(rb_errinfo());
20 }
21 return ast_value;
22}
23
24static void
25pm_prelude_load(pm_parse_result_t *result, VALUE name, VALUE code, int line)
26{
27 pm_parse_result_init(result);
28 pm_options_line_set(result->options, line);
29 VALUE error = pm_parse_string(result, code, name, NULL);
30
31 if (!NIL_P(error)) {
32 pm_parse_result_free(result);
33 rb_exc_raise(error);
34 }
35}
36
37static const rb_iseq_t *
38builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
39{
40 VALUE name_str = 0;
41 int start_line;
42 const rb_iseq_t *iseq;
43 VALUE code = rb_builtin_find(feature_name, &name_str, &start_line);
44 if (NIL_P(code)) {
45 rb_fatal("builtin_iseq_load: can not find %s; "
46 "probably miniprelude.c is out of date",
47 feature_name);
48 }
49
50 rb_vm_t *vm = GET_VM();
51 static const rb_compile_option_t optimization = {
52 .inline_const_cache = TRUE,
53 .peephole_optimization = TRUE,
54 .tailcall_optimization = FALSE,
55 .specialized_instruction = TRUE,
56 .operands_unification = TRUE,
57 .instructions_unification = TRUE,
58 .frozen_string_literal = TRUE,
59 .debug_frozen_string_literal = FALSE,
60 .coverage_enabled = FALSE,
61 .debug_level = 0,
62 };
63
64 if (rb_ruby_prism_p()) {
65 pm_parse_result_t result;
66 pm_prelude_load(&result, name_str, code, start_line);
67
68 vm->builtin_function_table = table;
69 int error_state;
70 iseq = pm_iseq_new_with_opt(&result.node, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, &error_state);
71
72 vm->builtin_function_table = NULL;
73 pm_parse_result_free(&result);
74
75 if (error_state) {
76 RUBY_ASSERT(iseq == NULL);
77 rb_jump_tag(error_state);
78 }
79 }
80 else {
81 VALUE ast_value = prelude_ast_value(name_str, code, start_line);
82 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
83
84 vm->builtin_function_table = table;
85 iseq = rb_iseq_new_with_opt(ast_value, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, Qnil);
86
87 vm->builtin_function_table = NULL;
88 rb_ast_dispose(ast);
89 }
90
91 // for debug
92 if (0 && strcmp("prelude", feature_name) == 0) {
93 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
94 }
95
96 BUILTIN_LOADED(feature_name, iseq);
97
98 return iseq;
99}
100
101void
102rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
103{
104 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
105 rb_iseq_eval(iseq, rb_root_box());
106}
107
108VALUE
109rb_define_gem_modules(VALUE _a, VALUE _b)
110{
111 // do nothing - moniruby doesn't load gem_prelude.rb.
112 return Qnil;
113}
114
115void
116rb_load_gem_prelude(VALUE _)
117{
118 // do nothing - miniruby doesn't support loading RubyGems.
119}
#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:678
VALUE rb_stdout
STDOUT constant.
Definition io.c:194
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