FunctionInstance.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using Esprima.Ast;
  5. using Jint.Native.Object;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Environments;
  9. using Jint.Runtime.Interpreter;
  10. namespace Jint.Native.Function
  11. {
  12. public abstract class FunctionInstance : ObjectInstance, ICallable
  13. {
  14. protected PropertyDescriptor _prototypeDescriptor;
  15. protected internal PropertyDescriptor _length;
  16. private PropertyDescriptor _nameDescriptor;
  17. protected internal LexicalEnvironment _environment;
  18. internal readonly JintFunctionDefinition _functionDefinition;
  19. internal readonly FunctionThisMode _thisMode;
  20. internal JsValue _homeObject = Undefined;
  21. internal ConstructorKind _constructorKind = ConstructorKind.Base;
  22. internal FunctionInstance(
  23. Engine engine,
  24. JintFunctionDefinition function,
  25. LexicalEnvironment scope,
  26. FunctionThisMode thisMode)
  27. : this(engine, !string.IsNullOrWhiteSpace(function.Name) ? new JsString(function.Name) : null, thisMode)
  28. {
  29. _functionDefinition = function;
  30. _environment = scope;
  31. }
  32. internal FunctionInstance(
  33. Engine engine,
  34. JsString name,
  35. FunctionThisMode thisMode = FunctionThisMode.Global,
  36. ObjectClass objectClass = ObjectClass.Function)
  37. : base(engine, objectClass)
  38. {
  39. if (name is not null)
  40. {
  41. _nameDescriptor = new PropertyDescriptor(name, PropertyFlag.Configurable);
  42. }
  43. _thisMode = thisMode;
  44. }
  45. protected FunctionInstance(
  46. Engine engine,
  47. JsString name)
  48. : this(engine, name, FunctionThisMode.Global, ObjectClass.Function)
  49. {
  50. }
  51. // for example RavenDB wants to inspect this
  52. public IFunction FunctionDeclaration => _functionDefinition.Function;
  53. /// <summary>
  54. /// Executed when a function object is used as a function
  55. /// </summary>
  56. /// <param name="thisObject"></param>
  57. /// <param name="arguments"></param>
  58. /// <returns></returns>
  59. public abstract JsValue Call(JsValue thisObject, JsValue[] arguments);
  60. public bool Strict => _thisMode == FunctionThisMode.Strict;
  61. internal override bool IsConstructor => this is IConstructor;
  62. public virtual bool HasInstance(JsValue v)
  63. {
  64. if (!(v is ObjectInstance o))
  65. {
  66. return false;
  67. }
  68. var p = Get(CommonProperties.Prototype);
  69. if (!(p is ObjectInstance prototype))
  70. {
  71. ExceptionHelper.ThrowTypeError(_engine, $"Function has non-object prototype '{TypeConverter.ToString(p)}' in instanceof check");
  72. }
  73. while (true)
  74. {
  75. o = o.Prototype;
  76. if (o is null)
  77. {
  78. return false;
  79. }
  80. if (SameValue(p, o))
  81. {
  82. return true;
  83. }
  84. }
  85. }
  86. public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  87. {
  88. if (_prototypeDescriptor != null)
  89. {
  90. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Prototype, _prototypeDescriptor);
  91. }
  92. if (_length != null)
  93. {
  94. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  95. }
  96. if (_nameDescriptor != null)
  97. {
  98. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Name, GetOwnProperty(CommonProperties.Name));
  99. }
  100. foreach (var entry in base.GetOwnProperties())
  101. {
  102. yield return entry;
  103. }
  104. }
  105. public override List<JsValue> GetOwnPropertyKeys(Types types)
  106. {
  107. var keys = new List<JsValue>();
  108. if (_prototypeDescriptor != null)
  109. {
  110. keys.Add(CommonProperties.Prototype);
  111. }
  112. if (_length != null)
  113. {
  114. keys.Add(CommonProperties.Length);
  115. }
  116. if (_nameDescriptor != null)
  117. {
  118. keys.Add(CommonProperties.Name);
  119. }
  120. keys.AddRange(base.GetOwnPropertyKeys(types));
  121. return keys;
  122. }
  123. public override PropertyDescriptor GetOwnProperty(JsValue property)
  124. {
  125. if (property == CommonProperties.Prototype)
  126. {
  127. return _prototypeDescriptor ?? PropertyDescriptor.Undefined;
  128. }
  129. if (property == CommonProperties.Length)
  130. {
  131. return _length ?? PropertyDescriptor.Undefined;
  132. }
  133. if (property == CommonProperties.Name)
  134. {
  135. return _nameDescriptor ?? PropertyDescriptor.Undefined;
  136. }
  137. return base.GetOwnProperty(property);
  138. }
  139. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  140. {
  141. if (property == CommonProperties.Prototype)
  142. {
  143. _prototypeDescriptor = desc;
  144. }
  145. else if (property == CommonProperties.Length)
  146. {
  147. _length = desc;
  148. }
  149. else if (property == CommonProperties.Name)
  150. {
  151. _nameDescriptor = desc;
  152. }
  153. else
  154. {
  155. base.SetOwnProperty(property, desc);
  156. }
  157. }
  158. public override bool HasOwnProperty(JsValue property)
  159. {
  160. if (property == CommonProperties.Prototype)
  161. {
  162. return _prototypeDescriptor != null;
  163. }
  164. if (property == CommonProperties.Length)
  165. {
  166. return _length != null;
  167. }
  168. if (property == CommonProperties.Name)
  169. {
  170. return _nameDescriptor != null;
  171. }
  172. return base.HasOwnProperty(property);
  173. }
  174. public override void RemoveOwnProperty(JsValue property)
  175. {
  176. if (property == CommonProperties.Prototype)
  177. {
  178. _prototypeDescriptor = null;
  179. }
  180. if (property == CommonProperties.Length)
  181. {
  182. _length = null;
  183. }
  184. if (property == CommonProperties.Name)
  185. {
  186. _nameDescriptor = null;
  187. }
  188. base.RemoveOwnProperty(property);
  189. }
  190. internal void SetFunctionName(JsValue name, string prefix = null, bool force = false)
  191. {
  192. if (!force && _nameDescriptor != null && !UnwrapJsValue(_nameDescriptor).IsUndefined())
  193. {
  194. return;
  195. }
  196. if (name is JsSymbol symbol)
  197. {
  198. name = symbol._value.IsUndefined()
  199. ? JsString.Empty
  200. : new JsString("[" + symbol._value + "]");
  201. }
  202. if (!string.IsNullOrWhiteSpace(prefix))
  203. {
  204. name = prefix + " " + name;
  205. }
  206. _nameDescriptor = new PropertyDescriptor(name, PropertyFlag.Configurable);
  207. }
  208. /// <summary>
  209. /// https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
  210. /// </summary>
  211. /// <remarks>
  212. /// Uses separate builder to get correct type with state support to prevent allocations.
  213. /// </remarks>
  214. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  215. internal T OrdinaryCreateFromConstructor<T>(
  216. JsValue constructor,
  217. ObjectInstance intrinsicDefaultProto,
  218. Func<Engine, JsValue, T> objectCreator,
  219. JsValue state = null) where T : ObjectInstance
  220. {
  221. var proto = GetPrototypeFromConstructor(constructor, intrinsicDefaultProto);
  222. var obj = objectCreator(_engine, state);
  223. obj._prototype = proto;
  224. return obj;
  225. }
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. private static ObjectInstance GetPrototypeFromConstructor(JsValue constructor, ObjectInstance intrinsicDefaultProto)
  228. {
  229. var proto = constructor.Get(CommonProperties.Prototype, constructor) as ObjectInstance;
  230. // If Type(proto) is not Object, then
  231. // Let realm be ? GetFunctionRealm(constructor).
  232. // Set proto to realm's intrinsic object named intrinsicDefaultProto.
  233. return proto ?? intrinsicDefaultProto;
  234. }
  235. internal void MakeMethod(ObjectInstance homeObject)
  236. {
  237. _homeObject = homeObject;
  238. }
  239. /// <summary>
  240. /// https://tc39.es/ecma262/#sec-ordinarycallbindthis
  241. /// </summary>
  242. protected void OrdinaryCallBindThis(ExecutionContext calleeContext, JsValue thisArgument)
  243. {
  244. var thisMode = _thisMode;
  245. if (thisMode == FunctionThisMode.Lexical)
  246. {
  247. return;
  248. }
  249. // Let calleeRealm be F.[[Realm]].
  250. var localEnv = (FunctionEnvironmentRecord) calleeContext.LexicalEnvironment._record;
  251. JsValue thisValue;
  252. if (_thisMode == FunctionThisMode.Strict)
  253. {
  254. thisValue = thisArgument;
  255. }
  256. else
  257. {
  258. if (thisArgument.IsNullOrUndefined())
  259. {
  260. // Let globalEnv be calleeRealm.[[GlobalEnv]].
  261. var globalEnv = _engine.GlobalEnvironment;
  262. var globalEnvRec = (GlobalEnvironmentRecord) globalEnv._record;
  263. thisValue = globalEnvRec.GlobalThisValue;
  264. }
  265. else
  266. {
  267. thisValue = TypeConverter.ToObject(_engine, thisArgument);
  268. }
  269. }
  270. localEnv.BindThisValue(thisValue);
  271. }
  272. protected Completion OrdinaryCallEvaluateBody(
  273. JsValue[] arguments,
  274. ExecutionContext calleeContext)
  275. {
  276. var argumentsInstance = _engine.FunctionDeclarationInstantiation(
  277. functionInstance: this,
  278. arguments,
  279. calleeContext.LexicalEnvironment);
  280. var result = _functionDefinition.Execute();
  281. var value = result.GetValueOrDefault().Clone();
  282. argumentsInstance?.FunctionWasCalled();
  283. return new Completion(result.Type, value, result.Identifier, result.Location);
  284. }
  285. /// <summary>
  286. /// https://tc39.es/ecma262/#sec-prepareforordinarycall
  287. /// </summary>
  288. protected ExecutionContext PrepareForOrdinaryCall(JsValue newTarget)
  289. {
  290. // ** PrepareForOrdinaryCall **
  291. // var callerContext = _engine.ExecutionContext;
  292. // Let calleeRealm be F.[[Realm]].
  293. // Set the Realm of calleeContext to calleeRealm.
  294. // Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].
  295. var calleeContext = LexicalEnvironment.NewFunctionEnvironment(_engine, this, newTarget);
  296. // If callerContext is not already suspended, suspend callerContext.
  297. // Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  298. // NOTE: Any exception objects produced after this point are associated with calleeRealm.
  299. // Return calleeContext.
  300. return _engine.EnterExecutionContext(calleeContext, calleeContext);
  301. }
  302. public override string ToString()
  303. {
  304. // TODO no way to extract SourceText from Esprima at the moment, just returning native code
  305. var nameValue = _nameDescriptor != null ? UnwrapJsValue(_nameDescriptor) : JsString.Empty;
  306. var name = "";
  307. if (!nameValue.IsUndefined())
  308. {
  309. name = TypeConverter.ToString(nameValue);
  310. }
  311. return "function " + name + "() {{[native code]}}";
  312. }
  313. }
  314. }