mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Add NumberObject and make to_object() on number values create it
This commit is contained in:
parent
d4dfe7e525
commit
3a026a1ede
Notes:
sideshowbarker
2024-07-19 07:55:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/3a026a1edef
8 changed files with 184 additions and 0 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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 };
|
||||
|
||||
|
|
|
@ -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 \
|
||||
|
|
45
Libraries/LibJS/Runtime/NumberObject.cpp
Normal file
45
Libraries/LibJS/Runtime/NumberObject.cpp
Normal file
|
@ -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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
46
Libraries/LibJS/Runtime/NumberObject.h
Normal file
46
Libraries/LibJS/Runtime/NumberObject.h
Normal file
|
@ -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 };
|
||||
};
|
||||
|
||||
}
|
39
Libraries/LibJS/Runtime/NumberPrototype.cpp
Normal file
39
Libraries/LibJS/Runtime/NumberPrototype.cpp
Normal file
|
@ -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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
42
Libraries/LibJS/Runtime/NumberPrototype.h
Normal file
42
Libraries/LibJS/Runtime/NumberPrototype.h
Normal file
|
@ -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"; }
|
||||
};
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue