summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2024-09-08 21:06:54 +0900
committerNobuyoshi Nakada <[email protected]>2024-09-08 23:16:46 +0900
commit70871fa6e3172d10d615174e9aa909d3e08687f0 (patch)
tree9037ab957a5fb8fd8c514a0b6644c303a0b8233a
parentbe84abffbaf315c7534ec4c76edc7b30c3ec8111 (diff)
Extract `rb_builtin_find`
Refactor out the same code from `rb_builtin_ast_value` and `pm_builtin_ast_value.
-rw-r--r--mini_builtin.c51
-rw-r--r--template/prelude.c.tmpl62
2 files changed, 43 insertions, 70 deletions
diff --git a/mini_builtin.c b/mini_builtin.c
index e6dfce6e79..b9be7c58f8 100644
--- a/mini_builtin.c
+++ b/mini_builtin.c
@@ -6,15 +6,43 @@
#include "miniprelude.c"
+static VALUE
+prelude_ast_value(VALUE name, VALUE code, int line)
+{
+ rb_ast_t *ast;
+ VALUE ast_value = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
+ ast = rb_ruby_ast_data_get(ast_value);
+ if (!ast || !ast->body.root) {
+ if (ast) rb_ast_dispose(ast);
+ rb_exc_raise(rb_errinfo());
+ }
+ return ast_value;
+}
+
+static void
+pm_prelude_load(pm_parse_result_t *result, VALUE name, VALUE code, int line)
+{
+ pm_options_line_set(&result->options, line);
+ VALUE error = pm_parse_string(result, code, name, NULL);
-bool pm_builtin_ast_value(pm_parse_result_t *result, const char *feature_name, VALUE *name_str);
-VALUE rb_builtin_ast_value(const char *feature_name, VALUE *name_str);
+ if (!NIL_P(error)) {
+ pm_parse_result_free(result);
+ rb_exc_raise(error);
+ }
+}
static const rb_iseq_t *
builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *table)
{
VALUE name_str = 0;
+ int start_line;
const rb_iseq_t *iseq;
+ VALUE code = rb_builtin_find(feature_name, &name_str, &start_line);
+ if (NIL_P(code)) {
+ rb_fatal("builtin_iseq_load: can not find %s; "
+ "probably miniprelude.c is out of date",
+ feature_name);
+ }
rb_vm_t *vm = GET_VM();
static const rb_compile_option_t optimization = {
@@ -32,33 +60,22 @@ builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *ta
if (*rb_ruby_prism_ptr()) {
pm_parse_result_t result = { 0 };
- if (!pm_builtin_ast_value(&result, feature_name, &name_str)) {
- rb_fatal("builtin_iseq_load: can not find %s; "
- "probably miniprelude.c is out of date",
- feature_name);
- }
+ pm_prelude_load(&result, name_str, code, start_line);
vm->builtin_function_table = table;
iseq = pm_iseq_new_with_opt(&result.node, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization);
- GET_VM()->builtin_function_table = NULL;
+ vm->builtin_function_table = NULL;
pm_parse_result_free(&result);
}
else {
- VALUE ast_value = rb_builtin_ast_value(feature_name, &name_str);
-
- if (NIL_P(ast_value)) {
- rb_fatal("builtin_iseq_load: can not find %s; "
- "probably miniprelude.c is out of date",
- feature_name);
- }
-
+ VALUE ast_value = prelude_ast_value(name_str, code, start_line);
rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
vm->builtin_function_table = table;
iseq = rb_iseq_new_with_opt(ast_value, name_str, name_str, Qnil, 0, NULL, 0, ISEQ_TYPE_TOP, &optimization, Qnil);
- GET_VM()->builtin_function_table = NULL;
+ vm->builtin_function_table = NULL;
rb_ast_dispose(ast);
}
diff --git a/template/prelude.c.tmpl b/template/prelude.c.tmpl
index 125a293423..63659c5a4d 100644
--- a/template/prelude.c.tmpl
+++ b/template/prelude.c.tmpl
@@ -58,7 +58,7 @@ class Prelude
if line.size > LINE_LIMIT
raise "#{filename}:#{lines.size+1}: too long line"
end
- line.sub!(/require(_relative)?\s*\(?\s*(["'])(.*?)(?:\.rb)?\2\)?/) do
+ line.sub!(/require(_relative)?\s*\(?\s*([\"\'])(.*?)(?:\.rb)?\2\)?/) do
orig, rel, path = $&, $2, $3
if rel
path = File.join(File.dirname(filename), path)
@@ -141,73 +141,29 @@ COMPILER_WARNING_POP
#define PRELUDE_NAME(n) rb_usascii_str_new_static(prelude_name##n, sizeof(prelude_name##n)-1)
#define PRELUDE_CODE(n) rb_utf8_str_new_static(prelude_code##n.L0, sizeof(prelude_code##n))
-static VALUE
-prelude_ast_value(VALUE name, VALUE code, int line)
-{
- rb_ast_t *ast;
- VALUE ast_value = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
- ast = rb_ruby_ast_data_get(ast_value);
- if (!ast || !ast->body.root) {
- if (ast) rb_ast_dispose(ast);
- rb_exc_raise(rb_errinfo());
- }
- return ast_value;
-}
-
-static void
-pm_prelude_load(pm_parse_result_t *result, VALUE name, VALUE code, int line)
-{
- pm_options_line_set(&result->options, line);
- VALUE error = pm_parse_string(result, code, name, NULL);
-
- if (!NIL_P(error)) {
- pm_parse_result_free(result);
- rb_exc_raise(error);
- }
-}
-
% end
% if @builtin_count > 0
-#define PRELUDE_VAST(n, name_str, start_line) \
+#define PRELUDE_MATCH(n) \
(((sizeof(prelude_name<%='##'%><%=%>n) - prefix_len - 2) == namelen) && \
- (strncmp(prelude_name<%='##'%><%=%>n + prefix_len, feature_name, namelen) == 0) ? \
- prelude_ast_value((name_str) = PRELUDE_NAME(n), PRELUDE_CODE(n), start_line) : Qnil)
+ (strncmp(prelude_name<%='##'%><%=%>n + prefix_len, feature_name, namelen) == 0))
VALUE
-rb_builtin_ast_value(const char *feature_name, VALUE *name_str)
+rb_builtin_find(const char *feature_name, VALUE *name_str, int *start_line)
{
const size_t prefix_len = rb_strlen_lit("<internal:");
size_t namelen = strlen(feature_name);
- VALUE ast_value = Qnil;
-% @preludes.each_value do |i, prelude, lines, sub, start_line|
-% if sub
- if (!NIL_P(ast_value = PRELUDE_VAST(<%=i%><%=%>, *name_str, <%=start_line%>))) return ast_value;
-% end
-% end
- return ast_value;
-}
-
-bool
-pm_builtin_ast_value(pm_parse_result_t *result, const char *feature_name, VALUE *name_str)
-{
- const size_t prefix_len = rb_strlen_lit("<internal:");
- size_t namelen = strlen(feature_name);
+#define PRELUDE_FOUND(n, l) \
+ (*name_str = PRELUDE_NAME(n), *start_line = (l), PRELUDE_CODE(n))
% @preludes.each_value do |i, prelude, lines, sub, start_line|
% if sub
- if (
- (sizeof(prelude_name<%= i %>) - prefix_len - 2 == namelen) &&
- (strncmp(prelude_name<%= i %> + prefix_len, feature_name, namelen) == 0)
- ) {
- *name_str = PRELUDE_NAME(<%= i %>);
- pm_prelude_load(result, *name_str, PRELUDE_CODE(<%= i %>), <%= start_line %>);
- return true;
- }
+ if (PRELUDE_MATCH(<%=i%>)) return PRELUDE_FOUND(<%=i%>, <%=start_line%>);
% end
% end
+#undef PRELUDE_FOUND
- return false;
+ return Qnil;
}
% end