2
0

ScriptFunction.cs 7.9 KB

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