ScriptFunctionInstance.cs 11 KB

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