FunctionPrototype.cs 5.1 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 (!ReferenceEquals(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. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  57. f.DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  58. f.DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  59. return f;
  60. }
  61. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  62. {
  63. var func = thisObj.TryCast<FunctionInstance>();
  64. if (ReferenceEquals(func, null))
  65. {
  66. throw new JavaScriptException(Engine.TypeError, "Function object expected.");
  67. }
  68. return "function() {{ ... }}";
  69. }
  70. public JsValue Apply(JsValue thisObject, JsValue[] arguments)
  71. {
  72. var func = thisObject.TryCast<ICallable>();
  73. var thisArg = arguments.At(0);
  74. var argArray = arguments.At(1);
  75. if (func == null)
  76. {
  77. throw new JavaScriptException(Engine.TypeError);
  78. }
  79. if (argArray.IsNull() || argArray.IsUndefined())
  80. {
  81. return func.Call(thisArg, Arguments.Empty);
  82. }
  83. var argArrayObj = argArray.TryCast<ObjectInstance>();
  84. if (ReferenceEquals(argArrayObj, null))
  85. {
  86. throw new JavaScriptException(Engine.TypeError);
  87. }
  88. var len = ((JsNumber) argArrayObj.Get("length"))._value;
  89. uint n = TypeConverter.ToUint32(len);
  90. var argList = n < 10
  91. ? Engine.JsValueArrayPool.RentArray((int) n)
  92. : new JsValue[n];
  93. for (int index = 0; index < n; index++)
  94. {
  95. string indexName = TypeConverter.ToString(index);
  96. var nextArg = argArrayObj.Get(indexName);
  97. argList[index] = nextArg;
  98. }
  99. var result = func.Call(thisArg, argList);
  100. Engine.JsValueArrayPool.ReturnArray(argList);
  101. return result;
  102. }
  103. public JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  104. {
  105. var func = thisObject.TryCast<ICallable>();
  106. if (func == null)
  107. {
  108. throw new JavaScriptException(Engine.TypeError);
  109. }
  110. return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1));
  111. }
  112. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  113. {
  114. return Undefined;
  115. }
  116. }
  117. }