ScriptFunctionInstance.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System.Collections.Generic;
  2. using Esprima.Ast;
  3. using Jint.Native.Argument;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Descriptors.Specialized;
  8. using Jint.Runtime.Environments;
  9. namespace Jint.Native.Function
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  15. {
  16. private const string PropertyNameName = "name";
  17. private const int PropertyNameNameLength = 4;
  18. private PropertyDescriptor _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. _length = new PropertyDescriptor(JsNumber.Create(FormalParameters.Length), PropertyFlag.AllForbidden);
  34. var proto = new ObjectInstanceWithConstructor(engine, this)
  35. {
  36. Extensible = true,
  37. Prototype = _engine.Object.PrototypeObject
  38. };
  39. _prototype = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  40. if (_functionDeclaration.Id != null)
  41. {
  42. _name = new PropertyDescriptor(_functionDeclaration.Id.Name, PropertyFlag.None);
  43. }
  44. if (strict)
  45. {
  46. var thrower = engine.Function.ThrowTypeError;
  47. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  48. DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  49. DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), 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. 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 (thisArg._type == Types.Undefined || thisArg._type == Types.Null)
  130. {
  131. thisBinding = _engine.Global;
  132. }
  133. else if (thisArg._type != Types.Object)
  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. try
  144. {
  145. var argumentInstanceRented = _engine.DeclarationBindingInstantiation(
  146. DeclarationBindingType.FunctionCode,
  147. _functionDeclaration.HoistingScope.FunctionDeclarations,
  148. _functionDeclaration.HoistingScope.VariableDeclarations,
  149. this,
  150. arguments);
  151. var result = _engine.ExecuteStatement(_functionDeclaration.Body);
  152. var value = result.GetValueOrDefault();
  153. // we can safely release arguments if they don't escape the scope
  154. if (argumentInstanceRented
  155. && _engine.ExecutionContext.LexicalEnvironment?.Record is DeclarativeEnvironmentRecord der
  156. && !(result.Value is ArgumentsInstance))
  157. {
  158. der.ReleaseArguments();
  159. }
  160. if (result.Type == CompletionType.Throw)
  161. {
  162. var ex = new JavaScriptException(value).SetCallstack(_engine, result.Location);
  163. throw ex;
  164. }
  165. if (result.Type == CompletionType.Return)
  166. {
  167. return value;
  168. }
  169. }
  170. finally
  171. {
  172. _engine.LeaveExecutionContext();
  173. }
  174. return Undefined;
  175. }
  176. }
  177. /// <summary>
  178. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  179. /// </summary>
  180. /// <param name="arguments"></param>
  181. /// <returns></returns>
  182. public ObjectInstance Construct(JsValue[] arguments)
  183. {
  184. var proto = Get("prototype").TryCast<ObjectInstance>();
  185. var obj = new ObjectInstance(_engine)
  186. {
  187. Extensible = true,
  188. Prototype = proto ?? _engine.Object.PrototypeObject
  189. };
  190. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  191. if (!ReferenceEquals(result, null))
  192. {
  193. return result;
  194. }
  195. return obj;
  196. }
  197. private class ObjectInstanceWithConstructor : ObjectInstance
  198. {
  199. private const string PropertyNameConstructor = "constructor";
  200. private const int PropertyNameConstructorLength = 11;
  201. private PropertyDescriptor _constructor;
  202. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  203. {
  204. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  205. }
  206. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  207. {
  208. if (_constructor != null)
  209. {
  210. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  211. }
  212. foreach (var entry in base.GetOwnProperties())
  213. {
  214. yield return entry;
  215. }
  216. }
  217. public override PropertyDescriptor GetOwnProperty(string propertyName)
  218. {
  219. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  220. {
  221. return _constructor ?? PropertyDescriptor.Undefined;
  222. }
  223. return base.GetOwnProperty(propertyName);
  224. }
  225. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  226. {
  227. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  228. {
  229. _constructor = desc;
  230. }
  231. else
  232. {
  233. base.SetOwnProperty(propertyName, desc);
  234. }
  235. }
  236. public override bool HasOwnProperty(string propertyName)
  237. {
  238. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  239. {
  240. return _constructor != null;
  241. }
  242. return base.HasOwnProperty(propertyName);
  243. }
  244. public override void RemoveOwnProperty(string propertyName)
  245. {
  246. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  247. {
  248. _constructor = null;
  249. }
  250. else
  251. {
  252. base.RemoveOwnProperty(propertyName);
  253. }
  254. }
  255. }
  256. }
  257. }