diff options
author | Brian Gianforcaro <b.gianfo@gmail.com> | 2020-04-05 05:49:46 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-05 15:28:45 +0200 |
commit | 4233c8662b1af9e46568dbd40c4b1a86867c9b97 (patch) | |
tree | 121b4b97879c744f084f7846fe6db07d6aeef22a | |
parent | a7d708e47d761c76024c1be565894187aa837739 (diff) | |
download | serenity-4233c8662b1af9e46568dbd40c4b1a86867c9b97.zip |
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.
-rw-r--r-- | Userland/js.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
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<JS::Error>("TypeError", "No arguments specified"); + + auto assertion_value = interpreter.argument(0); + if (!assertion_value.is_boolean()) + return interpreter.throw_exception<JS::Error>("TypeError", "The first argument is not a boolean"); + + if (!assertion_value.to_boolean()) + return interpreter.throw_exception<JS::Error>("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<ReplObject>(); 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<Line::Editor>(); editor->initialize(); @@ -356,6 +376,9 @@ int main(int argc, char** argv) auto interpreter = JS::Interpreter::create<JS::GlobalObject>(); 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)) { |