mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 13:05:00 +00:00

This patch implements: - Spec compliant [[Call]] and [[Construct]] internal slots, as virtual FunctionObject::internal_{call,construct}(). These effectively replace the old virtual FunctionObject::{call,construct}(), but with several advantages: - Clear and consistent naming, following the object internal methods - Use of completions - internal_construct() returns an Object, and not Value! This has been a source of confusion for a long time, since in the spec there's always an Object returned but the Value return type in LibJS meant that this could not be fully trusted and something could screw you over. - Arguments are passed explicitly in form of a MarkedValueList, allowing manipulation (BoundFunction). We still put them on the execution context as a lot of code depends on it (VM::arguments()), but not from the Call() / Construct() AOs anymore, which now allows for bypassing them and invoking [[Call]] / [[Construct]] directly. Nothing but Call() / Construct() themselves do that at the moment, but future additions to ECMA262 or already existing web specs might. - Spec compliant, standalone Call() and Construct() AOs: currently the closest we have is VM::{call,construct}(), but those try to cater to all the different function object subclasses at once, resulting in a horrible mess and calling AOs with functions they should never be called with; most prominently PrepareForOrdinaryCall and OrdinaryCallBindThis, which are only for ECMAScriptFunctionObject. As a result this also contains an implicit optimization: we no longer need to create a new function environment for NativeFunctions - which, worth mentioning, is what started this whole crusade in the first place :^)
216 lines
8.8 KiB
C++
216 lines
8.8 KiB
C++
/*
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibJS/Interpreter.h>
|
|
#include <LibJS/Runtime/FunctionEnvironment.h>
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
namespace JS {
|
|
|
|
NativeFunction* NativeFunction::create(GlobalObject& global_object, const FlyString& name, Function<Value(VM&, GlobalObject&)> function)
|
|
{
|
|
return global_object.heap().allocate<NativeFunction>(global_object, name, move(function), *global_object.function_prototype());
|
|
}
|
|
|
|
// FIXME: m_realm is supposed to be the realm argument of CreateBuiltinFunction, or the current
|
|
// Realm Record. The former is not something that's commonly used or we support, the
|
|
// latter is impossible as no ExecutionContext exists when most NativeFunctions are created...
|
|
|
|
NativeFunction::NativeFunction(Object& prototype)
|
|
: FunctionObject(prototype)
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
|
{
|
|
}
|
|
|
|
NativeFunction::NativeFunction(FlyString name, Function<Value(VM&, GlobalObject&)> native_function, Object& prototype)
|
|
: FunctionObject(prototype)
|
|
, m_name(move(name))
|
|
, m_native_function(move(native_function))
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
|
{
|
|
}
|
|
|
|
NativeFunction::NativeFunction(FlyString name, Object& prototype)
|
|
: FunctionObject(prototype)
|
|
, m_name(move(name))
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
|
{
|
|
}
|
|
|
|
NativeFunction::~NativeFunction()
|
|
{
|
|
}
|
|
|
|
// NOTE: Do not attempt to DRY these, it's not worth it. The difference in return types (Value vs Object*),
|
|
// called functions (call() vs construct(FunctionObject&)), and this value (passed vs uninitialized) make
|
|
// these good candidates for a bit of code duplication :^)
|
|
|
|
// 10.3.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-built-in-function-objects-call-thisargument-argumentslist
|
|
ThrowCompletionOr<Value> NativeFunction::internal_call(Value this_argument, MarkedValueList arguments_list)
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = this->global_object();
|
|
|
|
// 1. Let callerContext be the running execution context.
|
|
auto& caller_context = vm.running_execution_context();
|
|
|
|
// 2. If callerContext is not already suspended, suspend callerContext.
|
|
// NOTE: We don't support this concept yet.
|
|
|
|
// 3. Let calleeContext be a new execution context.
|
|
ExecutionContext callee_context(heap());
|
|
|
|
// 4. Set the Function of calleeContext to F.
|
|
callee_context.function = this;
|
|
callee_context.function_name = m_name;
|
|
|
|
// 5. Let calleeRealm be F.[[Realm]].
|
|
auto* callee_realm = m_realm;
|
|
// NOTE: This non-standard fallback is needed until we can guarantee that literally
|
|
// every function has a realm - especially in LibWeb that's sometimes not the case
|
|
// when a function is created while no JS is running, as we currently need to rely on
|
|
// that (:acid2:, I know - see set_event_handler_attribute() for an example).
|
|
// If there's no 'current realm' either, we can't continue and crash.
|
|
if (!callee_realm)
|
|
callee_realm = vm.current_realm();
|
|
VERIFY(callee_realm);
|
|
|
|
// 6. Set the Realm of calleeContext to calleeRealm.
|
|
callee_context.realm = callee_realm;
|
|
|
|
// 7. Set the ScriptOrModule of calleeContext to null.
|
|
// FIXME: Our execution context struct currently does not track this item.
|
|
|
|
// 8. Perform any necessary implementation-defined initialization of calleeContext.
|
|
|
|
callee_context.this_value = this_argument;
|
|
callee_context.arguments.extend(move(arguments_list));
|
|
|
|
callee_context.lexical_environment = caller_context.lexical_environment;
|
|
callee_context.variable_environment = caller_context.variable_environment;
|
|
|
|
// NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
|
|
callee_context.is_strict_mode = vm.in_strict_mode();
|
|
|
|
if (auto* interpreter = vm.interpreter_if_exists())
|
|
callee_context.current_node = interpreter->current_node();
|
|
|
|
// </8.> --------------------------------------------------------------------------
|
|
|
|
// 9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
|
|
vm.push_execution_context(callee_context, global_object);
|
|
|
|
// 10. Let result be the Completion Record that is the result of evaluating F in a manner that conforms to the specification of F. thisArgument is the this value, argumentsList provides the named parameters, and the NewTarget value is undefined.
|
|
auto result = call();
|
|
|
|
// 11. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
|
|
vm.pop_execution_context();
|
|
|
|
// 12. Return result.
|
|
if (auto* exception = vm.exception())
|
|
return throw_completion(exception->value());
|
|
return result;
|
|
}
|
|
|
|
// 10.3.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-built-in-function-objects-construct-argumentslist-newtarget
|
|
ThrowCompletionOr<Object*> NativeFunction::internal_construct(MarkedValueList arguments_list, FunctionObject& new_target)
|
|
{
|
|
auto& vm = this->vm();
|
|
auto& global_object = this->global_object();
|
|
|
|
// 1. Let callerContext be the running execution context.
|
|
auto& caller_context = vm.running_execution_context();
|
|
|
|
// 2. If callerContext is not already suspended, suspend callerContext.
|
|
// NOTE: We don't support this concept yet.
|
|
|
|
// 3. Let calleeContext be a new execution context.
|
|
ExecutionContext callee_context(heap());
|
|
|
|
// 4. Set the Function of calleeContext to F.
|
|
callee_context.function = this;
|
|
callee_context.function_name = m_name;
|
|
|
|
// 5. Let calleeRealm be F.[[Realm]].
|
|
auto* callee_realm = m_realm;
|
|
// NOTE: This non-standard fallback is needed until we can guarantee that literally
|
|
// every function has a realm - especially in LibWeb that's sometimes not the case
|
|
// when a function is created while no JS is running, as we currently need to rely on
|
|
// that (:acid2:, I know - see set_event_handler_attribute() for an example).
|
|
// If there's no 'current realm' either, we can't continue and crash.
|
|
if (!callee_realm)
|
|
callee_realm = vm.current_realm();
|
|
VERIFY(callee_realm);
|
|
|
|
// 6. Set the Realm of calleeContext to calleeRealm.
|
|
callee_context.realm = callee_realm;
|
|
|
|
// 7. Set the ScriptOrModule of calleeContext to null.
|
|
// FIXME: Our execution context struct currently does not track this item.
|
|
|
|
// 8. Perform any necessary implementation-defined initialization of calleeContext.
|
|
|
|
callee_context.arguments.extend(move(arguments_list));
|
|
|
|
callee_context.lexical_environment = caller_context.lexical_environment;
|
|
callee_context.variable_environment = caller_context.variable_environment;
|
|
|
|
// NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
|
|
callee_context.is_strict_mode = vm.in_strict_mode();
|
|
|
|
if (auto* interpreter = vm.interpreter_if_exists())
|
|
callee_context.current_node = interpreter->current_node();
|
|
|
|
// </8.> --------------------------------------------------------------------------
|
|
|
|
// 9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
|
|
vm.push_execution_context(callee_context, global_object);
|
|
|
|
// 10. Let result be the Completion Record that is the result of evaluating F in a manner that conforms to the specification of F. The this value is uninitialized, argumentsList provides the named parameters, and newTarget provides the NewTarget value.
|
|
auto result = construct(new_target);
|
|
|
|
// 11. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
|
|
vm.pop_execution_context();
|
|
|
|
// 12. Return result.
|
|
if (auto* exception = vm.exception())
|
|
return throw_completion(exception->value());
|
|
return &result.as_object();
|
|
}
|
|
|
|
Value NativeFunction::call()
|
|
{
|
|
return m_native_function(vm(), global_object());
|
|
}
|
|
|
|
Value NativeFunction::construct(FunctionObject&)
|
|
{
|
|
// Needs to be overridden if [[Construct]] is needed.
|
|
VERIFY_NOT_REACHED();
|
|
}
|
|
|
|
FunctionEnvironment* NativeFunction::new_function_environment(Object* new_target)
|
|
{
|
|
// Simplified version of 9.1.2.4 NewFunctionEnvironment ( F, newTarget )
|
|
Environment* parent_scope = nullptr;
|
|
if (!vm().execution_context_stack().is_empty())
|
|
parent_scope = vm().lexical_environment();
|
|
|
|
auto* environment = heap().allocate<FunctionEnvironment>(global_object(), parent_scope);
|
|
environment->set_new_target(new_target ? new_target : js_undefined());
|
|
|
|
return environment;
|
|
}
|
|
|
|
bool NativeFunction::is_strict_mode() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
}
|