ScriptFunctionInstance.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 (_engine._isDebugMode)
  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 context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
  122. var result = OrdinaryCallEvaluateBody(context, arguments, calleeContext);
  123. // The DebugHandler needs the current execution context before the return for stepping through the return point
  124. if (_engine._isDebugMode && result.Type != CompletionType.Throw)
  125. {
  126. // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
  127. // the function body:
  128. _engine.DebugHandler.OnReturnPoint(
  129. _functionDefinition.Function.Body,
  130. result.Type == CompletionType.Normal ? thisArgument : result.Value
  131. );
  132. }
  133. if (result.Type == CompletionType.Return)
  134. {
  135. if (result.Value is ObjectInstance oi)
  136. {
  137. return oi;
  138. }
  139. if (kind == ConstructorKind.Base)
  140. {
  141. return (ObjectInstance) thisArgument!;
  142. }
  143. if (!result.Value.IsUndefined())
  144. {
  145. ExceptionHelper.ThrowTypeError(callerContext.Realm);
  146. }
  147. }
  148. else if (result.Type == CompletionType.Throw)
  149. {
  150. ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
  151. }
  152. }
  153. finally
  154. {
  155. _engine.LeaveExecutionContext();
  156. }
  157. }
  158. return (ObjectInstance) constructorEnv.GetThisBinding();
  159. }
  160. internal void MakeConstructor(bool writableProperty = true, ObjectInstance prototype = null)
  161. {
  162. _constructorKind = ConstructorKind.Base;
  163. if (prototype is null)
  164. {
  165. prototype = new ObjectInstanceWithConstructor(_engine, this)
  166. {
  167. _prototype = _realm.Intrinsics.Object.PrototypeObject
  168. };
  169. }
  170. _prototypeDescriptor = new PropertyDescriptor(prototype, writableProperty, enumerable: false, configurable: false);
  171. }
  172. internal void MakeClassConstructor()
  173. {
  174. _isClassConstructor = true;
  175. }
  176. private class ObjectInstanceWithConstructor : ObjectInstance
  177. {
  178. private PropertyDescriptor _constructor;
  179. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  180. {
  181. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  182. }
  183. public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  184. {
  185. if (_constructor != null)
  186. {
  187. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Constructor, _constructor);
  188. }
  189. foreach (var entry in base.GetOwnProperties())
  190. {
  191. yield return entry;
  192. }
  193. }
  194. public override PropertyDescriptor GetOwnProperty(JsValue property)
  195. {
  196. if (property == CommonProperties.Constructor)
  197. {
  198. return _constructor ?? PropertyDescriptor.Undefined;
  199. }
  200. return base.GetOwnProperty(property);
  201. }
  202. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  203. {
  204. if (property == CommonProperties.Constructor)
  205. {
  206. _constructor = desc;
  207. }
  208. else
  209. {
  210. base.SetOwnProperty(property, desc);
  211. }
  212. }
  213. public override bool HasOwnProperty(JsValue property)
  214. {
  215. if (property == CommonProperties.Constructor)
  216. {
  217. return _constructor != null;
  218. }
  219. return base.HasOwnProperty(property);
  220. }
  221. public override void RemoveOwnProperty(JsValue property)
  222. {
  223. if (property == CommonProperties.Constructor)
  224. {
  225. _constructor = null;
  226. }
  227. else
  228. {
  229. base.RemoveOwnProperty(property);
  230. }
  231. }
  232. }
  233. }
  234. }