From 6d5d6685858cf12636eb164f32d9eb393caec3db Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 12 Apr 2020 19:42:19 +0100 Subject: [PATCH] js: Coerce assert() argument to boolean It's JavaScript after all :^) --- Userland/js.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Userland/js.cpp b/Userland/js.cpp index cc825e1599..c105b4f2be 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -360,16 +360,13 @@ void repl(JS::Interpreter& interpreter) JS::Value assert_impl(JS::Interpreter& interpreter) { if (!interpreter.argument_count()) - return interpreter.throw_exception("TypeError", "No arguments specified"); + return interpreter.throw_exception("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()) + auto assertion_value = interpreter.argument(0).to_boolean(); + if (!assertion_value) return interpreter.throw_exception("AssertionError", "The assertion failed!"); - return assertion_value; + return JS::Value(assertion_value); } int main(int argc, char** argv)