1414use crate :: {
1515 builtins:: {
1616 array:: Array ,
17- object:: { Object , ObjectInternalMethods , ObjectKind , INSTANCE_PROTOTYPE , PROTOTYPE } ,
17+ object:: { Object , ObjectData , INSTANCE_PROTOTYPE , PROTOTYPE } ,
1818 property:: Property ,
1919 value:: { ResultValue , Value } ,
2020 } ,
@@ -39,7 +39,7 @@ pub enum ConstructorKind {
3939/// Defines how this references are interpreted within the formal parameters and code body of the function.
4040///
4141/// Arrow functions don't define a `this` and thus are lexical, `function`s do define a this and thus are NonLexical
42- #[ derive( Trace , Finalize , Debug , Clone ) ]
42+ #[ derive( Trace , Finalize , Debug , Clone , PartialEq , PartialOrd , Hash ) ]
4343pub enum ThisMode {
4444 Lexical ,
4545 NonLexical ,
@@ -55,12 +55,24 @@ pub enum FunctionBody {
5555impl Debug for FunctionBody {
5656 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
5757 match self {
58- Self :: BuiltIn ( _) => write ! ( f, "native code " ) ,
58+ Self :: BuiltIn ( _) => write ! ( f, "[ native] " ) ,
5959 Self :: Ordinary ( statements) => write ! ( f, "{:?}" , statements) ,
6060 }
6161 }
6262}
6363
64+ impl PartialEq for FunctionBody {
65+ fn eq ( & self , other : & Self ) -> bool {
66+ match ( self , other) {
67+ ( Self :: BuiltIn ( a) , Self :: BuiltIn ( b) ) => std:: ptr:: eq ( a, b) ,
68+ ( Self :: Ordinary ( a) , Self :: Ordinary ( b) ) => a == b,
69+ ( _, _) => false ,
70+ }
71+ }
72+ }
73+
74+ impl Eq for FunctionBody { }
75+
6476/// `Trace` implementation for `FunctionBody`.
6577///
6678/// This is indeed safe, but we need to mark this as an empty trace because neither
@@ -158,19 +170,19 @@ impl Function {
158170 /// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist>
159171 pub fn call (
160172 & self ,
161- this : & mut Value , // represents a pointer to this function object wrapped in a GC (not a `this` JS object)
173+ function : Value , // represents a pointer to this function object wrapped in a GC (not a `this` JS object)
174+ this : & mut Value ,
162175 args_list : & [ Value ] ,
163176 interpreter : & mut Interpreter ,
164- this_obj : & mut Value ,
165177 ) -> ResultValue {
166178 if self . callable {
167179 match self . body {
168- FunctionBody :: BuiltIn ( func) => func ( this_obj , args_list, interpreter) ,
180+ FunctionBody :: BuiltIn ( func) => func ( this , args_list, interpreter) ,
169181 FunctionBody :: Ordinary ( ref body) => {
170182 // Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
171183 // <https://tc39.es/ecma262/#sec-prepareforordinarycall>
172184 let local_env = new_function_environment (
173- this . clone ( ) ,
185+ function ,
174186 None ,
175187 Some ( self . environment . as_ref ( ) . unwrap ( ) . clone ( ) ) ,
176188 BindingStatus :: Uninitialized ,
@@ -216,23 +228,23 @@ impl Function {
216228 /// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget>
217229 pub fn construct (
218230 & self ,
219- this : & mut Value , // represents a pointer to this function object wrapped in a GC (not a `this` JS object)
231+ function : Value , // represents a pointer to this function object wrapped in a GC (not a `this` JS object)
232+ this : & mut Value ,
220233 args_list : & [ Value ] ,
221234 interpreter : & mut Interpreter ,
222- this_obj : & mut Value ,
223235 ) -> ResultValue {
224236 if self . constructable {
225237 match self . body {
226238 FunctionBody :: BuiltIn ( func) => {
227- func ( this_obj , args_list, interpreter) . unwrap ( ) ;
228- Ok ( this_obj . clone ( ) )
239+ func ( this , args_list, interpreter) ? ;
240+ Ok ( this . clone ( ) )
229241 }
230242 FunctionBody :: Ordinary ( ref body) => {
231243 // Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
232244 // <https://tc39.es/ecma262/#sec-prepareforordinarycall>
233245 let local_env = new_function_environment (
234- this . clone ( ) ,
235- Some ( this_obj . clone ( ) ) ,
246+ function ,
247+ Some ( this . clone ( ) ) ,
236248 Some ( self . environment . as_ref ( ) . unwrap ( ) . clone ( ) ) ,
237249 BindingStatus :: Initialized ,
238250 ) ;
@@ -357,7 +369,7 @@ pub fn create_unmapped_arguments_object(arguments_list: &[Value]) -> Value {
357369 . writable ( true )
358370 . configurable ( true ) ;
359371
360- obj. properties . insert ( index. to_string ( ) , prop) ;
372+ obj. properties_mut ( ) . insert ( index. to_string ( ) , prop) ;
361373 index += 1 ;
362374 }
363375
@@ -368,7 +380,10 @@ pub fn create_unmapped_arguments_object(arguments_list: &[Value]) -> Value {
368380///
369381// This gets called when a new Function() is created.
370382pub fn make_function ( this : & mut Value , _: & [ Value ] , _: & mut Interpreter ) -> ResultValue {
371- this. set_kind ( ObjectKind :: Function ) ;
383+ this. set_data ( ObjectData :: Function ( Function :: builtin (
384+ Vec :: new ( ) ,
385+ |_, _, _| Ok ( Value :: undefined ( ) ) ,
386+ ) ) ) ;
372387 Ok ( this. clone ( ) )
373388}
374389
@@ -399,8 +414,7 @@ pub fn make_constructor_fn(
399414 let func_prototype = global. get_field ( "Function" ) . get_field ( PROTOTYPE ) ;
400415
401416 // Create the function object and point its instance prototype to Function.prototype
402- let mut constructor_obj = Object :: function ( ) ;
403- constructor_obj. set_func ( constructor_fn) ;
417+ let mut constructor_obj = Object :: function ( constructor_fn) ;
404418
405419 constructor_obj. set_internal_slot ( INSTANCE_PROTOTYPE , func_prototype) ;
406420 let constructor_val = Value :: from ( constructor_obj) ;
@@ -414,14 +428,14 @@ pub fn make_constructor_fn(
414428 . writable ( false )
415429 . configurable ( false )
416430 . enumerable ( false ) ;
417- constructor_val. set_property_slice ( "length" , length) ;
431+ constructor_val. set_property ( "length" , length) ;
418432
419433 let name = Property :: new ( )
420434 . value ( Value :: from ( name) )
421435 . writable ( false )
422436 . configurable ( false )
423437 . enumerable ( false ) ;
424- constructor_val. set_property_slice ( "name" , name) ;
438+ constructor_val. set_property ( "name" , name) ;
425439
426440 constructor_val
427441}
@@ -433,12 +447,9 @@ pub fn make_builtin_fn<N>(function: NativeFunctionData, name: N, parent: &Value,
433447where
434448 N : Into < String > ,
435449{
436- let func = Function :: builtin ( Vec :: new ( ) , function) ;
437-
438- let mut new_func = Object :: function ( ) ;
439- new_func. set_func ( func) ;
450+ let func = Object :: function ( Function :: builtin ( Vec :: new ( ) , function) ) ;
440451
441- let new_func_obj = Value :: from ( new_func ) ;
452+ let new_func_obj = Value :: from ( func ) ;
442453 new_func_obj. set_field ( "length" , length) ;
443454
444455 parent. set_field ( name. into ( ) , new_func_obj) ;
0 commit comments