14#include "ruby/internal/config.h"
22# include <sys/cygwin.h>
25#if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR)
33#if defined(HAVE_FCNTL_H)
35#elif defined(HAVE_SYS_FCNTL_H)
36# include <sys/fcntl.h>
39#ifdef HAVE_SYS_PARAM_H
40# include <sys/param.h>
44#include "eval_intern.h"
46#include "internal/cmdlineopt.h"
47#include "internal/coverage.h"
48#include "internal/error.h"
49#include "internal/file.h"
50#include "internal/inits.h"
51#include "internal/io.h"
52#include "internal/load.h"
53#include "internal/loadpath.h"
54#include "internal/missing.h"
55#include "internal/object.h"
56#include "internal/thread.h"
57#include "internal/ruby_parser.h"
58#include "internal/variable.h"
59#include "prism_compile.h"
66#define singlebit_only_p(x) !((x) & ((x)-1))
67STATIC_ASSERT(Qnil_1bit_from_Qfalse, singlebit_only_p(
Qnil^
Qfalse));
68STATIC_ASSERT(Qundef_1bit_from_Qnil, singlebit_only_p(
Qundef^
Qnil));
71# define MAXPATHLEN 1024
74# define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
83#ifndef DISABLE_RUBYGEMS
84# define DISABLE_RUBYGEMS 0
87#define DEFAULT_RUBYGEMS_ENABLED "disabled"
89#define DEFAULT_RUBYGEMS_ENABLED "enabled"
92void rb_warning_category_update(
unsigned int mask,
unsigned int bits);
95#define FEATURE_BIT(bit) (1U << feature_##bit)
96#define EACH_FEATURES(X, SEP) \
107 X(frozen_string_literal) \
113#define EACH_DEBUG_FEATURES(X, SEP) \
114 X(frozen_string_literal) \
116#define AMBIGUOUS_FEATURE_NAMES 0
117#define DEFINE_FEATURE(bit) feature_##bit
118#define DEFINE_DEBUG_FEATURE(bit) feature_debug_##bit
119enum feature_flag_bits {
120 EACH_FEATURES(DEFINE_FEATURE, COMMA),
121 DEFINE_FEATURE(frozen_string_literal_set),
122 feature_debug_flag_first,
123#if !USE_YJIT && USE_ZJIT
124 DEFINE_FEATURE(jit) = feature_zjit,
126 DEFINE_FEATURE(jit) = feature_yjit,
128 feature_jit_mask = FEATURE_BIT(yjit) | FEATURE_BIT(zjit),
130 feature_debug_flag_begin = feature_debug_flag_first - 1,
131 EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
135#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
137#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
139#define DUMP_BIT(bit) (1U << dump_##bit)
140#define DEFINE_DUMP(bit) dump_##bit
141#define EACH_DUMPS(X, SEP) \
160 dump_opt_error_tolerant,
163 EACH_DUMPS(DEFINE_DUMP, COMMA),
164 dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
165 DUMP_BIT(parsetree) | DUMP_BIT(insns)),
166 dump_optional_bits = (DUMP_BIT(opt_error_tolerant) |
167 DUMP_BIT(opt_comment) |
168 DUMP_BIT(opt_optimize))
172rb_feature_set_to(
ruby_features_t *feat,
unsigned int bit_mask,
unsigned int bit_set)
174 feat->mask |= bit_mask;
175 feat->set = (feat->set & ~bit_mask) | bit_set;
178#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
179 rb_feature_set_to(&(feat), bit_mask, bit_set)
180#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
181#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
182#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
183#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
184#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
188#define src_encoding_index GET_VM()->src_encoding_index
191 COMPILATION_FEATURES = (
193 | FEATURE_BIT(frozen_string_literal)
194 | FEATURE_BIT(frozen_string_literal_set)
195 | FEATURE_BIT(debug_frozen_string_literal)
198 (FEATURE_BIT(debug_flag_first)-1)
202 & ~FEATURE_BIT(frozen_string_literal)
203 & ~FEATURE_BIT(frozen_string_literal_set)
208#define BACKTRACE_LENGTH_LIMIT_VALID_P(n) ((n) >= -1)
209#define OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt) \
210 BACKTRACE_LENGTH_LIMIT_VALID_P((opt)->backtrace_length_limit)
217 opt->src.enc.index = src_encoding_index;
218 opt->ext.enc.index = -1;
219 opt->intern.enc.index = -1;
220 opt->features.set = DEFAULT_FEATURES;
221#if defined(YJIT_FORCE_ENABLE)
222 opt->features.set |= FEATURE_BIT(yjit);
224 opt->dump |= DUMP_BIT(opt_optimize);
225 opt->backtrace_length_limit = LONG_MIN;
232static VALUE open_load_file(
VALUE fname_v,
int *xflag);
234#define forbid_setid(s) forbid_setid((s), opt)
241static const char esc_standout[] =
"\n\033[1;7m";
242static const char esc_bold[] =
"\033[1m";
243static const char esc_reset[] =
"\033[0m";
244static const char esc_none[] =
"";
245#define USAGE_INDENT " "
248show_usage_part(
const char *str,
const unsigned int namelen,
249 const char *str2,
const unsigned int secondlen,
251 int help,
int highlight,
unsigned int w,
int columns)
253 static const int indent_width = (int)
rb_strlen_lit(USAGE_INDENT);
254 const char *sb = highlight ? esc_bold : esc_none;
255 const char *se = highlight ? esc_reset : esc_none;
256 unsigned int desclen = (
unsigned int)strcspn(desc,
"\n");
257 if (!help && desclen > 0 && strchr(
".;:", desc[desclen-1])) --desclen;
258 if (help && (namelen + 1 > w) &&
259 (int)(namelen + secondlen + indent_width) >= columns) {
260 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, namelen, str, se);
262 const int second_end = secondlen;
264 if (str2[n] ==
',') n++;
265 if (str2[n] ==
' ') n++;
266 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, second_end-n, str2+n, se);
268 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
271 const int wrap = help && namelen + secondlen >= w;
272 printf(USAGE_INDENT
"%s%.*s%-*.*s%s%-*s%.*s\n", sb, namelen, str,
273 (wrap ? 0 : w - namelen),
274 (help ? secondlen : 0), str2, se,
276 (wrap ?
"\n" USAGE_INDENT :
""),
280 while (desc[desclen]) {
282 desclen = (
unsigned int)strcspn(desc,
"\n");
283 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
290 int help,
int highlight,
unsigned int w,
int columns)
292 const char *str = m->str;
293 const unsigned int namelen = m->namelen, secondlen = m->secondlen;
294 const char *desc = str + namelen + secondlen;
295 show_usage_part(str, namelen - 1, str + namelen, secondlen - 1, desc,
296 help, highlight, w, columns);
300ruby_show_usage_line(
const char *name,
const char *secondary,
const char *description,
301 int help,
int highlight,
unsigned int width,
int columns)
303 unsigned int namelen = (
unsigned int)strlen(name);
304 unsigned int secondlen = (secondary ? (
unsigned int)strlen(secondary) : 0);
305 show_usage_part(name, namelen, secondary, secondlen,
306 description, help, highlight, width, columns);
312usage(
const char *name,
int help,
int highlight,
int columns)
314#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
317# define DEFAULT_JIT_OPTION "--yjit"
319# define DEFAULT_JIT_OPTION "--zjit"
325 M(
"-0[octal]",
"",
"Set input record separator ($/):\n"
326 "-0 for \\0; -00 for paragraph mode; -0777 for slurp mode."),
327 M(
"-a",
"",
"Split each input line ($_) into fields ($F)."),
328 M(
"-c",
"",
"Check syntax (no execution)."),
329 M(
"-Cdirpath",
"",
"Execute program in specified directory."),
330 M(
"-d",
", --debug",
"Set debugging flag ($DEBUG) and $VERBOSE to true."),
331 M(
"-e 'code'",
"",
"Execute given Ruby code; multiple -e allowed."),
332 M(
"-Eex[:in]",
", --encoding=ex[:in]",
"Set default external and internal encodings."),
333 M(
"-Fpattern",
"",
"Set input field separator ($;); used with -a."),
334 M(
"-i[extension]",
"",
"Set ARGF in-place mode;\n"
335 "create backup files with given extension."),
336 M(
"-Idirpath",
"",
"Prepend specified directory to load paths ($LOAD_PATH);\n"
337 "relative paths are expanded; multiple -I are allowed."),
338 M(
"-l",
"",
"Set output record separator ($\\) to $/;\n"
339 "used for line-oriented output."),
340 M(
"-n",
"",
"Run program in gets loop."),
341 M(
"-p",
"",
"Like -n, with printing added."),
342 M(
"-rlibrary",
"",
"Require the given library."),
343 M(
"-s",
"",
"Define global variables using switches following program path."),
344 M(
"-S",
"",
"Search directories found in the PATH environment variable."),
345 M(
"-v",
"",
"Print version; set $VERBOSE to true."),
346 M(
"-w",
"",
"Synonym for -W1."),
347 M(
"-W[level=2|:category]",
"",
"Set warning flag ($-W):\n"
348 "0 for silent; 1 for moderate; 2 for verbose."),
349 M(
"-x[dirpath]",
"",
"Execute Ruby code starting from a #!ruby line."),
350#if USE_YJIT || USE_ZJIT
351 M(
"--jit",
"",
"Enable the default JIT for the build; same as " DEFAULT_JIT_OPTION
"."),
354 M(
"--yjit",
"",
"Enable in-process JIT compiler."),
357 M(
"--zjit",
"",
"Enable method-based JIT compiler."),
359 M(
"-h",
"",
"Print this help message; use --help for longer message."),
361 STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 26);
364 M(
"--backtrace-limit=num",
"",
"Set backtrace limit."),
365 M(
"--copyright",
"",
"Print Ruby copyright."),
366 M(
"--crash-report=template",
"",
"Set template for crash report file."),
367 M(
"--disable=features",
"",
"Disable features; see list below."),
368 M(
"--dump=items",
"",
"Dump items; see list below."),
369 M(
"--enable=features",
"",
"Enable features; see list below."),
370 M(
"--external-encoding=encoding",
"",
"Set default external encoding."),
371 M(
"--help",
"",
"Print long help message; use -h for short message."),
372 M(
"--internal-encoding=encoding",
"",
"Set default internal encoding."),
373 M(
"--parser=parser",
"",
"Set Ruby parser: parse.y or prism."),
374 M(
"--verbose",
"",
"Set $VERBOSE to true; ignore input from $stdin."),
375 M(
"--version",
"",
"Print Ruby version."),
376 M(
"-y",
", --yydebug",
"Print parser log; backward compatibility not guaranteed."),
379 M(
"insns",
"",
"Instruction sequences."),
380 M(
"yydebug",
"",
"yydebug of yacc parser generator."),
381 M(
"parsetree",
"",
"Abstract syntax tree (AST)."),
382 M(
"-optimize",
"",
"Disable optimization (affects insns)."),
383 M(
"+error-tolerant",
"",
"Error-tolerant parsing (affects yydebug, parsetree)."),
384 M(
"+comment",
"",
"Add comments to AST (affects parsetree with --parser=parse.y)."),
387 M(
"gems",
"",
"Rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED
")."),
388 M(
"error_highlight",
"",
"error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
389 M(
"did_you_mean",
"",
"did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
390 M(
"syntax_suggest",
"",
"syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
391 M(
"rubyopt",
"",
"RUBYOPT environment variable (default: enabled)."),
392 M(
"frozen-string-literal",
"",
"Freeze all string literals (default: disabled)."),
394 M(
"yjit",
"",
"In-process JIT compiler (default: disabled)."),
397 M(
"zjit",
"",
"Method-based JIT compiler (default: disabled)."),
401 M(
"deprecated",
"",
"Deprecated features."),
402 M(
"experimental",
"",
"Experimental features."),
403 M(
"performance",
"",
"Performance issues."),
404 M(
"strict_unused_block",
"",
"Warning unused block strictly"),
407 const char *sb = highlight ? esc_standout+1 : esc_none;
408 const char *se = highlight ? esc_reset : esc_none;
409 const int num = numberof(usage_msg) - (help ? 1 : 0);
410 unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
411#define SHOW(m) show_usage_line(&(m), help, highlight, w, columns)
413 printf(
"%sUsage:%s %s [options] [--] [filepath] [arguments]\n", sb, se, name);
414 for (i = 0; i < num; ++i)
419 if (highlight) sb = esc_standout;
421 for (i = 0; i < numberof(help_msg); ++i)
423 printf(
"%s""Dump List:%s\n", sb, se);
424 for (i = 0; i < numberof(dumps); ++i)
426 printf(
"%s""Features:%s\n", sb, se);
427 for (i = 0; i < numberof(features); ++i)
429 printf(
"%s""Warning categories:%s\n", sb, se);
430 for (i = 0; i < numberof(warn_categories); ++i)
431 SHOW(warn_categories[i]);
433 printf(
"%s""YJIT options:%s\n", sb, se);
434 rb_yjit_show_usage(help, highlight, w, columns);
437 printf(
"%s""ZJIT options:%s\n", sb, se);
438 extern void rb_zjit_show_usage(
int help,
int highlight,
unsigned int width,
int columns);
439 rb_zjit_show_usage(help, highlight, w, columns);
443#define rubylib_path_new rb_str_new
446ruby_push_include(
const char *path,
VALUE (*filter)(
VALUE))
450 VALUE load_path = rb_root_box()->load_path;
452 char rubylib[FILENAME_MAX];
454# define is_path_sep(c) ((c) == sep || (c) == ';')
456# define is_path_sep(c) ((c) == sep)
459 if (path == 0)
return;
463 while (is_path_sep(*p))
467 for (s = p; *s && !is_path_sep(*s); s++);
475 p = RSTRING_PTR(buf);
478 rb_str_resize(buf,
len);
479 p = strncpy(RSTRING_PTR(buf), p,
len);
482#ifdef HAVE_CYGWIN_CONV_PATH
483#define CONV_TO_POSIX_PATH(p, lib) \
484 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
486# error no cygwin_conv_path
488 if (CONV_TO_POSIX_PATH(p, rubylib) == 0) {
499identical_path(
VALUE path)
505locale_path(
VALUE path)
507 rb_enc_associate(path, rb_locale_encoding());
514 ruby_push_include(path, locale_path);
518expand_include_path(
VALUE path)
520 char *p = RSTRING_PTR(path);
523 if (*p ==
'.' && p[1] ==
'/')
525 return rb_file_expand_path(path,
Qnil);
529ruby_incpush_expand(
const char *path)
531 ruby_push_include(path, expand_include_path);
535#if defined _WIN32 || defined __CYGWIN__
536static HMODULE libruby;
539DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
541 if (reason == DLL_PROCESS_ATTACH)
547rb_libruby_handle(
void)
553translit_char_bin(
char *p,
int from,
int to)
556 if ((
unsigned char)*p == from)
565# define chdir rb_w32_uchdir
573# define IF_UTF8_PATH(t, f) t
575# define IF_UTF8_PATH(t, f) f
587# define str_conv_enc(str, from, to) (str)
592#if defined(LOAD_RELATIVE)
594runtime_libruby_path(
void)
596#if defined _WIN32 || defined __CYGWIN__
604 while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
605 ret = GetModuleFileNameW(libruby, wlibpath,
len),
611 if (!ret || ret >
len) rb_fatal(
"failed to get module file name");
612#if defined __CYGWIN__
614 const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
615 size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
616 if (!newsize) rb_fatal(
"failed to convert module path to cygwin");
618 libpath = RSTRING_PTR(path);
619 if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
620 rb_str_resize(path, 0);
626 for (
len = ret, i = 0; i <
len; ++i) {
627 if (wlibpath[i] == L
'\\') {
633 len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
635 libpath = RSTRING_PTR(path);
636 WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath,
len, NULL, NULL);
638 rb_str_resize(wsopath, 0);
640#elif defined(HAVE_DLADDR)
643 const void* addr = (
void *)(
VALUE)expand_include_path;
645 if (!dladdr((
void *)addr, &dli)) {
649 else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
651 path = rb_readlink(fname, NULL);
656 path = rb_realpath_internal(
Qnil, fname, 1);
658 rb_str_resize(fname, 0);
661# error relative load path is not supported on this platform.
666#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
668VALUE ruby_archlibdir_path, ruby_prefix_path;
673 VALUE load_path, archlibdir = 0;
674 ID id_initial_load_path_mark;
675 const char *paths = ruby_initial_load_paths;
677#if defined LOAD_RELATIVE
678#if !defined ENABLE_MULTIARCH
679# define RUBY_ARCH_PATH ""
680#elif defined RUBY_ARCH
681# define RUBY_ARCH_PATH "/"RUBY_ARCH
683# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
690 sopath = runtime_libruby_path();
691 libpath = RSTRING_PTR(sopath);
693 p = strrchr(libpath,
'/');
695 static const char libdir[] =
"/"
696#ifdef LIBDIR_BASENAME
702 const ptrdiff_t libdir_len = (ptrdiff_t)
sizeof(libdir)
704 static const char bindir[] =
"/bin";
705 const ptrdiff_t bindir_len = (ptrdiff_t)
sizeof(bindir) - 1;
707 const char *p2 = NULL;
709#ifdef ENABLE_MULTIARCH
712 if (p - libpath >= bindir_len && !
STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
718 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
719 archlibdir =
rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
723#ifdef ENABLE_MULTIARCH
729 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
730 if (p)
goto multiarch;
734 baselen = p - libpath;
739 rb_str_resize(sopath, baselen);
740 libpath = RSTRING_PTR(sopath);
741#define PREFIX_PATH() sopath
742#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
743#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
745 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
746#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
747#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
749 rb_gc_register_address(&ruby_prefix_path);
750 ruby_prefix_path = PREFIX_PATH();
752 if (!archlibdir) archlibdir = ruby_prefix_path;
753 rb_gc_register_address(&ruby_archlibdir_path);
754 ruby_archlibdir_path = archlibdir;
756 load_path = rb_root_box()->load_path;
758 ruby_push_include(getenv(
"RUBYLIB"), identical_path);
760 id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
762 size_t len = strlen(paths);
763 VALUE path = RUBY_RELATIVE(paths,
len);
764 rb_ivar_set(path, id_initial_load_path_mark, path);
772add_modules(
VALUE *req_list,
const char *mod)
774 VALUE list = *req_list;
785require_libraries(
VALUE *req_list)
787 VALUE list = *req_list;
788 VALUE self = rb_vm_top_self();
790 rb_encoding *extenc = rb_default_external_encoding();
795 rb_enc_associate(feature, extenc);
804require_libraries_in_main_box(
VALUE *req_list)
806 const rb_box_t *main_box = rb_main_box();
807 VALUE list = *req_list;
809 rb_encoding *extenc = rb_default_external_encoding();
814 rb_enc_associate(feature, extenc);
817 rb_funcallv(main_box->box_object, require, 1, &feature);
829process_sflag(
int sflag)
847 if (s[1] ==
'-' && s[2] ==
'\0')
852 for (p = s + 1; *p; p++) {
861 else if (*p !=
'_' && !
ISALNUM(*p)) {
864 rb_str_new2(
"invalid name for global variable - ");
865 if (!(p = strchr(p,
'='))) {
871 name_error[1] = args[-1];
877 for (p = s + 1; *p; ++p) {
902 VALUE argstr, argary;
904 VALUE src_enc_name = opt->src.enc.name;
905 VALUE ext_enc_name = opt->ext.enc.name;
906 VALUE int_enc_name = opt->intern.enc.name;
909 long backtrace_length_limit = opt->backtrace_length_limit;
910 const char *crash_report = opt->crash_report;
915 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
917 const int hyphen = *s !=
'-';
921 p = RSTRING_PTR(argstr);
922 if (hyphen) *p =
'-';
923 memcpy(p + hyphen, s,
len + 1);
929 while (*p && !
ISSPACE(*p)) ++p;
934 argc = RSTRING_LEN(argary) /
sizeof(ap);
940 MEMMOVE(argv, RSTRING_PTR(argary),
char *, argc);
942 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
954 opt->src.enc.name = src_enc_name;
957 opt->ext.enc.name = ext_enc_name;
960 opt->intern.enc.name = int_enc_name;
962 FEATURE_SET_RESTORE(opt->features, feat);
963 FEATURE_SET_RESTORE(opt->warn, warn);
964 if (BACKTRACE_LENGTH_LIMIT_VALID_P(backtrace_length_limit)) {
965 opt->backtrace_length_limit = backtrace_length_limit;
968 opt->crash_report = crash_report;
974 rb_str_resize(argary, 0);
975 rb_str_resize(argstr, 0);
979name_match_p(
const char *name,
const char *str,
size_t len)
981 if (
len == 0)
return 0;
983 while (
TOLOWER(*str) == *name) {
984 if (!--
len)
return 1;
988 if (*str !=
'-' && *str !=
'_')
return 0;
990 if (*name !=
'-' && *name !=
'_')
return 0;
991 if (!*++name)
return 1;
993 if (--
len == 0)
return 1;
997#define NAME_MATCH_P(name, str, len) \
998 ((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
1000#define UNSET_WHEN(name, bit, str, len) \
1001 if (NAME_MATCH_P((name), (str), (len))) { \
1002 *(unsigned int *)arg &= ~(bit); \
1006#define SET_WHEN(name, bit, str, len) \
1007 if (NAME_MATCH_P((name), (str), (len))) { \
1008 *(unsigned int *)arg |= (bit); \
1012#define LITERAL_NAME_ELEMENT(name) #name
1015feature_option(
const char *str,
int len,
void *arg,
const unsigned int enable)
1017 static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1019 unsigned int mask = ~0
U;
1020 unsigned int set = 0
U;
1021#if AMBIGUOUS_FEATURE_NAMES
1023# define FEATURE_FOUND ++matched
1025# define FEATURE_FOUND goto found
1027#define SET_FEATURE(bit) \
1028 if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;}
1029 EACH_FEATURES(SET_FEATURE, ;);
1030 if (NAME_MATCH_P(
"jit", str,
len)) {
1031 set |= mask = FEATURE_BIT(jit);
1034 if (NAME_MATCH_P(
"all", str,
len)) {
1036 mask &= ~feature_jit_mask | FEATURE_BIT(jit);
1039#if AMBIGUOUS_FEATURE_NAMES
1040 if (matched == 1)
goto found;
1042 VALUE mesg = rb_sprintf(
"ambiguous feature: '%.*s' (",
len, str);
1043#define ADD_FEATURE_NAME(bit) \
1044 if (FEATURE_BIT(bit) & set) { \
1045 rb_str_cat_cstr(mesg, #bit); \
1046 if (--matched) rb_str_cat_cstr(mesg, ", "); \
1048 EACH_FEATURES(ADD_FEATURE_NAME, ;);
1051#undef ADD_FEATURE_NAME
1056 rb_warn(
"unknown argument for --%s: '%.*s'",
1057 enable ?
"enable" :
"disable",
len, str);
1058 rb_warn(
"features are [%.*s].", (
int)strlen(list), list);
1062 FEATURE_SET_TO(*argp, mask, (mask & enable));
1063 if (NAME_MATCH_P(
"frozen_string_literal", str,
len)) {
1064 FEATURE_SET_TO(*argp, FEATURE_BIT(frozen_string_literal_set), FEATURE_BIT(frozen_string_literal_set));
1070enable_option(
const char *str,
int len,
void *arg)
1072 feature_option(str,
len, arg, ~0
U);
1076disable_option(
const char *str,
int len,
void *arg)
1078 feature_option(str,
len, arg, 0
U);
1082int ruby_env_debug_option(
const char *str,
int len,
void *arg);
1085debug_option(
const char *str,
int len,
void *arg)
1087 static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1089#define SET_WHEN_DEBUG(bit) \
1090 if (NAME_MATCH_P(#bit, str, len)) { \
1091 FEATURE_SET(*argp, DEBUG_BIT(bit)); \
1094 EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;);
1098 rb_warn(
"unknown argument for --debug: '%.*s'",
len, str);
1099 rb_warn(
"debug features are [%.*s].", (
int)strlen(list), list);
1103memtermspn(
const char *str,
char term,
int len)
1106 if (
len <= 0)
return 0;
1107 const char *next = memchr(str, term,
len);
1108 return next ? (int)(next - str) :
len;
1111static const char additional_opt_sep =
'+';
1114dump_additional_option_flag(
const char *str,
int len,
unsigned int bits,
bool set)
1116#define SET_DUMP_OPT(bit) if (NAME_MATCH_P(#bit, str, len)) { \
1117 return set ? (bits | DUMP_BIT(opt_ ## bit)) : (bits & ~DUMP_BIT(opt_ ## bit)); \
1119 SET_DUMP_OPT(error_tolerant);
1120 SET_DUMP_OPT(comment);
1121 SET_DUMP_OPT(optimize);
1123 rb_warn(
"don't know how to dump with%s '%.*s'", set ?
"" :
"out",
len, str);
1128dump_additional_option(
const char *str,
int len,
unsigned int bits)
1131 for (;
len-- > 0 && *str++ == additional_opt_sep;
len -= w, str += w) {
1132 w = memtermspn(str, additional_opt_sep,
len);
1134 if (*str ==
'-' || *str ==
'+') {
1135 set = *str++ ==
'+';
1139 int n = memtermspn(str,
'-', w);
1140 if (str[n] ==
'-') {
1141 if (NAME_MATCH_P(
"with", str, n)) {
1145 else if (NAME_MATCH_P(
"without", str, n)) {
1152 bits = dump_additional_option_flag(str, w, bits, set);
1158dump_option(
const char *str,
int len,
void *arg)
1160 static const char list[] = EACH_DUMPS(LITERAL_NAME_ELEMENT,
", ");
1161 unsigned int *bits_ptr = (
unsigned int *)arg;
1162 if (*str ==
'+' || *str ==
'-') {
1163 bool set = *str++ ==
'+';
1164 *bits_ptr = dump_additional_option_flag(str, --
len, *bits_ptr, set);
1167 int w = memtermspn(str, additional_opt_sep,
len);
1169#define SET_WHEN_DUMP(bit) \
1170 if (NAME_MATCH_P(#bit "-", (str), (w))) { \
1171 *bits_ptr = dump_additional_option(str + w, len - w, *bits_ptr | DUMP_BIT(bit)); \
1174 EACH_DUMPS(SET_WHEN_DUMP, ;);
1175 rb_warn(
"don't know how to dump '%.*s',",
len, str);
1176 rb_warn(
"but only [%.*s].", (
int)strlen(list), list);
1180set_option_encoding_once(
const char *
type,
VALUE *name,
const char *e,
rb_len_t elen)
1184 if (!elen) elen = strlen(e);
1190 "%s already set to %"PRIsVALUE,
type, *name);
1195#define set_internal_encoding_once(opt, e, elen) \
1196 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
1197#define set_external_encoding_once(opt, e, elen) \
1198 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
1199#define set_source_encoding_once(opt, e, elen) \
1200 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
1202#define yjit_opt_match_noarg(s, l, name) \
1203 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --yjit-" name " is ignored"), 1) : 1)
1204#define yjit_opt_match_arg(s, l, name) \
1205 opt_match(s, l, name) && (*(s) && *(s+1) ? 1 : (rb_raise(rb_eRuntimeError, "--yjit-" name " needs an argument"), 0))
1209setup_yjit_options(
const char *s)
1212 bool rb_yjit_parse_option(
const char* s);
1213 bool success = rb_yjit_parse_option(s);
1221 "invalid YJIT option '%s' (--help will show valid yjit options)",
1229setup_zjit_options(
const char *s)
1232 extern bool rb_zjit_parse_option(
const char *s);
1234 if (!rb_zjit_parse_option(s)) {
1235 rb_raise(
rb_eRuntimeError,
"invalid ZJIT option '%s' (--help will show valid zjit options)", s);
1257 unsigned int bits = 0;
1258 static const char no_prefix[] =
"no-";
1259 int enable = strncmp(s += 2, no_prefix,
sizeof(no_prefix)-1) != 0;
1260 if (!enable) s +=
sizeof(no_prefix)-1;
1261 size_t len = strlen(s);
1262 if (NAME_MATCH_P(
"deprecated", s,
len)) {
1265 else if (NAME_MATCH_P(
"experimental", s,
len)) {
1268 else if (NAME_MATCH_P(
"performance", s,
len)) {
1271 else if (NAME_MATCH_P(
"strict_unused_block", s,
len)) {
1275 rb_warn(
"unknown warning category: '%s'", s);
1277 if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
1290 if (!opt->warning) {
1306 FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS, 0);
1312 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1331 if (!opt->e_script) {
1333 if (opt->script == 0)
1346 const char *enc_name = 0;
1349 enc_name =
"EUC-JP";
1352 enc_name =
"Windows-31J";
1357 case 'N':
case 'n':
case 'A':
case 'a':
1358 enc_name =
"ASCII-8BIT";
1363 if (!opt->ext.enc.name)
1364 opt->ext.enc.name = opt->src.enc.name;
1383 else if (v == 0 && numlen >= 2) {
1384 rb_rs = rb_fstring_lit(
"");
1398# define set_encoding_part(type) \
1399 if (!(p = strchr(s, ':'))) { \
1400 set_##type##_encoding_once(opt, s, 0); \
1404 set_##type##_encoding_once(opt, s, p-s); \
1406 set_encoding_part(external);
1407 if (!*(s = ++p))
return;
1408 set_encoding_part(internal);
1409 if (!*(s = ++p))
return;
1410#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1411 set_encoding_part(source);
1412 if (!*(s = ++p))
return;
1415# undef set_encoding_part
1424# define is_option_end(c, allow_hyphen) \
1425 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1426# define check_envopt(name, allow_envopt) \
1427 (((allow_envopt) || !envopt) ? (void)0 : \
1428 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1429# define need_argument(name, s, needs_arg, next_arg) \
1430 ((*(s) ? !*++(s) : (next_arg) && (argc <= 1 || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
1431 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1433# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1434 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
1435# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
1436 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
1437 (s[n] != '-' || (s[n] && s[n+1])) ? \
1438 (check_envopt(name, (allow_envopt)), s += n, \
1439 need_argument(name, s, needs_arg, next_arg), 1) : 0)
1441 if (strcmp(
"copyright", s) == 0) {
1442 if (envopt)
goto noenvopt_long;
1443 opt->dump |= DUMP_BIT(copyright);
1454 else if (is_option_with_arg(
"enable",
Qtrue,
Qtrue)) {
1457 else if (is_option_with_arg(
"disable",
Qtrue,
Qtrue)) {
1460 else if (is_option_with_arg(
"encoding",
Qfalse,
Qtrue)) {
1461 proc_encoding_option(opt, s,
"--encoding");
1463 else if (is_option_with_arg(
"internal-encoding",
Qfalse,
Qtrue)) {
1464 set_internal_encoding_once(opt, s, 0);
1466 else if (is_option_with_arg(
"external-encoding",
Qfalse,
Qtrue)) {
1467 set_external_encoding_once(opt, s, 0);
1469 else if (is_option_with_arg(
"parser",
Qfalse,
Qtrue)) {
1470 if (strcmp(
"prism", s) == 0) {
1471 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PRISM);
1473 else if (strcmp(
"parse.y", s) == 0) {
1474 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PARSE_Y);
1480#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1481 else if (is_option_with_arg(
"source-encoding",
Qfalse,
Qtrue)) {
1482 set_source_encoding_once(opt, s, 0);
1485 else if (strcmp(
"version", s) == 0) {
1486 if (envopt)
goto noenvopt_long;
1487 opt->dump |= DUMP_BIT(version);
1489 else if (strcmp(
"verbose", s) == 0) {
1493 else if (strcmp(
"jit", s) == 0) {
1494#if USE_YJIT || USE_ZJIT
1495 FEATURE_SET(opt->features, FEATURE_BIT(jit));
1497 rb_warn(
"Ruby was built without JIT support");
1500 else if (is_option_with_optarg(
"yjit",
'-',
true,
false,
false)) {
1502 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1503 setup_yjit_options(s);
1505 rb_warn(
"Ruby was built without YJIT support."
1506 " You may need to install rustc and configure with --enable-yjit to build Ruby with YJIT.");
1509 else if (is_option_with_optarg(
"zjit",
'-',
true,
false,
false)) {
1511 FEATURE_SET(opt->features, FEATURE_BIT(zjit));
1512 setup_zjit_options(s);
1514 rb_warn(
"Ruby was built without ZJIT support."
1515 " You may need to install rustc to build Ruby with ZJIT.");
1518 else if (strcmp(
"yydebug", s) == 0) {
1519 if (envopt)
goto noenvopt_long;
1520 opt->dump |= DUMP_BIT(yydebug);
1525 else if (strcmp(
"help", s) == 0) {
1526 if (envopt)
goto noenvopt_long;
1527 opt->dump |= DUMP_BIT(help);
1530 else if (is_option_with_arg(
"backtrace-limit",
Qfalse,
Qtrue)) {
1532 long n = strtol(s, &e, 10);
1533 if (
errno == ERANGE || !BACKTRACE_LENGTH_LIMIT_VALID_P(n) || *e) {
1537 opt->backtrace_length_limit = n;
1540 else if (is_option_with_arg(
"crash-report",
true,
true)) {
1541 opt->crash_report = s;
1545 "invalid option --%s (-h will show valid options)", s);
1547 return argc0 - argc + 1;
1551# undef is_option_end
1553# undef need_argument
1554# undef is_option_with_arg
1555# undef is_option_with_optarg
1562 long n, argc0 = argc;
1564 int warning = opt->warning;
1566 if (argc <= 0 || !argv)
1569 for (argc--, argv++; argc > 0; argc--, argv++) {
1570 const char *
const arg = argv[0];
1571 if (!arg || arg[0] !=
'-' || !arg[1])
1578 if (envopt)
goto noenvopt;
1579 opt->do_split = TRUE;
1584 if (envopt)
goto noenvopt;
1585 opt->do_print = TRUE;
1588 if (envopt)
goto noenvopt;
1589 opt->do_loop = TRUE;
1600 if (envopt)
goto noenvopt;
1601 opt->dump |= DUMP_BIT(yydebug);
1610 opt->dump |= DUMP_BIT(version_v);
1613 if (!opt->warning) {
1617 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1622 if (!(s = proc_W_option(opt, s, &warning)))
break;
1626 if (envopt)
goto noenvopt;
1627 opt->dump |= DUMP_BIT(syntax);
1632 if (envopt)
goto noenvopt;
1634 if (!opt->sflag) opt->sflag = 1;
1639 if (envopt)
goto noenvopt;
1640 opt->dump |= DUMP_BIT(usage);
1644 if (envopt)
goto noenvopt;
1645 opt->do_line = TRUE;
1651 if (envopt)
goto noenvopt;
1653 opt->do_search = TRUE;
1658 if (envopt)
goto noenvopt;
1659 if (!(n = proc_e_option(opt, s, argc, argv)))
break;
1668 add_modules(&opt->req_list, s);
1670 else if (argc > 1) {
1671 add_modules(&opt->req_list, argv[1]);
1677 if (envopt)
goto noenvopt;
1679 ruby_set_inplace_mode(s + 1);
1683 if (envopt)
goto noenvopt;
1687 if (*s && chdir(s) < 0) {
1688 rb_fatal(
"Can't chdir to %s", s);
1694 if (envopt)
goto noenvopt;
1695 if (!*++s && (!--argc || !(s = *++argv) || !*s)) {
1696 rb_fatal(
"Can't chdir");
1699 rb_fatal(
"Can't chdir to %s", s);
1704 if (envopt)
goto noenvopt;
1711 if (!*++s && (!--argc || !(s = *++argv))) {
1714 proc_encoding_option(opt, s,
"-E");
1718 set_internal_encoding_once(opt,
"UTF-8", 0);
1723 if (!(s = proc_K_option(opt, s)))
break;
1729 ruby_incpush_expand(s);
1730 else if (argc > 1) {
1731 ruby_incpush_expand(argv[1]);
1737 if (envopt)
goto noenvopt;
1738 if (!(s = proc_0_option(opt, s)))
break;
1742 if (!s[1] || (s[1] ==
'\r' && !s[2])) {
1748 if (!(n = proc_long_options(opt, s, argc, argv, envopt)))
goto switch_end;
1759 rb_encoding *enc = IF_UTF8_PATH(rb_utf8_encoding(), rb_locale_encoding());
1760 const char *e = s + strlen(s);
1761 int r = rb_enc_precise_mbclen(s, e, enc);
1762 unsigned int c = (
unsigned char)*s;
1764 c = rb_enc_mbc_to_codepoint(s, e, enc);
1765 if (ONIGENC_IS_CODE_GRAPH(enc, c) ||
1766 ((s = ruby_escaped_char(c)) != 0 &&
1767 (r = (
int)strlen(s), 1))) {
1769 "invalid option -%.*s (-h will show valid options)",
1774 "invalid option -\\x%.2x (-h will show valid options)",
1791 if (warning) opt->warning = warning;
1792 return argc0 - argc;
1796void rb_prism_init_autoload(
void);
1797void Init_builtin_features(
void);
1800ruby_init_prelude(
void)
1802 Init_builtin_features();
1805void rb_call_builtin_inits(
void);
1809#if RBIMPL_HAS_ATTRIBUTE(weak)
1813Init_extra_exts(
void)
1820 rb_warning_category_update(opt->warn.mask, opt->warn.set);
1822 if (opt->dump & dump_exit_bits)
return;
1827 GET_VM()->running = 0;
1828 rb_call_builtin_inits();
1829 GET_VM()->running = 1;
1830 memset(ruby_vm_redefined_flag, 0,
sizeof(ruby_vm_redefined_flag));
1835 rb_yjit_init_builtin_cmes();
1838 extern void rb_zjit_init_builtin_cmes(
void);
1839 rb_zjit_init_builtin_cmes();
1844 if (rb_ruby_prism_p()) {
1845 rb_prism_init_autoload();
1852 if (rb_box_available()) {
1853 rb_initialize_mandatory_boxes();
1858 if (FEATURE_SET_P(opt->features, gems)) {
1860 .gem = FEATURE_SET_P(opt->features, gems),
1861 .error_highlight = opt->features.set & FEATURE_BIT(error_highlight),
1862 .did_you_mean = opt->features.set & FEATURE_BIT(did_you_mean),
1863 .syntax_suggest = opt->features.set & FEATURE_BIT(syntax_suggest)
1866 if (rb_box_available()) {
1867 rb_vm_call_cfunc_in_box(
Qnil, rb_define_gem_modules, (
VALUE)&gem_flags,
Qnil,
1869 rb_vm_call_cfunc_in_box(
Qnil, rb_define_gem_modules, (
VALUE)&gem_flags,
Qnil,
1872 rb_box_set_gem_flags(&gem_flags);
1875 rb_define_gem_modules((
VALUE)&gem_flags,
Qnil);
1881 ruby_init_prelude();
1885 rb_yjit_init(opt->yjit);
1888 extern void rb_zjit_init(
bool);
1889 rb_zjit_init(opt->zjit);
1893 if (rb_box_available()) {
1894 require_libraries_in_main_box(&opt->req_list);
1897 require_libraries(&opt->req_list);
1902opt_enc_index(
VALUE enc_name)
1904 const char *s = RSTRING_PTR(enc_name);
1905 int i = rb_enc_find_index(s);
1910 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1916#define rb_progname (GET_VM()->progname)
1917#define rb_orig_progname (GET_VM()->orig_progname)
1933#define rb_define_readonly_boolean(name, val) \
1934 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1943 rb_raise(
rb_eTypeError,
"$_ value need to be String (%s given)",
2025setup_pager_env(
void)
2027 if (!getenv(
"LESS")) {
2029 ruby_setenv(
"LESS",
"-R +/^[A-Z].*");
2037 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
2039 if (!GetConsoleMode(h, &m))
return 0;
2040# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
2041# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
2043 if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;
2046#elif !defined(HAVE_WORKING_FORK)
2047# define tty_enabled() 0
2056 return rb_enc_associate(
rb_str_dup(str), enc);
2061#if USE_YJIT || USE_ZJIT
2064env_var_truthy(
const char *name)
2066 const char *value = getenv(name);
2070 if (strcmp(value,
"1") == 0)
2072 if (strcmp(value,
"true") == 0)
2074 if (strcmp(value,
"yes") == 0)
2081rb_pid_t rb_fork_ruby(
int *status);
2084show_help(
const char *progname,
int help)
2086 int tty = isatty(1);
2089 const char *pager_env = getenv(
"RUBY_PAGER");
2090 if (!pager_env) pager_env = getenv(
"PAGER");
2091 if (pager_env && *pager_env && isatty(0)) {
2092 const char *columns_env = getenv(
"COLUMNS");
2093 if (columns_env) columns = atoi(columns_env);
2095#ifdef HAVE_WORKING_FORK
2098 rb_pid_t pid = rb_fork_ruby(NULL);
2103 else if (pid == 0) {
2121 int oldout = dup(1);
2122 int olderr = dup(2);
2123 int fd =
RFILE(port)->fptr->fd;
2124 tty = tty_enabled();
2127 usage(progname, 1, tty, columns);
2137 usage(progname, help, tty, columns);
2145 VALUE parser = rb_parser_new();
2146 const unsigned int dump = opt->dump;
2148 if (dump & DUMP_BIT(yydebug)) {
2149 rb_parser_set_yydebug(parser,
Qtrue);
2152 if ((dump & dump_exit_bits) && (dump & DUMP_BIT(opt_error_tolerant))) {
2153 rb_parser_error_tolerant(parser);
2156 if (opt->e_script) {
2157 VALUE progname = rb_progname;
2158 rb_parser_set_context(parser, 0, TRUE);
2162 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2163 opt->do_line, opt->do_split);
2164 ast_value = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
2168 int xflag = opt->xflag;
2169 f = open_load_file(opt->script_name, &xflag);
2170 opt->xflag = xflag != 0;
2171 rb_parser_set_context(parser, 0, f ==
rb_stdin);
2172 ast_value = load_file(parser, opt->script_name, f, 1, opt);
2174 ast = rb_ruby_ast_data_get(ast_value);
2175 if (!ast->body.root) {
2176 rb_ast_dispose(ast);
2185 uint8_t command_line = 0;
2191 return command_line;
2195prism_script_shebang_callback(
pm_options_t *options,
const uint8_t *source,
size_t length,
void *data)
2200 char *switches = malloc(length + 1);
2201 memcpy(switches, source, length);
2202 switches[length] =
'\0';
2204 int no_src_enc = !opt->src.enc.name;
2205 int no_ext_enc = !opt->ext.enc.name;
2206 int no_int_enc = !opt->intern.enc.name;
2208 moreswitches(switches, opt, 0);
2211 pm_options_command_line_set(options, prism_script_command_line(opt));
2213 if (no_src_enc && opt->src.enc.name) {
2214 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2217 if (no_ext_enc && opt->ext.enc.name) {
2218 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2220 if (no_int_enc && opt->intern.enc.name) {
2221 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2232 pm_parse_result_init(result);
2234 pm_options_main_script_set(options,
true);
2236 const bool read_stdin = (strcmp(opt->script,
"-") == 0);
2239 pm_options_encoding_set(options, rb_enc_name(IF_UTF8_PATH(rb_utf8_encoding(), rb_locale_encoding())));
2241 if (opt->src.enc.name != 0) {
2245 uint8_t command_line = prism_script_command_line(opt);
2249 pm_options_command_line_set(options, command_line);
2250 pm_options_filepath_set(options,
"-");
2251 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2254 error = pm_parse_stdin(result);
2263 else if (opt->e_script) {
2265 pm_options_command_line_set(options, command_line);
2268 result->
node.coverage_enabled = 0;
2269 error = pm_parse_string(result, opt->e_script,
rb_str_new2(
"-e"), NULL);
2272 VALUE script_name = rb_str_encode_ospath(opt->script_name);
2274 pm_options_command_line_set(options, command_line);
2275 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2277 error = pm_load_file(result, script_name,
true);
2284 error = pm_parse_file(result, opt->script_name, NULL);
2289 if (
RTEST(rb_get_coverages())) {
2290 result->
node.coverage_enabled = 1;
2297 int xflag = opt->xflag;
2298 VALUE file = open_load_file(script_name, &xflag);
2302 const uint8_t *start = pm_parser_start(parser);
2303 const uint8_t *end = pm_parser_end(parser);
2304 uint32_t offset = data_loc->
start + 7;
2306 if ((start + offset < end) && start[offset] ==
'\r') offset++;
2307 if ((start + offset < end) && start[offset] ==
'\n') offset++;
2314 if (!
NIL_P(error)) {
2315 pm_parse_result_free(result);
2325 pm_prettyprint(output_buffer, result->
parser, result->
node.ast_node);
2326 VALUE tree =
rb_str_new(pm_buffer_value(output_buffer), pm_buffer_length(output_buffer));
2327 pm_buffer_free(output_buffer);
2334 if (OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt)) {
2335 rb_backtrace_length_limit = opt->backtrace_length_limit;
2345 rb_define_readonly_boolean(
"$-p", opt->do_print);
2346 rb_define_readonly_boolean(
"$-l", opt->do_line);
2347 rb_define_readonly_boolean(
"$-a", opt->do_split);
2349 rb_gvar_ractor_local(
"$-p");
2350 rb_gvar_ractor_local(
"$-l");
2351 rb_gvar_ractor_local(
"$-a");
2353 if ((rb_e_script = opt->e_script) != 0) {
2355 rb_vm_register_global_object(opt->e_script);
2359 VALUE script = (opt->e_script ? opt->e_script :
Qnil);
2360 rb_exec_event_hook_script_compiled(ec, iseq, script);
2364has_dir_sep(
const char *path)
2366 if (strchr(path,
'/'))
return true;
2368 if (strchr(path,
'\\'))
return true;
2381#define dispose_result() \
2382 (result.ast ? rb_ast_dispose(result.ast) : pm_parse_result_free(&result.prism))
2391 char fbuf[MAXPATHLEN];
2392 int i = (int)proc_options(argc, argv, opt, 0);
2393 unsigned int dump = opt->dump & dump_exit_bits;
2394 const rb_box_t *box = rb_root_box();
2397 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
2398 const char *
const progname =
2399 (argc > 0 && argv && argv[0] ? argv[0] :
2400 origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
2402 show_help(progname, (opt->dump & DUMP_BIT(help)));
2409 if (FEATURE_SET_P(opt->features, rubyopt) && (s = getenv(
"RUBYOPT"))) {
2410 moreswitches(s, opt, 1);
2413 if (opt->src.enc.name)
2417 rb_warning(
"-K is specified; it is for 1.8 compatibility and may cause odd behavior");
2419 if (!(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2421 if (!FEATURE_USED_P(opt->features, yjit) && env_var_truthy(
"RUBY_YJIT_ENABLE")) {
2422 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
2426 if (!FEATURE_USED_P(opt->features, zjit) && env_var_truthy(
"RUBY_ZJIT_ENABLE")) {
2427 FEATURE_SET(opt->features, FEATURE_BIT(zjit));
2431 if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2432 rb_warn(
"Only one JIT can be enabled at the same time. Exiting");
2437 if (FEATURE_SET_P(opt->features, yjit)) {
2438 bool rb_yjit_option_disable(
void);
2439 opt->yjit = !rb_yjit_option_disable();
2443 if (FEATURE_SET_P(opt->features, zjit)) {
2445 extern void rb_zjit_prepare_options(
void);
2446 rb_zjit_prepare_options();
2447 bool rb_zjit_option_enable(
void);
2448 opt->zjit = rb_zjit_option_enable();
2452 ruby_mn_threads_params();
2453 Init_ruby_description(opt);
2455 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
2457 if (opt->dump & DUMP_BIT(version))
return Qtrue;
2459 if (opt->dump & DUMP_BIT(copyright)) {
2464 if (!opt->e_script) {
2471 opt->script = argv[0];
2472 if (!opt->script || opt->script[0] ==
'\0') {
2475 else if (opt->do_search && !has_dir_sep(opt->script)) {
2476 const char *path = getenv(
"RUBYPATH");
2480 opt->script = dln_find_file_r(argv[0], path, fbuf,
sizeof(fbuf));
2483 opt->script = dln_find_file_r(argv[0], getenv(
PATH_ENV), fbuf,
sizeof(fbuf));
2486 opt->script = argv[0];
2491 if (opt->script[0] ==
'-' && !opt->script[1]) {
2492 forbid_setid(
"program input from stdin");
2497 opt->script = RSTRING_PTR(opt->script_name);
2500 translit_char_bin(RSTRING_PTR(opt->script_name),
'\\',
'/');
2503 ruby_gc_set_params();
2507 lenc = rb_locale_encoding();
2508 rb_enc_associate(rb_progname, lenc);
2510 if (opt->ext.enc.name != 0) {
2511 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2513 if (opt->intern.enc.name != 0) {
2514 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2516 if (opt->src.enc.name != 0) {
2517 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2518 src_encoding_index = opt->src.enc.index;
2520 if (opt->ext.enc.index >= 0) {
2521 enc = rb_enc_from_index(opt->ext.enc.index);
2524 enc = IF_UTF8_PATH(uenc, lenc);
2526 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2527 if (opt->intern.enc.index >= 0) {
2528 enc = rb_enc_from_index(opt->intern.enc.index);
2529 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2530 opt->intern.enc.index = -1;
2535 rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc));
2538 opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
2539 opt->script = RSTRING_PTR(opt->script_name);
2543 if (IF_UTF8_PATH(uenc != lenc, 1)) {
2545 VALUE load_path = box->load_path;
2546 const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
2547 int modifiable = FALSE;
2549 rb_get_expanded_load_path(NULL);
2550 for (i = 0; i <
RARRAY_LEN(load_path); ++i) {
2552 int mark = rb_attr_get(path, id_initial_load_path_mark) == path;
2555 if (newpath == path)
continue;
2558 if (!(path = copy_str(path, lenc, !mark)))
continue;
2560 if (mark)
rb_ivar_set(path, id_initial_load_path_mark, path);
2572 VALUE loaded_features = box->loaded_features;
2573 bool modified =
false;
2576 if (!(path = copy_str(path, IF_UTF8_PATH(uenc, lenc),
true)))
continue;
2588 if (opt->features.mask & COMPILATION_FEATURES) {
2589 VALUE option = rb_hash_new();
2590#define SET_COMPILE_OPTION(h, o, name) \
2591 rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \
2592 RBOOL(FEATURE_SET_P(o->features, name)))
2594 if (FEATURE_SET_P(opt->features, frozen_string_literal_set)) {
2595 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
2597 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
2599#undef SET_COMPILE_OPTION
2602 opt->sflag = process_sflag(opt->sflag);
2604 if (opt->e_script) {
2606 if (opt->src.enc.index >= 0) {
2607 eenc = rb_enc_from_index(opt->src.enc.index);
2612 if (ienc) eenc = ienc;
2617 opt->e_script = str_conv_enc(opt->e_script, uenc, eenc);
2620 rb_enc_associate(opt->e_script, eenc);
2623 if (!rb_ruby_prism_p()) {
2624 ast_value = process_script(opt);
2625 if (!(result.ast = rb_ruby_ast_data_get(ast_value)))
return Qfalse;
2628 prism_script(opt, &result.prism);
2631 if ((dump & DUMP_BIT(yydebug)) && !(dump &= ~DUMP_BIT(yydebug))) {
2636 if (opt->ext.enc.index >= 0) {
2637 enc = rb_enc_from_index(opt->ext.enc.index);
2640 enc = IF_UTF8_PATH(uenc, lenc);
2642 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2643 if (opt->intern.enc.index >= 0) {
2645 enc = rb_enc_from_index(opt->intern.enc.index);
2646 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2648 else if (!rb_default_internal_encoding())
2650 rb_enc_set_default_internal(
Qnil);
2651 rb_stdio_set_default_encoding();
2653 opt->sflag = process_sflag(opt->sflag);
2656 if (dump & DUMP_BIT(syntax)) {
2657 printf(
"Syntax OK\n");
2658 dump &= ~DUMP_BIT(syntax);
2665 if (dump & DUMP_BIT(parsetree)) {
2668 int comment = opt->dump & DUMP_BIT(opt_comment);
2669 tree = rb_parser_dump_tree(result.ast->body.root, comment);
2672 tree = prism_dump_tree(&result.prism);
2676 dump &= ~DUMP_BIT(parsetree);
2685 if (!opt->e_script && strcmp(opt->script,
"-")) {
2686 path = rb_realpath_internal(
Qnil, opt->script_name, 1);
2689 path = str_conv_enc(path, uenc, lenc);
2693 rb_enc_copy(path, opt->script_name);
2699 const struct rb_block *base_block = toplevel_context(toplevel_binding);
2700 const rb_iseq_t *parent = vm_block_iseq(base_block);
2701 bool optimize = (opt->dump & DUMP_BIT(opt_optimize)) != 0;
2706 iseq = pm_iseq_new_main(&pm->
node, opt->script_name, path, parent, optimize, &error_state);
2708 pm_parse_result_free(pm);
2712 rb_jump_tag(error_state);
2717 iseq = rb_iseq_new_main(ast_value, opt->script_name, path, parent, optimize);
2718 rb_ast_dispose(ast);
2722 if (dump & DUMP_BIT(insns)) {
2725 dump &= ~DUMP_BIT(insns);
2726 if (!dump)
return Qtrue;
2728 if (opt->dump & dump_exit_bits)
return Qtrue;
2730 process_options_global_setup(opt, iseq);
2738 if (
len > 1 && str[
len-1] ==
'\n' && str[
len-2] ==
'\r') {
2739 rb_warn(
"shebang line ending with \\r may cause problems");
2743#define warn_cr_in_shebang(str, len) (void)0
2746void rb_reset_argf_lineno(
long n);
2756void rb_set_script_lines_for(
VALUE vparser,
VALUE path);
2759load_file_internal(
VALUE argp_v)
2762 VALUE parser = argp->parser;
2763 VALUE orig_fname = argp->fname;
2764 int script = argp->script;
2772 CONST_ID(set_encoding,
"set_encoding");
2778 int no_src_enc = !opt->src.enc.name;
2779 int no_ext_enc = !opt->ext.enc.name;
2780 int no_int_enc = !opt->intern.enc.name;
2782 enc = rb_ascii8bit_encoding();
2783 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
2791 if (
len > 2 && str[0] ==
'#' && str[1] ==
'!') {
2792 if (line_start == 1) warn_cr_in_shebang(str,
len);
2806 warn_cr_in_shebang(str,
len);
2809 goto search_shebang;
2814 if (*str ==
'\n') *str-- =
'\0';
2815 if (*str ==
'\r') *str-- =
'\0';
2817 if ((p = strstr(p,
" -")) != 0) {
2819 moreswitches(p + 1, opt, 0);
2825 else if (!
NIL_P(c)) {
2829 if (no_src_enc && opt->src.enc.name) {
2830 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2831 src_encoding_index = opt->src.enc.index;
2833 if (no_ext_enc && opt->ext.enc.name) {
2834 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2836 if (no_int_enc && opt->intern.enc.name) {
2837 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2840 else if (!
NIL_P(c)) {
2846 rb_reset_argf_lineno(0);
2849 if (opt->src.enc.index >= 0) {
2850 enc = rb_enc_from_index(opt->src.enc.index);
2853 enc = IF_UTF8_PATH(rb_utf8_encoding(), rb_locale_encoding());
2856 enc = rb_utf8_encoding();
2858 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2859 opt->do_line, opt->do_split);
2861 rb_set_script_lines_for(parser, orig_fname);
2865 rb_enc_associate(f, enc);
2866 return rb_parser_compile_string_path(parser, orig_fname, f, line_start);
2869 ast_value = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
2870 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
2871 if (script && rb_parser_end_seen_p(parser)) {
2892disable_nonblock(
int fd)
2894#if defined(HAVE_FCNTL) && defined(F_SETFL)
2895 if (fcntl(fd, F_SETFL, 0) < 0) {
2896 const int e =
errno;
2899 if (e == ENOTSUP)
return 0;
2901# if defined B_UNSUPPORTED
2902 if (e == B_UNSUPPORTED)
return 0;
2911open_load_file(
VALUE fname_v,
int *xflag)
2913 const char *fname = (fname_v = rb_str_encode_ospath(fname_v),
2915 rb_len_t flen = RSTRING_LEN(fname_v);
2919 if (flen == 1 && fname[0] ==
'-') {
2926 const int MODE_TO_LOAD = O_RDONLY | (
2927#if defined O_NONBLOCK && HAVE_FCNTL
2929 !(O_NONBLOCK & O_ACCMODE) ? O_NONBLOCK :
2931#if defined O_NDELAY && HAVE_FCNTL
2932 !(O_NDELAY & O_ACCMODE) ? O_NDELAY :
2935 int mode = MODE_TO_LOAD;
2936#if defined DOSISH || defined __CYGWIN__
2937# define isdirsep(x) ((x) == '/' || (x) == '\\')
2939 static const char exeext[] =
".exe";
2940 enum {extlen =
sizeof(exeext)-1};
2941 if (flen > extlen && !isdirsep(fname[flen-extlen-1]) &&
2942 STRNCASECMP(fname+flen-extlen, exeext, extlen) == 0) {
2951 if (!rb_gc_for_fd(e)) {
2952 rb_load_fail(fname_v, strerror(e));
2955 rb_load_fail(fname_v, strerror(
errno));
2960 if (MODE_TO_LOAD != O_RDONLY && (e = disable_nonblock(fd)) != 0) {
2962 rb_load_fail(fname_v, strerror(e));
2965 e = ruby_is_fd_loadable(fd);
2969 rb_load_fail(fname_v, strerror(e));
2985restore_load_file(
VALUE arg)
3000 arg.parser = parser;
3002 arg.script = script;
3006 restore_load_file, (
VALUE)&arg);
3020 ast_value = rb_parser_load_file(rb_parser_new(), fname_v);
3021 return (
void *)rb_ruby_ast_data_get(ast_value);
3025rb_parser_load_file(
VALUE parser,
VALUE fname_v)
3029 VALUE f = open_load_file(fname_v, &xflag);
3030 cmdline_options_init(&opt)->xflag = xflag != 0;
3031 return load_file(parser, fname_v, f, 0, &opt);
3046proc_argv0(
VALUE process)
3048 return rb_orig_progname;
3073 return ruby_setproctitle(title);
3077ruby_setproctitle(
VALUE title)
3087 if (origarg.argv == 0)
3094external_str_new_cstr(
const char *p)
3098 str = str_conv_enc(str, NULL, rb_default_external_encoding());
3106set_progname(
VALUE name)
3108 rb_orig_progname = rb_progname = name;
3109 rb_vm_set_progname(rb_progname);
3133 rb_uid_t uid = getuid();
3134 rb_uid_t euid = geteuid();
3135 rb_gid_t gid = getgid();
3136 rb_gid_t egid = getegid();
3138 if (uid != euid) opt->setids |= 1;
3139 if (egid != gid) opt->setids |= 2;
3146 if (opt->setids & 1)
3148 if (opt->setids & 2)
3153verbose_getter(
ID id,
VALUE *ptr)
3155 return *rb_ruby_verbose_ptr();
3161 *rb_ruby_verbose_ptr() =
RTEST(val) ?
Qtrue : val;
3165opt_W_getter(
ID id,
VALUE *dmy)
3167 VALUE v = *rb_ruby_verbose_ptr();
3182debug_getter(
ID id,
VALUE *dmy)
3184 return *rb_ruby_debug_ptr();
3190 *rb_ruby_debug_ptr() = val;
3203 rb_gvar_ractor_local(
"$VERBOSE");
3204 rb_gvar_ractor_local(
"$-v");
3205 rb_gvar_ractor_local(
"$-w");
3206 rb_gvar_ractor_local(
"$-W");
3207 rb_gvar_ractor_local(
"$DEBUG");
3208 rb_gvar_ractor_local(
"$-d");
3232 for (i = 0; i < argc; i++) {
3233 VALUE arg = external_str_new_cstr(argv[i]);
3245 const char *script_name = (argc > 0 && argv[0]) ? argv[0] :
ruby_engine;
3247 if (!origarg.argv || origarg.argc <= 0) {
3248 origarg.argc = argc;
3249 origarg.argv = argv;
3251 set_progname(external_str_new_cstr(script_name));
3253 rb_vm_register_global_object(
rb_argv0);
3255#ifndef HAVE_SETPROCTITLE
3256 ruby_init_setproctitle(argc, argv);
3259 if (getenv(
"RUBY_FREE_AT_EXIT")) {
3260 rb_free_at_exit =
true;
3264 iseq = process_options(argc, argv, cmdline_options_init(&opt));
3266 if (opt.crash_report && *opt.crash_report) {
3267 void ruby_set_crash_report(
const char *
template);
3268 ruby_set_crash_report(opt.crash_report);
3271 return (
void*)(
struct RData*)iseq;
3275fill_standard_fds(
void)
3277 int f0, f1, f2, fds[2];
3279 f0 = fstat(0, &buf) == -1 &&
errno == EBADF;
3280 f1 = fstat(1, &buf) == -1 &&
errno == EBADF;
3281 f2 = fstat(2, &buf) == -1 &&
errno == EBADF;
3283 if (pipe(fds) == 0) {
3292 if (pipe(fds) == 0) {
3294 if (f1 && fds[1] != 1)
3296 if (f2 && fds[1] != 2)
3298 if (fds[1] != 1 && fds[1] != 2)
3308 rb_w32_sysinit(argc, argv);
3310 if (*argc >= 0 && *argv) {
3311 origarg.argc = *argc;
3312 origarg.argv = *argv;
3314 fill_standard_fds();
3317#ifdef RUBY_ASAN_ENABLED
3318RUBY_SYMBOL_EXPORT_BEGIN
3319const char ruby_asan_default_options[] =
"use_sigaltstack=0:detect_leaks=0";
3320RUBY_SYMBOL_EXPORT_END
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
#define PATH_SEP_CHAR
Identical to PATH_SEP, except it is of type char.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ISSPACE
Old name of rb_isspace.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define STRNCASECMP
Old name of st_locale_insensitive_strncasecmp.
#define TOLOWER
Old name of rb_tolower.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
#define NIL_P
Old name of RB_NIL_P.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define scan_oct(s, l, e)
Old name of ruby_scan_oct.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ISALNUM
Old name of rb_isalnum.
#define rb_str_new4
Old name of rb_str_new_frozen.
void ruby_script(const char *name)
Sets the current script name to this value.
void ruby_set_argv(int argc, char **argv)
Sets argv that ruby understands.
void ruby_set_script_name(VALUE name)
Sets the current script name to this value.
void ruby_init_loadpath(void)
Sets up $LOAD_PATH.
void * ruby_process_options(int argc, char **argv)
Identical to ruby_options(), except it raises ruby-level exceptions on failure.
void ruby_prog_init(void)
Defines built-in variables.
void ruby_incpush(const char *path)
Appends the given path to the end of the load path.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNameError
NameError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
VALUE rb_eSecurityError
SecurityError exception.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
VALUE rb_cObject
Object class.
VALUE rb_mProcess
Process module.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_stdin
STDIN constant.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
VALUE rb_stdout
STDOUT constant.
VALUE rb_cString
String class.
void ruby_show_copyright(void)
Prints the copyright notice of the CRuby interpreter to stdout.
void ruby_sysinit(int *argc, char ***argv)
Initializes the process for libruby.
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Identical to rb_str_conv_enc(), except it additionally takes IO encoder options.
VALUE rb_enc_interned_str(const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
VALUE rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv_public(), except you can pass the passed block.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
void rb_ary_modify(VALUE ary)
Declares that the array is about to be modified.
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_io_gets(VALUE io)
Reads a "line" from the given IO.
VALUE rb_io_ungetbyte(VALUE io, VALUE b)
Identical to rb_io_ungetc(), except it doesn't take the encoding of the passed IO into account.
VALUE rb_io_getbyte(VALUE io)
Reads a byte from the given IO.
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
VALUE rb_fs
The field separator character for inputs, or the $;.
VALUE rb_output_rs
The record separator character for outputs, or the $\.
int rb_pipe(int *pipes)
This is an rb_cloexec_pipe() + rb_update_max_fd() combo.
VALUE rb_io_close(VALUE io)
Closes the IO.
void rb_lastline_set(VALUE str)
Updates $_.
VALUE rb_lastline_get(void)
Queries the last line, or the $_.
rb_pid_t rb_waitpid(rb_pid_t pid, int *status, int flags)
Waits for a process, with releasing GVL.
VALUE rb_f_exec(int argc, const VALUE *argv)
Replaces the current process by running the given external command.
VALUE rb_reg_new(const char *src, rb_len_t len, int opts)
Creates a new Regular expression.
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
VALUE rb_str_tmp_new(rb_len_t len)
Allocates a "temporary" string.
#define rb_str_new_lit(str)
Identical to rb_str_new_static(), except it cannot take string variables.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, rb_len_t srclen)
Destructively appends the passed contents to the string.
#define rb_external_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "default external" encoding.
#define rb_strlen_lit(str)
Length of a string literal.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
VALUE rb_gv_set(const char *name, VALUE val)
Assigns to a global variable.
@ RUBY_IO_READABLE
IO::READABLE
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
int len
Length of the buffer.
void ruby_each_words(const char *str, void(*func)(const char *word, int len, void *argv), void *argv)
Scans the passed string, with calling the callback function every time it encounters a "word".
const char ruby_engine[]
This is just "ruby" for us.
const int ruby_patchlevel
This is a monotonic increasing integer that describes specific "patch" level.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_ALLOCV_N(type, v, n)
Allocates a memory region, possibly on stack.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_ALLOCV_END(v)
Polite way to declare that the given array is not used any longer.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
static const uint8_t PM_OPTIONS_COMMAND_LINE_E
A bit representing whether or not the command line -e option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_L
A bit representing whether or not the command line -l option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_A
A bit representing whether or not the command line -a option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_N
A bit representing whether or not the command line -n option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_X
A bit representing whether or not the command line -x option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_P
A bit representing whether or not the command line -p option was set.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RFILE(obj)
Convenient casting macro.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
VALUE rb_argv0
The value of $0 at process bootup.
void * rb_load_file_str(VALUE file)
Identical to rb_load_file(), except it takes the argument as a Ruby's string instead of C's.
void * rb_load_file(const char *file)
Loads the given file.
#define rb_argv
Just another name of rb_get_argv.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This struct represents a slice in the source code, defined by an offset and a length.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.
pm_parser_t * parser
The parser that will do the actual parsing.
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.
Internal header for Ruby Box.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.