FunctionInstance.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Jint.Native.Object;
  2. using Jint.Parser.Ast;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Native.Function
  5. {
  6. public abstract class FunctionInstance : ObjectInstance
  7. {
  8. private readonly Engine _engine;
  9. protected FunctionInstance(Engine engine, ObjectInstance prototype, Identifier[] parameters, LexicalEnvironment scope) : base(prototype)
  10. {
  11. _engine = engine;
  12. Parameters = parameters;
  13. Scope = scope;
  14. }
  15. /// <summary>
  16. /// Executed when a function object is used as a function
  17. /// </summary>
  18. /// <param name="interpreter"></param>
  19. /// <param name="state"></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 Identifier[] Parameters { get; private set; }
  25. public bool HasInstance(object instance)
  26. {
  27. var v = instance as ObjectInstance;
  28. if (v == null)
  29. {
  30. return false;
  31. }
  32. while (true)
  33. {
  34. v = v.Prototype;
  35. if (v == null)
  36. {
  37. return false;
  38. }
  39. if (v == this.Prototype)
  40. {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. public override string Class
  47. {
  48. get
  49. {
  50. return "Function";
  51. }
  52. }
  53. }
  54. }