ScriptFunction.cs 8.3 KB

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