FunctionPrototype.cs 3.0 KB

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