ConstructLateBound() public method

Creates an object, using this function as the constructor.
public ConstructLateBound ( ) : ObjectInstance
return ObjectInstance
Exemplo n.º 1
0
        /// <summary>
        /// Creates an object, using this function as the constructor.
        /// </summary>
        /// <param name="newTarget"> The value of 'new.target'. </param>
        /// <param name="argumentValues"> An array of argument values. </param>
        /// <returns> The object that was created. </returns>
        public override ObjectInstance ConstructLateBound(FunctionInstance newTarget, params object[] argumentValues)
        {
            // Check for revocation.
            if (target == null || handler == null)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Cannot call 'construct' on a proxy that has been revoked.");
            }

            if (!this.target.IsConstructor)
            {
                throw new JavaScriptException(ErrorType.TypeError, "proxy is not a constructor.");
            }

            // Call the handler, if one exists.
            var trap = handler.GetMethod("construct");

            if (trap == null)
            {
                return(target.ConstructLateBound(newTarget, argumentValues));
            }
            var array  = Engine.Array.New(argumentValues);
            var result = trap.CallLateBound(handler, target, array, newTarget);

            // Validate.
            if (result is ObjectInstance objectInstance)
            {
                return(objectInstance);
            }
            throw new JavaScriptException(ErrorType.TypeError, $"'construct' on proxy: trap returned non-object ('{TypeConverter.ToString(result)}').");
        }
Exemplo n.º 2
0
 public static object Construct(FunctionInstance target, ObjectInstance argumentsList, FunctionInstance newTarget = null)
 {
     if (newTarget == null)
     {
         newTarget = target;
     }
     return(target.ConstructLateBound(newTarget, TypeUtilities.CreateListFromArrayLike(argumentsList)));
 }