📄 main.c
字号:
case 'r': /* --secure */
if (strcmp (optarg, "file") == 0)
secure_builtin_file = 1;
else if (strcmp (optarg, "system") == 0)
secure_builtin_system = 1;
else
{
fprintf (stderr, "%s: unknown security option `%s'\n",
program, optarg);
exit (1);
}
break;
case 's': /* --stack-size */
stack_size = atoi (optarg);
break;
case 'S': /* --no-assemble */
no_assemble = 1;
break;
case 't': /* --stacktrace */
stacktrace_on_error = 1;
break;
case 'v': /* --verbose */
verbose++;
break;
case 'V': /* --version */
version ();
exit (0);
break;
case 'W': /* --compiler-option */
handle_compiler_option (optarg);
break;
case 'x': /* --executable */
generate_executable_bc_files = 1;
break;
case '?':
fprintf (stderr, "Try `%s --help' for more information.\n",
program);
exit (1);
break;
default:
printf ("Hey! main() didn't handle option \"%c\" (%d)", c, c);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
abort ();
break;
}
}
arguments_done:
interp = create_interp ();
/* Let's see what we have to do. */
if (compile)
{
char *jscname;
/*
* Treat all remaining arguments as JavaScript files and compile them.
*/
for (; optind < argc; optind++)
{
/* Create name for the byte-code file. */
jscname = malloc (strlen (argv[optind]) + 5);
assert (jscname != NULL);
strcpy (jscname, argv[optind]);
cp = strrchr (jscname, '.');
if (cp)
strcpy (++cp, "jsc");
else
strcat (jscname, ".jsc");
if (verbose)
printf ("%s: compiling `%s' to `%s'\n", program,
argv[optind], jscname);
if (!js_compile (interp, argv[optind], NULL, jscname))
{
fprintf (stderr, "%s\n", js_error_message (interp));
exit (1);
}
free (jscname);
}
}
else if (no_assemble)
{
char *jasname;
/* Compile argument files to assembler. */
for (; optind < argc; optind++)
{
/* Create name for the assembler file. */
jasname = malloc (strlen (argv[optind]) + 5);
assert (jasname != NULL);
strcpy (jasname, argv[optind]);
cp = strrchr (jasname, '.');
if (cp)
strcpy (++cp, "jas");
else
strcat (jasname, ".jas");
if (verbose)
printf ("%s: compiling `%s' to `%s'\n", program,
argv[optind], jasname);
if (!js_compile (interp, argv[optind], jasname, NULL))
{
fprintf (stderr, "%s\n", js_error_message (interp));
exit (1);
}
free (jasname);
}
}
else if (optind < argc)
{
char *main_file = argv[optind];
JSType args;
int i;
/*
* Assume that <main_file> contains JavaScript (or byte-code) and
* execute it. All the remaining arguments are passed to the
* interpreter through the ARGS array.
*/
/* Save all remaining arguments to ARGS */
js_type_make_array (interp, &args, argc - optind);
for (i = 0; optind + i < argc; i++)
js_type_make_string (interp, &args.u.array->data[i],
argv[optind + i], strlen (argv[optind + i]));
js_set_var (interp, "ARGS", &args);
if (!js_eval_file (interp, main_file))
{
fprintf (stderr, "%s: evaluation of file `%s' failed:\n%s\n",
program, main_file, js_error_message (interp));
exit (1);
}
}
js_destroy_interp (interp);
return 0;
}
/*
* Static functions.
*/
static __inline int
is_prefix (char *prefix, char *str)
{
int i;
for (i = 0; prefix[i] && str[i] && prefix[i] == str[i]; i++)
;
if (!prefix[i])
return 1;
return 0;
}
static void
handle_compiler_option (char *name)
{
int i;
int value = 1;
int nmatches = 0;
int did_match = 0;
if (name[0] == 'n' && name[1] == 'o' && name[2] == '-')
{
value = 0;
name += 3;
}
for (i = 0; compiler_options[i].name; i++)
{
int was_prefix = 0;
if ((was_prefix = is_prefix (name, compiler_options[i].name))
|| (strcmp (name, "runtime") == 0
&& (compiler_options[i].flags & JSC_RUNTIME))
|| (strcmp (name, "all") == 0
&& (compiler_options[i].flags & JSC_WALL))
|| (strcmp (name, "pedantic") == 0
&& (compiler_options[i].flags & (JSC_WALL | JSC_PEDANTIC))))
{
*compiler_options[i].option = value;
if (was_prefix)
nmatches++;
did_match = 1;
}
}
if (!did_match)
{
fprintf (stderr, "%s: unknown compiler option `-W%s%s'\n", program,
value ? "" : "no-", name);
exit (1);
}
if (nmatches > 1)
{
fprintf (stderr, "%s: ambiguous compiler option `-W%s%s'\n",
program, value ? "" : "no-", name);
exit (1);
}
}
static int
show_events_hook (int event, void *context)
{
char *event_name;
switch (event)
{
case JS_EVENT_OPERAND_COUNT:
event_name = "operand count";
break;
case JS_EVENT_GARBAGE_COLLECT:
event_name = "garbage collect";
break;
default:
event_name = "unknown";
break;
}
fprintf (stderr, "[%s: %s]\n", program, event_name);
return 0;
}
static JSInterpPtr
create_interp ()
{
JSInterpOptions options;
JSInterpPtr interp;
js_init_default_options (&options);
options.stack_size = stack_size;
options.dispatch_method = dispatch_method;
options.verbose = verbose;
options.no_compiler = no_compiler;
options.stacktrace_on_error = stacktrace_on_error;
options.secure_builtin_file = secure_builtin_file;
options.secure_builtin_system = secure_builtin_system;
options.annotate_assembler = annotate_assembler;
options.debug_info = generate_debug_info;
options.executable_bc_files = generate_executable_bc_files;
options.warn_unused_argument = warn_unused_argument;
options.warn_unused_variable = warn_unused_variable;
options.warn_undef = warn_undef;
options.warn_shadow = warn_shadow;
options.warn_with_clobber = warn_with_clobber;
options.warn_missing_semicolon = warn_missing_semicolon;
options.warn_strict_ecma = warn_strict_ecma;
options.warn_deprecated = warn_deprecated;
/* As a default, no optimization */
options.optimize_peephole = 0;
options.optimize_jumps_to_jumps = 0;
options.optimize_bc_size = 0;
options.optimize_heavy = 0;
if (optimize >= 1)
{
options.optimize_peephole = 1;
options.optimize_jumps_to_jumps = 1;
options.optimize_bc_size = 1;
}
if (optimize >= 2)
{
options.optimize_heavy = 1;
}
/* Show events? */
if (events)
{
options.hook = show_events_hook;
options.hook_operand_count_trigger = 1000000;
}
interp = js_create_interp (&options);
if (interp == NULL)
{
fprintf (stderr, "%s: couldn't create interpreter\n", program);
exit (1);
}
/* And finally, define the requested modules. */
#if WITH_JS
if (!js_define_module (interp, js_ext_JS))
fprintf (stderr, "%s: warning: couldn't create the JS extension\n",
program);
#endif
#if WITH_CURSES
if (!js_define_module (interp, js_ext_curses))
fprintf (stderr, "%s: warning: couldn't create the curses extension\n",
program);
#endif
#if WITH_MD5
if (!js_define_module (interp, js_ext_MD5))
fprintf (stderr, "%s: warning: couldn't create the MD5 extension\n",
program);
#endif
return interp;
}
static void
usage ()
{
printf ("\
Usage: %s [OPTION]... FILE [ARGUMENT]...\n\
Mandatory arguments to long options are mandatory for short options too.\n\
-a, --annotate-assembler annotate generated assembler listing with\n\
the original source code\n\
-c, --compile compile JavaScript input file to byte-code\n\
and save the result to the file `FILE.jsc'\n\
-d, --dispatch=METHOD use method METHOD for byte-code instruction\n\
dispatching\n\
-e, --eval=CODE evaluate JavaScript code CODE\n\
-E, --events print interpreter events\n\
-f, --file evaluate the next argument file and pass\n\
all remaining arguments to the interpreter\n\
through the ARGS array\n\
-g, --debug generate debugging information\n\
-h, --help print this help and exit\n\
-l, --load evaluate argument files until option `-f',\n\
`--file' is encountered\n\
-N, --no-compiler do not define compiler to the JavaScript\n\
interpreter\n\
-O, --optimize[=LEVEL] optimize at level LEVEL\n\
-r, --secure=OPTION turn on security option OPTION\n\
-s, --stack-size=SIZE set the interpreter stack size to SIZE nodes\n\
-S, --assembler compile JavaScript intput file to assembler\n\
and save the result to the file `FILE.jas'\n\
-t, --stacktrace print stacktrace on error\n\
-v, --verbose tell what the interpreter is doing\n\
-V, --version print version number\n\
-W, --compiler-option=OPTION\n\
set compilation option OPTION\n\
-x, --executable generate executable byte-code files\n",
program);
printf ("\nReport bugs to mtr@ngs.fi.\n");
}
static void
version ()
{
printf ("NGS JavaScript Interpter %s\n\
Copyright (C) 1998 New Generation Software (NGS) Oy.\n\
NGS JavaScript Interpreter comes with NO WARRANTY, to the extent\n\
permitted by law. You may redistribute copies of NGS JavaScript\n\
Interpreter under the terms of the GNU Library General Public License.\n\
For more information about these matters, see the files named COPYING.\n\
",
VERSION);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -