FunctionInstance.cs 1.7 KB

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