diff options
author | Jack Karamanian <karamanian.jack@gmail.com> | 2020-04-06 22:51:16 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-07 08:41:25 +0200 |
commit | edae926cb01cee1a110ac52052346bcf3649d07c (patch) | |
tree | 9139622f572ade2c80fef2d073309cebdf10b0d4 /Libraries/LibJS/Runtime | |
parent | 57bd194e5a11ae6bc8477eb976fb7d2c1e10ac35 (diff) | |
download | serenity-edae926cb01cee1a110ac52052346bcf3649d07c.zip |
LibJS: Add Boolean constructor object
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r-- | Libraries/LibJS/Runtime/BooleanConstructor.cpp | 52 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/BooleanConstructor.h | 44 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/BooleanObject.cpp | 40 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/BooleanObject.h | 47 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/BooleanPrototype.cpp | 70 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/BooleanPrototype.h | 43 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/GlobalObject.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Object.h | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 4 |
9 files changed, 303 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.cpp b/Libraries/LibJS/Runtime/BooleanConstructor.cpp new file mode 100644 index 0000000000..6d756d5865 --- /dev/null +++ b/Libraries/LibJS/Runtime/BooleanConstructor.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> + * 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/Heap/Heap.h> +#include <LibJS/Interpreter.h> +#include <LibJS/Runtime/BooleanConstructor.h> +#include <LibJS/Runtime/BooleanObject.h> +#include <LibJS/Runtime/BooleanPrototype.h> + +namespace JS { +BooleanConstructor::BooleanConstructor() +{ + put("prototype", Value(interpreter().boolean_prototype())); + put("length", Value(1)); +} + +BooleanConstructor::~BooleanConstructor() {} + +Value BooleanConstructor::call(Interpreter& interpreter) +{ + return Value(interpreter.argument(0).to_boolean()); +} + +Value BooleanConstructor::construct(Interpreter& interpreter) +{ + auto bool_object = interpreter.heap().allocate<BooleanObject>(interpreter.argument(0).to_boolean()); + return Value(bool_object); +} +} diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.h b/Libraries/LibJS/Runtime/BooleanConstructor.h new file mode 100644 index 0000000000..05fa5a5b63 --- /dev/null +++ b/Libraries/LibJS/Runtime/BooleanConstructor.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> + * 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 BooleanConstructor final : public NativeFunction { +public: + BooleanConstructor(); + virtual ~BooleanConstructor() 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 "BooleanConstructor"; } +}; +} diff --git a/Libraries/LibJS/Runtime/BooleanObject.cpp b/Libraries/LibJS/Runtime/BooleanObject.cpp new file mode 100644 index 0000000000..4b30001de7 --- /dev/null +++ b/Libraries/LibJS/Runtime/BooleanObject.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> + * 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/BooleanObject.h> + +namespace JS { +BooleanObject::BooleanObject(bool value) + : m_value(value) +{ + set_prototype(interpreter().boolean_prototype()); +} + +BooleanObject::~BooleanObject() +{ +} +} diff --git a/Libraries/LibJS/Runtime/BooleanObject.h b/Libraries/LibJS/Runtime/BooleanObject.h new file mode 100644 index 0000000000..a27a129f61 --- /dev/null +++ b/Libraries/LibJS/Runtime/BooleanObject.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> + * 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/Object.h> + +namespace JS { +class BooleanObject : public Object { +public: + explicit BooleanObject(bool); + virtual ~BooleanObject() override; + + virtual Value value_of() const override + { + return Value(m_value); + } + +private: + virtual const char* class_name() const override { return "BooleanObject"; } + virtual bool is_boolean() const override { return true; } + bool m_value { false }; +}; +} diff --git a/Libraries/LibJS/Runtime/BooleanPrototype.cpp b/Libraries/LibJS/Runtime/BooleanPrototype.cpp new file mode 100644 index 0000000000..c7423dd492 --- /dev/null +++ b/Libraries/LibJS/Runtime/BooleanPrototype.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> + * 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 <AK/Function.h> +#include <LibJS/Interpreter.h> +#include <LibJS/Runtime/BooleanPrototype.h> +#include <LibJS/Runtime/Error.h> + +namespace JS { +BooleanPrototype::BooleanPrototype() + : BooleanObject(false) +{ + put_native_function("toString", to_string); + put_native_function("valueOf", value_of); +} + +BooleanPrototype::~BooleanPrototype() {} + +Value BooleanPrototype::to_string(Interpreter& interpreter) +{ + auto this_object = interpreter.this_value(); + if (this_object.is_boolean()) { + return js_string(interpreter.heap(), this_object.as_bool() ? "true" : "false"); + } + if (!this_object.is_object() || !this_object.as_object().is_boolean()) { + interpreter.throw_exception<Error>("TypeError", "Not a Boolean"); + return {}; + } + + bool bool_value = static_cast<BooleanObject&>(this_object.as_object()).value_of().as_bool(); + return js_string(interpreter.heap(), bool_value ? "true" : "false"); +} + +Value BooleanPrototype::value_of(Interpreter& interpreter) +{ + auto this_object = interpreter.this_value(); + if (this_object.is_boolean()) { + return this_object; + } + if (!this_object.is_object() || !this_object.as_object().is_boolean()) { + interpreter.throw_exception<Error>("TypeError", "Not a Boolean"); + return {}; + } + + return static_cast<BooleanObject&>(this_object.as_object()).value_of(); +} +} diff --git a/Libraries/LibJS/Runtime/BooleanPrototype.h b/Libraries/LibJS/Runtime/BooleanPrototype.h new file mode 100644 index 0000000000..4050767839 --- /dev/null +++ b/Libraries/LibJS/Runtime/BooleanPrototype.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com> + * 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/BooleanObject.h> + +namespace JS { +class BooleanPrototype final : public BooleanObject { +public: + BooleanPrototype(); + virtual ~BooleanPrototype() override; + +private: + virtual const char* class_name() const override { return "BooleanPrototype"; } + + static Value to_string(Interpreter&); + static Value value_of(Interpreter&); +}; +} diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index af85902425..4bda346d81 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -29,6 +29,7 @@ #include <LibJS/Heap/Heap.h> #include <LibJS/Interpreter.h> #include <LibJS/Runtime/ArrayConstructor.h> +#include <LibJS/Runtime/BooleanConstructor.h> #include <LibJS/Runtime/ConsoleObject.h> #include <LibJS/Runtime/DateConstructor.h> #include <LibJS/Runtime/ErrorConstructor.h> @@ -58,6 +59,7 @@ GlobalObject::GlobalObject() put("Math", heap().allocate<MathObject>()); put("Object", heap().allocate<ObjectConstructor>()); put("Array", heap().allocate<ArrayConstructor>()); + put("Boolean", heap().allocate<BooleanConstructor>()); } GlobalObject::~GlobalObject() diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 8772e6b1e2..7b9c8dcdfb 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -59,6 +59,7 @@ public: void put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter); virtual bool is_array() const { return false; } + virtual bool is_boolean() const { return false; } virtual bool is_date() const { return false; } virtual bool is_error() const { return false; } virtual bool is_function() const { return false; } diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index 6e5d11c3ba..fa489a65b7 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -29,6 +29,7 @@ #include <LibJS/Heap/Heap.h> #include <LibJS/Interpreter.h> #include <LibJS/Runtime/Array.h> +#include <LibJS/Runtime/BooleanObject.h> #include <LibJS/Runtime/Error.h> #include <LibJS/Runtime/NumberObject.h> #include <LibJS/Runtime/Object.h> @@ -110,6 +111,9 @@ Object* Value::to_object(Heap& heap) const if (is_number()) return heap.allocate<NumberObject>(m_value.as_double); + if (is_boolean()) + return heap.allocate<BooleanObject>(m_value.as_bool); + if (is_null() || is_undefined()) { heap.interpreter().throw_exception<Error>("TypeError", "ToObject on null or undefined."); return nullptr; |