2020-03-13 10:08:52 +01:00
/*
2021-05-29 12:38:28 +02:00
* Copyright ( c ) 2020 , Stephan Unverwerth < s . unverwerth @ serenityos . org >
2020-03-13 10:08:52 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-13 10:08:52 +01:00
*/
2021-06-07 15:17:37 +02:00
# include <AK/Debug.h>
2020-04-04 14:34:27 +01:00
# include <AK/Function.h>
2020-03-18 11:23:53 +01:00
# include <LibJS/AST.h>
2021-06-09 06:49:58 +04:30
# include <LibJS/Bytecode/BasicBlock.h>
2021-06-05 15:53:36 +02:00
# include <LibJS/Bytecode/Generator.h>
# include <LibJS/Bytecode/Interpreter.h>
2020-03-13 10:08:52 +01:00
# include <LibJS/Interpreter.h>
2021-09-22 12:44:56 +02:00
# include <LibJS/Runtime/AbstractOperations.h>
2020-05-04 16:05:13 +01:00
# include <LibJS/Runtime/Array.h>
2021-09-24 22:40:38 +02:00
# include <LibJS/Runtime/ECMAScriptFunctionObject.h>
2020-04-04 14:34:27 +01:00
# include <LibJS/Runtime/Error.h>
2021-07-01 12:24:46 +02:00
# include <LibJS/Runtime/FunctionEnvironment.h>
2021-06-11 01:38:30 +04:30
# include <LibJS/Runtime/GeneratorObject.h>
2021-06-15 00:04:08 -07:00
# include <LibJS/Runtime/GeneratorObjectPrototype.h>
2020-04-17 19:59:32 +02:00
# include <LibJS/Runtime/GlobalObject.h>
2021-06-11 01:38:30 +04:30
# include <LibJS/Runtime/NativeFunction.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/Value.h>
2020-03-13 10:08:52 +01:00
namespace JS {
2021-10-08 12:43:38 +02:00
ECMAScriptFunctionObject * ECMAScriptFunctionObject : : create ( GlobalObject & global_object , FlyString name , Statement const & ecmascript_code , Vector < FunctionNode : : Parameter > parameters , i32 m_function_length , Environment * parent_scope , FunctionKind kind , bool is_strict , bool might_need_arguments_object , bool contains_direct_call_to_eval , bool is_arrow_function )
2020-04-17 19:59:32 +02:00
{
2021-06-15 00:04:08 -07:00
Object * prototype = nullptr ;
switch ( kind ) {
case FunctionKind : : Regular :
prototype = global_object . function_prototype ( ) ;
break ;
case FunctionKind : : Generator :
prototype = global_object . generator_function_prototype ( ) ;
break ;
}
2021-10-08 12:43:38 +02:00
return global_object . heap ( ) . allocate < ECMAScriptFunctionObject > ( global_object , move ( name ) , ecmascript_code , move ( parameters ) , m_function_length , parent_scope , * prototype , kind , is_strict , might_need_arguments_object , contains_direct_call_to_eval , is_arrow_function ) ;
2020-04-17 19:59:32 +02:00
}
2021-10-08 12:43:38 +02:00
ECMAScriptFunctionObject : : ECMAScriptFunctionObject ( FlyString name , Statement const & ecmascript_code , Vector < FunctionNode : : Parameter > formal_parameters , i32 function_length , Environment * parent_scope , Object & prototype , FunctionKind kind , bool strict , bool might_need_arguments_object , bool contains_direct_call_to_eval , bool is_arrow_function )
2021-09-25 00:38:23 +02:00
: FunctionObject ( prototype )
2021-06-25 21:22:37 +02:00
, m_environment ( parent_scope )
2021-09-24 23:10:21 +02:00
, m_formal_parameters ( move ( formal_parameters ) )
, m_ecmascript_code ( ecmascript_code )
2021-09-12 23:10:16 +01:00
, m_realm ( vm ( ) . interpreter_if_exists ( ) ? & vm ( ) . interpreter ( ) . realm ( ) : nullptr )
2021-09-24 23:10:21 +02:00
, m_strict ( strict )
, m_name ( move ( name ) )
2021-06-24 13:29:25 +02:00
, m_function_length ( function_length )
2021-06-11 03:38:05 +04:30
, m_kind ( kind )
LibJS: Add an optimization to avoid needless arguments object creation
This gives FunctionNode a "might need arguments object" boolean flag and
sets it based on the simplest possible heuristic for this: if we
encounter an identifier called "arguments" or "eval" up to the next
(nested) function declaration or expression, we won't need an arguments
object. Otherwise, we *might* need one - the final decision is made in
the FunctionDeclarationInstantiation AO.
Now, this is obviously not perfect. Even if you avoid eval, something
like `foo.arguments` will still trigger a false positive - but it's a
start and already massively cuts down on needlessly allocated objects,
especially in real-world code that is often minified, and so a full
"arguments" identifier will be an actual arguments object more often
than not.
To illustrate the actual impact of this change, here's the number of
allocated arguments objects during a full test-js run:
Before:
- Unmapped arguments objects: 78765
- Mapped arguments objects: 2455
After:
- Unmapped arguments objects: 18
- Mapped arguments objects: 37
This results in a ~5% speedup of test-js on my Linux host machine, and
about 3.5% on i686 Serenity in QEMU (warm runs, average of 5).
The following microbenchmark (calling an empty function 1M times) runs
25% faster on Linux and 45% on Serenity:
function foo() {}
for (var i = 0; i < 1_000_000; ++i)
foo();
test262 reports no changes in either direction, apart from a speedup :^)
2021-10-05 08:44:58 +01:00
, m_might_need_arguments_object ( might_need_arguments_object )
2021-10-08 12:43:38 +02:00
, m_contains_direct_call_to_eval ( contains_direct_call_to_eval )
2020-05-30 00:10:42 -05:00
, m_is_arrow_function ( is_arrow_function )
2020-03-13 10:08:52 +01:00
{
2021-06-25 20:53:17 +02:00
// NOTE: This logic is from OrdinaryFunctionCreate, https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
if ( m_is_arrow_function )
2021-09-24 23:19:36 +02:00
m_this_mode = ThisMode : : Lexical ;
2021-09-24 23:10:21 +02:00
else if ( m_strict )
2021-09-24 23:19:36 +02:00
m_this_mode = ThisMode : : Strict ;
2021-06-25 20:53:17 +02:00
else
2021-09-24 23:19:36 +02:00
m_this_mode = ThisMode : : Global ;
2021-06-28 11:18:32 +02:00
// 15.1.3 Static Semantics: IsSimpleParameterList, https://tc39.es/ecma262/#sec-static-semantics-issimpleparameterlist
2021-09-25 00:10:09 +02:00
m_has_simple_parameter_list = all_of ( m_formal_parameters , [ & ] ( auto & parameter ) {
2021-06-28 11:18:32 +02:00
if ( parameter . is_rest )
return false ;
if ( parameter . default_value )
return false ;
if ( ! parameter . binding . template has < FlyString > ( ) )
return false ;
return true ;
2021-09-25 00:10:09 +02:00
} ) ;
2020-06-20 17:11:11 +02:00
}
2021-09-24 22:40:38 +02:00
void ECMAScriptFunctionObject : : initialize ( GlobalObject & global_object )
2020-06-20 17:11:11 +02:00
{
2020-10-13 23:49:19 +02:00
auto & vm = this - > vm ( ) ;
2021-06-27 21:48:34 +02:00
Base : : initialize ( global_object ) ;
2020-06-08 13:31:21 -05:00
if ( ! m_is_arrow_function ) {
2021-06-27 22:15:58 +02:00
auto * prototype = vm . heap ( ) . allocate < Object > ( global_object , * global_object . new_ordinary_function_prototype_object_shape ( ) ) ;
2021-06-15 00:04:08 -07:00
switch ( m_kind ) {
case FunctionKind : : Regular :
2021-10-03 01:35:36 +01:00
MUST ( prototype - > define_property_or_throw ( vm . names . constructor , { . value = this , . writable = true , . enumerable = false , . configurable = true } ) ) ;
2021-06-15 00:04:08 -07:00
break ;
case FunctionKind : : Generator :
// prototype is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
2021-10-02 14:32:37 +01:00
set_prototype ( global_object . generator_object_prototype ( ) ) ;
2021-06-15 00:04:08 -07:00
break ;
}
2021-07-06 02:15:08 +03:00
define_direct_property ( vm . names . prototype , prototype , Attribute : : Writable ) ;
2020-06-08 13:24:15 -05:00
}
2021-10-03 01:35:36 +01:00
MUST ( define_property_or_throw ( vm . names . length , { . value = Value ( m_function_length ) , . writable = false , . enumerable = false , . configurable = true } ) ) ;
MUST ( define_property_or_throw ( vm . names . name , { . value = js_string ( vm , m_name . is_null ( ) ? " " : m_name ) , . writable = false , . enumerable = false , . configurable = true } ) ) ;
2020-03-13 10:08:52 +01:00
}
2021-09-24 22:40:38 +02:00
ECMAScriptFunctionObject : : ~ ECMAScriptFunctionObject ( )
2020-03-13 10:08:52 +01:00
{
}
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
// 10.2.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
ThrowCompletionOr < Value > ECMAScriptFunctionObject : : internal_call ( Value this_argument , MarkedValueList arguments_list )
{
auto & vm = this - > vm ( ) ;
// 1. Let callerContext be the running execution context.
// NOTE: No-op, kept by the VM in its execution context stack.
ExecutionContext callee_context ( heap ( ) ) ;
// Non-standard
callee_context . arguments . extend ( move ( arguments_list ) ) ;
if ( auto * interpreter = vm . interpreter_if_exists ( ) )
callee_context . current_node = interpreter - > current_node ( ) ;
// 2. Let calleeContext be PrepareForOrdinaryCall(F, undefined).
2021-10-08 21:14:22 +01:00
prepare_for_ordinary_call ( callee_context , nullptr ) ;
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
// NOTE: We throw if the end of the native stack is reached, so unlike in the spec this _does_ need an exception check.
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
// 3. Assert: calleeContext is now the running execution context.
VERIFY ( & vm . running_execution_context ( ) = = & callee_context ) ;
// 4. If F.[[IsClassConstructor]] is true, then
if ( m_is_class_constructor ) {
// a. Let error be a newly created TypeError object.
// b. NOTE: error is created in calleeContext with F's associated Realm Record.
auto throw_completion = vm . throw_completion < TypeError > ( global_object ( ) , ErrorType : : ClassConstructorWithoutNew , m_name ) ;
// c. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
vm . pop_execution_context ( ) ;
// d. Return ThrowCompletion(error).
return throw_completion ;
}
// 5. Perform OrdinaryCallBindThis(F, calleeContext, thisArgument).
2021-10-08 21:15:53 +01:00
ordinary_call_bind_this ( callee_context , this_argument ) ;
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
// 6. Let result be OrdinaryCallEvaluateBody(F, argumentsList).
auto result = ordinary_call_evaluate_body ( ) ;
// 7. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
vm . pop_execution_context ( ) ;
// 8. If result.[[Type]] is return, return NormalCompletion(result.[[Value]]).
if ( result . type ( ) = = Completion : : Type : : Return )
return result . value ( ) ;
// 9. ReturnIfAbrupt(result).
if ( result . is_abrupt ( ) ) {
// NOTE: I'm not sure if EvaluateBody can return a completion other than Normal, Return, or Throw.
// We're far from using completions in the AST anyway; in the meantime assume Throw.
VERIFY ( result . is_error ( ) ) ;
return result ;
}
// 10. Return NormalCompletion(undefined).
return js_undefined ( ) ;
}
// 10.2.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
ThrowCompletionOr < Object * > ECMAScriptFunctionObject : : 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.
// NOTE: No-op, kept by the VM in its execution context stack.
// 2. Let kind be F.[[ConstructorKind]].
auto kind = m_constructor_kind ;
Object * this_argument = nullptr ;
// 3. If kind is base, then
if ( kind = = ConstructorKind : : Base ) {
// a. Let thisArgument be ? OrdinaryCreateFromConstructor(newTarget, "%Object.prototype%").
this_argument = TRY ( ordinary_create_from_constructor < Object > ( global_object , new_target , & GlobalObject : : object_prototype ) ) ;
}
ExecutionContext callee_context ( heap ( ) ) ;
// Non-standard
callee_context . arguments . extend ( move ( arguments_list ) ) ;
if ( auto * interpreter = vm . interpreter_if_exists ( ) )
callee_context . current_node = interpreter - > current_node ( ) ;
// 4. Let calleeContext be PrepareForOrdinaryCall(F, newTarget).
2021-10-08 21:14:22 +01:00
prepare_for_ordinary_call ( callee_context , & new_target ) ;
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
// NOTE: We throw if the end of the native stack is reached, so unlike in the spec this _does_ need an exception check.
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
// 5. Assert: calleeContext is now the running execution context.
VERIFY ( & vm . running_execution_context ( ) = = & callee_context ) ;
// 6. If kind is base, then
if ( kind = = ConstructorKind : : Base ) {
// a. Perform OrdinaryCallBindThis(F, calleeContext, thisArgument).
2021-10-08 21:15:53 +01:00
ordinary_call_bind_this ( callee_context , this_argument ) ;
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
// b. Let initializeResult be InitializeInstanceElements(thisArgument, F).
auto initialize_result = vm . initialize_instance_elements ( * this_argument , * this ) ;
// c. If initializeResult is an abrupt completion, then
if ( initialize_result . is_throw_completion ( ) ) {
// i. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
vm . pop_execution_context ( ) ;
// ii. Return Completion(initializeResult).
return initialize_result . throw_completion ( ) ;
}
}
// 7. Let constructorEnv be the LexicalEnvironment of calleeContext.
auto * constructor_env = callee_context . lexical_environment ;
// 8. Let result be OrdinaryCallEvaluateBody(F, argumentsList).
auto result = ordinary_call_evaluate_body ( ) ;
// 9. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
vm . pop_execution_context ( ) ;
// 10. If result.[[Type]] is return, then
if ( result . type ( ) = = Completion : : Type : : Return ) {
// FIXME: This is leftover from untangling the call/construct mess - doesn't belong here in any way, but removing it breaks derived classes.
// Likely fixed by making ClassDefinitionEvaluation fully spec compliant.
if ( kind = = ConstructorKind : : Derived & & result . value ( ) . is_object ( ) ) {
auto prototype = TRY ( new_target . get ( vm . names . prototype ) ) ;
if ( prototype . is_object ( ) )
TRY ( result . value ( ) . as_object ( ) . internal_set_prototype_of ( & prototype . as_object ( ) ) ) ;
}
// EOF (End of FIXME)
// a. If Type(result.[[Value]]) is Object, return NormalCompletion(result.[[Value]]).
if ( result . value ( ) . is_object ( ) )
return & result . value ( ) . as_object ( ) ;
// b. If kind is base, return NormalCompletion(thisArgument).
if ( kind = = ConstructorKind : : Base )
return this_argument ;
// c. If result.[[Value]] is not undefined, throw a TypeError exception.
if ( ! result . value ( ) . is_undefined ( ) )
return vm . throw_completion < TypeError > ( global_object , ErrorType : : DerivedConstructorReturningInvalidValue ) ;
}
// 11. Else, ReturnIfAbrupt(result).
else {
// NOTE: I'm not sure if EvaluateBody can return a completion other than Normal, Return, or Throw.
// We're far from using completions in the AST anyway; in the meantime assume Throw.
VERIFY ( result . is_error ( ) ) ;
return result ;
}
// 12. Return ? constructorEnv.GetThisBinding().
auto this_binding = constructor_env - > get_this_binding ( global_object ) ;
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
return & this_binding . as_object ( ) ;
}
2021-09-24 22:40:38 +02:00
void ECMAScriptFunctionObject : : visit_edges ( Visitor & visitor )
2020-04-15 21:58:22 +02:00
{
2021-06-27 21:48:34 +02:00
Base : : visit_edges ( visitor ) ;
2021-06-25 21:22:37 +02:00
visitor . visit ( m_environment ) ;
2021-09-11 19:19:08 +03:00
visitor . visit ( m_realm ) ;
2021-09-24 23:49:24 +02:00
visitor . visit ( m_home_object ) ;
2021-09-25 00:01:09 +02:00
for ( auto & field : m_fields ) {
field . name . visit_edges ( visitor ) ;
visitor . visit ( field . initializer ) ;
}
2020-04-15 21:58:22 +02:00
}
2021-09-22 12:44:56 +02:00
// 9.1.2.4 NewFunctionEnvironment ( F, newTarget ), https://tc39.es/ecma262/#sec-newfunctionenvironment
FunctionEnvironment * ECMAScriptFunctionObject : : new_function_environment ( Object * new_target )
2020-04-15 21:58:22 +02:00
{
2021-09-22 12:44:56 +02:00
auto * environment = heap ( ) . allocate < FunctionEnvironment > ( global_object ( ) , m_environment ) ;
environment - > set_function_object ( * this ) ;
if ( this_mode ( ) = = ThisMode : : Lexical ) {
environment - > set_this_binding_status ( FunctionEnvironment : : ThisBindingStatus : : Lexical ) ;
}
environment - > set_new_target ( new_target ? new_target : js_undefined ( ) ) ;
return environment ;
}
// 10.2.11 FunctionDeclarationInstantiation ( func, argumentsList ), https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
ThrowCompletionOr < void > ECMAScriptFunctionObject : : function_declaration_instantiation ( Interpreter * interpreter )
{
auto & vm = this - > vm ( ) ;
auto & callee_context = vm . running_execution_context ( ) ;
// Needed to extract declarations and functions
ScopeNode const * scope_body = nullptr ;
if ( is < ScopeNode > ( * m_ecmascript_code ) )
scope_body = static_cast < ScopeNode const * > ( m_ecmascript_code . ptr ( ) ) ;
bool has_parameter_expressions = false ;
// FIXME: Maybe compute has duplicates at parse time? (We need to anyway since it's an error in some cases)
bool has_duplicates = false ;
HashTable < FlyString > parameter_names ;
2021-09-24 23:10:21 +02:00
for ( auto & parameter : m_formal_parameters ) {
2021-09-22 12:44:56 +02:00
if ( parameter . default_value )
has_parameter_expressions = true ;
2021-05-29 16:03:19 +04:30
parameter . binding . visit (
2021-09-22 12:44:56 +02:00
[ & ] ( FlyString const & name ) {
if ( parameter_names . set ( name ) ! = AK : : HashSetResult : : InsertedNewEntry )
has_duplicates = true ;
} ,
[ & ] ( NonnullRefPtr < BindingPattern > const & pattern ) {
if ( pattern - > contains_expression ( ) )
has_parameter_expressions = true ;
pattern - > for_each_bound_name ( [ & ] ( auto & name ) {
if ( parameter_names . set ( name ) ! = AK : : HashSetResult : : InsertedNewEntry )
has_duplicates = true ;
2021-05-29 16:03:19 +04:30
} ) ;
} ) ;
2020-04-15 21:58:22 +02:00
}
LibJS: Add an optimization to avoid needless arguments object creation
This gives FunctionNode a "might need arguments object" boolean flag and
sets it based on the simplest possible heuristic for this: if we
encounter an identifier called "arguments" or "eval" up to the next
(nested) function declaration or expression, we won't need an arguments
object. Otherwise, we *might* need one - the final decision is made in
the FunctionDeclarationInstantiation AO.
Now, this is obviously not perfect. Even if you avoid eval, something
like `foo.arguments` will still trigger a false positive - but it's a
start and already massively cuts down on needlessly allocated objects,
especially in real-world code that is often minified, and so a full
"arguments" identifier will be an actual arguments object more often
than not.
To illustrate the actual impact of this change, here's the number of
allocated arguments objects during a full test-js run:
Before:
- Unmapped arguments objects: 78765
- Mapped arguments objects: 2455
After:
- Unmapped arguments objects: 18
- Mapped arguments objects: 37
This results in a ~5% speedup of test-js on my Linux host machine, and
about 3.5% on i686 Serenity in QEMU (warm runs, average of 5).
The following microbenchmark (calling an empty function 1M times) runs
25% faster on Linux and 45% on Serenity:
function foo() {}
for (var i = 0; i < 1_000_000; ++i)
foo();
test262 reports no changes in either direction, apart from a speedup :^)
2021-10-05 08:44:58 +01:00
auto arguments_object_needed = m_might_need_arguments_object ;
if ( this_mode ( ) = = ThisMode : : Lexical )
arguments_object_needed = false ;
2021-09-22 12:44:56 +02:00
if ( parameter_names . contains ( vm . names . arguments . as_string ( ) ) )
2021-10-04 23:59:54 +01:00
arguments_object_needed = false ;
2021-09-22 12:44:56 +02:00
HashTable < FlyString > function_names ;
Vector < FunctionDeclaration const & > functions_to_initialize ;
if ( scope_body ) {
scope_body - > for_each_var_function_declaration_in_reverse_order ( [ & ] ( FunctionDeclaration const & function ) {
if ( function_names . set ( function . name ( ) ) = = AK : : HashSetResult : : InsertedNewEntry )
functions_to_initialize . append ( function ) ;
} ) ;
2021-10-08 01:31:39 +02:00
auto const & arguments_name = vm . names . arguments . as_string ( ) ;
2021-09-22 12:44:56 +02:00
if ( ! has_parameter_expressions & & function_names . contains ( arguments_name ) )
2021-10-04 23:59:54 +01:00
arguments_object_needed = false ;
2021-09-22 12:44:56 +02:00
2021-10-04 23:59:54 +01:00
if ( ! has_parameter_expressions & & arguments_object_needed ) {
2021-09-22 12:44:56 +02:00
scope_body - > for_each_lexically_declared_name ( [ & ] ( auto const & name ) {
2021-10-05 00:02:26 +01:00
if ( name = = arguments_name ) {
2021-10-04 23:59:54 +01:00
arguments_object_needed = false ;
2021-10-05 00:02:26 +01:00
return IterationDecision : : Break ;
}
2021-09-22 12:44:56 +02:00
return IterationDecision : : Continue ;
} ) ;
2020-04-15 21:58:22 +02:00
}
2021-10-05 00:04:25 +01:00
} else {
arguments_object_needed = false ;
2020-04-15 21:58:22 +02:00
}
2020-06-08 13:31:21 -05:00
2021-09-22 12:44:56 +02:00
Environment * environment ;
if ( is_strict_mode ( ) | | ! has_parameter_expressions ) {
environment = callee_context . lexical_environment ;
} else {
environment = new_declarative_environment ( * callee_context . lexical_environment ) ;
VERIFY ( callee_context . variable_environment = = callee_context . lexical_environment ) ;
callee_context . lexical_environment = environment ;
2020-11-28 16:02:27 +01:00
}
2021-09-22 12:44:56 +02:00
for ( auto const & parameter_name : parameter_names ) {
if ( environment - > has_binding ( parameter_name ) )
continue ;
environment - > create_mutable_binding ( global_object ( ) , parameter_name , false ) ;
if ( has_duplicates )
environment - > initialize_binding ( global_object ( ) , parameter_name , js_undefined ( ) ) ;
VERIFY ( ! vm . exception ( ) ) ;
}
2021-10-04 23:59:54 +01:00
if ( arguments_object_needed ) {
2021-09-22 12:44:56 +02:00
Object * arguments_object ;
if ( is_strict_mode ( ) | | ! has_simple_parameter_list ( ) )
arguments_object = create_unmapped_arguments_object ( global_object ( ) , vm . running_execution_context ( ) . arguments ) ;
else
arguments_object = create_mapped_arguments_object ( global_object ( ) , * this , formal_parameters ( ) , vm . running_execution_context ( ) . arguments , * environment ) ;
if ( is_strict_mode ( ) )
environment - > create_immutable_binding ( global_object ( ) , vm . names . arguments . as_string ( ) , false ) ;
else
environment - > create_mutable_binding ( global_object ( ) , vm . names . arguments . as_string ( ) , false ) ;
environment - > initialize_binding ( global_object ( ) , vm . names . arguments . as_string ( ) , arguments_object ) ;
parameter_names . set ( vm . names . arguments . as_string ( ) ) ;
}
// We now treat parameterBindings as parameterNames.
// The spec makes an iterator here to do IteratorBindingInitialization but we just do it manually
auto & execution_context_arguments = vm . running_execution_context ( ) . arguments ;
for ( size_t i = 0 ; i < m_formal_parameters . size ( ) ; + + i ) {
auto & parameter = m_formal_parameters [ i ] ;
parameter . binding . visit (
[ & ] ( auto const & param ) {
Value argument_value ;
if ( parameter . is_rest ) {
auto * array = Array : : create ( global_object ( ) , 0 ) ;
for ( size_t rest_index = i ; rest_index < execution_context_arguments . size ( ) ; + + rest_index )
array - > indexed_properties ( ) . append ( execution_context_arguments [ rest_index ] ) ;
argument_value = move ( array ) ;
} else if ( i < execution_context_arguments . size ( ) & & ! execution_context_arguments [ i ] . is_undefined ( ) ) {
argument_value = execution_context_arguments [ i ] ;
} else if ( parameter . default_value ) {
// FIXME: Support default arguments in the bytecode world!
if ( interpreter )
argument_value = parameter . default_value - > execute ( * interpreter , global_object ( ) ) ;
if ( vm . exception ( ) )
return ;
} else {
argument_value = js_undefined ( ) ;
}
Environment * used_environment = has_duplicates ? nullptr : environment ;
if constexpr ( IsSame < FlyString const & , decltype ( param ) > ) {
Reference reference = vm . resolve_binding ( param , used_environment ) ;
if ( vm . exception ( ) )
return ;
// Here the difference from hasDuplicates is important
if ( has_duplicates )
reference . put_value ( global_object ( ) , argument_value ) ;
else
reference . initialize_referenced_binding ( global_object ( ) , argument_value ) ;
} else if ( IsSame < NonnullRefPtr < BindingPattern > const & , decltype ( param ) > ) {
// Here the difference from hasDuplicates is important
auto result = vm . binding_initialization ( param , argument_value , used_environment , global_object ( ) ) ;
if ( result . is_error ( ) )
return ;
}
if ( vm . exception ( ) )
return ;
} ) ;
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
}
Environment * var_environment ;
HashTable < FlyString > instantiated_var_names ;
2021-10-08 01:32:12 +02:00
if ( scope_body )
instantiated_var_names . ensure_capacity ( scope_body - > var_declaration_count ( ) ) ;
2021-09-22 12:44:56 +02:00
if ( ! has_parameter_expressions ) {
if ( scope_body ) {
scope_body - > for_each_var_declared_name ( [ & ] ( auto const & name ) {
if ( ! parameter_names . contains ( name ) & & instantiated_var_names . set ( name ) = = AK : : HashSetResult : : InsertedNewEntry ) {
environment - > create_mutable_binding ( global_object ( ) , name , false ) ;
environment - > initialize_binding ( global_object ( ) , name , js_undefined ( ) ) ;
}
} ) ;
}
var_environment = environment ;
} else {
var_environment = new_declarative_environment ( * environment ) ;
callee_context . variable_environment = var_environment ;
if ( scope_body ) {
scope_body - > for_each_var_declared_name ( [ & ] ( auto const & name ) {
if ( instantiated_var_names . set ( name ) ! = AK : : HashSetResult : : InsertedNewEntry )
return IterationDecision : : Continue ;
var_environment - > create_mutable_binding ( global_object ( ) , name , false ) ;
Value initial_value ;
if ( ! parameter_names . contains ( name ) | | function_names . contains ( name ) )
initial_value = js_undefined ( ) ;
else
initial_value = environment - > get_binding_value ( global_object ( ) , name , false ) ;
var_environment - > initialize_binding ( global_object ( ) , name , initial_value ) ;
return IterationDecision : : Continue ;
} ) ;
}
}
// B.3.2.1 Changes to FunctionDeclarationInstantiation, https://tc39.es/ecma262/#sec-web-compat-functiondeclarationinstantiation
if ( ! m_strict & & scope_body ) {
scope_body - > for_each_function_hoistable_with_annexB_extension ( [ & ] ( FunctionDeclaration & function_declaration ) {
auto & function_name = function_declaration . name ( ) ;
if ( parameter_names . contains ( function_name ) )
return IterationDecision : : Continue ;
// The spec says 'initializedBindings' here but that does not exist and it then adds it to 'instantiatedVarNames' so it probably means 'instantiatedVarNames'.
if ( ! instantiated_var_names . contains ( function_name ) & & function_name ! = vm . names . arguments . as_string ( ) ) {
var_environment - > create_mutable_binding ( global_object ( ) , function_name , false ) ;
VERIFY ( ! vm . exception ( ) ) ;
var_environment - > initialize_binding ( global_object ( ) , function_name , js_undefined ( ) ) ;
instantiated_var_names . set ( function_name ) ;
}
function_declaration . set_should_do_additional_annexB_steps ( ) ;
return IterationDecision : : Continue ;
} ) ;
}
Environment * lex_environment ;
2021-10-08 12:49:10 +02:00
// 30. If strict is false, then
if ( ! is_strict_mode ( ) ) {
// Optimization: We avoid creating empty top-level declarative environments in non-strict mode, if both of these conditions are true:
// 1. there is no direct call to eval() within this function
// 2. there are no lexical declarations that would go into the environment
bool can_elide_declarative_environment = ! m_contains_direct_call_to_eval & & ( ! scope_body | | ! scope_body - > has_lexical_declarations ( ) ) ;
if ( can_elide_declarative_environment ) {
lex_environment = var_environment ;
} else {
// a. Let lexEnv be NewDeclarativeEnvironment(varEnv).
// b. NOTE: Non-strict functions use a separate Environment Record for top-level lexical declarations so that a direct eval
// can determine whether any var scoped declarations introduced by the eval code conflict with pre-existing top-level
// lexically scoped declarations. This is not needed for strict functions because a strict direct eval always places
// all declarations into a new Environment Record.
lex_environment = new_declarative_environment ( * var_environment ) ;
}
} else {
// 31. Else, let lexEnv be varEnv.
2021-09-22 12:44:56 +02:00
lex_environment = var_environment ;
2021-10-08 12:49:10 +02:00
}
2021-09-22 12:44:56 +02:00
2021-10-08 12:49:10 +02:00
// 32. Set the LexicalEnvironment of calleeContext to lexEnv.
2021-09-22 12:44:56 +02:00
callee_context . lexical_environment = lex_environment ;
if ( ! scope_body )
return { } ;
scope_body - > for_each_lexically_scoped_declaration ( [ & ] ( Declaration const & declaration ) {
declaration . for_each_bound_name ( [ & ] ( auto const & name ) {
if ( declaration . is_constant_declaration ( ) )
lex_environment - > create_immutable_binding ( global_object ( ) , name , true ) ;
else
lex_environment - > create_mutable_binding ( global_object ( ) , name , false ) ;
return IterationDecision : : Continue ;
} ) ;
} ) ;
VERIFY ( ! vm . exception ( ) ) ;
for ( auto & declaration : functions_to_initialize ) {
2021-10-08 12:43:38 +02:00
auto * function = ECMAScriptFunctionObject : : create ( global_object ( ) , declaration . name ( ) , declaration . body ( ) , declaration . parameters ( ) , declaration . function_length ( ) , lex_environment , declaration . kind ( ) , declaration . is_strict_mode ( ) , declaration . might_need_arguments_object ( ) , declaration . contains_direct_call_to_eval ( ) ) ;
2021-09-22 12:44:56 +02:00
var_environment - > set_mutable_binding ( global_object ( ) , declaration . name ( ) , function , false ) ;
}
return { } ;
2020-04-15 21:58:22 +02:00
}
2021-10-08 21:14:22 +01:00
// 10.2.1.1 PrepareForOrdinaryCall ( F, newTarget ), https://tc39.es/ecma262/#sec-prepareforordinarycall
void ECMAScriptFunctionObject : : prepare_for_ordinary_call ( ExecutionContext & callee_context , Object * new_target )
{
auto & vm = this - > vm ( ) ;
// Non-standard
callee_context . is_strict_mode = m_strict ;
// 1. Let callerContext be the running execution context.
// 2. Let calleeContext be a new ECMAScript code execution context.
// NOTE: In the specification, PrepareForOrdinaryCall "returns" a new callee execution context.
// To avoid heap allocations, we put our ExecutionContext objects on the C++ stack instead.
// Whoever calls us should put an ExecutionContext on their stack and pass that as the `callee_context`.
// 3. Set the Function of calleeContext to F.
callee_context . function = this ;
callee_context . function_name = m_name ;
// 4. 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 ) ;
// 5. Set the Realm of calleeContext to calleeRealm.
callee_context . realm = callee_realm ;
// 6. Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].
// FIXME: Our execution context struct currently does not track this item.
// 7. Let localEnv be NewFunctionEnvironment(F, newTarget).
auto * local_environment = new_function_environment ( new_target ) ;
// 8. Set the LexicalEnvironment of calleeContext to localEnv.
callee_context . lexical_environment = local_environment ;
// 9. Set the VariableEnvironment of calleeContext to localEnv.
callee_context . variable_environment = local_environment ;
// 10. Set the PrivateEnvironment of calleeContext to F.[[PrivateEnvironment]].
// FIXME: We currently don't support private environments.
// 11. If callerContext is not already suspended, suspend callerContext.
// FIXME: We don't have this concept yet.
// 12. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
vm . push_execution_context ( callee_context , global_object ( ) ) ;
// 13. NOTE: Any exception objects produced after this point are associated with calleeRealm.
// 14. Return calleeContext. (See NOTE above about how contexts are allocated on the C++ stack.)
}
2021-10-08 21:15:53 +01:00
// 10.2.1.2 OrdinaryCallBindThis ( F, calleeContext, thisArgument ), https://tc39.es/ecma262/#sec-ordinarycallbindthis
void ECMAScriptFunctionObject : : ordinary_call_bind_this ( ExecutionContext & callee_context , Value this_argument )
{
// 1. Let thisMode be F.[[ThisMode]].
auto this_mode = m_this_mode ;
// If thisMode is lexical, return NormalCompletion(undefined).
if ( this_mode = = ThisMode : : Lexical )
return ;
// 3. Let calleeRealm be F.[[Realm]].
auto * callee_realm = m_realm ;
// 4. Let localEnv be the LexicalEnvironment of calleeContext.
auto * local_env = callee_context . lexical_environment ;
Value this_value ;
// 5. If thisMode is strict, let thisValue be thisArgument.
if ( this_mode = = ThisMode : : Strict ) {
this_value = this_argument ;
}
// 6. Else,
else {
// a. If thisArgument is undefined or null, then
if ( this_argument . is_nullish ( ) ) {
// i. Let globalEnv be calleeRealm.[[GlobalEnv]].
// ii. Assert: globalEnv is a global Environment Record.
auto & global_env = callee_realm - > global_environment ( ) ;
// iii. Let thisValue be globalEnv.[[GlobalThisValue]].
this_value = & global_env . global_this_value ( ) ;
}
// b. Else,
else {
// i. Let thisValue be ! ToObject(thisArgument).
this_value = this_argument . to_object ( global_object ( ) ) ;
// ii. NOTE: ToObject produces wrapper objects using calleeRealm.
// FIXME: It currently doesn't, as we pass the function's global object.
}
}
// 7. Assert: localEnv is a function Environment Record.
// 8. Assert: The next step never returns an abrupt completion because localEnv.[[ThisBindingStatus]] is not initialized.
// 9. Return localEnv.BindThisValue(thisValue).
verify_cast < FunctionEnvironment > ( local_env ) - > bind_this_value ( global_object ( ) , this_value ) ;
}
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
// 10.2.1.4 OrdinaryCallEvaluateBody ( F, argumentsList ), https://tc39.es/ecma262/#sec-ordinarycallevaluatebody
Completion ECMAScriptFunctionObject : : ordinary_call_evaluate_body ( )
2020-03-13 10:08:52 +01:00
{
2020-11-11 21:37:23 +00:00
auto & vm = this - > vm ( ) ;
2021-06-05 15:53:36 +02:00
auto * bytecode_interpreter = Bytecode : : Interpreter : : current ( ) ;
if ( bytecode_interpreter ) {
2021-09-22 12:44:56 +02:00
// FIXME: pass something to evaluate default arguments with
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
TRY ( function_declaration_instantiation ( nullptr ) ) ;
2021-06-09 09:11:20 +02:00
if ( ! m_bytecode_executable . has_value ( ) ) {
2021-09-24 23:10:21 +02:00
m_bytecode_executable = Bytecode : : Generator : : generate ( m_ecmascript_code , m_kind = = FunctionKind : : Generator ) ;
2021-06-13 20:40:20 +04:30
auto & passes = JS : : Bytecode : : Interpreter : : optimization_pipeline ( ) ;
passes . perform ( * m_bytecode_executable ) ;
2021-06-07 15:19:48 +02:00
if constexpr ( JS_BYTECODE_DEBUG ) {
2021-06-13 20:40:20 +04:30
dbgln ( " Optimisation passes took {}us " , passes . elapsed ( ) ) ;
2021-06-07 15:19:48 +02:00
dbgln ( " Compiled Bytecode::Block for function '{}': " , m_name ) ;
2021-06-09 09:11:20 +02:00
for ( auto & block : m_bytecode_executable - > basic_blocks )
2021-06-09 10:02:01 +02:00
block . dump ( * m_bytecode_executable ) ;
2021-06-07 15:19:48 +02:00
}
2021-06-07 15:17:37 +02:00
}
2021-06-11 01:38:30 +04:30
auto result = bytecode_interpreter - > run ( * m_bytecode_executable ) ;
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
// NOTE: Running the bytecode should eventually return a completion.
// Until it does, we assume "return" and include the undefined fallback from the call site.
2021-06-11 03:38:05 +04:30
if ( m_kind ! = FunctionKind : : Generator )
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
return { Completion : : Type : : Return , result . value_or ( js_undefined ( ) ) , { } } ;
2021-06-11 01:38:30 +04:30
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
return normal_completion ( GeneratorObject : : create ( global_object ( ) , result , this , vm . running_execution_context ( ) . lexical_environment , bytecode_interpreter - > snapshot_frame ( ) ) ) ;
2021-06-05 15:53:36 +02:00
} else {
2021-06-11 03:38:05 +04:30
VERIFY ( m_kind ! = FunctionKind : : Generator ) ;
2021-06-05 15:53:36 +02:00
OwnPtr < Interpreter > local_interpreter ;
2021-09-22 12:44:56 +02:00
Interpreter * ast_interpreter = vm . interpreter_if_exists ( ) ;
2021-06-05 15:53:36 +02:00
if ( ! ast_interpreter ) {
2021-09-12 12:33:54 +01:00
local_interpreter = Interpreter : : create_with_existing_realm ( * realm ( ) ) ;
2021-06-05 15:53:36 +02:00
ast_interpreter = local_interpreter . ptr ( ) ;
}
2020-09-27 17:24:14 +02:00
2021-06-05 15:53:36 +02:00
VM : : InterpreterExecutionScope scope ( * ast_interpreter ) ;
2021-05-29 16:03:19 +04:30
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
TRY ( function_declaration_instantiation ( ast_interpreter ) ) ;
2020-10-04 13:54:44 +02:00
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
auto result = m_ecmascript_code - > execute ( * ast_interpreter , global_object ( ) ) ;
if ( auto * exception = vm . exception ( ) )
return throw_completion ( exception - > value ( ) ) ;
// NOTE: Running the AST node should eventually return a completion.
// Until it does, we assume "return" and include the undefined fallback from the call site.
return { Completion : : Type : : Return , result . value_or ( js_undefined ( ) ) , { } } ;
2020-08-25 12:52:32 +02:00
}
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
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
:^)
2021-10-08 20:37:21 +01:00
VERIFY_NOT_REACHED ( ) ;
2020-04-01 18:31:24 +01:00
}
2021-09-24 22:40:38 +02:00
void ECMAScriptFunctionObject : : set_name ( const FlyString & name )
2020-04-04 14:34:27 +01:00
{
2021-07-05 18:02:27 +03:00
VERIFY ( ! name . is_null ( ) ) ;
auto & vm = this - > vm ( ) ;
m_name = name ;
2021-10-03 01:35:36 +01:00
auto success = MUST ( define_property_or_throw ( vm . names . name , { . value = js_string ( vm , m_name ) , . writable = false , . enumerable = false , . configurable = true } ) ) ;
2021-07-05 18:02:27 +03:00
VERIFY ( success ) ;
2020-04-04 14:34:27 +01:00
}
2021-09-25 00:01:09 +02:00
// 7.3.31 DefineField ( receiver, fieldRecord ), https://tc39.es/ecma262/#sec-definefield
void ECMAScriptFunctionObject : : InstanceField : : define_field ( VM & vm , Object & receiver ) const
{
Value init_value = js_undefined ( ) ;
if ( initializer ) {
auto init_value_or_error = vm . call ( * initializer , receiver . value_of ( ) ) ;
if ( init_value_or_error . is_error ( ) )
return ;
init_value = init_value_or_error . release_value ( ) ;
}
2021-10-03 01:18:46 +01:00
( void ) receiver . create_data_property_or_throw ( name , init_value ) ;
2021-09-25 00:01:09 +02:00
}
2020-03-13 10:08:52 +01:00
}