FunctionPrototype.cs 5.0 KB

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