ScriptFunctionInstance.cs 9.4 KB

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