FunctionInstance.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 const int PropertyNamePrototypeLength = 9;
  12. protected internal PropertyDescriptor _prototype;
  13. private const string PropertyNameLength = "length";
  14. private const int PropertyNameLengthLength = 6;
  15. protected PropertyDescriptor _length;
  16. private const string PropertyNameName = "name";
  17. private const int PropertyNameNameLength = 4;
  18. private PropertyDescriptor _name;
  19. protected readonly LexicalEnvironment _scope;
  20. protected internal readonly string[] _formalParameters;
  21. protected readonly bool _strict;
  22. protected FunctionInstance(
  23. Engine engine,
  24. string name,
  25. string[] parameters,
  26. LexicalEnvironment scope,
  27. bool strict)
  28. : this(engine, name, parameters, scope, strict, objectClass: "Function")
  29. {
  30. }
  31. protected FunctionInstance(
  32. Engine engine,
  33. string name,
  34. string[] parameters,
  35. LexicalEnvironment scope,
  36. bool strict,
  37. string objectClass)
  38. : base(engine, objectClass)
  39. {
  40. if (!string.IsNullOrWhiteSpace(name))
  41. {
  42. _name = new PropertyDescriptor(name, PropertyFlag.Configurable);
  43. }
  44. _formalParameters = parameters;
  45. _scope = scope;
  46. _strict = strict;
  47. }
  48. /// <summary>
  49. /// Executed when a function object is used as a function
  50. /// </summary>
  51. /// <param name="thisObject"></param>
  52. /// <param name="arguments"></param>
  53. /// <returns></returns>
  54. public abstract JsValue Call(JsValue thisObject, JsValue[] arguments);
  55. public LexicalEnvironment Scope => _scope;
  56. public string[] FormalParameters => _formalParameters;
  57. public bool Strict => _strict;
  58. public virtual bool HasInstance(JsValue v)
  59. {
  60. var vObj = v.TryCast<ObjectInstance>();
  61. if (ReferenceEquals(vObj, null))
  62. {
  63. return false;
  64. }
  65. var po = Get("prototype");
  66. if (!po.IsObject())
  67. {
  68. ExceptionHelper.ThrowTypeError(_engine, $"Function has non-object prototype '{TypeConverter.ToString(po)}' in instanceof check");
  69. }
  70. var o = po.AsObject();
  71. if (ReferenceEquals(o, null))
  72. {
  73. ExceptionHelper.ThrowTypeError(_engine);
  74. }
  75. while (true)
  76. {
  77. vObj = vObj.Prototype;
  78. if (ReferenceEquals(vObj, null))
  79. {
  80. return false;
  81. }
  82. if (vObj == o)
  83. {
  84. return true;
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.4
  90. /// </summary>
  91. /// <param name="propertyName"></param>
  92. /// <returns></returns>
  93. public override JsValue Get(string propertyName)
  94. {
  95. var v = base.Get(propertyName);
  96. if (propertyName.Length == 6
  97. && propertyName == "caller"
  98. && ((v.As<FunctionInstance>()?._strict).GetValueOrDefault()))
  99. {
  100. ExceptionHelper.ThrowTypeError(_engine);
  101. }
  102. return v;
  103. }
  104. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  105. {
  106. if (_prototype != null)
  107. {
  108. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNamePrototype, _prototype);
  109. }
  110. if (_length != null)
  111. {
  112. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameLength, _length);
  113. }
  114. if (_name != null)
  115. {
  116. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameName, _name);
  117. }
  118. foreach (var entry in base.GetOwnProperties())
  119. {
  120. yield return entry;
  121. }
  122. }
  123. public override PropertyDescriptor GetOwnProperty(string propertyName)
  124. {
  125. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  126. {
  127. return _prototype ?? PropertyDescriptor.Undefined;
  128. }
  129. if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  130. {
  131. return _length ?? PropertyDescriptor.Undefined;
  132. }
  133. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  134. {
  135. return _name ?? PropertyDescriptor.Undefined;
  136. }
  137. return base.GetOwnProperty(propertyName);
  138. }
  139. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  140. {
  141. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  142. {
  143. _prototype = desc;
  144. }
  145. else if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  146. {
  147. _length = desc;
  148. }
  149. else if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  150. {
  151. _name = desc;
  152. }
  153. else
  154. {
  155. base.SetOwnProperty(propertyName, desc);
  156. }
  157. }
  158. public override bool HasOwnProperty(string propertyName)
  159. {
  160. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  161. {
  162. return _prototype != null;
  163. }
  164. if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  165. {
  166. return _length != null;
  167. }
  168. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  169. {
  170. return _name != null;
  171. }
  172. return base.HasOwnProperty(propertyName);
  173. }
  174. public override void RemoveOwnProperty(string propertyName)
  175. {
  176. if (propertyName.Length == PropertyNamePrototypeLength && propertyName == PropertyNamePrototype)
  177. {
  178. _prototype = null;
  179. }
  180. if (propertyName.Length == PropertyNameLengthLength && propertyName == PropertyNameLength)
  181. {
  182. _length = null;
  183. }
  184. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  185. {
  186. _name = null;
  187. }
  188. base.RemoveOwnProperty(propertyName);
  189. }
  190. internal void SetFunctionName(string name, bool throwIfExists = false)
  191. {
  192. if (!HasOwnProperty("name"))
  193. {
  194. _name = new PropertyDescriptor(name, PropertyFlag.Configurable);
  195. }
  196. else if (throwIfExists)
  197. {
  198. ExceptionHelper.ThrowError(_engine, "cannot set name");
  199. }
  200. }
  201. }
  202. }