ScriptFunctionInstance.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 readonly IFunction _functionDeclaration;
  17. /// <summary>
  18. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  19. /// </summary>
  20. /// <param name="engine"></param>
  21. /// <param name="functionDeclaration"></param>
  22. /// <param name="scope"></param>
  23. /// <param name="strict"></param>
  24. public ScriptFunctionInstance(
  25. Engine engine,
  26. IFunction functionDeclaration,
  27. LexicalEnvironment scope,
  28. bool strict)
  29. : base(engine, functionDeclaration.Id?.Name ?? "", GetParameterNames(functionDeclaration), scope, strict)
  30. {
  31. _functionDeclaration = functionDeclaration;
  32. Extensible = true;
  33. Prototype = _engine.Function.PrototypeObject;
  34. _length = new PropertyDescriptor(JsNumber.Create(_formalParameters.Length), PropertyFlag.AllForbidden);
  35. var proto = new ObjectInstanceWithConstructor(engine, this)
  36. {
  37. Extensible = true,
  38. Prototype = _engine.Object.PrototypeObject
  39. };
  40. _prototype = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  41. if (_functionDeclaration.Id != null)
  42. {
  43. DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, PropertyFlag.None), false);
  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. private static string[] GetParameterNames(IFunction functionDeclaration)
  54. {
  55. var list = functionDeclaration.Params;
  56. var count = list.Count;
  57. if (count == 0)
  58. {
  59. return System.ArrayExt.Empty<string>();
  60. }
  61. var names = new string[count];
  62. for (var i = 0; i < count; ++i)
  63. {
  64. names[i] = ((Identifier) list[i]).Name;
  65. }
  66. return names;
  67. }
  68. /// <summary>
  69. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  70. /// </summary>
  71. /// <param name="thisArg"></param>
  72. /// <param name="arguments"></param>
  73. /// <returns></returns>
  74. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  75. {
  76. using (new StrictModeScope(Strict, true))
  77. {
  78. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  79. JsValue thisBinding;
  80. if (StrictModeScope.IsStrictModeCode)
  81. {
  82. thisBinding = thisArg;
  83. }
  84. else if (thisArg._type == Types.Undefined || thisArg._type == Types.Null)
  85. {
  86. thisBinding = _engine.Global;
  87. }
  88. else if (thisArg._type != Types.Object)
  89. {
  90. thisBinding = TypeConverter.ToObject(_engine, thisArg);
  91. }
  92. else
  93. {
  94. thisBinding = thisArg;
  95. }
  96. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _scope);
  97. _engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  98. try
  99. {
  100. var argumentInstanceRented = _engine.DeclarationBindingInstantiation(
  101. DeclarationBindingType.FunctionCode,
  102. _functionDeclaration.HoistingScope.FunctionDeclarations,
  103. _functionDeclaration.HoistingScope.VariableDeclarations,
  104. this,
  105. arguments);
  106. var result = _engine.ExecuteStatement(_functionDeclaration.Body);
  107. var value = result.GetValueOrDefault();
  108. // we can safely release arguments if they don't escape the scope
  109. if (argumentInstanceRented
  110. && _engine.ExecutionContext.LexicalEnvironment?._record is DeclarativeEnvironmentRecord der
  111. && !(result.Value is ArgumentsInstance))
  112. {
  113. der.ReleaseArguments();
  114. }
  115. if (result.Type == CompletionType.Throw)
  116. {
  117. var ex = new JavaScriptException(value).SetCallstack(_engine, result.Location);
  118. throw ex;
  119. }
  120. if (result.Type == CompletionType.Return)
  121. {
  122. return value;
  123. }
  124. }
  125. finally
  126. {
  127. _engine.LeaveExecutionContext();
  128. }
  129. return Undefined;
  130. }
  131. }
  132. /// <summary>
  133. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  134. /// </summary>
  135. /// <param name="arguments"></param>
  136. /// <returns></returns>
  137. public ObjectInstance Construct(JsValue[] arguments)
  138. {
  139. var proto = Get("prototype").TryCast<ObjectInstance>();
  140. var obj = new ObjectInstance(_engine)
  141. {
  142. Extensible = true,
  143. Prototype = proto ?? _engine.Object.PrototypeObject
  144. };
  145. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  146. if (!ReferenceEquals(result, null))
  147. {
  148. return result;
  149. }
  150. return obj;
  151. }
  152. private class ObjectInstanceWithConstructor : ObjectInstance
  153. {
  154. private const string PropertyNameConstructor = "constructor";
  155. private const int PropertyNameConstructorLength = 11;
  156. private PropertyDescriptor _constructor;
  157. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  158. {
  159. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  160. }
  161. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  162. {
  163. if (_constructor != null)
  164. {
  165. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  166. }
  167. foreach (var entry in base.GetOwnProperties())
  168. {
  169. yield return entry;
  170. }
  171. }
  172. public override PropertyDescriptor GetOwnProperty(string propertyName)
  173. {
  174. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  175. {
  176. return _constructor ?? PropertyDescriptor.Undefined;
  177. }
  178. return base.GetOwnProperty(propertyName);
  179. }
  180. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  181. {
  182. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  183. {
  184. _constructor = desc;
  185. }
  186. else
  187. {
  188. base.SetOwnProperty(propertyName, desc);
  189. }
  190. }
  191. public override bool HasOwnProperty(string propertyName)
  192. {
  193. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  194. {
  195. return _constructor != null;
  196. }
  197. return base.HasOwnProperty(propertyName);
  198. }
  199. public override void RemoveOwnProperty(string propertyName)
  200. {
  201. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  202. {
  203. _constructor = null;
  204. }
  205. else
  206. {
  207. base.RemoveOwnProperty(propertyName);
  208. }
  209. }
  210. }
  211. }
  212. }