FunctionPrototype.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  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.FastAddProperty("length", 0, false, false, false);
  22. return obj;
  23. }
  24. public void Configure()
  25. {
  26. FastAddProperty("constructor", Engine.Function, true, false, true);
  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.FastAddProperty("length", System.Math.Max(l, 0), false, false, false);
  49. }
  50. else
  51. {
  52. f.FastAddProperty("length", 0, false, false, false);
  53. }
  54. var thrower = Engine.Function.ThrowTypeError;
  55. f.DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
  56. f.DefineOwnProperty("arguments", new PropertyDescriptor(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 System.String.Format("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 (argArray == Null.Instance || argArray == Undefined.Instance)
  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 = new JsValue[n];
  89. for (int index = 0; index < n; index++)
  90. {
  91. string indexName = TypeConverter.ToString(index);
  92. var nextArg = argArrayObj.Get(indexName);
  93. argList[index] = nextArg;
  94. }
  95. return func.Call(thisArg, argList);
  96. }
  97. public JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  98. {
  99. var func = thisObject.TryCast<ICallable>();
  100. if (func == null)
  101. {
  102. throw new JavaScriptException(Engine.TypeError);
  103. }
  104. return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1));
  105. }
  106. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  107. {
  108. return Undefined.Instance;
  109. }
  110. }
  111. }