From ee6472fef2626ce0e38dc7ecfa7c82540c81e3b5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 1 Apr 2020 19:42:07 +0100 Subject: LibJS: Implement Error function/constructor --- Libraries/LibJS/Makefile | 1 + Libraries/LibJS/Runtime/ErrorConstructor.cpp | 56 ++++++++++++++++++++++++++++ Libraries/LibJS/Runtime/ErrorConstructor.h | 46 +++++++++++++++++++++++ Libraries/LibJS/Runtime/GlobalObject.cpp | 2 + Libraries/LibJS/Tests/Error.js | 29 ++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 Libraries/LibJS/Runtime/ErrorConstructor.cpp create mode 100644 Libraries/LibJS/Runtime/ErrorConstructor.h create mode 100644 Libraries/LibJS/Tests/Error.js (limited to 'Libraries/LibJS') diff --git a/Libraries/LibJS/Makefile b/Libraries/LibJS/Makefile index 6e1cff91fc..624f2dd591 100644 --- a/Libraries/LibJS/Makefile +++ b/Libraries/LibJS/Makefile @@ -14,6 +14,7 @@ OBJS = \ Runtime/DateConstructor.o \ Runtime/DatePrototype.o \ Runtime/Error.o \ + Runtime/ErrorConstructor.o \ Runtime/ErrorPrototype.o \ Runtime/Exception.o \ Runtime/Function.o \ diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp new file mode 100644 index 0000000000..9c07c4c2c2 --- /dev/null +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +namespace JS { + +ErrorConstructor::ErrorConstructor() +{ + put("prototype", interpreter().error_prototype()); + put("length", Value(1)); +} + +ErrorConstructor::~ErrorConstructor() +{ +} + +Value ErrorConstructor::call(Interpreter& interpreter) +{ + return construct(interpreter); +} + +Value ErrorConstructor::construct(Interpreter& interpreter) +{ + String message = ""; + if (!interpreter.call_frame().arguments.is_empty() && !interpreter.call_frame().arguments[0].is_undefined()) + message = interpreter.call_frame().arguments[0].to_string(); + return interpreter.heap().allocate("Error", message); +} + +} diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.h b/Libraries/LibJS/Runtime/ErrorConstructor.h new file mode 100644 index 0000000000..43cc3eb8bb --- /dev/null +++ b/Libraries/LibJS/Runtime/ErrorConstructor.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020, Linus Groh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +namespace JS { + +class ErrorConstructor final : public NativeFunction { +public: + ErrorConstructor(); + virtual ~ErrorConstructor() override; + + virtual Value call(Interpreter&) override; + virtual Value construct(Interpreter&) override; + +private: + virtual bool has_constructor() const override { return true; } + virtual const char* class_name() const override { return "ErrorConstructor"; } +}; + +} diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 627a7b9776..4c3b4daa55 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ GlobalObject::GlobalObject() put("console", heap().allocate()); put("Date", heap().allocate()); + put("Error", heap().allocate()); put("Math", heap().allocate()); put("Object", heap().allocate()); } diff --git a/Libraries/LibJS/Tests/Error.js b/Libraries/LibJS/Tests/Error.js new file mode 100644 index 0000000000..713fc4e884 --- /dev/null +++ b/Libraries/LibJS/Tests/Error.js @@ -0,0 +1,29 @@ +function assert(x) { if (!x) throw 1; } + +try { + var e; + + e = Error(); + assert(e.name === "Error"); + assert(e.message === ""); + + e = Error(undefined); + assert(e.name === "Error"); + assert(e.message === ""); + + e = Error("test"); + assert(e.name === "Error"); + assert(e.message === "test"); + + e = Error(42); + assert(e.name === "Error"); + assert(e.message === "42"); + + e = Error(null); + assert(e.name === "Error"); + assert(e.message === "null"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} -- cgit v1.2.3