LibJS: Remove ProxyPrototype

Proxy is an "exotic object" and doesn't have its own prototype. Use the
regular object prototype instead, but most stuff is happening on the
target object anyway. :^)
This commit is contained in:
Linus Groh 2020-11-30 22:50:43 +00:00 committed by Andreas Kling
parent 78f1b5e359
commit 48c19cdb06
Notes: sideshowbarker 2024-07-19 01:08:24 +09:00
9 changed files with 33 additions and 110 deletions

View file

@ -55,7 +55,6 @@ set(SOURCES
Runtime/PropertyAttributes.cpp
Runtime/ProxyConstructor.cpp
Runtime/ProxyObject.cpp
Runtime/ProxyPrototype.cpp
Runtime/Reference.cpp
Runtime/ReflectObject.cpp
Runtime/RegExpConstructor.cpp

View file

@ -44,6 +44,7 @@
#define JS_DEFINE_NATIVE_SETTER(name) \
void name([[maybe_unused]] JS::VM& vm, [[maybe_unused]] JS::GlobalObject& global_object, JS::Value value)
// NOTE: Proxy is not included here as it doesn't have a prototype - m_proxy_constructor is initialized separately.
#define JS_ENUMERATE_NATIVE_OBJECTS \
__JS_ENUMERATE(Array, array, ArrayPrototype, ArrayConstructor) \
__JS_ENUMERATE(BigIntObject, bigint, BigIntPrototype, BigIntConstructor) \
@ -53,7 +54,6 @@
__JS_ENUMERATE(Function, function, FunctionPrototype, FunctionConstructor) \
__JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor) \
__JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor) \
__JS_ENUMERATE(ProxyObject, proxy, ProxyPrototype, ProxyConstructor) \
__JS_ENUMERATE(RegExpObject, regexp, RegExpPrototype, RegExpConstructor) \
__JS_ENUMERATE(StringObject, string, StringPrototype, StringConstructor) \
__JS_ENUMERATE(SymbolObject, symbol, SymbolPrototype, SymbolConstructor)
@ -134,6 +134,10 @@ class VM;
class Value;
enum class DeclarationKind;
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
class ProxyObject;
class ProxyConstructor;
#define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName) \
class ClassName; \
class ConstructorName; \

View file

