private JsValue Bind(JsValue thisObj, JsValue[] arguments) { var target = thisObj.TryCast <ICallable>(x => { ExceptionHelper.ThrowTypeError(Engine); }); var thisArg = arguments.At(0); var f = new BindFunctionInstance(Engine) { TargetFunction = thisObj, BoundThis = thisArg, BoundArgs = arguments.Skip(1), _prototype = Engine.Function.PrototypeObject }; if (target is FunctionInstance functionInstance) { var l = TypeConverter.ToNumber(functionInstance.Get(CommonProperties.Length, functionInstance)) - (arguments.Length - 1); f.SetOwnProperty(CommonProperties.Length, new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden)); } else { f.SetOwnProperty(CommonProperties.Length, PropertyDescriptor.AllForbiddenDescriptor.NumberZero); } f.DefineOwnProperty(CommonProperties.Caller, _engine._getSetThrower); f.DefineOwnProperty(CommonProperties.Arguments, _engine._getSetThrower); return(f); }
private JsValue Bind(JsValue thisObj, JsValue[] arguments) { var target = thisObj.TryCast <ICallable>(x => { ExceptionHelper.ThrowTypeError(Engine); }); var thisArg = arguments.At(0); var f = new BindFunctionInstance(Engine) { Extensible = true }; f.TargetFunction = thisObj; f.BoundThis = thisArg; f.BoundArgs = arguments.Skip(1); f.Prototype = Engine.Function.PrototypeObject; var o = target as FunctionInstance; if (!ReferenceEquals(o, null)) { var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1); f.SetOwnProperty("length", new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden)); } else { f.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden)); } var thrower = Engine.Function.ThrowTypeError; const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet; f.DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false); f.DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false); return(f); }