1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

js: Make load() available when running with --test-mode

The work I did to add assert as a native function in js
was a step in the wrong direction. Now that js supports
load() it makes sense to just move assert and anything
we want to add to the test harness into pure javascript.
This commit is contained in:
Brian Gianforcaro 2020-04-12 22:49:04 -07:00 committed by Andreas Kling
parent a0592912c3
commit 50b6b6ef86

View file

@ -52,15 +52,17 @@ public:
ReplObject(); ReplObject();
virtual ~ReplObject() override; virtual ~ReplObject() override;
static JS::Value load_file(JS::Interpreter&);
private: private:
virtual const char* class_name() const override { return "ReplObject"; } virtual const char* class_name() const override { return "ReplObject"; }
static JS::Value exit_interpreter(JS::Interpreter&); static JS::Value exit_interpreter(JS::Interpreter&);
static JS::Value repl_help(JS::Interpreter&); static JS::Value repl_help(JS::Interpreter&);
static JS::Value load_file(JS::Interpreter&);
static JS::Value save_to_file(JS::Interpreter&); static JS::Value save_to_file(JS::Interpreter&);
}; };
bool dump_ast = false; bool dump_ast = false;
bool print_last_result = false;
static OwnPtr<Line::Editor> editor; static OwnPtr<Line::Editor> editor;
static int repl_line_level = 0; static int repl_line_level = 0;
@ -330,7 +332,8 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
if (dump_ast) if (dump_ast)
program->dump(0); program->dump(0);
interpreter.run(*program); interpreter.run(*program);
print(interpreter.last_value()); if (print_last_result)
print(interpreter.last_value());
} }
return JS::Value(true); return JS::Value(true);
} }
@ -369,10 +372,15 @@ JS::Value assert_impl(JS::Interpreter& interpreter)
return JS::Value(assertion_value); return JS::Value(assertion_value);
} }
void enable_test_mode(JS::Interpreter& interpreter)
{
interpreter.global_object().put_native_function("load", ReplObject::load_file);
interpreter.global_object().put_native_function("assert", assert_impl);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
bool gc_on_every_allocation = false; bool gc_on_every_allocation = false;
bool print_last_result = false;
bool syntax_highlight = false; bool syntax_highlight = false;
bool test_mode = false; bool test_mode = false;
const char* script_path = nullptr; const char* script_path = nullptr;
@ -382,16 +390,15 @@ int main(int argc, char** argv)
args_parser.add_option(print_last_result, "Print last result", "print-last-result", 'l'); args_parser.add_option(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(syntax_highlight, "Enable live syntax highlighting", "syntax-highlight", 's'); args_parser.add_option(syntax_highlight, "Enable live syntax highlighting", "syntax-highlight", 's');
args_parser.add_option(test_mode, "Run the interpretter with added functionality for the test harness", "test-mode", 't'); args_parser.add_option(test_mode, "Run the interpreter with added functionality for the test harness", "test-mode", 't');
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No); args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv); args_parser.parse(argc, argv);
if (script_path == nullptr) { if (script_path == nullptr) {
auto interpreter = JS::Interpreter::create<ReplObject>(); auto interpreter = JS::Interpreter::create<ReplObject>();
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
if (test_mode) { if (test_mode)
interpreter->global_object().put_native_function("assert", assert_impl); enable_test_mode(*interpreter);
}
editor = make<Line::Editor>(); editor = make<Line::Editor>();
@ -611,9 +618,8 @@ int main(int argc, char** argv)
} else { } else {
auto interpreter = JS::Interpreter::create<JS::GlobalObject>(); auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
if (test_mode) { if (test_mode)
interpreter->global_object().put_native_function("assert", assert_impl); enable_test_mode(*interpreter);
}
auto file = Core::File::construct(script_path); auto file = Core::File::construct(script_path);
if (!file->open(Core::IODevice::ReadOnly)) { if (!file->open(Core::IODevice::ReadOnly)) {