ScriptFunctionInstance.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using Esprima.Ast;
  4. using Jint.Native.Argument;
  5. using Jint.Native.Object;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Descriptors.Specialized;
  9. using Jint.Runtime.Environments;
  10. namespace Jint.Native.Function
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  16. {
  17. private const string PropertyNameName = "name";
  18. private const int PropertyNameNameLength = 4;
  19. private PropertyDescriptor _name;
  20. private readonly IFunction _functionDeclaration;
  21. /// <summary>
  22. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  23. /// </summary>
  24. /// <param name="engine"></param>
  25. /// <param name="functionDeclaration"></param>
  26. /// <param name="scope"></param>
  27. /// <param name="strict"></param>
  28. public ScriptFunctionInstance(Engine engine, IFunction functionDeclaration, LexicalEnvironment scope, bool strict)
  29. : base(engine, GetParameterNames(functionDeclaration), scope, strict)
  30. {
  31. _functionDeclaration = functionDeclaration;
  32. Extensible = true;
  33. Prototype = engine.Function.PrototypeObject;
  34. DefineOwnProperty("length", new PropertyDescriptor(JsNumber.Create(FormalParameters.Length), PropertyFlag.AllForbidden), false);
  35. var proto = new ObjectInstanceWithConstructor(engine, this)
  36. {
  37. Extensible = true,
  38. Prototype = Engine.Object.PrototypeObject
  39. };
  40. SetOwnProperty("prototype", new PropertyDescriptor(proto, PropertyFlag.OnlyWritable));
  41. if (_functionDeclaration.Id != null)
  42. {
  43. _name = new PropertyDescriptor(_functionDeclaration.Id.Name, PropertyFlag.None);
  44. }
  45. if (strict)
  46. {
  47. var thrower = engine.Function.ThrowTypeError;
  48. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  49. DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  50. DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  51. }
  52. }
  53. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  54. {
  55. if (_name != null)
  56. {
  57. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameName, _name);
  58. }
  59. foreach (var entry in base.GetOwnProperties())
  60. {
  61. yield return entry;
  62. }
  63. }
  64. public override PropertyDescriptor GetOwnProperty(string propertyName)
  65. {
  66. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  67. {
  68. return _name ?? PropertyDescriptor.Undefined;
  69. }
  70. return base.GetOwnProperty(propertyName);
  71. }
  72. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  73. {
  74. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  75. {
  76. _name = desc;
  77. }
  78. else
  79. {
  80. base.SetOwnProperty(propertyName, desc);
  81. }
  82. }
  83. public override bool HasOwnProperty(string propertyName)
  84. {
  85. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  86. {
  87. return _name != null;
  88. }
  89. return base.HasOwnProperty(propertyName);
  90. }
  91. public override void RemoveOwnProperty(string propertyName)
  92. {
  93. if (propertyName.Length == PropertyNameNameLength && propertyName == PropertyNameName)
  94. {
  95. _name = null;
  96. }
  97. base.RemoveOwnProperty(propertyName);
  98. }
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. private static string[] GetParameterNames(IFunction functionDeclaration)
  101. {
  102. var list = functionDeclaration.Params;
  103. var count = list.Count;
  104. if (count == 0)
  105. {
  106. return System.Array.Empty<string>();
  107. }
  108. var names = new string[count];
  109. for (var i = 0; i < count; ++i)
  110. {
  111. names[i] = ((Identifier) list[i]).Name;
  112. }
  113. return names;
  114. }
  115. /// <summary>
  116. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  117. /// </summary>
  118. /// <param name="thisArg"></param>
  119. /// <param name="arguments"></param>
  120. /// <returns></returns>
  121. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  122. {
  123. using (new StrictModeScope(Strict, true))
  124. {
  125. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  126. JsValue thisBinding;
  127. if (StrictModeScope.IsStrictModeCode)
  128. {
  129. thisBinding = thisArg;
  130. }
  131. else if (ReferenceEquals(thisArg, Undefined) || ReferenceEquals(thisArg, Null))
  132. {
  133. thisBinding = Engine.Global;
  134. }
  135. else if (!thisArg.IsObject())
  136. {
  137. thisBinding = TypeConverter.ToObject(Engine, thisArg);
  138. }
  139. else
  140. {
  141. thisBinding = thisArg;
  142. }
  143. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Scope);
  144. Engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  145. Completion result = null;
  146. try
  147. {
  148. var argumentInstanceRented = Engine.DeclarationBindingInstantiation(
  149. DeclarationBindingType.FunctionCode,
  150. _functionDeclaration.HoistingScope.FunctionDeclarations,
  151. _functionDeclaration.HoistingScope.VariableDeclarations,
  152. this,
  153. arguments);
  154. result = Engine.ExecuteStatement(_functionDeclaration.Body);
  155. var value = result.GetValueOrDefault();
  156. // we can safely release arguments if they don't escape the scope
  157. if (argumentInstanceRented
  158. && Engine.ExecutionContext.LexicalEnvironment?.Record is DeclarativeEnvironmentRecord der
  159. && !(result.Value is ArgumentsInstance))
  160. {
  161. der.ReleaseArguments();
  162. }
  163. if (result.Type == Completion.Throw)
  164. {
  165. var ex = new JavaScriptException(value).SetCallstack(Engine, result.Location);
  166. throw ex;
  167. }
  168. if (result.Type == Completion.Return)
  169. {
  170. return value;
  171. }
  172. }
  173. finally
  174. {
  175. Engine.CompletionPool.Return(result);
  176. Engine.LeaveExecutionContext();
  177. }
  178. return Undefined;
  179. }
  180. }
  181. /// <summary>
  182. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  183. /// </summary>
  184. /// <param name="arguments"></param>
  185. /// <returns></returns>
  186. public ObjectInstance Construct(JsValue[] arguments)
  187. {
  188. var proto = Get("prototype").TryCast<ObjectInstance>();
  189. var obj = new ObjectInstance(Engine)
  190. {
  191. Extensible = true,
  192. Prototype = proto ?? Engine.Object.PrototypeObject
  193. };
  194. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  195. if (!ReferenceEquals(result, null))
  196. {
  197. return result;
  198. }
  199. return obj;
  200. }
  201. private class ObjectInstanceWithConstructor : ObjectInstance
  202. {
  203. private const string PropertyNameConstructor = "constructor";
  204. private const int PropertyNameConstructorLength = 11;
  205. private PropertyDescriptor _constructor;
  206. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  207. {
  208. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  209. }
  210. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  211. {
  212. if (_constructor != null)
  213. {
  214. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  215. }
  216. foreach (var entry in base.GetOwnProperties())
  217. {
  218. yield return entry;
  219. }
  220. }
  221. public override PropertyDescriptor GetOwnProperty(string propertyName)
  222. {
  223. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  224. {
  225. return _constructor ?? PropertyDescriptor.Undefined;
  226. }
  227. return base.GetOwnProperty(propertyName);
  228. }
  229. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  230. {
  231. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  232. {
  233. _constructor = desc;
  234. }
  235. else
  236. {
  237. base.SetOwnProperty(propertyName, desc);
  238. }
  239. }
  240. public override bool HasOwnProperty(string propertyName)
  241. {
  242. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  243. {
  244. return _constructor != null;
  245. }
  246. return base.HasOwnProperty(propertyName);
  247. }
  248. public override void RemoveOwnProperty(string propertyName)
  249. {
  250. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  251. {
  252. _constructor = null;
  253. }
  254. else
  255. {
  256. base.RemoveOwnProperty(propertyName);
  257. }
  258. }
  259. }
  260. }
  261. }