@ -14,7 +14,7 @@
use crate ::{
use crate ::{
builtins ::{
builtins ::{
array ::Array ,
array ::Array ,
object ::{ Object , ObjectInternalMethods , ObjectKind , PROTOTYPE } ,
object ::{ Object , ObjectInternalMethods , ObjectKind , INSTANCE_PROTOTYPE , PROTOTYPE } ,
property ::Property ,
property ::Property ,
value ::{ ResultValue , Value } ,
value ::{ ResultValue , Value } ,
} ,
} ,
@ -72,18 +72,6 @@ unsafe impl Trace for FunctionBody {
unsafe_empty_trace ! ( ) ;
unsafe_empty_trace ! ( ) ;
}
}
/// Signal what sort of function this is
#[ derive(Clone, Debug, Copy, Finalize) ]
pub enum FunctionKind {
BuiltIn ,
Ordinary ,
}
/// Waiting on <https://github.com/Manishearth/rust-gc/issues/87> until we can derive Copy
unsafe impl Trace for FunctionKind {
unsafe_empty_trace ! ( ) ;
}
/// Boa representation of a Function Object.
/// Boa representation of a Function Object.
///
///
/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects>
/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects>
@ -95,48 +83,73 @@ pub struct Function {
pub params : Box < [ FormalParameter ] > ,
pub params : Box < [ FormalParameter ] > ,
/// This Mode
/// This Mode
pub this_mode : ThisMode ,
pub this_mode : ThisMode ,
/// Function kind
pub kind : FunctionKind ,
// Environment, built-in functions don't need Environments
// Environment, built-in functions don't need Environments
pub environment : Option < Environment > ,
pub environment : Option < Environment > ,
/// Is it constructable
constructable : bool ,
/// Is it callable.
callable : bool ,
}
}
impl Function {
impl Function {
/// This will create an ordinary function object
pub fn new < P > (
///
/// <https://tc39.es/ecma262/#sec-ordinaryfunctioncreate>
pub fn create_ordinary < P > (
parameter_list : P ,
parameter_list : P ,
scope : Environment ,
scope : Option < Environment > ,
body : FunctionBody ,
body : FunctionBody ,
this_mode : ThisMode ,
this_mode : ThisMode ,
constructable : bool ,
callable : bool ,
) -> Self
) -> Self
where
where
P : Into < Box < [ FormalParameter ] > > ,
P : Into < Box < [ FormalParameter ] > > ,
{
{
Self {
Self {
body ,
body ,
environment : Some ( scope ) ,
environment : scope ,
params : parameter_list . into ( ) ,
params : parameter_list . into ( ) ,
kind : FunctionKind ::Ordinary ,
this_mode ,
this_mode ,
constructable ,
callable ,
}
}
}
}
/// This will create an ordinary function object
///
/// <https://tc39.es/ecma262/#sec-ordinaryfunctioncreate>
pub fn ordinary < P > (
parameter_list : P ,
scope : Environment ,
body : StatementList ,
this_mode : ThisMode ,
) -> Self
where
P : Into < Box < [ FormalParameter ] > > ,
{
Self ::new (
parameter_list . into ( ) ,
Some ( scope ) ,
FunctionBody ::Ordinary ( body ) ,
this_mode ,
true ,
true ,
)
}
/// This will create a built-in function object
/// This will create a built-in function object
///
///
/// <https://tc39.es/ecma262/#sec-createbuiltinfunction>
/// <https://tc39.es/ecma262/#sec-createbuiltinfunction>
pub fn create_builtin < P > ( parameter_list : P , body : FunctionBody ) -> Self
pub fn builtin < P > ( parameter_list : P , body : NativeFunctionData ) -> Self
where
where
P : Into < Box < [ FormalParameter ] > > ,
P : Into < Box < [ FormalParameter ] > > ,
{
{
Self {
Self ::new (
body ,
parameter_list . into ( ) ,
params : parameter_list . into ( ) ,
None ,
this_mode : ThisMode ::NonLexical ,
FunctionBody ::BuiltIn ( body ) ,
kind : FunctionKind ::BuiltIn ,
ThisMode ::NonLexical ,
environment : None ,
false ,
}
true ,
)
}
}
/// This will handle calls for both ordinary and built-in functions
/// This will handle calls for both ordinary and built-in functions
@ -150,57 +163,53 @@ impl Function {
interpreter : & mut Interpreter ,
interpreter : & mut Interpreter ,
this_obj : & mut Value ,
this_obj : & mut Value ,
) -> ResultValue {
) -> ResultValue {
match self . kind {
if self . callable {
FunctionKind ::BuiltIn = > match & self . body {
match self . body {
FunctionBody ::BuiltIn ( func ) = > func ( this_obj , args_list , interpreter ) ,
FunctionBody ::BuiltIn ( func ) = > func ( this_obj , args_list , interpreter ) ,
FunctionBody ::Ordinary ( _ ) = > {
FunctionBody ::Ordinary ( ref body ) = > {
panic! ( "Builtin function should not have Ordinary Function body" )
// Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
}
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
} ,
let local_env = new_function_environment (
FunctionKind ::Ordinary = > {
this . clone ( ) ,
// Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
None ,
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
Some ( self . environment . as_ref ( ) . unwrap ( ) . clone ( ) ) ,
let local_env = new_function_environment (
BindingStatus ::Uninitialized ,
this . clone ( ) ,
) ;
None ,
Some ( self . environment . as_ref ( ) . unwrap ( ) . clone ( ) ) ,
// Add argument bindings to the function environment
BindingStatus ::Uninitialized ,
for i in 0 .. self . params . len ( ) {
) ;
let param = self . params . get ( i ) . expect ( "Could not get param" ) ;
// Rest Parameters
// Add argument bindings to the function environment
if param . is_rest_param ( ) {
for i in 0 .. self . params . len ( ) {
self . add_rest_param ( param , i , args_list , interpreter , & local_env ) ;
let param = self . params . get ( i ) . expect ( "Could not get param" ) ;
break ;
// Rest Parameters
}
if param . is_rest_param ( ) {
self . add_rest_param ( param , i , args_list , interpreter , & local_env ) ;
let value = args_list . get ( i ) . expect ( "Could not get value" ) ;
break ;
self . add_arguments_to_environment ( param , value . clone ( ) , & local_env ) ;
}
}
let value = args_list . get ( i ) . expect ( "Could not get value" ) ;
// Add arguments object
self . add_arguments_to_environment ( param , value . clone ( ) , & local_env ) ;
let arguments_obj = create_unmapped_arguments_object ( args_list ) ;
}
local_env
. borrow_mut ( )
. create_mutable_binding ( "arguments" . to_string ( ) , false ) ;
local_env
. borrow_mut ( )
. initialize_binding ( "arguments" , arguments_obj ) ;
interpreter . realm . environment . push ( local_env ) ;
// Add arguments object
// Call body should be set before reaching here
let arguments_obj = create_unmapped_arguments_object ( args_list ) ;
let result = body . run ( interpreter ) ;
local_env
. borrow_mut ( )
// local_env gets dropped here, its no longer needed
. create_mutable_binding ( "arguments" . to_string ( ) , false ) ;
interpreter . realm . environment . pop ( ) ;
local_env
result
. borrow_mut ( )
}
. initialize_binding ( "arguments" , arguments_obj ) ;
interpreter . realm . environment . push ( local_env ) ;
// Call body should be set before reaching here
let result = match & self . body {
FunctionBody ::Ordinary ( ref body ) = > body . run ( interpreter ) ,
_ = > panic! ( "Ordinary function should not have BuiltIn Function body" ) ,
} ;
// local_env gets dropped here, its no longer needed
interpreter . realm . environment . pop ( ) ;
result
}
}
} else {
panic! ( "TypeError: class constructors must be invoked with 'new'" ) ;
}
}
}
}
@ -212,59 +221,56 @@ impl Function {
interpreter : & mut Interpreter ,
interpreter : & mut Interpreter ,
this_obj : & mut Value ,
this_obj : & mut Value ,
) -> ResultValue {
) -> ResultValue {
match self . kind {
if self . constructable {
FunctionKind ::BuiltIn = > match & self . body {
match self . body {
FunctionBody ::BuiltIn ( func ) = > {
FunctionBody ::BuiltIn ( func ) = > {
func ( this_obj , args_list , interpreter ) . unwrap ( ) ;
func ( this_obj , args_list , interpreter ) . unwrap ( ) ;
Ok ( this_obj . clone ( ) )
Ok ( this_obj . clone ( ) )
}
}
FunctionBody ::Ordinary ( _ ) = > {
FunctionBody ::Ordinary ( ref body ) = > {
panic! ( "Builtin function should not have Ordinary Function body" )
// Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
}
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
} ,
let local_env = new_function_environment (
FunctionKind ::Ordinary = > {
this . clone ( ) ,
// Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
Some ( this_obj . clone ( ) ) ,
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
Some ( self . environment . as_ref ( ) . unwrap ( ) . clone ( ) ) ,
let local_env = new_function_environment (
BindingStatus ::Initialized ,
this . clone ( ) ,
) ;
Some ( this_obj . clone ( ) ) ,
Some ( self . environment . as_ref ( ) . unwrap ( ) . clone ( ) ) ,
// Add argument bindings to the function environment
BindingStatus ::Initialized ,
for ( i , param ) in self . params . iter ( ) . enumerate ( ) {
) ;
// Rest Parameters
if param . is_rest_param ( ) {
// Add argument bindings to the function environment
self . add_rest_param ( param , i , args_list , interpreter , & local_env ) ;
for ( i , param ) in self . params . iter ( ) . enumerate ( ) {
break ;
// Rest Parameters
}
if param . is_rest_param ( ) {
self . add_rest_param ( param , i , args_list , interpreter , & local_env ) ;
let value = args_list . get ( i ) . expect ( "Could not get value" ) ;
break ;
self . add_arguments_to_environment ( param , value . clone ( ) , & local_env ) ;
}
}
let value = args_list . get ( i ) . expect ( "Could not get value" ) ;
// Add arguments object
self . add_arguments_to_environment ( param , value . clone ( ) , & local_env ) ;
let arguments_obj = create_unmapped_arguments_object ( args_list ) ;
}
local_env
. borrow_mut ( )
. create_mutable_binding ( "arguments" . to_string ( ) , false ) ;
local_env
. borrow_mut ( )
. initialize_binding ( "arguments" , arguments_obj ) ;
interpreter . realm . environment . push ( local_env ) ;
// Call body should be set before reaching here
let _ = body . run ( interpreter ) ;
// Add arguments object
// local_env gets dropped here, its no longer needed
let arguments_obj = create_unmapped_arguments_object ( args_list ) ;
let binding = interpreter . realm . environment . get_this_binding ( ) ;
local_env
Ok ( binding )
. borrow_mut ( )
}
. create_mutable_binding ( "arguments" . to_string ( ) , false ) ;
local_env
. borrow_mut ( )
. initialize_binding ( "arguments" , arguments_obj ) ;
interpreter . realm . environment . push ( local_env ) ;
// Call body should be set before reaching here
let _ = match & self . body {
FunctionBody ::Ordinary ( ref body ) = > body . run ( interpreter ) ,
_ = > panic! ( "Ordinary function should not have BuiltIn Function body" ) ,
} ;
// local_env gets dropped here, its no longer needed
let binding = interpreter . realm . environment . get_this_binding ( ) ;
Ok ( binding )
}
}
} else {
let name = this . get_field ( "name" ) . to_string ( ) ;
panic! ( "TypeError: {} is not a constructor" , name ) ;
}
}
}
}
@ -309,6 +315,16 @@ impl Function {
. borrow_mut ( )
. borrow_mut ( )
. initialize_binding ( param . name ( ) , value ) ;
. initialize_binding ( param . name ( ) , value ) ;
}
}
/// Returns true if the function object is callable.
pub fn is_callable ( & self ) -> bool {
self . callable
}
/// Returns true if the function object is constructable.
pub fn is_constructable ( & self ) -> bool {
self . constructable
}
}
}
impl Debug for Function {
impl Debug for Function {
@ -319,16 +335,6 @@ impl Debug for Function {
}
}
}
}
/// Function Prototype.
///
/// <https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object>
pub fn create_function_prototype ( ) {
let mut function_prototype : Object = Object ::default ( ) ;
// Set Kind to function (for historical & compatibility reasons)
// <https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object>
function_prototype . kind = ObjectKind ::Function ;
}
/// Arguments.
/// Arguments.
///
///
/// <https://tc39.es/ecma262/#sec-createunmappedargumentsobject>
/// <https://tc39.es/ecma262/#sec-createunmappedargumentsobject>
@ -369,19 +375,25 @@ pub fn make_function(this: &mut Value, _: &[Value], _: &mut Interpreter) -> Resu
pub fn create ( global : & Value ) -> Value {
pub fn create ( global : & Value ) -> Value {
let prototype = Value ::new_object ( Some ( global ) ) ;
let prototype = Value ::new_object ( Some ( global ) ) ;
make_constructor_fn ( make_function , global , prototype )
make_constructor_fn ( "Function" , 1 , make_function , global , prototype , tru e )
}
}
/// Creates a new constructor function
/// Creates a new constructor function
///
///
/// This utility function handling linking the new Constructor to the prototype.
/// This utility function handling linking the new Constructor to the prototype.
/// So far this is only used by internal functions
/// So far this is only used by internal functions
pub fn make_constructor_fn ( body : NativeFunctionData , global : & Value , proto : Value ) -> Value {
pub fn make_constructor_fn (
name : & str ,
length : i32 ,
body : NativeFunctionData ,
global : & Value ,
proto : Value ,
constructable : bool ,
) -> Value {
// Create the native function
// Create the native function
let constructor_fn = crate ::builtins ::function ::Function ::create_builtin (
let mut constructor_fn = Function ::builtin ( Vec ::new ( ) , body ) ;
vec! [ ] ,
crate ::builtins ::function ::FunctionBody ::BuiltIn ( body ) ,
constructor_fn . constructable = constructable ;
) ;
// Get reference to Function.prototype
// Get reference to Function.prototype
let func_prototype = global . get_field ( "Function" ) . get_field ( PROTOTYPE ) ;
let func_prototype = global . get_field ( "Function" ) . get_field ( PROTOTYPE ) ;
@ -390,13 +402,27 @@ pub fn make_constructor_fn(body: NativeFunctionData, global: &Value, proto: Valu
let mut constructor_obj = Object ::function ( ) ;
let mut constructor_obj = Object ::function ( ) ;
constructor_obj . set_func ( constructor_fn ) ;
constructor_obj . set_func ( constructor_fn ) ;
constructor_obj . set_internal_slot ( "__proto__" , func_prototype ) ;
constructor_obj . set_internal_slot ( INSTANCE_PROTOTYPE , func_prototype ) ;
let constructor_val = Value ::from ( constructor_obj ) ;
let constructor_val = Value ::from ( constructor_obj ) ;
// Set proto.constructor -> constructor_obj
// Set proto.constructor -> constructor_obj
proto . set_field ( "constructor" , constructor_val . clone ( ) ) ;
proto . set_field ( "constructor" , constructor_val . clone ( ) ) ;
constructor_val . set_field ( PROTOTYPE , proto ) ;
constructor_val . set_field ( PROTOTYPE , proto ) ;
let length = Property ::new ( )
. value ( Value ::from ( length ) )
. writable ( false )
. configurable ( false )
. enumerable ( false ) ;
constructor_val . set_property_slice ( "length" , length ) ;
let name = Property ::new ( )
. value ( Value ::from ( name ) )
. writable ( false )
. configurable ( false )
. enumerable ( false ) ;
constructor_val . set_property_slice ( "name" , name ) ;
constructor_val
constructor_val
}
}
@ -407,7 +433,7 @@ pub fn make_builtin_fn<N>(function: NativeFunctionData, name: N, parent: &Value,
where
where
N : Into < String > ,
N : Into < String > ,
{
{
let func = Function ::create_ builtin( vec! [ ] , FunctionBody ::BuiltIn ( function ) ) ;
let func = Function ::builtin ( Vec ::new ( ) , function ) ;
let mut new_func = Object ::function ( ) ;
let mut new_func = Object ::function ( ) ;
new_func . set_func ( func ) ;
new_func . set_func ( func ) ;