FunctionPrototype.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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), false, false, false);
  29. FastAddProperty("apply", new ClrFunctionInstance<object, object>(Engine, Apply), false, false, false);
  30. FastAddProperty("call", new ClrFunctionInstance<object, object>(Engine, Call, 1), false, false, false);
  31. FastAddProperty("bind", new ClrFunctionInstance<object, object>(Engine, Bind), false, false, false);
  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. throw new NotImplementedException();
  40. }
  41. public object Apply(object thisObject, object[] arguments)
  42. {
  43. if (arguments.Length != 2)
  44. {
  45. throw new ArgumentException("Apply has to be called with two arguments.");
  46. }
  47. var func = thisObject as ICallable;
  48. object thisArg = arguments[0];
  49. object argArray = arguments[1];
  50. if (func == null)
  51. {
  52. throw new JavaScriptException(Engine.TypeError);
  53. }
  54. if (argArray == Null.Instance || argArray == Undefined.Instance)
  55. {
  56. return func.Call(thisArg, Arguments.Empty);
  57. }
  58. var argArrayObj = argArray as ObjectInstance;
  59. if (argArrayObj == null)
  60. {
  61. throw new JavaScriptException(Engine.TypeError);
  62. }
  63. object len = argArrayObj.Get("length");
  64. uint n = TypeConverter.ToUint32(len);
  65. var argList = new List<object>();
  66. for (int index = 0; index < n; index++)
  67. {
  68. string indexName = index.ToString();
  69. object nextArg = argArrayObj.Get(indexName);
  70. argList.Add(nextArg);
  71. }
  72. return func.Call(thisArg, argList.ToArray());
  73. }
  74. public override object Call(object thisObject, object[] arguments)
  75. {
  76. var func = thisObject as ICallable;
  77. if (func == null)
  78. {
  79. return new JavaScriptException(Engine.TypeError);
  80. }
  81. return func.Call(arguments[0], arguments.Skip(1).ToArray());
  82. }
  83. }
  84. }