FunctionPrototype.cs 4.6 KB

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