2
0

FunctionInstance.cs 2.4 KB

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