mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Implement Error function/constructor
This commit is contained in:
parent
849e2c77e4
commit
ee6472fef2
Notes:
sideshowbarker
2024-07-19 08:00:48 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/ee6472fef26 Pull-request: https://github.com/SerenityOS/serenity/pull/1568
5 changed files with 134 additions and 0 deletions
|
@ -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 \
|
||||
|
|
56
Libraries/LibJS/Runtime/ErrorConstructor.cpp
Normal file
56
Libraries/LibJS/Runtime/ErrorConstructor.cpp
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
46
Libraries/LibJS/Runtime/ErrorConstructor.h
Normal file
46
Libraries/LibJS/Runtime/ErrorConstructor.h
Normal file
|
@ -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"; }
|
||||
};
|
||||
|
||||
}
|
|
@ -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>());
|
||||
}
|
||||
|
|
29
Libraries/LibJS/Tests/Error.js
Normal file
29
Libraries/LibJS/Tests/Error.js
Normal file
|
@ -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);
|
||||
}
|
Loading…
Add table
Reference in a new issue