FunctionPrototype.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using Jint.Collections;
  3. using Jint.Native.Array;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Function
  9. {
  10. /// <summary>
  11. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
  12. /// </summary>
  13. public sealed class FunctionPrototype : FunctionInstance
  14. {
  15. private static readonly JsString _functionName = new JsString("Function");
  16. private FunctionPrototype(Engine engine)
  17. : base(engine, _functionName, strict: false)
  18. {
  19. }
  20. public static FunctionPrototype CreatePrototypeObject(Engine engine)
  21. {
  22. var obj = new FunctionPrototype(engine)
  23. {
  24. Extensible = true,
  25. // The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
  26. Prototype = engine.Object.PrototypeObject,
  27. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero
  28. };
  29. return obj;
  30. }
  31. protected override void Initialize()
  32. {
  33. _properties = new StringDictionarySlim<PropertyDescriptor>(5)
  34. {
  35. ["constructor"] = new PropertyDescriptor(Engine.Function, PropertyFlag.NonEnumerable),
  36. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToString), true, false, true),
  37. ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 2), true, false, true),
  38. ["call"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "call", CallImpl, 1), true, false, true),
  39. ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "bind", Bind, 1), true, false, true)
  40. };
  41. }
  42. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  43. {
  44. var target = thisObj.TryCast<ICallable>(x =>
  45. {
  46. ExceptionHelper.ThrowTypeError(Engine);
  47. });
  48. var thisArg = arguments.At(0);
  49. var f = new BindFunctionInstance(Engine)
  50. {
  51. Extensible = true,
  52. TargetFunction = thisObj,
  53. BoundThis = thisArg,
  54. BoundArgs = arguments.Skip(1),
  55. Prototype = Engine.Function.PrototypeObject
  56. };
  57. if (target is FunctionInstance functionInstance)
  58. {
  59. var l = TypeConverter.ToNumber(functionInstance.Get("length")) - (arguments.Length - 1);
  60. f.SetOwnProperty(KnownKeys.Length, new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden));
  61. }
  62. else
  63. {
  64. f.SetOwnProperty(KnownKeys.Length, PropertyDescriptor.AllForbiddenDescriptor.NumberZero);
  65. }
  66. f.DefineOwnProperty(KnownKeys.Caller, _engine._getSetThrower, false);
  67. f.DefineOwnProperty(KnownKeys.Arguments, _engine._getSetThrower, false);
  68. return f;
  69. }
  70. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  71. {
  72. if (!(thisObj is FunctionInstance))
  73. {
  74. return ExceptionHelper.ThrowTypeError<FunctionInstance>(_engine, "Function object expected.");
  75. }
  76. return "function() {{ ... }}";
  77. }
  78. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  79. {
  80. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  81. var thisArg = arguments.At(0);
  82. var argArray = arguments.At(1);
  83. if (argArray.IsNullOrUndefined())
  84. {
  85. return func.Call(thisArg, Arguments.Empty);
  86. }
  87. var argArrayObj = argArray as ObjectInstance ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(Engine);
  88. var operations = ArrayPrototype.ArrayOperations.For(argArrayObj);
  89. var argList = operations.GetAll();
  90. var result = func.Call(thisArg, argList);
  91. return result;
  92. }
  93. private JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  94. {
  95. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  96. JsValue[] values = ArrayExt.Empty<JsValue>();
  97. if (arguments.Length > 1)
  98. {
  99. values = new JsValue[arguments.Length - 1];
  100. System.Array.Copy(arguments, 1, values, 0, arguments.Length - 1);
  101. }
  102. var result = func.Call(arguments.At(0), values);
  103. return result;
  104. }
  105. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  106. {
  107. return Undefined;
  108. }
  109. }
  110. }