@@ -446,26 +446,19 @@ pub struct FunctionObject<'gc> {
446446}
447447
448448impl < ' gc > FunctionObject < ' gc > {
449- /// Construct a function with any combination of regular and constructor parts .
449+ /// Builds a new function object .
450450 ///
451451 /// `fn_proto` refers to the implicit proto of the function object, and the
452- /// `prototype` refers to the explicit prototype of the function.
453- /// The function and its prototype will be linked to each other.
454- fn allocate_function (
452+ /// `prototype` refers to the explicit prototype of the function. If the
453+ /// prototype is present, it will be linked with the newly-created object.
454+ pub fn build (
455+ self ,
455456 context : & StringContext < ' gc > ,
456- function : Executable < ' gc > ,
457- constructor : Option < NativeFunction > ,
458457 fn_proto : Object < ' gc > ,
459458 prototype : Option < Object < ' gc > > ,
460459 ) -> Object < ' gc > {
461460 let obj = Object :: new ( context, Some ( fn_proto) ) ;
462- let native = NativeObject :: Function ( Gc :: new (
463- context. gc ( ) ,
464- Self {
465- function,
466- constructor,
467- } ,
468- ) ) ;
461+ let native = NativeObject :: Function ( Gc :: new ( context. gc ( ) , self ) ) ;
469462 obj. set_native ( context. gc ( ) , native) ;
470463
471464 if let Some ( prototype) = prototype {
@@ -486,62 +479,45 @@ impl<'gc> FunctionObject<'gc> {
486479 obj
487480 }
488481
489- /// Constructs a function that does nothing.
482+ /// A function that does nothing.
490483 ///
491484 /// This can also serve as a no-op constructor.
492- pub fn empty (
493- context : & StringContext < ' gc > ,
494- fn_proto : Object < ' gc > ,
495- prototype : Object < ' gc > ,
496- ) -> Object < ' gc > {
497- let empty = Executable :: Native ( |_, _, _| Ok ( Value :: Undefined ) ) ;
498- Self :: allocate_function ( context, empty, None , fn_proto, Some ( prototype) )
485+ pub fn empty ( ) -> Self {
486+ Self {
487+ function : Executable :: Native ( |_, _, _| Ok ( Value :: Undefined ) ) ,
488+ constructor : None ,
489+ }
499490 }
500491
501- /// Construct a function from AVM1 bytecode and associated protos.
502- pub fn function (
503- context : & StringContext < ' gc > ,
504- function : Gc < ' gc , Avm1Function < ' gc > > ,
505- fn_proto : Object < ' gc > ,
506- prototype : Object < ' gc > ,
507- ) -> Object < ' gc > {
508- let function = Executable :: Action ( function) ;
509- Self :: allocate_function ( context, function, None , fn_proto, Some ( prototype) )
492+ /// A function with AVM1 bytecode.
493+ pub fn bytecode ( function : Gc < ' gc , Avm1Function < ' gc > > ) -> Self {
494+ Self {
495+ function : Executable :: Action ( function) ,
496+ constructor : None ,
497+ }
510498 }
511499
512- /// Construct a function from a native executable and associated protos.
513- pub fn native (
514- context : & StringContext < ' gc > ,
515- function : NativeFunction ,
516- fn_proto : Object < ' gc > ,
517- prototype : Option < Object < ' gc > > ,
518- ) -> Object < ' gc > {
519- let function = Executable :: Native ( function) ;
520- Self :: allocate_function ( context, function, None , fn_proto, prototype)
500+ /// A function with a native executable.
501+ pub fn native ( function : NativeFunction ) -> Self {
502+ Self {
503+ function : Executable :: Native ( function) ,
504+ constructor : None ,
505+ }
521506 }
522507
523- /// Construct a native constructor from native executables and associated protos .
508+ /// A native constructor.
524509 ///
525510 /// This differs from [`Self::native`] in two important ways:
526511 /// - When called through `new`, the return value will always become the result of the
527512 /// operation. Native constructors should therefore generally return either `this`,
528513 /// if the object was successfully constructed, or `undefined` if not.
529514 /// - When called as a normal function, `function` will be called instead of `constructor`;
530515 /// if it is `None`, the return value will be `undefined`.
531- pub fn constructor (
532- context : & StringContext < ' gc > ,
533- constructor : NativeFunction ,
534- function : Option < NativeFunction > ,
535- fn_proto : Object < ' gc > ,
536- prototype : Object < ' gc > ,
537- ) -> Object < ' gc > {
538- Self :: allocate_function (
539- context,
540- Executable :: Native ( function. unwrap_or ( |_, _, _| Ok ( Value :: Undefined ) ) ) ,
541- Some ( constructor) ,
542- fn_proto,
543- Some ( prototype) ,
544- )
516+ pub fn constructor ( constructor : NativeFunction , function : Option < NativeFunction > ) -> Self {
517+ Self {
518+ function : Executable :: Native ( function. unwrap_or ( |_, _, _| Ok ( Value :: Undefined ) ) ) ,
519+ constructor : Some ( constructor) ,
520+ }
545521 }
546522
547523 /// Execute the given code.
0 commit comments