FunctionInstance.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  63. {
  64. if (_prototypeDescriptor != null)
  65. {
  66. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Prototype, _prototypeDescriptor);
  67. }
  68. if (_length != null)
  69. {
  70. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  71. }
  72. if (_nameDescriptor != null)
  73. {
  74. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Name, GetOwnProperty(CommonProperties.Name));
  75. }
  76. foreach (var entry in base.GetOwnProperties())
  77. {
  78. yield return entry;
  79. }
  80. }
  81. public override List<JsValue> GetOwnPropertyKeys(Types types)
  82. {
  83. var keys = new List<JsValue>();
  84. if (_prototypeDescriptor != null)
  85. {
  86. keys.Add(CommonProperties.Prototype);
  87. }
  88. if (_length != null)
  89. {
  90. keys.Add(CommonProperties.Length);
  91. }
  92. if (_nameDescriptor != null)
  93. {
  94. keys.Add(CommonProperties.Name);
  95. }
  96. keys.AddRange(base.GetOwnPropertyKeys(types));
  97. return keys;
  98. }
  99. public override PropertyDescriptor GetOwnProperty(JsValue property)
  100. {
  101. if (property == CommonProperties.Prototype)
  102. {
  103. return _prototypeDescriptor ?? PropertyDescriptor.Undefined;
  104. }
  105. if (property == CommonProperties.Length)
  106. {
  107. return _length ?? PropertyDescriptor.Undefined;
  108. }
  109. if (property == CommonProperties.Name)
  110. {
  111. return _nameDescriptor ?? PropertyDescriptor.Undefined;
  112. }
  113. return base.GetOwnProperty(property);
  114. }
  115. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  116. {
  117. if (property == CommonProperties.Prototype)
  118. {
  119. _prototypeDescriptor = desc;
  120. }
  121. else if (property == CommonProperties.Length)
  122. {
  123. _length = desc;
  124. }
  125. else if (property == CommonProperties.Name)
  126. {
  127. _nameDescriptor = desc;
  128. }
  129. else
  130. {
  131. base.SetOwnProperty(property, desc);
  132. }
  133. }
  134. public override bool HasOwnProperty(JsValue property)
  135. {
  136. if (property == CommonProperties.Prototype)
  137. {
  138. return _prototypeDescriptor != null;
  139. }
  140. if (property == CommonProperties.Length)
  141. {
  142. return _length != null;
  143. }
  144. if (property == CommonProperties.Name)
  145. {
  146. return _nameDescriptor != null;
  147. }
  148. return base.HasOwnProperty(property);
  149. }
  150. public override void RemoveOwnProperty(JsValue property)
  151. {
  152. if (property == CommonProperties.Prototype)
  153. {
  154. _prototypeDescriptor = null;
  155. }
  156. if (property == CommonProperties.Length)
  157. {
  158. _length = null;
  159. }
  160. if (property == CommonProperties.Name)
  161. {
  162. _nameDescriptor = null;
  163. }
  164. base.RemoveOwnProperty(property);
  165. }
  166. internal void SetFunctionName(JsValue name, string prefix = null, bool force = false)
  167. {
  168. if (!force && _nameDescriptor != null && !UnwrapJsValue(_nameDescriptor).IsUndefined())
  169. {
  170. return;
  171. }
  172. if (name is JsSymbol symbol)
  173. {
  174. name = symbol._value.IsUndefined()
  175. ? JsString.Empty
  176. : new JsString("[" + symbol._value + "]");
  177. }
  178. if (!string.IsNullOrWhiteSpace(prefix))
  179. {
  180. name = prefix + " " + name;
  181. }
  182. _nameDescriptor = new PropertyDescriptor(name, PropertyFlag.Configurable);
  183. }
  184. /// <summary>
  185. /// https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor
  186. /// </summary>
  187. /// <remarks>
  188. /// Uses separate builder to get correct type with state support to prevent allocations.
  189. /// </remarks>
  190. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  191. internal T OrdinaryCreateFromConstructor<T>(
  192. JsValue constructor,
  193. ObjectInstance intrinsicDefaultProto,
  194. Func<Engine, JsValue, T> objectCreator,
  195. JsValue state = null) where T : ObjectInstance
  196. {
  197. var proto = GetPrototypeFromConstructor(constructor, intrinsicDefaultProto);
  198. var obj = objectCreator(_engine, state);
  199. obj._prototype = proto;
  200. return obj;
  201. }
  202. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  203. internal static ObjectInstance GetPrototypeFromConstructor(JsValue constructor, ObjectInstance intrinsicDefaultProto)
  204. {
  205. var proto = constructor.Get(CommonProperties.Prototype, constructor) as ObjectInstance;
  206. // If Type(proto) is not Object, then
  207. // Let realm be ? GetFunctionRealm(constructor).
  208. // Set proto to realm's intrinsic object named intrinsicDefaultProto.
  209. return proto ?? intrinsicDefaultProto;
  210. }
  211. internal void MakeMethod(ObjectInstance homeObject)
  212. {
  213. _homeObject = homeObject;
  214. }
  215. /// <summary>
  216. /// https://tc39.es/ecma262/#sec-ordinarycallbindthis
  217. /// </summary>
  218. protected void OrdinaryCallBindThis(ExecutionContext calleeContext, JsValue thisArgument)
  219. {
  220. var thisMode = _thisMode;
  221. if (thisMode == FunctionThisMode.Lexical)
  222. {
  223. return;
  224. }
  225. // Let calleeRealm be F.[[Realm]].
  226. var localEnv = (FunctionEnvironmentRecord) calleeContext.LexicalEnvironment._record;
  227. JsValue thisValue;
  228. if (_thisMode == FunctionThisMode.Strict)
  229. {
  230. thisValue = thisArgument;
  231. }
  232. else
  233. {
  234. if (thisArgument.IsNullOrUndefined())
  235. {
  236. // Let globalEnv be calleeRealm.[[GlobalEnv]].
  237. var globalEnv = _engine.GlobalEnvironment;
  238. var globalEnvRec = (GlobalEnvironmentRecord) globalEnv._record;
  239. thisValue = globalEnvRec.GlobalThisValue;
  240. }
  241. else
  242. {
  243. thisValue = TypeConverter.ToObject(_engine, thisArgument);
  244. }
  245. }
  246. localEnv.BindThisValue(thisValue);
  247. }
  248. protected Completion OrdinaryCallEvaluateBody(
  249. JsValue[] arguments,
  250. ExecutionContext calleeContext)
  251. {
  252. var argumentsInstance = _engine.FunctionDeclarationInstantiation(
  253. functionInstance: this,
  254. arguments,
  255. calleeContext.LexicalEnvironment);
  256. var result = _functionDefinition.Execute();
  257. var value = result.GetValueOrDefault().Clone();
  258. argumentsInstance?.FunctionWasCalled();
  259. return new Completion(result.Type, value, result.Identifier, result.Location);
  260. }
  261. /// <summary>
  262. /// https://tc39.es/ecma262/#sec-prepareforordinarycall
  263. /// </summary>
  264. protected ExecutionContext PrepareForOrdinaryCall(JsValue newTarget)
  265. {
  266. // ** PrepareForOrdinaryCall **
  267. // var callerContext = _engine.ExecutionContext;
  268. // Let calleeRealm be F.[[Realm]].
  269. // Set the Realm of calleeContext to calleeRealm.
  270. // Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].
  271. var calleeContext = LexicalEnvironment.NewFunctionEnvironment(_engine, this, newTarget);
  272. // If callerContext is not already suspended, suspend callerContext.
  273. // Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  274. // NOTE: Any exception objects produced after this point are associated with calleeRealm.
  275. // Return calleeContext.
  276. return _engine.EnterExecutionContext(calleeContext, calleeContext);
  277. }
  278. public override string ToString()
  279. {
  280. // TODO no way to extract SourceText from Esprima at the moment, just returning native code
  281. var nameValue = _nameDescriptor != null ? UnwrapJsValue(_nameDescriptor) : JsString.Empty;
  282. var name = "";
  283. if (!nameValue.IsUndefined())
  284. {
  285. name = TypeConverter.ToString(nameValue);
  286. }
  287. return "function " + name + "() { [native code] }";
  288. }
  289. }
  290. }