FunctionInstance.cs 2.4 KB

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