FunctionInstance.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, ICallable
  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="thisObject"></param>
  19. /// <param name="arguments"></param>
  20. /// <returns></returns>
  21. public abstract object Call(object thisObject, object[] arguments);
  22. public LexicalEnvironment Scope { get; private set; }
  23. public Identifier[] Parameters { get; private set; }
  24. public bool HasInstance(object instance)
  25. {
  26. var v = instance as ObjectInstance;
  27. if (v == null)
  28. {
  29. return false;
  30. }
  31. while (true)
  32. {
  33. v = v.Prototype;
  34. if (v == null)
  35. {
  36. return false;
  37. }
  38. if (v == this.Prototype)
  39. {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. public override string Class
  46. {
  47. get
  48. {
  49. return "Function";
  50. }
  51. }
  52. }
  53. }