ScriptFunctionInstance.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Esprima.Ast;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Descriptors.Specialized;
  6. using Jint.Runtime.Environments;
  7. using Jint.Runtime.Interpreter;
  8. namespace Jint.Native.Function
  9. {
  10. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  11. {
  12. internal bool _isClassConstructor;
  13. /// <summary>
  14. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  15. /// </summary>
  16. public ScriptFunctionInstance(
  17. Engine engine,
  18. IFunction functionDeclaration,
  19. EnvironmentRecord scope,
  20. bool strict,
  21. ObjectInstance? proto = null)
  22. : this(
  23. engine,
  24. new JintFunctionDefinition(functionDeclaration),
  25. scope,
  26. strict ? FunctionThisMode.Strict : FunctionThisMode.Global,
  27. proto)
  28. {
  29. }
  30. internal ScriptFunctionInstance(
  31. Engine engine,
  32. JintFunctionDefinition function,
  33. EnvironmentRecord scope,
  34. FunctionThisMode thisMode,
  35. ObjectInstance? proto = null)
  36. : base(engine, engine.Realm, function, scope, thisMode)
  37. {
  38. _prototype = proto ?? _engine.Realm.Intrinsics.Function.PrototypeObject;
  39. _length = new LazyPropertyDescriptor(null, _ => JsNumber.Create(function.Initialize().Length), PropertyFlag.Configurable);
  40. if (!function.Strict
  41. && !engine._isStrict
  42. && function.Function is not ArrowFunctionExpression
  43. && !function.Function.Generator)
  44. {
  45. SetProperty(KnownKeys.Arguments, new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue));
  46. SetProperty(KnownKeys.Caller, new PropertyDescriptor(Undefined, PropertyFlag.Configurable));
  47. }
  48. }
  49. /// <summary>
  50. /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  51. /// </summary>
  52. protected internal override JsValue Call(JsValue thisArgument, JsValue[] arguments)
  53. {
  54. var strict = _thisMode == FunctionThisMode.Strict || _engine._isStrict;
  55. using (new StrictModeScope(strict, true))
  56. {
  57. try
  58. {
  59. var calleeContext = PrepareForOrdinaryCall(Undefined);
  60. if (_isClassConstructor)
  61. {
  62. ExceptionHelper.ThrowTypeError(calleeContext.Realm, $"Class constructor {_functionDefinition.Name} cannot be invoked without 'new'");
  63. }
  64. OrdinaryCallBindThis(calleeContext, thisArgument);
  65. // actual call
  66. var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
  67. var result = _functionDefinition.EvaluateBody(context, this, arguments);
  68. if (result.Type == CompletionType.Throw)
  69. {
  70. ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
  71. }
  72. // The DebugHandler needs the current execution context before the return for stepping through the return point
  73. if (context.DebugMode)
  74. {
  75. // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
  76. // the function body:
  77. _engine.DebugHandler.OnReturnPoint(
  78. _functionDefinition.Function.Body,
  79. result.Type == CompletionType.Normal ? Undefined : result.Value
  80. );
  81. }
  82. if (result.Type == CompletionType.Return)
  83. {
  84. return result.Value;
  85. }
  86. }
  87. finally
  88. {
  89. _engine.LeaveExecutionContext();
  90. }
  91. return Undefined;
  92. }
  93. }
  94. internal override bool IsConstructor
  95. {
  96. get
  97. {
  98. if (!_homeObject.IsUndefined() && !_isClassConstructor)
  99. {
  100. return false;
  101. }
  102. var function = _functionDefinition?.Function;
  103. return function is not null
  104. && function is not ArrowFunctionExpression
  105. && !function.Generator
  106. && !function.Async;
  107. }
  108. }
  109. /// <summary>
  110. /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
  111. /// </summary>
  112. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
  113. {
  114. var callerContext = _engine.ExecutionContext;
  115. var kind = _constructorKind;
  116. var thisArgument = Undefined;
  117. if (kind == ConstructorKind.Base)
  118. {
  119. thisArgument = OrdinaryCreateFromConstructor(
  120. newTarget,
  121. static intrinsics => intrinsics.Object.PrototypeObject,
  122. static (Engine engine, Realm _, object? _) => new JsObject(engine));
  123. }
  124. var calleeContext = PrepareForOrdinaryCall(newTarget);
  125. if (kind == ConstructorKind.Base)
  126. {
  127. OrdinaryCallBindThis(calleeContext, thisArgument);
  128. InitializeInstanceElements(thisArgument, this);
  129. }
  130. var constructorEnv = (FunctionEnvironmentRecord) calleeContext.LexicalEnvironment;
  131. var strict = _thisMode == FunctionThisMode.Strict;
  132. using (new StrictModeScope(strict, force: true))
  133. {
  134. try
  135. {
  136. var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
  137. var result = _functionDefinition.EvaluateBody(context, this, arguments);
  138. // The DebugHandler needs the current execution context before the return for stepping through the return point
  139. if (context.DebugMode && result.Type != CompletionType.Throw)
  140. {
  141. // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
  142. // the function body:
  143. _engine.DebugHandler.OnReturnPoint(
  144. _functionDefinition.Function.Body,
  145. result.Type == CompletionType.Normal ? thisArgument : result.Value
  146. );
  147. }
  148. if (result.Type == CompletionType.Return)
  149. {
  150. if (result.Value is ObjectInstance oi)
  151. {
  152. return oi;
  153. }
  154. if (kind == ConstructorKind.Base)
  155. {
  156. return (ObjectInstance) thisArgument;
  157. }
  158. if (!result.Value.IsUndefined())
  159. {
  160. ExceptionHelper.ThrowTypeError(callerContext.Realm);
  161. }
  162. }
  163. else if (result.Type == CompletionType.Throw)
  164. {
  165. ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
  166. }
  167. }
  168. finally
  169. {
  170. _engine.LeaveExecutionContext();
  171. }
  172. }
  173. return (ObjectInstance) constructorEnv.GetThisBinding();
  174. }
  175. /// <summary>
  176. /// https://tc39.es/ecma262/#sec-initializeinstanceelements
  177. /// </summary>
  178. private void InitializeInstanceElements(JsValue o, JsValue constructor)
  179. {
  180. // TODO private fields
  181. }
  182. internal void MakeClassConstructor()
  183. {
  184. _isClassConstructor = true;
  185. }
  186. }
  187. }