ScriptFunctionInstance.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System.Collections.Generic;
  2. using Esprima.Ast;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Descriptors.Specialized;
  7. using Jint.Runtime.Environments;
  8. using Jint.Runtime.Interpreter;
  9. namespace Jint.Native.Function
  10. {
  11. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  12. {
  13. private bool _isClassConstructor;
  14. /// <summary>
  15. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  16. /// </summary>
  17. public ScriptFunctionInstance(
  18. Engine engine,
  19. IFunction functionDeclaration,
  20. EnvironmentRecord scope,
  21. bool strict,
  22. ObjectInstance proto = null)
  23. : this(
  24. engine,
  25. new JintFunctionDefinition(engine, functionDeclaration),
  26. scope,
  27. strict ? FunctionThisMode.Strict : FunctionThisMode.Global,
  28. proto)
  29. {
  30. }
  31. internal ScriptFunctionInstance(
  32. Engine engine,
  33. JintFunctionDefinition function,
  34. EnvironmentRecord scope,
  35. FunctionThisMode thisMode,
  36. ObjectInstance proto = null)
  37. : base(engine, engine.Realm, function, scope, thisMode)
  38. {
  39. _prototype = proto ?? _engine.Realm.Intrinsics.Function.PrototypeObject;
  40. _length = new LazyPropertyDescriptor(null, _ => JsNumber.Create(function.Initialize(this).Length), PropertyFlag.Configurable);
  41. if (!function.Strict && !engine._isStrict && function.Function is not ArrowFunctionExpression)
  42. {
  43. DefineOwnProperty(CommonProperties.Arguments, engine._callerCalleeArgumentsThrowerConfigurable);
  44. DefineOwnProperty(CommonProperties.Caller, new PropertyDescriptor(Undefined, PropertyFlag.Configurable));
  45. }
  46. }
  47. /// <summary>
  48. /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  49. /// </summary>
  50. public override JsValue Call(JsValue thisArgument, JsValue[] arguments)
  51. {
  52. var strict = _thisMode == FunctionThisMode.Strict || _engine._isStrict;
  53. using (new StrictModeScope(strict, true))
  54. {
  55. try
  56. {
  57. var calleeContext = PrepareForOrdinaryCall(Undefined);
  58. if (_isClassConstructor)
  59. {
  60. ExceptionHelper.ThrowTypeError(calleeContext.Realm, $"Class constructor {_functionDefinition.Name} cannot be invoked without 'new'");
  61. }
  62. OrdinaryCallBindThis(calleeContext, thisArgument);
  63. // actual call
  64. var result = OrdinaryCallEvaluateBody(arguments, calleeContext);
  65. if (result.Type == CompletionType.Throw)
  66. {
  67. ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
  68. }
  69. // The DebugHandler needs the current execution context before the return for stepping through the return point
  70. if (_engine._isDebugMode)
  71. {
  72. // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
  73. // the function body:
  74. _engine.DebugHandler.OnReturnPoint(
  75. _functionDefinition.Function.Body,
  76. result.Type == CompletionType.Normal ? Undefined : result.Value
  77. );
  78. }
  79. if (result.Type == CompletionType.Return)
  80. {
  81. return result.Value;
  82. }
  83. }
  84. finally
  85. {
  86. _engine.LeaveExecutionContext();
  87. }
  88. return Undefined;
  89. }
  90. }
  91. internal override bool IsConstructor =>
  92. (_homeObject.IsUndefined() || _isClassConstructor)
  93. && _functionDefinition?.Function is not ArrowFunctionExpression;
  94. /// <summary>
  95. /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
  96. /// </summary>
  97. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  98. {
  99. var callerContext = _engine.ExecutionContext;
  100. var kind = _constructorKind;
  101. var thisArgument = Undefined;
  102. if (kind == ConstructorKind.Base)
  103. {
  104. thisArgument = OrdinaryCreateFromConstructor(
  105. newTarget,
  106. static intrinsics => intrinsics.Object.PrototypeObject,
  107. static (engine, realm, _) => new ObjectInstance(engine));
  108. }
  109. var calleeContext = PrepareForOrdinaryCall(newTarget);
  110. if (kind == ConstructorKind.Base)
  111. {
  112. OrdinaryCallBindThis(calleeContext, thisArgument);
  113. }
  114. var constructorEnv = (FunctionEnvironmentRecord) calleeContext.LexicalEnvironment;
  115. var strict = _thisMode == FunctionThisMode.Strict || _engine._isStrict;
  116. using (new StrictModeScope(strict, force: true))
  117. {
  118. try
  119. {
  120. var result = OrdinaryCallEvaluateBody(arguments, calleeContext);
  121. // The DebugHandler needs the current execution context before the return for stepping through the return point
  122. if (_engine._isDebugMode && result.Type != CompletionType.Throw)
  123. {
  124. // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
  125. // the function body:
  126. _engine.DebugHandler.OnReturnPoint(
  127. _functionDefinition.Function.Body,
  128. result.Type == CompletionType.Normal ? thisArgument : result.Value
  129. );
  130. }
  131. if (result.Type == CompletionType.Return)
  132. {
  133. if (result.Value is ObjectInstance oi)
  134. {
  135. return oi;
  136. }
  137. if (kind == ConstructorKind.Base)
  138. {
  139. return (ObjectInstance) thisArgument!;
  140. }
  141. if (!result.Value.IsUndefined())
  142. {
  143. ExceptionHelper.ThrowTypeError(callerContext.Realm);
  144. }
  145. }
  146. else if (result.Type == CompletionType.Throw)
  147. {
  148. ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
  149. }
  150. }
  151. finally
  152. {
  153. _engine.LeaveExecutionContext();
  154. }
  155. }
  156. return (ObjectInstance) constructorEnv.GetThisBinding();
  157. }
  158. internal void MakeConstructor(bool writableProperty = true, ObjectInstance prototype = null)
  159. {
  160. _constructorKind = ConstructorKind.Base;
  161. if (prototype is null)
  162. {
  163. prototype = new ObjectInstanceWithConstructor(_engine, this)
  164. {
  165. _prototype = _realm.Intrinsics.Object.PrototypeObject
  166. };
  167. }
  168. _prototypeDescriptor = new PropertyDescriptor(prototype, writableProperty, enumerable: false, configurable: false);
  169. }
  170. internal void MakeClassConstructor()
  171. {
  172. _isClassConstructor = true;
  173. }
  174. private class ObjectInstanceWithConstructor : ObjectInstance
  175. {
  176. private PropertyDescriptor _constructor;
  177. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  178. {
  179. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  180. }
  181. public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  182. {
  183. if (_constructor != null)
  184. {
  185. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Constructor, _constructor);
  186. }
  187. foreach (var entry in base.GetOwnProperties())
  188. {
  189. yield return entry;
  190. }
  191. }
  192. public override PropertyDescriptor GetOwnProperty(JsValue property)
  193. {
  194. if (property == CommonProperties.Constructor)
  195. {
  196. return _constructor ?? PropertyDescriptor.Undefined;
  197. }
  198. return base.GetOwnProperty(property);
  199. }
  200. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  201. {
  202. if (property == CommonProperties.Constructor)
  203. {
  204. _constructor = desc;
  205. }
  206. else
  207. {
  208. base.SetOwnProperty(property, desc);
  209. }
  210. }
  211. public override bool HasOwnProperty(JsValue property)
  212. {
  213. if (property == CommonProperties.Constructor)
  214. {
  215. return _constructor != null;
  216. }
  217. return base.HasOwnProperty(property);
  218. }
  219. public override void RemoveOwnProperty(JsValue property)
  220. {
  221. if (property == CommonProperties.Constructor)
  222. {
  223. _constructor = null;
  224. }
  225. else
  226. {
  227. base.RemoveOwnProperty(property);
  228. }
  229. }
  230. }
  231. }
  232. }