FunctionInstance.cs 7.9 KB

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