FunctionPrototype.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  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. // 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, false, false, false);
  27. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToFunctionString), false, false, false);
  28. FastAddProperty("apply", new ClrFunctionInstance<object, object>(Engine, Apply), false, false, false);
  29. FastAddProperty("call", new ClrFunctionInstance<object, object>(Engine, Call), false, false, false);
  30. FastAddProperty("bind", new ClrFunctionInstance<object, object>(Engine, Bind), false, false, false);
  31. }
  32. private object Bind(object thisObj, object[] arguments)
  33. {
  34. throw new NotImplementedException();
  35. }
  36. private object ToFunctionString(object thisObj, object[] arguments)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. public object Apply(object thisObject, object[] arguments)
  41. {
  42. if (arguments.Length != 2)
  43. {
  44. throw new ArgumentException("Apply has to be called with two arguments.");
  45. }
  46. var func = thisObject as ICallable;
  47. object thisArg = arguments[0];
  48. object argArray = arguments[1];
  49. if (func == null)
  50. {
  51. throw new JavaScriptException(Engine.TypeError);
  52. }
  53. if (argArray == Null.Instance || argArray == Undefined.Instance)
  54. {
  55. return func.Call(thisArg, Arguments.Empty);
  56. }
  57. var argArrayObj = argArray as ObjectInstance;
  58. if (argArrayObj == null)
  59. {
  60. throw new JavaScriptException(Engine.TypeError);
  61. }
  62. object len = argArrayObj.Get("length");
  63. uint n = TypeConverter.ToUint32(len);
  64. var argList = new List<object>();
  65. for (int index = 0; index < n; index++)
  66. {
  67. string indexName = index.ToString();
  68. object nextArg = argArrayObj.Get(indexName);
  69. argList.Add(nextArg);
  70. }
  71. return func.Call(thisArg, argList.ToArray());
  72. }
  73. public override object Call(object thisObject, object[] arguments)
  74. {
  75. return Undefined.Instance;
  76. }
  77. }
  78. }