浏览代码

js: Coerce assert() argument to boolean

It's JavaScript after all :^)
Linus Groh 5 年之前
父节点
当前提交
6d5d668585
共有 1 个文件被更改,包括 4 次插入7 次删除
  1. 4 7
      Userland/js.cpp

+ 4 - 7
Userland/js.cpp

@@ -360,16 +360,13 @@ void repl(JS::Interpreter& interpreter)
 JS::Value assert_impl(JS::Interpreter& interpreter)
 JS::Value assert_impl(JS::Interpreter& interpreter)
 {
 {
     if (!interpreter.argument_count())
     if (!interpreter.argument_count())
-        return interpreter.throw_exception<JS::Error>("TypeError", "No arguments specified");
+        return interpreter.throw_exception<JS::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())
+    auto assertion_value = interpreter.argument(0).to_boolean();
+    if (!assertion_value)
         return interpreter.throw_exception<JS::Error>("AssertionError", "The assertion failed!");
         return interpreter.throw_exception<JS::Error>("AssertionError", "The assertion failed!");
 
 
-    return assertion_value;
+    return JS::Value(assertion_value);
 }
 }
 
 
 int main(int argc, char** argv)
 int main(int argc, char** argv)