@ -53,7 +53,6 @@
#include <LibJS/Runtime/ObjectConstructor.h>
#include <LibJS/Runtime/ObjectPrototype.h>
#include <LibJS/Runtime/ProxyConstructor.h>
#include <LibJS/Runtime/ProxyPrototype.h>
#include <LibJS/Runtime/ReflectObject.h>
#include <LibJS/Runtime/RegExpConstructor.h>
#include <LibJS/Runtime/RegExpPrototype.h>
@ -124,21 +123,21 @@ void GlobalObject::initialize()
define_property(vm.names.JSON, heap().allocate<JSONObject>(*this, *this), attr);
define_property(vm.names.Reflect, heap().allocate<ReflectObject>(*this, *this), attr);
add_constructor(vm.names.Array, m_array_constructor, *m_array_prototype);
add_constructor(vm.names.BigInt, m_bigint_constructor, *m_bigint_prototype);
add_constructor(vm.names.Boolean, m_boolean_constructor, *m_boolean_prototype);
add_constructor(vm.names.Date, m_date_constructor, *m_date_prototype);
add_constructor(vm.names.Error, m_error_constructor, *m_error_prototype);
add_constructor(vm.names.Function, m_function_constructor, *m_function_prototype);
add_constructor(vm.names.Number, m_number_constructor, *m_number_prototype);
add_constructor(vm.names.Object, m_object_constructor, *m_object_prototype);
add_constructor(vm.names.Proxy, m_proxy_constructor, *m_proxy_prototype);
add_constructor(vm.names.RegExp, m_regexp_constructor, *m_regexp_prototype);
add_constructor(vm.names.String, m_string_constructor, *m_string_prototype);
add_constructor(vm.names.Symbol, m_symbol_constructor, *m_symbol_prototype);
add_constructor(vm.names.Array, m_array_constructor, m_array_prototype);
add_constructor(vm.names.BigInt, m_bigint_constructor, m_bigint_prototype);
add_constructor(vm.names.Boolean, m_boolean_constructor, m_boolean_prototype);
add_constructor(vm.names.Date, m_date_constructor, m_date_prototype);
add_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
add_constructor(vm.names.Function, m_function_constructor, m_function_prototype);
add_constructor(vm.names.Number, m_number_constructor, m_number_prototype);
add_constructor(vm.names.Object, m_object_constructor, m_object_prototype);
add_constructor(vm.names.Proxy, m_proxy_constructor, nullptr);
add_constructor(vm.names.RegExp, m_regexp_constructor, m_regexp_prototype);
add_constructor(vm.names.String, m_string_constructor, m_string_prototype);
add_constructor(vm.names.Symbol, m_symbol_constructor, m_symbol_prototype);
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
add_constructor(vm.names.ClassName, m_##snake_name##_constructor, *m_##snake_name##_prototype);
add_constructor(vm.names.ClassName, m_##snake_name##_constructor, m_##snake_name##_prototype);
JS_ENUMERATE_ERROR_SUBCLASSES
#undef __JS_ENUMERATE
}

View file

@ -53,6 +53,9 @@ public:
Shape* new_object_shape() { return m_new_object_shape; }
Shape* new_script_function_prototype_object_shape() { return m_new_script_function_prototype_object_shape; }
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
ProxyConstructor* proxy_constructor() { return m_proxy_constructor; }
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
ConstructorName* snake_name##_constructor() { return m_##snake_name##_constructor; } \
Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
@ -68,7 +71,7 @@ protected:
virtual void visit_edges(Visitor&) override;
template<typename ConstructorType>
void add_constructor(const FlyString& property_name, ConstructorType*&, Object& prototype);
void add_constructor(const FlyString& property_name, ConstructorType*&, Object* prototype);
private:
virtual bool is_global_object() const final { return true; }
@ -84,6 +87,9 @@ private:
Shape* m_new_object_shape { nullptr };
Shape* m_new_script_function_prototype_object_shape { nullptr };
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
ProxyConstructor* m_proxy_constructor { nullptr };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
ConstructorName* m_##snake_name##_constructor { nullptr }; \
Object* m_##snake_name##_prototype { nullptr };
@ -97,16 +103,18 @@ private:
};
template<typename ConstructorType>
inline void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object& prototype)
inline void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object* prototype)
{
auto& vm = this->vm();
constructor = heap().allocate<ConstructorType>(*this, *this);
constructor->define_property(vm.names.name, js_string(heap(), property_name), Attribute::Configurable);
if (vm.exception())
return;
prototype.define_property(vm.names.constructor, constructor, Attribute::Writable | Attribute::Configurable);
if (vm.exception())
return;
if (prototype) {
prototype->define_property(vm.names.constructor, constructor, Attribute::Writable | Attribute::Configurable);
if (vm.exception())
return;
}
define_property(property_name, constructor, Attribute::Writable | Attribute::Configurable);
}

View file

@ -41,7 +41,6 @@ void ProxyConstructor::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
NativeFunction::initialize(global_object);
define_property(vm.names.prototype, global_object.proxy_prototype(), 0);
define_property(vm.names.length, Value(2), Attribute::Configurable);
}

View file

@ -59,7 +59,7 @@ bool static is_compatible_property_descriptor(bool is_extensible, PropertyDescri
ProxyObject* ProxyObject::create(GlobalObject& global_object, Object& target, Object& handler)
{
return global_object.heap().allocate<ProxyObject>(global_object, target, handler, *global_object.proxy_prototype());
return global_object.heap().allocate<ProxyObject>(global_object, target, handler, *global_object.object_prototype());
}
ProxyObject::ProxyObject(Object& target, Object& handler, Object& prototype)

View file

@ -1,45 +0,0 @@
/*
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@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 <AK/StringBuilder.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/ProxyPrototype.h>
namespace JS {
ProxyPrototype::ProxyPrototype(GlobalObject& global_object)
: Object(*global_object.object_prototype())
{
}
ProxyPrototype::~ProxyPrototype()
{
}
}

View file

@ -1,41 +0,0 @@
/*
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@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 ProxyPrototype final : public Object {
JS_OBJECT(ProxyPrototype, Object);
public:
explicit ProxyPrototype(GlobalObject&);
virtual ~ProxyPrototype() override;
};
}

View file

@ -87,7 +87,7 @@ void WindowObject::initialize()
m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this);
m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(*this, *this);
m_xhr_constructor->define_property("prototype", m_xhr_prototype, 0);
add_constructor("XMLHttpRequest", m_xhr_constructor, *m_xhr_prototype);
add_constructor("XMLHttpRequest", m_xhr_constructor, m_xhr_prototype);
}
WindowObject::~WindowObject()