ScriptFunctionInstance.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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(engine, 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(this).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. (_homeObject.IsUndefined() || _isClassConstructor)
  96. && _functionDefinition?.Function is not ArrowFunctionExpression
  97. && _functionDefinition?.Function?.Generator != true;
  98. /// <summary>
  99. /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
  100. /// </summary>
  101. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
  102. {
  103. var callerContext = _engine.ExecutionContext;
  104. var kind = _constructorKind;
  105. var thisArgument = Undefined;
  106. if (kind == ConstructorKind.Base)
  107. {
  108. thisArgument = OrdinaryCreateFromConstructor(
  109. newTarget,
  110. static intrinsics => intrinsics.Object.PrototypeObject,
  111. static (Engine engine, Realm _, object? _) => new ObjectInstance(engine));
  112. }
  113. var calleeContext = PrepareForOrdinaryCall(newTarget);
  114. if (kind == ConstructorKind.Base)
  115. {
  116. OrdinaryCallBindThis(calleeContext, thisArgument);
  117. InitializeInstanceElements(thisArgument, this);
  118. }
  119. var constructorEnv = (FunctionEnvironmentRecord) calleeContext.LexicalEnvironment;
  120. var strict = _thisMode == FunctionThisMode.Strict;
  121. using (new StrictModeScope(strict, force: true))
  122. {
  123. try
  124. {
  125. var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
  126. var result = _functionDefinition.EvaluateBody(context, this, arguments);
  127. // The DebugHandler needs the current execution context before the return for stepping through the return point
  128. if (context.DebugMode && result.Type != CompletionType.Throw)
  129. {
  130. // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
  131. // the function body:
  132. _engine.DebugHandler.OnReturnPoint(
  133. _functionDefinition.Function.Body,
  134. result.Type == CompletionType.Normal ? thisArgument : result.Value
  135. );
  136. }
  137. if (result.Type == CompletionType.Return)
  138. {
  139. if (result.Value is ObjectInstance oi)
  140. {
  141. return oi;
  142. }
  143. if (kind == ConstructorKind.Base)
  144. {
  145. return (ObjectInstance) thisArgument;
  146. }
  147. if (!result.Value.IsUndefined())
  148. {
  149. ExceptionHelper.ThrowTypeError(callerContext.Realm);
  150. }
  151. }
  152. else if (result.Type == CompletionType.Throw)
  153. {
  154. ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
  155. }
  156. }
  157. finally
  158. {
  159. _engine.LeaveExecutionContext();
  160. }
  161. }
  162. return (ObjectInstance) constructorEnv.GetThisBinding();
  163. }
  164. /// <summary>
  165. /// https://tc39.es/ecma262/#sec-initializeinstanceelements
  166. /// </summary>
  167. private void InitializeInstanceElements(JsValue o, JsValue constructor)
  168. {
  169. // TODO private fields
  170. }
  171. internal void MakeClassConstructor()
  172. {
  173. _isClassConstructor = true;
  174. }
  175. }
  176. }