diff --git a/common.mk b/common.mk index 4f3c0a9a08..ebbaf1aa6c 100644 --- a/common.mk +++ b/common.mk @@ -19522,6 +19522,7 @@ version.$(OBJEXT): $(top_srcdir)/internal/cmdlineopt.h version.$(OBJEXT): $(top_srcdir)/internal/compilers.h version.$(OBJEXT): $(top_srcdir)/internal/gc.h version.$(OBJEXT): $(top_srcdir)/internal/imemo.h +version.$(OBJEXT): $(top_srcdir)/internal/parse.h version.$(OBJEXT): $(top_srcdir)/internal/sanitizers.h version.$(OBJEXT): $(top_srcdir)/internal/serial.h version.$(OBJEXT): $(top_srcdir)/internal/static_assert.h diff --git a/internal/parse.h b/internal/parse.h index e0c81f4f96..de42acbed9 100644 --- a/internal/parse.h +++ b/internal/parse.h @@ -13,11 +13,15 @@ #include "internal/static_assert.h" // The default parser to use for Ruby code. -// 0: parse.y -// 1: Prism -#ifndef RB_DEFAULT_PARSER -#define RB_DEFAULT_PARSER 1 -#endif +typedef enum { + RB_DEFAULT_PARSER_PARSE_Y, + RB_DEFAULT_PARSER_PRISM, +} ruby_default_parser_enum; + +ruby_default_parser_enum rb_ruby_default_parser(void); +void rb_ruby_default_parser_set(ruby_default_parser_enum parser); + +#define rb_ruby_prism_p() (rb_ruby_default_parser() == RB_DEFAULT_PARSER_PRISM) #ifdef UNIVERSAL_PARSER #define rb_encoding const void diff --git a/iseq.c b/iseq.c index 2c02043d55..d267c7e08b 100644 --- a/iseq.c +++ b/iseq.c @@ -1564,7 +1564,7 @@ iseqw_s_compile_parser(int argc, VALUE *argv, VALUE self, bool prism) static VALUE iseqw_s_compile(int argc, VALUE *argv, VALUE self) { - return iseqw_s_compile_parser(argc, argv, self, *rb_ruby_prism_ptr()); + return iseqw_s_compile_parser(argc, argv, self, rb_ruby_prism_p()); } /* diff --git a/load.c b/load.c index c6fb43e9e8..c1862c38fa 100644 --- a/load.c +++ b/load.c @@ -743,7 +743,7 @@ load_iseq_eval(rb_execution_context_t *ec, VALUE fname) rb_thread_t *th = rb_ec_thread_ptr(ec); VALUE realpath_map = get_loaded_features_realpath_map(th->vm); - if (*rb_ruby_prism_ptr()) { + if (rb_ruby_prism_p()) { pm_parse_result_t result = { 0 }; result.options.line = 1; result.node.coverage_enabled = 1; diff --git a/mini_builtin.c b/mini_builtin.c index b9be7c58f8..2fbc00234d 100644 --- a/mini_builtin.c +++ b/mini_builtin.c @@ -58,7 +58,7 @@ builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *ta .debug_level = 0, }; - if (*rb_ruby_prism_ptr()) { + if (rb_ruby_prism_p()) { pm_parse_result_t result = { 0 }; pm_prelude_load(&result, name_str, code, start_line); diff --git a/prism_compile.h b/prism_compile.h index 28f32cfbe9..4015091fc1 100644 --- a/prism_compile.h +++ b/prism_compile.h @@ -65,7 +65,6 @@ typedef struct pm_scope_node { void pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous); void pm_scope_node_destroy(pm_scope_node_t *scope_node); -bool *rb_ruby_prism_ptr(void); typedef struct { /** The parser that will do the actual parsing. */ diff --git a/ruby.c b/ruby.c index b3ddf5d3b0..accf53eec2 100644 --- a/ruby.c +++ b/ruby.c @@ -1430,10 +1430,10 @@ proc_long_options(ruby_cmdline_options_t *opt, const char *s, long argc, char ** } else if (is_option_with_arg("parser", Qfalse, Qtrue)) { if (strcmp("prism", s) == 0) { - *rb_ruby_prism_ptr() = true; + rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PRISM); } else if (strcmp("parse.y", s) == 0) { - *rb_ruby_prism_ptr() = false; + rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PARSE_Y); } else { rb_raise(rb_eRuntimeError, "unknown parser %s", s); @@ -2522,7 +2522,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_enc_associate(opt->e_script, eenc); } - if (!(*rb_ruby_prism_ptr())) { + if (!rb_ruby_prism_p()) { ast_value = process_script(opt); if (!(result.ast = rb_ruby_ast_data_get(ast_value))) return Qfalse; } diff --git a/version.c b/version.c index 86415b83d1..3bf3ff1db4 100644 --- a/version.c +++ b/version.c @@ -10,6 +10,7 @@ **********************************************************************/ #include "internal/cmdlineopt.h" +#include "internal/parse.h" #include "ruby/ruby.h" #include "version.h" #include "vm_core.h" @@ -141,7 +142,22 @@ Init_version(void) int ruby_mn_threads_enabled; -bool * rb_ruby_prism_ptr(void); +#ifndef RB_DEFAULT_PARSER +#define RB_DEFAULT_PARSER RB_DEFAULT_PARSER_PRISM +#endif +static ruby_default_parser_enum default_parser = RB_DEFAULT_PARSER; + +ruby_default_parser_enum +rb_ruby_default_parser(void) +{ + return default_parser; +} + +void +rb_ruby_default_parser_set(ruby_default_parser_enum parser) +{ + default_parser = parser; +} static void define_ruby_description(const char *const jit_opt) @@ -158,7 +174,7 @@ define_ruby_description(const char *const jit_opt) # define append(s) (n += (int)strlcpy(desc + n, s, sizeof(desc) - n), assert(n < sizeof(desc))) if (*jit_opt) append(jit_opt); if (ruby_mn_threads_enabled) append(" +MN"); - if (*rb_ruby_prism_ptr()) append(" +PRISM"); + if (rb_ruby_prism_p()) append(" +PRISM"); append(ruby_description + ruby_description_opt_point); # undef append diff --git a/vm.c b/vm.c index 6b75a7ad5a..4a31d79df7 100644 --- a/vm.c +++ b/vm.c @@ -4456,14 +4456,6 @@ rb_ruby_verbose_ptr(void) return &cr->verbose; } -static bool prism = (RB_DEFAULT_PARSER == 1); - -bool * -rb_ruby_prism_ptr(void) -{ - return &prism; -} - VALUE * rb_ruby_debug_ptr(void) { diff --git a/vm_eval.c b/vm_eval.c index baddb88b70..ff8e2e6780 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1787,7 +1787,7 @@ static const rb_iseq_t * eval_make_iseq(VALUE src, VALUE fname, int line, const struct rb_block *base_block) { - if (*rb_ruby_prism_ptr()) { + if (rb_ruby_prism_p()) { return pm_eval_make_iseq(src, fname, line, base_block); } const VALUE parser = rb_parser_new();