mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:37:46 +00:00
LibJS: Add Boolean constructor object
This commit is contained in:
parent
57bd194e5a
commit
edae926cb0
16 changed files with 400 additions and 0 deletions
52
Libraries/LibJS/Runtime/BooleanConstructor.cpp
Normal file
52
Libraries/LibJS/Runtime/BooleanConstructor.cpp
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
44
Libraries/LibJS/Runtime/BooleanConstructor.h
Normal file
44
Libraries/LibJS/Runtime/BooleanConstructor.h
Normal file
|
@ -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"; }
|
||||
};
|
||||
}
|
40
Libraries/LibJS/Runtime/BooleanObject.cpp
Normal file
40
Libraries/LibJS/Runtime/BooleanObject.cpp
Normal file
|
@ -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()
|
||||
{
|
||||
}
|
||||
}
|
47
Libraries/LibJS/Runtime/BooleanObject.h
Normal file
47
Libraries/LibJS/Runtime/BooleanObject.h
Normal file
|
@ -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 };
|
||||
};
|
||||
}
|
70
Libraries/LibJS/Runtime/BooleanPrototype.cpp
Normal file
70
Libraries/LibJS/Runtime/BooleanPrototype.cpp
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
43
Libraries/LibJS/Runtime/BooleanPrototype.h
Normal file
43
Libraries/LibJS/Runtime/BooleanPrototype.h
Normal file
|
@ -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&);
|
||||
};
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue