ScriptFunctionInstance.cs 10.0 KB

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