summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-01 19:42:07 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-01 20:47:37 +0200
commitee6472fef2626ce0e38dc7ecfa7c82540c81e3b5 (patch)
treef874037c8df8cda04642cd6fabf9ac6eb6ec0548 /Libraries/LibJS
parent849e2c77e474a788121dc84b5341319c776cd45d (diff)
downloadserenity-ee6472fef2626ce0e38dc7ecfa7c82540c81e3b5.zip
LibJS: Implement Error function/constructor
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Makefile1
-rw-r--r--Libraries/LibJS/Runtime/ErrorConstructor.cpp56
-rw-r--r--Libraries/LibJS/Runtime/ErrorConstructor.h46
-rw-r--r--Libraries/LibJS/Runtime/GlobalObject.cpp2
-rw-r--r--Libraries/LibJS/Tests/Error.js29
5 files changed, 134 insertions, 0 deletions
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 <mail@linusgroh.de>
+ * 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 <LibJS/Interpreter.h>
+#include <LibJS/Runtime/Error.h>
+#include <LibJS/Runtime/ErrorConstructor.h>
+
+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>("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 <mail@linusgroh.de>
+ * 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 <LibJS/Runtime/NativeFunction.h>
+
+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 <LibJS/Interpreter.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/DateConstructor.h>
+#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/MathObject.h>
#include <LibJS/Runtime/NativeFunction.h>
@@ -19,6 +20,7 @@ GlobalObject::GlobalObject()
put("console", heap().allocate<ConsoleObject>());
put("Date", heap().allocate<DateConstructor>());
+ put("Error", heap().allocate<ErrorConstructor>());
put("Math", heap().allocate<MathObject>());
put("Object", heap().allocate<ObjectConstructor>());
}
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);
+}