Ruby 3.5.0dev (2025-01-10 revision 5fab31b15e32622c4b71d1d347a41937e9f9c212)
mini_builtin.c (5fab31b15e32622c4b71d1d347a41937e9f9c212)
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
9static VALUE
10prelude_ast_value(VALUE name, VALUE code, int line)
11{
12 rb_ast_t *ast;
13 VALUE ast_value = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
14 ast = rb_ruby_ast_data_get(ast_value);
15 if (!ast || !ast->body.root) {
16 if (ast) rb_ast_dispose(ast);
17 rb_exc_raise(rb_errinfo());
18 }
19 return ast_value;
20}
21
22static void
23pm_prelude_load(pm_parse_result_t *result, VALUE name, VALUE code, int line)
24{
25 pm_options_line_set(&result->options, line);
26 VALUE error = pm_parse_string(result, code, name, NULL);
27
28 if (!NIL_P(error)) {
29 pm_parse_result_free(result);
30 rb_exc_raise(error);
31 }
32}
33
34static const rb_iseq_t *
35builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
36{
37 VALUE name_str = 0;
38 int start_line;
39 const rb_iseq_t *iseq;
40 VALUE code = rb_builtin_find(feature_name, &name_str, &start_line);
41 if (NIL_P(code)) {
42 rb_fatal("builtin_iseq_load: can not find %s; "
43 "probably miniprelude.c is out of date",
44 feature_name);
45 }
46
47 rb_vm_t *vm = GET_VM();
48 static const rb_compile_option_t optimization = {
49 .inline_const_cache = TRUE,
50 .peephole_optimization = TRUE,
51 .tailcall_optimization = FALSE,
52 .specialized_instruction = TRUE,
53 .operands_unification = TRUE,
54 .instructions_unification = TRUE,
55 .frozen_string_literal = TRUE,
56 .debug_frozen_string_literal = FALSE,
57 .coverage_enabled = FALSE,
58 .debug_level = 0,
59 };
60
61 if (rb_ruby_prism_p()) {
62 pm_parse_result_t result = { 0 };
63 pm_prelude_load(&result, name_str, code, start_line);
64
65 vm->builtin_function_table = table;
66 int error_state;
67 iseq = pm_iseq_new_with_opt(&result.node, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, &error_state);
68
69 vm->builtin_function_table = NULL;
70 pm_parse_result_free(&result);
71
72 if (error_state) {
73 RUBY_ASSERT(iseq == NULL);
74 rb_jump_tag(error_state);
75 }
76 }
77 else {
78 VALUE ast_value = prelude_ast_value(name_str, code, start_line);
79 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
80
81 vm->builtin_function_table = table;
82 iseq = rb_iseq_new_with_opt(ast_value, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, Qnil);
83
84 vm->builtin_function_table = NULL;
85 rb_ast_dispose(ast);
86 }
87
88 // for debug
89 if (0 && strcmp("prelude", feature_name) == 0) {
90 rb_io_write(rb_stdout, rb_iseq_disasm((const rb_iseq_t *)iseq));
91 }
92
93 BUILTIN_LOADED(feature_name, iseq);
94
95 return iseq;
96}
97
98void
99rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin_function *table)
100{
101 const rb_iseq_t *iseq = builtin_iseq_load(feature_name, table);
102 rb_iseq_eval(iseq);
103}
#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.
VALUE rb_stdout
STDOUT constant.
Definition io.c:201
Defines RBIMPL_HAS_BUILTIN.
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