FunctionPrototype.cs 3.4 KB

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