FunctionPrototype.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.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. // 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.FastAddProperty("length", 0, false, false, false);
  23. return obj;
  24. }
  25. public void Configure()
  26. {
  27. FastAddProperty("constructor", Engine.Function, false, false, false);
  28. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString), true, false, true);
  29. FastAddProperty("apply", new ClrFunctionInstance<object, object>(Engine, Apply), true, false, true);
  30. FastAddProperty("call", new ClrFunctionInstance<object, object>(Engine, Call, 1), true, false, true);
  31. FastAddProperty("bind", new ClrFunctionInstance<object, object>(Engine, Bind), true, false, true);
  32. }
  33. private object Bind(object thisObj, object[] arguments)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. private object ToString(object thisObj, object[] arguments)
  38. {
  39. var func = thisObj as FunctionInstance;
  40. if (func == null)
  41. {
  42. throw new JavaScriptException(Engine.TypeError, "Function object expected.");
  43. }
  44. return System.String.Format("function() {{ ... }}");
  45. }
  46. public object Apply(object thisObject, object[] arguments)
  47. {
  48. if (arguments.Length != 2)
  49. {
  50. throw new ArgumentException("Apply has to be called with two arguments.");
  51. }
  52. var func = thisObject as ICallable;
  53. object thisArg = arguments[0];
  54. object argArray = arguments[1];
  55. if (func == null)
  56. {
  57. throw new JavaScriptException(Engine.TypeError);
  58. }
  59. if (argArray == Null.Instance || argArray == Undefined.Instance)
  60. {
  61. return func.Call(thisArg, Arguments.Empty);
  62. }
  63. var argArrayObj = argArray as ObjectInstance;
  64. if (argArrayObj == null)
  65. {
  66. throw new JavaScriptException(Engine.TypeError);
  67. }
  68. object len = argArrayObj.Get("length");
  69. uint n = TypeConverter.ToUint32(len);
  70. var argList = new List<object>();
  71. for (int index = 0; index < n; index++)
  72. {
  73. string indexName = index.ToString();
  74. object nextArg = argArrayObj.Get(indexName);
  75. argList.Add(nextArg);
  76. }
  77. return func.Call(thisArg, argList.ToArray());
  78. }
  79. public override object Call(object thisObject, object[] arguments)
  80. {
  81. var func = thisObject as ICallable;
  82. if (func == null)
  83. {
  84. return new JavaScriptException(Engine.TypeError);
  85. }
  86. return func.Call(arguments[0], arguments.Skip(1).ToArray());
  87. }
  88. }
  89. }