FunctionPrototype.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using Jint.Native.Array;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Descriptors.Specialized;
  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 FunctionPrototype(Engine engine)
  16. : base(engine, "Function", null, null, false)
  17. {
  18. }
  19. public static FunctionPrototype CreatePrototypeObject(Engine engine)
  20. {
  21. var obj = new FunctionPrototype(engine)
  22. {
  23. Extensible = true,
  24. // The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
  25. Prototype = engine.Object.PrototypeObject
  26. };
  27. obj.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
  28. return obj;
  29. }
  30. public void Configure()
  31. {
  32. SetOwnProperty("constructor", new PropertyDescriptor(Engine.Function, PropertyFlag.NonEnumerable));
  33. FastAddProperty("toString", new ClrFunctionInstance(Engine, "toString", ToString), true, false, true);
  34. FastAddProperty("apply", new ClrFunctionInstance(Engine, "apply", Apply, 2), true, false, true);
  35. FastAddProperty("call", new ClrFunctionInstance(Engine, "call", CallImpl, 1), true, false, true);
  36. FastAddProperty("bind", new ClrFunctionInstance(Engine, "bind", Bind, 1), true, false, true);
  37. }
  38. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  39. {
  40. var target = thisObj.TryCast<ICallable>(x =>
  41. {
  42. ExceptionHelper.ThrowTypeError(Engine);
  43. });
  44. var thisArg = arguments.At(0);
  45. var f = new BindFunctionInstance(Engine)
  46. {
  47. Extensible = true,
  48. TargetFunction = thisObj,
  49. BoundThis = thisArg,
  50. BoundArgs = arguments.Skip(1),
  51. Prototype = Engine.Function.PrototypeObject
  52. };
  53. if (target is FunctionInstance functionInstance)
  54. {
  55. var l = TypeConverter.ToNumber(functionInstance.Get("length")) - (arguments.Length - 1);
  56. f.SetOwnProperty("length", new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden));
  57. }
  58. else
  59. {
  60. f.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
  61. }
  62. var thrower = Engine.Function.ThrowTypeError;
  63. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  64. f.DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  65. f.DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  66. return f;
  67. }
  68. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  69. {
  70. if (!(thisObj is FunctionInstance))
  71. {
  72. return ExceptionHelper.ThrowTypeError<FunctionInstance>(_engine, "Function object expected.");
  73. }
  74. return "function() {{ ... }}";
  75. }
  76. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  77. {
  78. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  79. var thisArg = arguments.At(0);
  80. var argArray = arguments.At(1);
  81. if (argArray.IsNullOrUndefined())
  82. {
  83. return func.Call(thisArg, Arguments.Empty);
  84. }
  85. var argArrayObj = argArray as ObjectInstance ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(Engine);
  86. var operations = ArrayPrototype.ArrayOperations.For(argArrayObj);
  87. uint n = operations.GetLength();
  88. var argList = _engine._jsValueArrayPool.RentArray((int) n);
  89. for (uint i = 0; i < n; i++)
  90. {
  91. var nextArg = operations.Get(i);
  92. argList[i] = nextArg;
  93. }
  94. var result = func.Call(thisArg, argList);
  95. _engine._jsValueArrayPool.ReturnArray(argList);
  96. return result;
  97. }
  98. private JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  99. {
  100. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  101. JsValue[] values = ArrayExt.Empty<JsValue>();
  102. if (arguments.Length > 1)
  103. {
  104. values = new JsValue[arguments.Length - 1];
  105. System.Array.Copy(arguments, 1, values, 0, arguments.Length - 1);
  106. }
  107. var result = func.Call(arguments.At(0), values);
  108. return result;
  109. }
  110. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  111. {
  112. return Undefined;
  113. }
  114. }
  115. }