From 4233c8662b1af9e46568dbd40c4b1a86867c9b97 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sun, 5 Apr 2020 05:49:46 -0700 Subject: js: Add a new --test-mode option, which exposes an assert() function. Introduce a central assert implementation so that the LibJS test suite can take advantage of one implementation instead of copy/pasting the same function into every test case file. --- Userland/js.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Userland/js.cpp b/Userland/js.cpp index a09f91ef06..a41565dd44 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -331,16 +331,33 @@ void repl(JS::Interpreter& interpreter) } } +JS::Value assert_impl(JS::Interpreter& interpreter) +{ + if (!interpreter.argument_count()) + return interpreter.throw_exception("TypeError", "No arguments specified"); + + auto assertion_value = interpreter.argument(0); + if (!assertion_value.is_boolean()) + return interpreter.throw_exception("TypeError", "The first argument is not a boolean"); + + if (!assertion_value.to_boolean()) + return interpreter.throw_exception("AssertionError", "The assertion failed!"); + + return assertion_value; +} + int main(int argc, char** argv) { bool gc_on_every_allocation = false; bool print_last_result = false; + bool test_mode = false; const char* script_path = nullptr; Core::ArgsParser args_parser; args_parser.add_option(dump_ast, "Dump the AST", "dump-ast", 'A'); 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(test_mode, "Run the interpretter 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.parse(argc, argv); @@ -348,6 +365,9 @@ int main(int argc, char** argv) auto interpreter = JS::Interpreter::create(); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->global_object().put("global", &interpreter->global_object()); + if (test_mode) { + interpreter->global_object().put_native_function("assert", assert_impl); + } editor = make(); editor->initialize(); @@ -356,6 +376,9 @@ int main(int argc, char** argv) auto interpreter = JS::Interpreter::create(); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->global_object().put("global", &interpreter->global_object()); + if (test_mode) { + interpreter->global_object().put_native_function("assert", assert_impl); + } auto file = Core::File::construct(script_path); if (!file->open(Core::IODevice::ReadOnly)) { -- cgit v1.2.3