FunctionPrototype.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors.Specialized;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Function
  6. {
  7. /// <summary>
  8. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
  9. /// </summary>
  10. public sealed class FunctionPrototype : FunctionInstance
  11. {
  12. private FunctionPrototype(Engine engine) : base(engine, null, null, false)
  13. {
  14. }
  15. public static FunctionPrototype CreatePrototypeObject(Engine engine)
  16. {
  17. var obj = new FunctionPrototype(engine);
  18. obj.Extensible = true;
  19. // The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
  20. obj.Prototype = engine.Object.PrototypeObject;
  21. obj.SetOwnProperty("length", new AllForbiddenPropertyDescriptor(0));
  22. return obj;
  23. }
  24. public void Configure()
  25. {
  26. SetOwnProperty("constructor", new NonEnumerablePropertyDescriptor(Engine.Function));
  27. FastAddProperty("toString", new ClrFunctionInstance(Engine, ToString), true, false, true);
  28. FastAddProperty("apply", new ClrFunctionInstance(Engine, Apply, 2), true, false, true);
  29. FastAddProperty("call", new ClrFunctionInstance(Engine, CallImpl, 1), true, false, true);
  30. FastAddProperty("bind", new ClrFunctionInstance(Engine, Bind, 1), true, false, true);
  31. }
  32. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  33. {
  34. var target = thisObj.TryCast<ICallable>(x =>
  35. {
  36. throw new JavaScriptException(Engine.TypeError);
  37. });
  38. var thisArg = arguments.At(0);
  39. var f = new BindFunctionInstance(Engine) {Extensible = true};
  40. f.TargetFunction = thisObj;
  41. f.BoundThis = thisArg;
  42. f.BoundArgs = arguments.Skip(1);
  43. f.Prototype = Engine.Function.PrototypeObject;
  44. var o = target as FunctionInstance;
  45. if (o != null)
  46. {
  47. var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1);
  48. f.SetOwnProperty("length", new AllForbiddenPropertyDescriptor(System.Math.Max(l, 0)));
  49. }
  50. else
  51. {
  52. f.SetOwnProperty("length", new AllForbiddenPropertyDescriptor(0));
  53. }
  54. var thrower = Engine.Function.ThrowTypeError;
  55. f.DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, false, false), false);
  56. f.DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, false, false), false);
  57. return f;
  58. }
  59. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  60. {
  61. var func = thisObj.TryCast<FunctionInstance>();
  62. if (func == null)
  63. {
  64. throw new JavaScriptException(Engine.TypeError, "Function object expected.");
  65. }
  66. return "function() {{ ... }}";
  67. }
  68. public JsValue Apply(JsValue thisObject, JsValue[] arguments)
  69. {
  70. var func = thisObject.TryCast<ICallable>();
  71. var thisArg = arguments.At(0);
  72. var argArray = arguments.At(1);
  73. if (func == null)
  74. {
  75. throw new JavaScriptException(Engine.TypeError);
  76. }
  77. if (ReferenceEquals(argArray, Null) || ReferenceEquals(argArray, Undefined))
  78. {
  79. return func.Call(thisArg, Arguments.Empty);
  80. }
  81. var argArrayObj = argArray.TryCast<ObjectInstance>();
  82. if (argArrayObj == null)
  83. {
  84. throw new JavaScriptException(Engine.TypeError);
  85. }
  86. var len = argArrayObj.Get("length").AsNumber();
  87. uint n = TypeConverter.ToUint32(len);
  88. var argList = n < 10
  89. ? Engine.JsValueArrayPool.RentArray((int) n)
  90. : new JsValue[n];
  91. for (int index = 0; index < n; index++)
  92. {
  93. string indexName = TypeConverter.ToString(index);
  94. var nextArg = argArrayObj.Get(indexName);
  95. argList[index] = nextArg;
  96. }
  97. var result = func.Call(thisArg, argList);
  98. Engine.JsValueArrayPool.ReturnArray(argList);
  99. return result;
  100. }
  101. public JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  102. {
  103. var func = thisObject.TryCast<ICallable>();
  104. if (func == null)
  105. {
  106. throw new JavaScriptException(Engine.TypeError);
  107. }
  108. return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1));
  109. }
  110. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  111. {
  112. return Undefined;
  113. }
  114. }
  115. }