FunctionInstance.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Native.Function
  5. {
  6. public abstract class FunctionInstance : ObjectInstance, ICallable
  7. {
  8. private readonly Engine _engine;
  9. protected FunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine)
  10. {
  11. _engine = engine;
  12. FormalParameters = parameters;
  13. Scope = scope;
  14. Strict = strict;
  15. }
  16. /// <summary>
  17. /// Executed when a function object is used as a function
  18. /// </summary>
  19. /// <param name="thisObject"></param>
  20. /// <param name="arguments"></param>
  21. /// <returns></returns>
  22. public abstract object Call(object thisObject, object[] arguments);
  23. public LexicalEnvironment Scope { get; private set; }
  24. public string[] FormalParameters { get; private set; }
  25. public bool Strict { get; private set; }
  26. // todo: implement
  27. public object TargetFunction { get; set; }
  28. // todo: implement
  29. public object BoundThis { get; set; }
  30. // todo: implement
  31. public object BoundArgs { get; set; }
  32. /// <summary>
  33. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.3
  34. /// </summary>
  35. /// <param name="instance"></param>
  36. /// <returns></returns>
  37. public bool HasInstance(object v)
  38. {
  39. var vObj = v as ObjectInstance;
  40. if (vObj == null)
  41. {
  42. return false;
  43. }
  44. var o = Get("prototype") as ObjectInstance;
  45. if (o == null)
  46. {
  47. throw new JavaScriptException(_engine.TypeError);
  48. }
  49. while (true)
  50. {
  51. v = vObj.Prototype;
  52. if (v == null)
  53. {
  54. return false;
  55. }
  56. if (v == o)
  57. {
  58. return true;
  59. }
  60. }
  61. }
  62. public override string Class
  63. {
  64. get
  65. {
  66. return "Function";
  67. }
  68. }
  69. /// <summary>
  70. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.4
  71. /// </summary>
  72. /// <param name="propertyName"></param>
  73. /// <returns></returns>
  74. public override object Get(string propertyName)
  75. {
  76. var v = base.Get(propertyName);
  77. var f = v as FunctionInstance;
  78. if (propertyName == "caller" && f != null && f.Strict)
  79. {
  80. throw new JavaScriptException(_engine.TypeError);
  81. }
  82. return v;
  83. }
  84. }
  85. }