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:
sin-ack 2021-06-16 13:55:34 +00:00 committed by Andreas Kling
parent b9d9187feb
commit aa91284485
Notes: sideshowbarker 2024-07-18 12:09:14 +09:00

View File

@ -826,7 +826,7 @@ int main(int argc, char** argv)
{
bool gc_on_every_allocation = false;
bool disable_syntax_highlight = false;
const char* script_path = nullptr;
Vector<String> script_paths;
Core::ArgsParser args_parser;
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(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_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);
bool syntax_highlight = !disable_syntax_highlight;
@ -870,7 +870,7 @@ int main(int argc, char** argv)
vm->throw_exception(interpreter->global_object(), error);
};
if (script_path == nullptr) {
if (script_paths.is_empty()) {
s_print_last_result = true;
interpreter = JS::Interpreter::create<ReplObject>(*vm);
ReplConsoleClient console_client(interpreter->global_object().console());
@ -1083,9 +1083,11 @@ int main(int argc, char** argv)
sigint_handler();
});
auto file = Core::File::construct(script_path);
StringBuilder builder;
for (auto& path : script_paths) {
auto file = Core::File::construct(path);
if (!file->open(Core::OpenMode::ReadOnly)) {
warnln("Failed to open {}: {}", script_path, file->error_string());
warnln("Failed to open {}: {}", path, file->error_string());
return 1;
}
auto file_contents = file->read_all();
@ -1096,8 +1098,10 @@ int main(int argc, char** argv)
} else {
source = file_contents;
}
builder.append(source);
}
if (!parse_and_run(*interpreter, source))
if (!parse_and_run(*interpreter, builder.to_string()))
return 1;
}