1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +00:00

LibJS: Implement Wrapped Function Exotic Objects

This is a new concept from the ShadowRealm API stage 3 proposal:
https://tc39.es/proposal-shadowrealm/#sec-wrapped-function-exotic-objects
This commit is contained in:
Linus Groh 2021-10-13 21:08:48 +01:00
parent d92967406a
commit 50f8755792
7 changed files with 194 additions and 0 deletions

View file

@ -130,6 +130,7 @@ set(SOURCES
Runtime/SetIterator.cpp
Runtime/SetIteratorPrototype.cpp
Runtime/SetPrototype.cpp
Runtime/ShadowRealm.cpp
Runtime/Shape.cpp
Runtime/StringConstructor.cpp
Runtime/StringIterator.cpp
@ -189,6 +190,7 @@ set(SOURCES
Runtime/WeakSet.cpp
Runtime/WeakSetConstructor.cpp
Runtime/WeakSetPrototype.cpp
Runtime/WrappedFunction.cpp
Script.cpp
SourceTextModule.cpp
SyntaxHighlighter.cpp

View file

@ -181,6 +181,7 @@ class Utf16String;
class VM;
class Value;
class WeakContainer;
class WrappedFunction;
enum class DeclarationKind;
struct AlreadyResolved;
struct JobCallback;

View file

@ -171,6 +171,7 @@
M(RegExpObjectRepeatedFlag, "Repeated RegExp flag '{}'") \
M(RestrictedFunctionPropertiesAccess, "Restricted function properties like 'callee', 'caller' and 'arguments' may " \
"not be accessed in strict mode") \
M(ShadowRealmWrappedValueNonFunctionObject, "Wrapped value must be primitive or a function object, got {}") \
M(SpeciesConstructorDidNotCreate, "Species constructor did not create {}") \
M(SpeciesConstructorReturned, "Species constructor returned {}") \
M(StringNonGlobalRegExp, "RegExp argument is non-global") \
@ -218,6 +219,7 @@
M(TypedArrayTypeIsNot, "Typed array {} element type is not {}") \
M(UnknownIdentifier, "'{}' is not defined") \
M(UnsupportedDeleteSuperProperty, "Can't delete a property on 'super'") \
M(WrappedFunctionCallThrowCompletion, "Call of wrapped target function did not complete normally") \
M(URIMalformed, "URI malformed") /* LibWeb bindings */ \
M(NotAByteString, "Argument to {}() must be a byte string") \
M(BadArgCountOne, "{}() needs one argument") \

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/ShadowRealm.h>
#include <LibJS/Runtime/WrappedFunction.h>
namespace JS {
// 3.1.3 GetWrappedValue ( callerRealm, value ), https://tc39.es/proposal-shadowrealm/#sec-getwrappedvalue
ThrowCompletionOr<Value> get_wrapped_value(GlobalObject& global_object, Realm& caller_realm, Value value)
{
auto& vm = global_object.vm();
// 1. Assert: callerRealm is a Realm Record.
// 2. If Type(value) is Object, then
if (value.is_object()) {
// a. If IsCallable(value) is false, throw a TypeError exception.
if (!value.is_function())
return vm.throw_completion<TypeError>(global_object, ErrorType::ShadowRealmWrappedValueNonFunctionObject, value);
// b. Return ! WrappedFunctionCreate(callerRealm, value).
return { WrappedFunction::create(global_object, caller_realm, value.as_function()) };
}
// 3. Return value.
return value;
}
}

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Realm.h>
namespace JS {
ThrowCompletionOr<Value> get_wrapped_value(GlobalObject&, Realm& caller_realm, Value);
}

View file

@ -0,0 +1,102 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/ShadowRealm.h>
#include <LibJS/Runtime/WrappedFunction.h>
namespace JS {
// 2.2 WrappedFunctionCreate ( callerRealm, targetFunction ), https://tc39.es/proposal-shadowrealm/#sec-wrappedfunctioncreate
WrappedFunction* WrappedFunction::create(GlobalObject& global_object, Realm& caller_realm, FunctionObject& target_function)
{
// 1. Assert: callerRealm is a Realm Record.
// 2. Assert: IsCallable(targetFunction) is true.
// 3. Let internalSlotsList be the internal slots listed in Table 2, plus [[Prototype]] and [[Extensible]].
// 4. Let obj be ! MakeBasicObject(internalSlotsList).
auto& prototype = *caller_realm.global_object().function_prototype();
auto* object = global_object.heap().allocate<WrappedFunction>(global_object, caller_realm, target_function, prototype);
// 5. Set obj.[[Prototype]] to callerRealm.[[Intrinsics]].[[%Function.prototype%]].
// 6. Set obj.[[Call]] as described in 2.1.
// 7. Set obj.[[WrappedTargetFunction]] to targetFunction.
// 8. Set obj.[[Realm]] to callerRealm.
// 9. Return obj.
return object;
}
// 2 Wrapped Function Exotic Objects, https://tc39.es/proposal-shadowrealm/#sec-wrapped-function-exotic-objects
WrappedFunction::WrappedFunction(Realm& realm, FunctionObject& wrapped_target_function, Object& prototype)
: FunctionObject(prototype)
, m_wrapped_target_function(wrapped_target_function)
, m_realm(realm)
{
}
// 2.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/proposal-shadowrealm/#sec-wrapped-function-exotic-objects-call-thisargument-argumentslist
ThrowCompletionOr<Value> WrappedFunction::internal_call(Value this_argument, MarkedValueList arguments_list)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
// 1. Let target be F.[[WrappedTargetFunction]].
auto& target = m_wrapped_target_function;
// 2. Assert: IsCallable(target) is true.
VERIFY(Value(&target).is_function());
// 3. Let targetRealm be ? GetFunctionRealm(target).
auto* target_realm = TRY(get_function_realm(global_object, target));
// 4. Let callerRealm be ? GetFunctionRealm(F).
auto* caller_realm = TRY(get_function_realm(global_object, *this));
// 5. NOTE: Any exception objects produced after this point are associated with callerRealm.
// 6. Let wrappedArgs be a new empty List.
auto wrapped_args = MarkedValueList { vm.heap() };
wrapped_args.ensure_capacity(arguments_list.size());
// 7. For each element arg of argumentsList, do
for (auto& arg : arguments_list) {
// a. Let wrappedValue be ? GetWrappedValue(targetRealm, arg).
auto wrapped_value = TRY(get_wrapped_value(global_object, *target_realm, arg));
// b. Append wrappedValue to wrappedArgs.
wrapped_args.append(wrapped_value);
}
// 8. Let wrappedThisArgument to ? GetWrappedValue(targetRealm, thisArgument).
auto wrapped_this_argument = TRY(get_wrapped_value(global_object, *target_realm, this_argument));
// 9. Let result be the Completion Record of Call(target, wrappedThisArgument, wrappedArgs).
auto result = call(global_object, &target, wrapped_this_argument, move(wrapped_args));
// 10. If result.[[Type]] is normal or result.[[Type]] is return, then
if (!result.is_throw_completion()) {
// a. Return ? GetWrappedValue(callerRealm, result.[[Value]]).
return get_wrapped_value(global_object, *caller_realm, result.value());
}
// 11. Else,
else {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(caller_realm->global_object(), ErrorType::WrappedFunctionCallThrowCompletion);
}
// NOTE: Also see "Editor's Note" in the spec regarding the TypeError above.
}
void WrappedFunction::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(&m_wrapped_target_function);
visitor.visit(&m_realm);
}
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/Realm.h>
namespace JS {
class WrappedFunction final : public FunctionObject {
JS_OBJECT(WrappedFunction, FunctionObject);
public:
static WrappedFunction* create(GlobalObject&, Realm& caller_realm, FunctionObject& target_function);
WrappedFunction(Realm&, FunctionObject&, Object& prototype);
virtual ~WrappedFunction() = default;
virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedValueList arguments_list) override;
// FIXME: Remove this (and stop inventing random internal slots that shouldn't exist, jeez)
virtual FlyString const& name() const override { return m_wrapped_target_function.name(); }
virtual Realm* realm() const override { return &m_realm; }
private:
virtual void visit_edges(Visitor&) override;
// Internal Slots of Wrapped Function Exotic Objects, https://tc39.es/proposal-shadowrealm/#table-internal-slots-of-wrapped-function-exotic-objects
FunctionObject& m_wrapped_target_function; // [[WrappedTargetFunction]]
Realm& m_realm; // [[Realm]]
};
}