ScriptFunctionInstance.cs 10 KB

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