FunctionInstance.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System.Collections.Generic;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Environments;
  6. namespace Jint.Native.Function
  7. {
  8. public abstract class FunctionInstance : ObjectInstance, ICallable
  9. {
  10. private const string PropertyNamePrototype = "prototype";
  11. private IPropertyDescriptor _prototype;
  12. private const string PropertyNameLength = "length";
  13. private IPropertyDescriptor _length;
  14. private readonly Engine _engine;
  15. protected FunctionInstance(Engine engine, string[] parameters, LexicalEnvironment scope, bool strict) : base(engine)
  16. {
  17. _engine = engine;
  18. FormalParameters = parameters;
  19. Scope = scope;
  20. Strict = strict;
  21. }
  22. /// <summary>
  23. /// Executed when a function object is used as a function
  24. /// </summary>
  25. /// <param name="thisObject"></param>
  26. /// <param name="arguments"></param>
  27. /// <returns></returns>
  28. public abstract JsValue Call(JsValue thisObject, JsValue[] arguments);
  29. public LexicalEnvironment Scope { get; }
  30. public string[] FormalParameters { get; }
  31. public bool Strict { get; }
  32. public virtual bool HasInstance(JsValue v)
  33. {
  34. var vObj = v.TryCast<ObjectInstance>();
  35. if (vObj == null)
  36. {
  37. return false;
  38. }
  39. var po = Get("prototype");
  40. if (!po.IsObject())
  41. {
  42. throw new JavaScriptException(_engine.TypeError, string.Format("Function has non-object prototype '{0}' in instanceof check", TypeConverter.ToString(po)));
  43. }
  44. var o = po.AsObject();
  45. if (o == null)
  46. {
  47. throw new JavaScriptException(_engine.TypeError);
  48. }
  49. while (true)
  50. {
  51. vObj = vObj.Prototype;
  52. if (vObj == null)
  53. {
  54. return false;
  55. }
  56. if (vObj == o)
  57. {
  58. return true;
  59. }
  60. }
  61. }
  62. public override string Class => "Function";
  63. /// <summary>
  64. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.4
  65. /// </summary>
  66. /// <param name="propertyName"></param>
  67. /// <returns></returns>
  68. public override JsValue Get(string propertyName)
  69. {
  70. var v = base.Get(propertyName);
  71. var f = v.As<FunctionInstance>();
  72. if (propertyName == "caller" && f != null && f.Strict)
  73. {
  74. throw new JavaScriptException(_engine.TypeError);
  75. }
  76. return v;
  77. }
  78. public override IEnumerable<KeyValuePair<string, IPropertyDescriptor>> GetOwnProperties()
  79. {
  80. if (_prototype != null)
  81. {
  82. yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNamePrototype, _prototype);
  83. }
  84. if (_length != null)
  85. {
  86. yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNameLength, _length);
  87. }
  88. foreach (var entry in base.GetOwnProperties())
  89. {
  90. yield return entry;
  91. }
  92. }
  93. public override IPropertyDescriptor GetOwnProperty(string propertyName)
  94. {
  95. if (propertyName == PropertyNamePrototype)
  96. {
  97. return _prototype ?? PropertyDescriptor.Undefined;
  98. }
  99. if (propertyName == PropertyNameLength)
  100. {
  101. return _length ?? PropertyDescriptor.Undefined;
  102. }
  103. return base.GetOwnProperty(propertyName);
  104. }
  105. protected internal override void SetOwnProperty(string propertyName, IPropertyDescriptor desc)
  106. {
  107. if (propertyName == PropertyNamePrototype)
  108. {
  109. _prototype = desc;
  110. }
  111. else if (propertyName == PropertyNameLength)
  112. {
  113. _length = desc;
  114. }
  115. else
  116. {
  117. base.SetOwnProperty(propertyName, desc);
  118. }
  119. }
  120. public override bool HasOwnProperty(string propertyName)
  121. {
  122. if (propertyName == PropertyNamePrototype)
  123. {
  124. return _prototype != null;
  125. }
  126. if (propertyName == PropertyNameLength)
  127. {
  128. return _length != null;
  129. }
  130. return base.HasOwnProperty(propertyName);
  131. }
  132. public override void RemoveOwnProperty(string propertyName)
  133. {
  134. if (propertyName == PropertyNamePrototype)
  135. {
  136. _prototype = null;
  137. }
  138. if (propertyName == PropertyNameLength)
  139. {
  140. _prototype = null;
  141. }
  142. base.RemoveOwnProperty(propertyName);
  143. }
  144. }
  145. }