FunctionPrototype.cs 4.7 KB

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