ScriptFunctionInstance.cs 9.6 KB

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