FunctionInstance.cs 2.5 KB

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