ScriptFunctionInstance.cs 8.3 KB

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