diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-04 23:13:13 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-04 23:13:13 +0200 |
commit | 3a026a1edefe23c265ebe4f28883154169d5b6a1 (patch) | |
tree | 01231a383eb5efe2fa3db772c350963c46139798 | |
parent | d4dfe7e5253229ccbdfb3a4fcc7911bbde49342f (diff) | |
download | serenity-3a026a1edefe23c265ebe4f28883154169d5b6a1.zip |
LibJS: Add NumberObject and make to_object() on number values create it
-rw-r--r-- | Libraries/LibJS/Interpreter.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibJS/Interpreter.h | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Makefile | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/NumberObject.cpp | 45 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/NumberObject.h | 46 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/NumberPrototype.cpp | 39 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/NumberPrototype.h | 42 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 5 |
8 files changed, 184 insertions, 0 deletions
diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 5192713995..acbcd07c4c 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -34,6 +34,7 @@ #include <LibJS/Runtime/FunctionPrototype.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/NativeFunction.h> +#include <LibJS/Runtime/NumberPrototype.h> #include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/ObjectPrototype.h> #include <LibJS/Runtime/Shape.h> @@ -53,6 +54,7 @@ Interpreter::Interpreter() m_array_prototype = heap().allocate<ArrayPrototype>(); m_error_prototype = heap().allocate<ErrorPrototype>(); m_date_prototype = heap().allocate<DatePrototype>(); + m_number_prototype = heap().allocate<NumberPrototype>(); } Interpreter::~Interpreter() @@ -174,6 +176,7 @@ void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots) roots.set(m_error_prototype); roots.set(m_date_prototype); roots.set(m_function_prototype); + roots.set(m_number_prototype); roots.set(m_exception); diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index af79de6b5e..9cbfb1c697 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -137,6 +137,7 @@ public: Object* error_prototype() { return m_error_prototype; } Object* date_prototype() { return m_date_prototype; } Object* function_prototype() { return m_function_prototype; } + Object* number_prototype() { return m_number_prototype; } Exception* exception() { return m_exception; } void clear_exception() { m_exception = nullptr; } @@ -170,6 +171,7 @@ private: Object* m_error_prototype { nullptr }; Object* m_date_prototype { nullptr }; Object* m_function_prototype { nullptr }; + Object* m_number_prototype { nullptr }; Exception* m_exception { nullptr }; diff --git a/Libraries/LibJS/Makefile b/Libraries/LibJS/Makefile index ef9514d9b3..611cfdefcd 100644 --- a/Libraries/LibJS/Makefile +++ b/Libraries/LibJS/Makefile @@ -25,6 +25,8 @@ OBJS = \ Runtime/MathObject.o \ Runtime/NativeFunction.o \ Runtime/NativeProperty.o \ + Runtime/NumberObject.o \ + Runtime/NumberPrototype.o \ Runtime/Object.o \ Runtime/ObjectConstructor.o \ Runtime/ObjectPrototype.o \ diff --git a/Libraries/LibJS/Runtime/NumberObject.cpp b/Libraries/LibJS/Runtime/NumberObject.cpp new file mode 100644 index 0000000000..de2955fb5c --- /dev/null +++ b/Libraries/LibJS/Runtime/NumberObject.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * 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/NumberObject.h> +#include <LibJS/Runtime/NumberPrototype.h> +#include <LibJS/Runtime/Value.h> + +namespace JS { + +NumberObject::NumberObject(double value) + : m_value(value) +{ + set_prototype(interpreter().number_prototype()); +} + +NumberObject::~NumberObject() +{ +} + +} diff --git a/Libraries/LibJS/Runtime/NumberObject.h b/Libraries/LibJS/Runtime/NumberObject.h new file mode 100644 index 0000000000..aaa7f2569e --- /dev/null +++ b/Libraries/LibJS/Runtime/NumberObject.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * 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 NumberObject final : public Object { +public: + explicit NumberObject(double); + virtual ~NumberObject() override; + + virtual Value value_of() const override { return Value(m_value); } + +private: + virtual const char* class_name() const override { return "NumberObject"; } + + double m_value { 0 }; +}; + +} diff --git a/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Libraries/LibJS/Runtime/NumberPrototype.cpp new file mode 100644 index 0000000000..ef87272141 --- /dev/null +++ b/Libraries/LibJS/Runtime/NumberPrototype.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * 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/Runtime/NumberPrototype.h> + +namespace JS { + +NumberPrototype::NumberPrototype() +{ +} + +NumberPrototype::~NumberPrototype() +{ +} + +} diff --git a/Libraries/LibJS/Runtime/NumberPrototype.h b/Libraries/LibJS/Runtime/NumberPrototype.h new file mode 100644 index 0000000000..8ec9c544cc --- /dev/null +++ b/Libraries/LibJS/Runtime/NumberPrototype.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * 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 NumberPrototype final : public Object { +public: + NumberPrototype(); + virtual ~NumberPrototype() override; + +private: + virtual const char* class_name() const override { return "NumberPrototype"; } +}; + +} diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index dc6506b0d1..ccbaf70d34 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -30,6 +30,7 @@ #include <LibJS/Interpreter.h> #include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Error.h> +#include <LibJS/Runtime/NumberObject.h> #include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/PrimitiveString.h> #include <LibJS/Runtime/StringObject.h> @@ -102,11 +103,15 @@ Object* Value::to_object(Heap& heap) const if (is_string()) return heap.allocate<StringObject>(m_value.as_string); + if (is_number()) + return heap.allocate<NumberObject>(m_value.as_double); + if (is_null() || is_undefined()) { heap.interpreter().throw_exception<Error>("TypeError", "ToObject on null or undefined."); return nullptr; } + dbg() << "Dying because I can't to_object() on " << *this; ASSERT_NOT_REACHED(); } |