FunctionPrototype.cs 4.7 KB

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