diff options
Diffstat (limited to 'ruby.c')
| -rw-r--r-- | ruby.c | 791 |
1 files changed, 243 insertions, 548 deletions
@@ -11,60 +11,36 @@ **********************************************************************/ -#include "ruby/internal/config.h" - -#include <ctype.h> -#include <stdio.h> -#include <sys/types.h> - #ifdef __CYGWIN__ -# include <windows.h> -# include <sys/cygwin.h> +#include <windows.h> +#include <sys/cygwin.h> #endif +#include "internal.h" +#include "ruby/thread.h" +#include "eval_intern.h" +#include "dln.h" +#include <stdio.h> +#include <sys/types.h> +#include <ctype.h> #ifdef __hpux -# include <sys/pstat.h> +#include <sys/pstat.h> #endif - #if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR) -# include <dlfcn.h> +#include <dlfcn.h> #endif #ifdef HAVE_UNISTD_H -# include <unistd.h> +#include <unistd.h> #endif - #if defined(HAVE_FCNTL_H) -# include <fcntl.h> +#include <fcntl.h> #elif defined(HAVE_SYS_FCNTL_H) -# include <sys/fcntl.h> +#include <sys/fcntl.h> #endif - #ifdef HAVE_SYS_PARAM_H # include <sys/param.h> #endif - -#include "dln.h" -#include "eval_intern.h" -#include "internal.h" -#include "internal/error.h" -#include "internal/file.h" -#include "internal/inits.h" -#include "internal/io.h" -#include "internal/load.h" -#include "internal/loadpath.h" -#include "internal/missing.h" -#include "internal/object.h" -#include "internal/parse.h" -#include "internal/process.h" -#include "internal/variable.h" -#include "mjit.h" -#include "ruby/encoding.h" -#include "ruby/thread.h" -#include "ruby/util.h" -#include "ruby/version.h" -#include "ruby/internal/error.h" - #ifndef MAXPATHLEN # define MAXPATHLEN 1024 #endif @@ -72,7 +48,7 @@ # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) #endif -void Init_ruby_description(void); +#include "ruby/util.h" #ifndef HAVE_STDLIB_H char *getenv(); @@ -87,8 +63,6 @@ char *getenv(); #define DEFAULT_RUBYGEMS_ENABLED "enabled" #endif -void rb_warning_category_update(unsigned int mask, unsigned int bits); - #define COMMA , #define FEATURE_BIT(bit) (1U << feature_##bit) #define EACH_FEATURES(X, SEP) \ @@ -99,8 +73,6 @@ void rb_warning_category_update(unsigned int mask, unsigned int bits); X(rubyopt) \ SEP \ X(frozen_string_literal) \ - SEP \ - X(jit) \ /* END OF FEATURES */ #define EACH_DEBUG_FEATURES(X, SEP) \ X(frozen_string_literal) \ @@ -149,24 +121,6 @@ enum dump_flag_bits { typedef struct ruby_cmdline_options ruby_cmdline_options_t; -typedef struct { - unsigned int mask; - unsigned int set; -} ruby_features_t; - -static inline void -rb_feature_set_to(ruby_features_t *feat, unsigned int bit_mask, unsigned int bit_set) -{ - feat->mask |= bit_mask; - feat->set = (feat->set & ~bit_mask) | bit_set; -} - -#define FEATURE_SET_TO(feat, bit_mask, bit_set) \ - rb_feature_set_to(&(feat), bit_mask, bit_set) -#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits) -#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask) -#define FEATURE_SET_P(feat, bits) ((feat).set & (bits)) - struct ruby_cmdline_options { const char *script; VALUE script_name; @@ -178,12 +132,9 @@ struct ruby_cmdline_options { } enc; } src, ext, intern; VALUE req_list; - ruby_features_t features; - ruby_features_t warn; + unsigned int features; unsigned int dump; -#if USE_MJIT - struct mjit_options mjit; -#endif + int safe_level; int sflag, xflag; unsigned int warning: 1; unsigned int verbose: 1; @@ -211,7 +162,6 @@ enum { & ~FEATURE_BIT(gems) #endif & ~FEATURE_BIT(frozen_string_literal) - & ~FEATURE_BIT(jit) ) }; @@ -223,10 +173,7 @@ cmdline_options_init(ruby_cmdline_options_t *opt) opt->src.enc.index = src_encoding_index; opt->ext.enc.index = -1; opt->intern.enc.index = -1; - opt->features.set = DEFAULT_FEATURES; -#ifdef MJIT_FORCE_ENABLE /* to use with: ./configure cppflags="-DMJIT_FORCE_ENABLE" */ - opt->features.set |= FEATURE_BIT(jit); -#endif + opt->features = DEFAULT_FEATURES; return opt; } @@ -241,26 +188,20 @@ static struct { char **argv; } origarg; -static const char esc_standout[] = "\n\033[1;7m"; -static const char esc_bold[] = "\033[1m"; -static const char esc_reset[] = "\033[0m"; -static const char esc_none[] = ""; - static void -show_usage_line(const char *str, unsigned int namelen, unsigned int secondlen, int help, int highlight, unsigned int w) +show_usage_line(const char *str, unsigned int namelen, unsigned int secondlen, int help) { - const char *sb = highlight ? esc_bold : esc_none; - const char *se = highlight ? esc_reset : esc_none; - const int wrap = help && namelen + secondlen - 1 > w; - printf(" %s%.*s%-*.*s%s%-*s%s\n", sb, namelen-1, str, + const unsigned int w = 16; + const int wrap = help && namelen + secondlen - 2 > w; + printf(" %.*s%-*.*s%-*s%s\n", namelen-1, str, (wrap ? 0 : w - namelen + 1), - (help ? secondlen-1 : 0), str + namelen, se, + (help ? secondlen-1 : 0), str + namelen, (wrap ? w + 3 : 0), (wrap ? "\n" : ""), str + namelen + secondlen); } static void -usage(const char *name, int help, int highlight, int columns) +usage(const char *name, int help) { /* This message really ought to be max 23 lines. * Removed -h because the user already knows that option. Others? */ @@ -291,12 +232,11 @@ usage(const char *name, int help, int highlight, int columns) M("-rlibrary", "", "require the library before executing your script"), M("-s", "", "enable some switch parsing for switches after script name"), M("-S", "", "look for the script using PATH environment variable"), - M("-v", "", "print the version number, then turn on verbose mode"), + M("-T[level=1]", "", "turn on tainting checks"), + M("-v", ", --verbose", "print version number, then turn on verbose mode"), M("-w", "", "turn warnings on for your script"), - M("-W[level=2|:category]", "", "set warning level; 0=silence, 1=medium, 2=verbose"), + M("-W[level=2]", "", "set warning level; 0=silence, 1=medium, 2=verbose"), M("-x[directory]", "", "strip off text before #!ruby line and perhaps cd to directory"), - M("--jit", "", "enable JIT with default options (experimental)"), - M("--jit-[option]","", "enable JIT with an option (experimental)"), M("-h", "", "show this message, --help for more info"), }; static const struct message help_msg[] = { @@ -307,10 +247,8 @@ usage(const char *name, int help, int highlight, int columns) "enable or disable features. see below for available features"), M("--external-encoding=encoding", ", --internal-encoding=encoding", "specify the default external or internal character encoding"), - M("--verbose", "", "turn on verbose mode and disable script from stdin"), - M("--version", "", "print the version number, then exit"), + M("--version", "", "print the version"), M("--help", "", "show this message, -h for short message"), - M("--backtrace-limit=num", "", "limit the maximum length of backtrace"), }; static const struct message dumps[] = { M("insns", "", "instruction sequences"), @@ -323,50 +261,25 @@ usage(const char *name, int help, int highlight, int columns) M("did_you_mean", "", "did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED")"), M("rubyopt", "", "RUBYOPT environment variable (default: enabled)"), M("frozen-string-literal", "", "freeze all string literals (default: disabled)"), - M("jit", "", "JIT compiler (default: disabled)"), - }; - static const struct message warn_categories[] = { - M("deprecated", "", "deprecated features"), - M("experimental", "", "experimental features"), - }; - static const struct message mjit_options[] = { - M("--jit-warnings", "", "Enable printing JIT warnings"), - M("--jit-debug", "", "Enable JIT debugging (very slow), or add cflags if specified"), - M("--jit-wait", "", "Wait until JIT compilation finishes every time (for testing)"), - M("--jit-save-temps", "", "Save JIT temporary files in $TMP or /tmp (for testing)"), - M("--jit-verbose=num", "", "Print JIT logs of level num or less to stderr (default: 0)"), - M("--jit-max-cache=num", "", "Max number of methods to be JIT-ed in a cache (default: 100)"), - M("--jit-min-calls=num", "", "Number of calls to trigger JIT (for testing, default: 10000)"), }; int i; - const char *sb = highlight ? esc_standout+1 : esc_none; - const char *se = highlight ? esc_reset : esc_none; const int num = numberof(usage_msg) - (help ? 1 : 0); - unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16; -#define SHOW(m) show_usage_line((m).str, (m).namelen, (m).secondlen, help, highlight, w) +#define SHOW(m) show_usage_line((m).str, (m).namelen, (m).secondlen, help) - printf("%sUsage:%s %s [switches] [--] [programfile] [arguments]\n", sb, se, name); + printf("Usage: %s [switches] [--] [programfile] [arguments]\n", name); for (i = 0; i < num; ++i) SHOW(usage_msg[i]); if (!help) return; - if (highlight) sb = esc_standout; - for (i = 0; i < numberof(help_msg); ++i) SHOW(help_msg[i]); - printf("%s""Dump List:%s\n", sb, se); + puts("Dump List:"); for (i = 0; i < numberof(dumps); ++i) SHOW(dumps[i]); - printf("%s""Features:%s\n", sb, se); + puts("Features:"); for (i = 0; i < numberof(features); ++i) SHOW(features[i]); - printf("%s""Warning categories:%s\n", sb, se); - for (i = 0; i < numberof(warn_categories); ++i) - SHOW(warn_categories[i]); - printf("%s""JIT options (experimental):%s\n", sb, se); - for (i = 0; i < numberof(mjit_options); ++i) - SHOW(mjit_options[i]); } #define rubylib_path_new rb_str_new @@ -529,61 +442,22 @@ str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to) # define str_conv_enc(str, from, to) (str) #endif -void ruby_init_loadpath(void); +void ruby_init_loadpath_safe(int safe_level); -#if defined(LOAD_RELATIVE) -static VALUE -runtime_libruby_path(void) +void +ruby_init_loadpath(void) { -#if defined _WIN32 || defined __CYGWIN__ - DWORD len = RSTRING_EMBED_LEN_MAX, ret; - VALUE path; - VALUE wsopath = rb_str_new(0, len*sizeof(WCHAR)); - WCHAR *wlibpath; - char *libpath; + ruby_init_loadpath_safe(0); +} - while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath), - ret = GetModuleFileNameW(libruby, wlibpath, len), - (ret == len)) - { - rb_str_modify_expand(wsopath, len*sizeof(WCHAR)); - rb_str_set_len(wsopath, (len += len)*sizeof(WCHAR)); - } - if (!ret || ret > len) rb_fatal("failed to get module file name"); -#if defined __CYGWIN__ - { - const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE; - size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0); - if (!newsize) rb_fatal("failed to convert module path to cygwin"); - path = rb_str_new(0, newsize); - libpath = RSTRING_PTR(path); - if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) { - rb_str_resize(path, 0); - } - } -#else - { - DWORD i; - for (len = ret, i = 0; i < len; ++i) { - if (wlibpath[i] == L'\\') { - wlibpath[i] = L'/'; - ret = i+1; /* chop after the last separator */ - } - } - } - len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL); - path = rb_utf8_str_new(0, len); - libpath = RSTRING_PTR(path); - WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath, len, NULL, NULL); -#endif - rb_str_resize(wsopath, 0); - return path; -#elif defined(HAVE_DLADDR) +#if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR) && !defined(__CYGWIN__) +static VALUE +dladdr_path(const void* addr) +{ Dl_info dli; VALUE fname, path; - const void* addr = (void *)(VALUE)expand_include_path; - if (!dladdr((void *)addr, &dli)) { + if (!dladdr(addr, &dli)) { return rb_str_new(0, 0); } #ifdef __linux__ @@ -598,66 +472,102 @@ runtime_libruby_path(void) } rb_str_resize(fname, 0); return path; -#else -# error relative load path is not supported on this platform. -#endif } #endif #define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index") -VALUE ruby_archlibdir_path, ruby_prefix_path; - void -ruby_init_loadpath(void) +ruby_init_loadpath_safe(int safe_level) { - VALUE load_path, archlibdir = 0; + VALUE load_path; ID id_initial_load_path_mark; const char *paths = ruby_initial_load_paths; #if defined LOAD_RELATIVE -#if !defined ENABLE_MULTIARCH -# define RUBY_ARCH_PATH "" -#elif defined RUBY_ARCH -# define RUBY_ARCH_PATH "/"RUBY_ARCH -#else -# define RUBY_ARCH_PATH "/"RUBY_PLATFORM -#endif +# if defined HAVE_DLADDR || defined __CYGWIN__ || defined _WIN32 +# define VARIABLE_LIBPATH 1 +# else +# define VARIABLE_LIBPATH 0 +# endif +# if VARIABLE_LIBPATH char *libpath; VALUE sopath; +# else + char libpath[MAXPATHLEN + 1]; +# endif size_t baselen; - const char *p; + char *p; - sopath = runtime_libruby_path(); +#if defined _WIN32 || defined __CYGWIN__ + { + DWORD len = RSTRING_EMBED_LEN_MAX, ret, i; + VALUE wsopath = rb_str_new(0, len*sizeof(WCHAR)); + WCHAR *wlibpath; + while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath), + ret = GetModuleFileNameW(libruby, wlibpath, len), + (ret == len)) + { + rb_str_modify_expand(wsopath, len*sizeof(WCHAR)); + rb_str_set_len(wsopath, (len += len)*sizeof(WCHAR)); + } + if (!ret || ret > len) rb_fatal("failed to get module file name"); + for (len = ret, i = 0; i < len; ++i) { + if (wlibpath[i] == L'\\') { + wlibpath[i] = L'/'; + ret = i+1; /* chop after the last separator */ + } + } + len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL); + sopath = rb_utf8_str_new(0, len); + libpath = RSTRING_PTR(sopath); + WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath, len, NULL, NULL); + rb_str_resize(wsopath, 0); + } +#elif defined(HAVE_DLADDR) + sopath = dladdr_path((void *)(VALUE)expand_include_path); libpath = RSTRING_PTR(sopath); +#endif +#if !VARIABLE_LIBPATH + libpath[sizeof(libpath) - 1] = '\0'; +#endif +#if defined DOSISH && !defined _WIN32 + translit_char(libpath, '\\', '/'); +#elif defined __CYGWIN__ + { + const int win_to_posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE; + size_t newsize = cygwin_conv_path(win_to_posix, libpath, 0, 0); + if (newsize > 0) { + VALUE rubylib = rb_str_new(0, newsize); + p = RSTRING_PTR(rubylib); + if (cygwin_conv_path(win_to_posix, libpath, p, newsize) == 0) { + rb_str_resize(sopath, 0); + sopath = rubylib; + libpath = p; + } + } + } +#endif p = strrchr(libpath, '/'); if (p) { - static const char libdir[] = "/" + static const char bindir[] = "/bin"; #ifdef LIBDIR_BASENAME - LIBDIR_BASENAME + static const char libdir[] = "/"LIBDIR_BASENAME; #else - "lib" + static const char libdir[] = "/lib"; #endif - RUBY_ARCH_PATH; - const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir) - - rb_strlen_lit(RUBY_ARCH_PATH) - 1; - static const char bindir[] = "/bin"; const ptrdiff_t bindir_len = (ptrdiff_t)sizeof(bindir) - 1; - - const char *p2 = NULL; + const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir) - 1; #ifdef ENABLE_MULTIARCH + char *p2 = NULL; + multiarch: #endif if (p - libpath >= bindir_len && !STRNCASECMP(p - bindir_len, bindir, bindir_len)) { p -= bindir_len; - archlibdir = rb_str_subseq(sopath, 0, p - libpath); - rb_str_cat_cstr(archlibdir, libdir); - OBJ_FREEZE_RAW(archlibdir); } else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) { - archlibdir = rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath); - OBJ_FREEZE_RAW(archlibdir); p -= libdir_len; } #ifdef ENABLE_MULTIARCH @@ -671,28 +581,37 @@ ruby_init_loadpath(void) p = p2; } #endif +#if !VARIABLE_LIBPATH + *p = 0; +#endif } +#if !VARIABLE_LIBPATH + else { + strlcpy(libpath, ".", sizeof(libpath)); + p = libpath + 1; + } + baselen = p - libpath; +#define PREFIX_PATH() rb_str_new(libpath, baselen) +#else baselen = p - libpath; rb_str_resize(sopath, baselen); libpath = RSTRING_PTR(sopath); #define PREFIX_PATH() sopath +#endif + #define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen) + #define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len)) #else const size_t exec_prefix_len = strlen(ruby_exec_prefix); #define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len)) #define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len) #endif - rb_gc_register_address(&ruby_prefix_path); - ruby_prefix_path = PREFIX_PATH(); - OBJ_FREEZE_RAW(ruby_prefix_path); - if (!archlibdir) archlibdir = ruby_prefix_path; - rb_gc_register_address(&ruby_archlibdir_path); - ruby_archlibdir_path = archlibdir; - load_path = GET_VM()->load_path; - ruby_push_include(getenv("RUBYLIB"), identical_path); + if (safe_level == 0) { + ruby_push_include(getenv("RUBYLIB"), identical_path); + } id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK; while (*paths) { @@ -703,7 +622,7 @@ ruby_init_loadpath(void) paths += len + 1; } - rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), ruby_prefix_path); + rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), rb_obj_freeze(PREFIX_PATH())); } @@ -714,9 +633,11 @@ add_modules(VALUE *req_list, const char *mod) VALUE feature; if (!list) { - *req_list = list = rb_ary_tmp_new(0); + *req_list = list = rb_ary_new(); + RBASIC_CLEAR_CLASS(list); } - feature = rb_str_cat_cstr(rb_str_tmp_new(0), mod); + feature = rb_str_new2(mod); + RBASIC_CLEAR_CLASS(feature); rb_ary_push(list, feature); } @@ -818,7 +739,6 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) char **argv, *p; const char *ap = 0; VALUE argstr, argary; - void *ptr; while (ISSPACE(*s)) s++; if (!*s) return; @@ -841,8 +761,7 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) argc = RSTRING_LEN(argary) / sizeof(ap); ap = 0; rb_str_cat(argary, (char *)&ap, sizeof(ap)); - argv = ptr = ALLOC_N(char *, argc); - MEMMOVE(argv, RSTRING_PTR(argary), char *, argc); + argv = (char **)RSTRING_PTR(argary); while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) { argv += i; @@ -855,7 +774,6 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) } } - ruby_xfree(ptr); /* get rid of GC */ rb_str_resize(argary, 0); rb_str_resize(argstr, 0); @@ -865,7 +783,7 @@ static int name_match_p(const char *name, const char *str, size_t len) { if (len == 0) return 0; - while (1) { + do { while (TOLOWER(*str) == *name) { if (!--len || !*++str) return 1; ++name; @@ -875,7 +793,8 @@ name_match_p(const char *name, const char *str, size_t len) if (*name != '-' && *name != '_') return 0; ++name; ++str; - } + } while (len > 0); + return !*name; } #define NAME_MATCH_P(name, str, len) \ @@ -899,20 +818,22 @@ static void feature_option(const char *str, int len, void *arg, const unsigned int enable) { static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT, ", "); - ruby_features_t *argp = arg; + unsigned int *argp = arg; unsigned int mask = ~0U; - unsigned int set = 0U; #if AMBIGUOUS_FEATURE_NAMES + unsigned int set = 0U; int matched = 0; -# define FEATURE_FOUND ++matched +#define SET_FEATURE(bit) \ + if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); ++matched;} #else -# define FEATURE_FOUND goto found -#endif #define SET_FEATURE(bit) \ - if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;} + if (NAME_MATCH_P(#bit, str, len)) {mask = FEATURE_BIT(bit); goto found;} +#endif EACH_FEATURES(SET_FEATURE, ;); if (NAME_MATCH_P("all", str, len)) { - goto found; + found: + *argp = (*argp & ~mask) | (mask & enable); + return; } #if AMBIGUOUS_FEATURE_NAMES if (matched == 1) goto found; @@ -932,11 +853,6 @@ feature_option(const char *str, int len, void *arg, const unsigned int enable) rb_warn("unknown argument for --%s: `%.*s'", enable ? "enable" : "disable", len, str); rb_warn("features are [%.*s].", (int)strlen(list), list); - return; - - found: - FEATURE_SET_TO(*argp, mask, (mask & enable)); - return; } static void @@ -958,12 +874,7 @@ static void debug_option(const char *str, int len, void *arg) { static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT, ", "); - ruby_features_t *argp = arg; -#define SET_WHEN_DEBUG(bit) \ - if (NAME_MATCH_P(#bit, str, len)) { \ - FEATURE_SET(*argp, DEBUG_BIT(bit)); \ - return; \ - } +#define SET_WHEN_DEBUG(bit) SET_WHEN(#bit, DEBUG_BIT(bit), str, len) EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;); #ifdef RUBY_DEVEL if (ruby_patchlevel < 0 && ruby_env_debug_option(str, len, 0)) return; @@ -1005,53 +916,6 @@ set_option_encoding_once(const char *type, VALUE *name, const char *e, long elen #define set_source_encoding_once(opt, e, elen) \ set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen)) -#if USE_MJIT -static void -setup_mjit_options(const char *s, struct mjit_options *mjit_opt) -{ -#define opt_match(s, l, name) \ - ((((l) > rb_strlen_lit(name)) ? (s)[rb_strlen_lit(name)] == '=' : \ - (l) == rb_strlen_lit(name)) && \ - memcmp((s), name, rb_strlen_lit(name)) == 0 && \ - (((s) += rb_strlen_lit(name)), 1)) -#define opt_match_noarg(s, l, name) \ - opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --jit-" name " is ignored"), 1) : 1) -#define opt_match_arg(s, l, name) \ - opt_match(s, l, name) && (*(s) ? 1 : (rb_raise(rb_eRuntimeError, "--jit-" name " needs an argument"), 0)) - if (*s != '-') return; - const size_t l = strlen(++s); - if (*s == 0) return; - else if (opt_match_noarg(s, l, "warnings")) { - mjit_opt->warnings = 1; - } - else if (opt_match(s, l, "debug")) { - if (*s) - mjit_opt->debug_flags = strdup(s + 1); - else - mjit_opt->debug = 1; - } - else if (opt_match_noarg(s, l, "wait")) { - mjit_opt->wait = 1; - } - else if (opt_match_noarg(s, l, "save-temps")) { - mjit_opt->save_temps = 1; - } - else if (opt_match(s, l, "verbose")) { - mjit_opt->verbose = *s ? atoi(s + 1) : 1; - } - else if (opt_match_arg(s, l, "max-cache")) { - mjit_opt->max_cache_size = atoi(s + 1); - } - else if (opt_match_arg(s, l, "min-calls")) { - mjit_opt->min_calls = atoi(s + 1); - } - else { - rb_raise(rb_eRuntimeError, - "invalid MJIT option `%s' (--help will show valid MJIT options)", s); - } -} -#endif - static long proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) { @@ -1110,29 +974,10 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) warning = 1; ruby_verbose = Qtrue; } - FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS); s++; goto reswitch; case 'W': - if (s[1] == ':') { - unsigned int bits = 0; - static const char no_prefix[] = "no-"; - int enable = strncmp(s += 2, no_prefix, sizeof(no_prefix)-1) != 0; - if (!enable) s += sizeof(no_prefix)-1; - size_t len = strlen(s); - if (NAME_MATCH_P("deprecated", s, len)) { - bits = 1U << RB_WARN_CATEGORY_DEPRECATED; - } - else if (NAME_MATCH_P("experimental", s, len)) { - bits = 1U << RB_WARN_CATEGORY_EXPERIMENTAL; - } - else { - rb_warn("unknown warning category: `%s'", s); - } - if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0); - break; - } { size_t numlen; int v = 2; /* -W as -W2 */ @@ -1140,7 +985,7 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) if (*++s) { v = scan_oct(s, 1, &numlen); if (numlen == 0) - v = 2; + v = 1; s += numlen; } if (!opt->warning) { @@ -1157,17 +1002,6 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) } } warning = 1; - switch (v) { - case 0: - FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_ALL_BITS, 0); - break; - case 1: - FEATURE_SET_TO(opt->warn, 1U << RB_WARN_CATEGORY_DEPRECATED, 0); - break; - default: - FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS); - break; - } } goto reswitch; @@ -1302,6 +1136,21 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) } goto reswitch; + case 'T': + { + size_t numlen; + int v = 1; + + if (*++s) { + v = scan_oct(s, 2, &numlen); + if (numlen == 0) + v = 1; + s += numlen; + } + if (v > opt->safe_level) opt->safe_level = v; + } + goto reswitch; + case 'I': forbid_setid("-I"); if (*++s) @@ -1419,14 +1268,6 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) opt->verbose = 1; ruby_verbose = Qtrue; } - else if (strncmp("jit", s, 3) == 0) { -#if USE_MJIT - FEATURE_SET(opt->features, FEATURE_BIT(jit)); - setup_mjit_options(s + 3, &opt->mjit); -#else - rb_warn("MJIT support is disabled."); -#endif - } else if (strcmp("yydebug", s) == 0) { if (envopt) goto noenvopt_long; opt->dump |= DUMP_BIT(yydebug); @@ -1439,12 +1280,6 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) opt->dump |= DUMP_BIT(help); goto switch_end; } - else if (is_option_with_arg("backtrace-limit", Qfalse, Qfalse)) { - char *e; - long n = strtol(s, &e, 10); - if (errno == ERANGE || n < 0 || *e) rb_raise(rb_eRuntimeError, "wrong limit for backtrace length"); - rb_backtrace_length_limit = n; - } else { rb_raise(rb_eRuntimeError, "invalid option --%s (-h will show valid options)", s); @@ -1457,9 +1292,16 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) default: { - rb_raise(rb_eRuntimeError, + if (ISPRINT(*s)) { + rb_raise(rb_eRuntimeError, "invalid option -%c (-h will show valid options)", (int)(unsigned char)*s); + } + else { + rb_raise(rb_eRuntimeError, + "invalid option -\\x%02X (-h will show valid options)", + (int)(unsigned char)*s); + } } goto switch_end; @@ -1487,37 +1329,13 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) return argc0 - argc; } -void Init_builtin_features(void); - static void ruby_init_prelude(void) { - Init_builtin_features(); + Init_prelude(); rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX")); } -void rb_call_builtin_inits(void); - -static void -ruby_opt_init(ruby_cmdline_options_t *opt) -{ - if (opt->dump & dump_exit_bits) return; - - if (opt->features.set & FEATURE_BIT(gems)) { - rb_define_module("Gem"); - if (opt->features.set & FEATURE_BIT(did_you_mean)) { - rb_define_module("DidYouMean"); - } - } - - Init_ext(); /* load statically linked extensions before rubygems */ - rb_call_builtin_inits(); - ruby_init_prelude(); - - ruby_set_script_name(opt->script_name); - require_libraries(&opt->req_list); -} - static int opt_enc_index(VALUE enc_name) { @@ -1536,16 +1354,15 @@ opt_enc_index(VALUE enc_name) #define rb_progname (GET_VM()->progname) #define rb_orig_progname (GET_VM()->orig_progname) VALUE rb_argv0; -VALUE rb_e_script; static VALUE -false_value(ID _x, VALUE *_y) +false_value(void) { return Qfalse; } static VALUE -true_value(ID _x, VALUE *_y) +true_value(void) { return Qtrue; } @@ -1577,7 +1394,7 @@ uscore_get(void) */ static VALUE -rb_f_sub(int argc, VALUE *argv, VALUE _) +rb_f_sub(int argc, VALUE *argv) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("sub"), argc, argv); rb_lastline_set(str); @@ -1596,7 +1413,7 @@ rb_f_sub(int argc, VALUE *argv, VALUE _) */ static VALUE -rb_f_gsub(int argc, VALUE *argv, VALUE _) +rb_f_gsub(int argc, VALUE *argv) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("gsub"), argc, argv); rb_lastline_set(str); @@ -1608,13 +1425,13 @@ rb_f_gsub(int argc, VALUE *argv, VALUE _) * chop -> $_ * * Equivalent to <code>($_.dup).chop!</code>, except <code>nil</code> - * is never returned. See String#chop!. + * is never returned. See <code>String#chop!</code>. * Available only when -p/-n command line option specified. * */ static VALUE -rb_f_chop(VALUE _) +rb_f_chop(void) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chop"), 0, 0); rb_lastline_set(str); @@ -1628,42 +1445,19 @@ rb_f_chop(VALUE _) * chomp(string) -> $_ * * Equivalent to <code>$_ = $_.chomp(<em>string</em>)</code>. See - * String#chomp. + * <code>String#chomp</code>. * Available only when -p/-n command line option specified. * */ static VALUE -rb_f_chomp(int argc, VALUE *argv, VALUE _) +rb_f_chomp(int argc, VALUE *argv) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chomp"), argc, argv); rb_lastline_set(str); return str; } -static void -setup_pager_env(void) -{ - if (!getenv("LESS")) ruby_setenv("LESS", "-R"); // Output "raw" control characters. -} - -#ifdef _WIN32 -static int -tty_enabled(void) -{ - HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); - DWORD m; - if (!GetConsoleMode(h, &m)) return 0; -# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING -# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x200 -# endif - if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return 0; - return 1; -} -#elif !defined(HAVE_WORKING_FORK) -# define tty_enabled() 0 -#endif - static VALUE process_options(int argc, char **argv, ruby_cmdline_options_t *opt) { @@ -1673,83 +1467,32 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) const rb_iseq_t *iseq; rb_encoding *enc, *lenc; #if UTF8_PATH - rb_encoding *ienc = 0; - rb_encoding *const uenc = rb_utf8_encoding(); + rb_encoding *uenc, *ienc = 0; #endif const char *s; char fbuf[MAXPATHLEN]; int i = (int)proc_options(argc, argv, opt, 0); + rb_binding_t *toplevel_binding; + const struct rb_block *base_block; unsigned int dump = opt->dump & dump_exit_bits; if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) { - int tty = isatty(1); const char *const progname = (argc > 0 && argv && argv[0] ? argv[0] : origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] : ruby_engine); - int columns = 0; - if ((opt->dump & DUMP_BIT(help)) && tty) { - const char *pager_env = getenv("RUBY_PAGER"); - if (!pager_env) pager_env = getenv("PAGER"); - if (pager_env && *pager_env && isatty(0)) { - const char *columns_env = getenv("COLUMNS"); - if (columns_env) columns = atoi(columns_env); - VALUE pager = rb_str_new_cstr(pager_env); -#ifdef HAVE_WORKING_FORK - int fds[2]; - if (rb_pipe(fds) == 0) { - rb_pid_t pid = rb_fork(); - if (pid > 0) { - /* exec PAGER with reading from child */ - dup2(fds[0], 0); - } - else if (pid == 0) { - /* send the help message to the parent PAGER */ - dup2(fds[1], 1); - dup2(fds[1], 2); - } - close(fds[0]); - close(fds[1]); - if (pid > 0) { - setup_pager_env(); - rb_f_exec(1, &pager); - kill(SIGTERM, pid); - rb_waitpid(pid, 0, 0); - } - } -#else - setup_pager_env(); - VALUE port = rb_io_popen(pager, rb_str_new_lit("w"), Qnil, Qnil); - if (!NIL_P(port)) { - int oldout = dup(1); - int olderr = dup(2); - int fd = RFILE(port)->fptr->fd; - tty = tty_enabled(); - dup2(fd, 1); - dup2(fd, 2); - usage(progname, 1, tty, columns); - fflush(stdout); - dup2(oldout, 1); - dup2(olderr, 2); - rb_io_close(port); - return Qtrue; - } -#endif - } - } - usage(progname, (opt->dump & DUMP_BIT(help)), tty, columns); + usage(progname, (opt->dump & DUMP_BIT(help))); return Qtrue; } argc -= i; argv += i; - if ((opt->features.set & FEATURE_BIT(rubyopt)) && (s = getenv("RUBYOPT"))) { + if ((opt->features & FEATURE_BIT(rubyopt)) && + opt->safe_level == 0 && (s = getenv("RUBYOPT"))) { VALUE src_enc_name = opt->src.enc.name; VALUE ext_enc_name = opt->ext.enc.name; VALUE int_enc_name = opt->intern.enc.name; - ruby_features_t feat = opt->features; - ruby_features_t warn = opt->warn; opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0; moreswitches(s, opt, 1); @@ -1759,25 +1502,12 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) opt->ext.enc.name = ext_enc_name; if (int_enc_name) opt->intern.enc.name = int_enc_name; - FEATURE_SET_RESTORE(opt->features, feat); - FEATURE_SET_RESTORE(opt->warn, warn); } if (opt->src.enc.name) - /* cannot set deprecated category, as enabling deprecation warnings based on flags - * has not happened yet. - */ - rb_warning("-K is specified; it is for 1.8 compatibility and may cause odd behavior"); + rb_warning("-K is specified; it is for 1.8 compatibility and may cause odd behavior"); -#if USE_MJIT - if (opt->features.set & FEATURE_BIT(jit)) { - opt->mjit.on = TRUE; /* set mjit.on for ruby_show_version() API and check to call mjit_init() */ - } -#endif if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) { -#if USE_MJIT - mjit_opts.on = opt->mjit.on; /* used by ruby_show_version(). mjit_init() still can't be called here. */ -#endif ruby_show_version(); if (opt->dump & DUMP_BIT(version)) return Qtrue; } @@ -1827,16 +1557,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) translit_char(RSTRING_PTR(opt->script_name), '\\', '/'); #endif - ruby_gc_set_params(); - ruby_init_loadpath(); - -#if USE_MJIT - if (opt->mjit.on) - /* Using TMP_RUBY_PREFIX created by ruby_init_loadpath(). */ - mjit_init(&opt->mjit); -#endif - - Init_ruby_description(); + ruby_gc_set_params(opt->safe_level); + ruby_init_loadpath_safe(opt->safe_level); Init_enc(); lenc = rb_locale_encoding(); rb_enc_associate(rb_progname, lenc); @@ -1859,7 +1581,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) enc = rb_enc_from_index(opt->ext.enc.index); } else { - enc = IF_UTF8_PATH(uenc, lenc); + enc = lenc; } rb_enc_set_default_external(rb_enc_from_encoding(enc)); if (opt->intern.enc.index >= 0) { @@ -1871,7 +1593,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) #endif } script_name = opt->script_name; - rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc)); + rb_enc_associate(opt->script_name, + IF_UTF8_PATH(uenc = rb_utf8_encoding(), lenc)); #if UTF8_PATH if (uenc != lenc) { opt->script_name = str_conv_enc(opt->script_name, uenc, lenc); @@ -1908,12 +1631,19 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_ary_replace(vm->load_path_snapshot, load_path); } } - - if (opt->features.mask & COMPILATION_FEATURES) { + Init_ext(); /* load statically linked extensions before rubygems */ + if (opt->features & FEATURE_BIT(gems)) { + rb_define_module("Gem"); + } + if (opt->features & FEATURE_BIT(did_you_mean)) { + rb_define_module("DidYouMean"); + } + ruby_init_prelude(); + if ((opt->features ^ DEFAULT_FEATURES) & COMPILATION_FEATURES) { VALUE option = rb_hash_new(); #define SET_COMPILE_OPTION(h, o, name) \ rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \ - (FEATURE_SET_P(o->features, FEATURE_BIT(name)) ? Qtrue : Qfalse)); + ((o)->features & FEATURE_BIT(name) ? Qtrue : Qfalse)); SET_COMPILE_OPTION(option, opt, frozen_string_literal); SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal); rb_funcallv(rb_cISeq, rb_intern_const("compile_option="), 1, &option); @@ -1922,7 +1652,13 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) ruby_set_argv(argc, argv); process_sflag(&opt->sflag); - rb_parser_set_context(parser, 0, TRUE); + GetBindingPtr(rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING")), + toplevel_binding); + /* need to acquire env from toplevel_binding each time, since it + * may update after eval() */ + + base_block = toplevel_context(toplevel_binding); + rb_parser_set_context(parser, base_block, TRUE); if (opt->e_script) { VALUE progname = rb_progname; @@ -1942,11 +1678,13 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) } #endif rb_enc_associate(opt->e_script, eenc); - ruby_opt_init(opt); + if (!(opt->dump & ~DUMP_BIT(version_v))) { + ruby_set_script_name(opt->script_name); + require_libraries(&opt->req_list); + } ruby_set_script_name(progname); rb_parser_set_options(parser, opt->do_print, opt->do_loop, opt->do_line, opt->do_split); - rb_warning_category_update(opt->warn.mask, opt->warn.set); ast = rb_parser_compile_string(parser, opt->script, opt->e_script, 1); } else { @@ -1964,7 +1702,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) enc = rb_enc_from_index(opt->ext.enc.index); } else { - enc = IF_UTF8_PATH(uenc, lenc); + enc = lenc; } rb_enc_set_default_external(rb_enc_from_encoding(enc)); if (opt->intern.enc.index >= 0) { @@ -1977,7 +1715,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_enc_set_default_internal(Qnil); rb_stdio_set_default_encoding(); - if (!ast->body.root) { + if (!ast->root) { rb_ast_dispose(ast); return Qfalse; } @@ -1999,7 +1737,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) } if (dump & (DUMP_BIT(parsetree)|DUMP_BIT(parsetree_with_comment))) { - rb_io_write(rb_stdout, rb_parser_dump_tree(ast->body.root, dump & DUMP_BIT(parsetree_with_comment))); + rb_io_write(rb_stdout, rb_parser_dump_tree(ast->root, dump & DUMP_BIT(parsetree_with_comment))); rb_io_flush(rb_stdout); dump &= ~DUMP_BIT(parsetree)&~DUMP_BIT(parsetree_with_comment); if (!dump) { @@ -2021,12 +1759,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_enc_copy(path, opt->script_name); } } - - rb_binding_t *toplevel_binding; - GetBindingPtr(rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING")), - toplevel_binding); - const struct rb_block *base_block = toplevel_context(toplevel_binding); - iseq = rb_iseq_new_main(&ast->body, opt->script_name, path, vm_block_iseq(base_block)); + base_block = toplevel_context(toplevel_binding); + iseq = rb_iseq_new_main(ast->root, opt->script_name, path, vm_block_iseq(base_block)); rb_ast_dispose(ast); } @@ -2042,26 +1776,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_define_readonly_boolean("$-l", opt->do_line); rb_define_readonly_boolean("$-a", opt->do_split); - rb_gvar_ractor_local("$-p"); - rb_gvar_ractor_local("$-l"); - rb_gvar_ractor_local("$-a"); + rb_set_safe_level(opt->safe_level); - if ((rb_e_script = opt->e_script) != 0) { - rb_gc_register_mark_object(opt->e_script); - } - - { - rb_execution_context_t *ec = GET_EC(); - - if (opt->e_script) { - /* -e */ - rb_exec_event_hook_script_compiled(ec, iseq, opt->e_script); - } - else { - /* file */ - rb_exec_event_hook_script_compiled(ec, iseq, Qnil); - } - } return (VALUE)iseq; } @@ -2170,10 +1886,13 @@ load_file_internal(VALUE argp_v) else if (!NIL_P(c)) { rb_io_ungetbyte(f, c); } - if (NIL_P(c)) { + else { argp->f = f = Qnil; } - ruby_opt_init(opt); + if (!(opt->dump & ~DUMP_BIT(version_v))) { + ruby_set_script_name(opt->script_name); + require_libraries(&opt->req_list); /* Why here? unnatural */ + } } if (opt->src.enc.index >= 0) { enc = rb_enc_from_index(opt->src.enc.index); @@ -2186,7 +1905,6 @@ load_file_internal(VALUE argp_v) } rb_parser_set_options(parser, opt->do_print, opt->do_loop, opt->do_line, opt->do_split); - rb_warning_category_update(opt->warn.mask, opt->warn.set); if (NIL_P(f)) { f = rb_str_new(0, 0); rb_enc_associate(f, enc); @@ -2253,7 +1971,7 @@ open_load_file(VALUE fname_v, int *xflag) #endif if ((fd = rb_cloexec_open(fname, mode, 0)) < 0) { - e = errno; + int e = errno; if (!rb_gc_for_fd(e)) { rb_load_fail(fname_v, strerror(e)); } @@ -2264,13 +1982,9 @@ open_load_file(VALUE fname_v, int *xflag) rb_update_max_fd(fd); #if defined HAVE_FCNTL && MODE_TO_LOAD != O_RDONLY -# ifdef ENOTSUP -# define IS_SUPPORTED_OP(e) ((e) != ENOTSUP) -# else -# define IS_SUPPORTED_OP(e) ((void)(e), 1) -# endif /* disabling O_NONBLOCK */ - if (fcntl(fd, F_SETFL, 0) < 0 && IS_SUPPORTED_OP(e = errno)) { + if (fcntl(fd, F_SETFL, 0) < 0) { + e = errno; (void)close(fd); rb_load_fail(fname_v, strerror(e)); } @@ -2392,7 +2106,7 @@ ruby_setproctitle(VALUE title) } static void -set_arg0(VALUE val, ID id, VALUE *_) +set_arg0(VALUE val, ID id) { if (origarg.argv == 0) rb_raise(rb_eRuntimeError, "$0 not initialized"); @@ -2406,6 +2120,7 @@ external_str_new_cstr(const char *p) #if UTF8_PATH VALUE str = rb_utf8_str_new_cstr(p); str = str_conv_enc(str, NULL, rb_default_external_encoding()); + OBJ_TAINT_RAW(str); return str; #else return rb_external_str_new_cstr(p); @@ -2447,6 +2162,9 @@ init_ids(ruby_cmdline_options_t *opt) if (uid != euid) opt->setids |= 1; if (egid != gid) opt->setids |= 2; + if (uid && opt->setids) { + if (opt->safe_level < 1) opt->safe_level = 1; + } } #undef forbid_setid @@ -2457,26 +2175,22 @@ forbid_setid(const char *s, const ruby_cmdline_options_t *opt) rb_raise(rb_eSecurityError, "no %s allowed while running setuid", s); if (opt->setids & 2) rb_raise(rb_eSecurityError, "no %s allowed while running setgid", s); -} - -static VALUE -verbose_getter(ID id, VALUE *ptr) -{ - return *rb_ruby_verbose_ptr(); + if (opt->safe_level > 0) + rb_raise(rb_eSecurityError, "no %s allowed in tainted mode", s); } static void -verbose_setter(VALUE val, ID id, VALUE *variable) +verbose_setter(VALUE val, ID id, void *data) { - *rb_ruby_verbose_ptr() = RTEST(val) ? Qtrue : val; + VALUE *variable = data; + *variable = RTEST(val) ? Qtrue : val; } static VALUE -opt_W_getter(ID id, VALUE *dmy) +opt_W_getter(ID id, void *data) { - VALUE v = *rb_ruby_verbose_ptr(); - - switch (v) { + VALUE *variable = data; + switch (*variable) { case Qnil: return INT2FIX(0); case Qfalse: @@ -2488,35 +2202,16 @@ opt_W_getter(ID id, VALUE *dmy) } } -static VALUE -debug_getter(ID id, VALUE *dmy) -{ - return *rb_ruby_debug_ptr(); -} - -static void -debug_setter(VALUE val, ID id, VALUE *dmy) -{ - *rb_ruby_debug_ptr() = val; -} - /*! Defines built-in variables */ void ruby_prog_init(void) { - rb_define_virtual_variable("$VERBOSE", verbose_getter, verbose_setter); - rb_define_virtual_variable("$-v", verbose_getter, verbose_setter); - rb_define_virtual_variable("$-w", verbose_getter, verbose_setter); - rb_define_virtual_variable("$-W", opt_W_getter, rb_gvar_readonly_setter); - rb_define_virtual_variable("$DEBUG", debug_getter, debug_setter); - rb_define_virtual_variable("$-d", debug_getter, debug_setter); - - rb_gvar_ractor_local("$VERBOSE"); - rb_gvar_ractor_local("$-v"); - rb_gvar_ractor_local("$-w"); - rb_gvar_ractor_local("$-W"); - rb_gvar_ractor_local("$DEBUG"); - rb_gvar_ractor_local("$-d"); + rb_define_hooked_variable("$VERBOSE", &ruby_verbose, 0, verbose_setter); + rb_define_hooked_variable("$-v", &ruby_verbose, 0, verbose_setter); + rb_define_hooked_variable("$-w", &ruby_verbose, 0, verbose_setter); + rb_define_hooked_variable("$-W", &ruby_verbose, opt_W_getter, rb_gvar_readonly_setter); + rb_define_variable("$DEBUG", &ruby_debug); + rb_define_variable("$-d", &ruby_debug); rb_define_hooked_variable("$0", &rb_progname, 0, set_arg0); rb_define_hooked_variable("$PROGRAM_NAME", &rb_progname, 0, set_arg0); |
