mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
Userland: Allow multiple files to be run by js
js only accepted a single script file to run before this. With this patch, multiple scripts can be run in the same execution environment, allowing the user to specify a "preamble script" to be executed before the main script.
This commit is contained in:
parent
b9d9187feb
commit
aa91284485
1 changed files with 19 additions and 15 deletions
|
@ -826,7 +826,7 @@ int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
bool gc_on_every_allocation = false;
|
bool gc_on_every_allocation = false;
|
||||||
bool disable_syntax_highlight = false;
|
bool disable_syntax_highlight = false;
|
||||||
const char* script_path = nullptr;
|
Vector<String> script_paths;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.set_general_help("This is a JavaScript interpreter.");
|
args_parser.set_general_help("This is a JavaScript interpreter.");
|
||||||
|
@ -837,7 +837,7 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l');
|
args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l');
|
||||||
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
||||||
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
|
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
|
||||||
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(script_paths, "Path to script files", "scripts", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
bool syntax_highlight = !disable_syntax_highlight;
|
bool syntax_highlight = !disable_syntax_highlight;
|
||||||
|
@ -870,7 +870,7 @@ int main(int argc, char** argv)
|
||||||
vm->throw_exception(interpreter->global_object(), error);
|
vm->throw_exception(interpreter->global_object(), error);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (script_path == nullptr) {
|
if (script_paths.is_empty()) {
|
||||||
s_print_last_result = true;
|
s_print_last_result = true;
|
||||||
interpreter = JS::Interpreter::create<ReplObject>(*vm);
|
interpreter = JS::Interpreter::create<ReplObject>(*vm);
|
||||||
ReplConsoleClient console_client(interpreter->global_object().console());
|
ReplConsoleClient console_client(interpreter->global_object().console());
|
||||||
|
@ -1083,21 +1083,25 @@ int main(int argc, char** argv)
|
||||||
sigint_handler();
|
sigint_handler();
|
||||||
});
|
});
|
||||||
|
|
||||||
auto file = Core::File::construct(script_path);
|
StringBuilder builder;
|
||||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
for (auto& path : script_paths) {
|
||||||
warnln("Failed to open {}: {}", script_path, file->error_string());
|
auto file = Core::File::construct(path);
|
||||||
return 1;
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
||||||
}
|
warnln("Failed to open {}: {}", path, file->error_string());
|
||||||
auto file_contents = file->read_all();
|
return 1;
|
||||||
|
}
|
||||||
|
auto file_contents = file->read_all();
|
||||||
|
|
||||||
StringView source;
|
StringView source;
|
||||||
if (file_has_shebang(file_contents)) {
|
if (file_has_shebang(file_contents)) {
|
||||||
source = strip_shebang(file_contents);
|
source = strip_shebang(file_contents);
|
||||||
} else {
|
} else {
|
||||||
source = file_contents;
|
source = file_contents;
|
||||||
|
}
|
||||||
|
builder.append(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parse_and_run(*interpreter, source))
|
if (!parse_and_run(*interpreter, builder.to_string()))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue