2
0

ScriptFunctionInstance.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 readonly JintFunctionDefinition _function;
  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. : this(engine, new JintFunctionDefinition(engine, functionDeclaration), scope, strict ? FunctionThisMode.Strict : FunctionThisMode.Global)
  23. {
  24. }
  25. internal ScriptFunctionInstance(
  26. Engine engine,
  27. JintFunctionDefinition function,
  28. LexicalEnvironment scope,
  29. FunctionThisMode thisMode)
  30. : base(engine, function, scope, thisMode)
  31. {
  32. _function = function;
  33. _prototype = _engine.Function.PrototypeObject;
  34. _length = new LazyPropertyDescriptor(() => JsNumber.Create(function.Initialize(engine, this).Length), PropertyFlag.Configurable);
  35. var proto = new ObjectInstanceWithConstructor(engine, this)
  36. {
  37. _prototype = _engine.Object.PrototypeObject
  38. };
  39. _prototypeDescriptor = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  40. if (thisMode == FunctionThisMode.Strict)
  41. {
  42. DefineOwnProperty(CommonProperties.Caller, engine._getSetThrower);
  43. DefineOwnProperty(CommonProperties.Arguments, engine._getSetThrower);
  44. }
  45. }
  46. // for example RavenDB wants to inspect this
  47. public IFunction FunctionDeclaration => _function.Function;
  48. /// <summary>
  49. /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  50. /// </summary>
  51. public override JsValue Call(JsValue thisArgument, JsValue[] arguments)
  52. {
  53. // ** PrepareForOrdinaryCall **
  54. // var callerContext = _engine.ExecutionContext;
  55. // Let calleeRealm be F.[[Realm]].
  56. // Set the Realm of calleeContext to calleeRealm.
  57. // Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].
  58. var localEnv = LexicalEnvironment.NewFunctionEnvironment(_engine, this, Undefined);
  59. // If callerContext is not already suspended, suspend callerContext.
  60. // Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  61. // NOTE: Any exception objects produced after this point are associated with calleeRealm.
  62. // Return calleeContext.
  63. _engine.EnterExecutionContext(localEnv, localEnv);
  64. // ** OrdinaryCallBindThis **
  65. JsValue thisValue;
  66. if (_thisMode == FunctionThisMode.Strict)
  67. {
  68. thisValue = thisArgument;
  69. }
  70. else
  71. {
  72. if (thisArgument.IsNullOrUndefined())
  73. {
  74. var globalEnv = _engine.GlobalEnvironment;
  75. var globalEnvRec = (GlobalEnvironmentRecord) globalEnv._record;
  76. thisValue = globalEnvRec.GlobalThisValue;
  77. }
  78. else
  79. {
  80. thisValue = TypeConverter.ToObject(_engine, thisArgument);
  81. }
  82. }
  83. var envRec = (FunctionEnvironmentRecord) localEnv._record;
  84. envRec.BindThisValue(thisValue);
  85. // actual call
  86. var strict = _thisMode == FunctionThisMode.Strict || _engine._isStrict;
  87. using (new StrictModeScope(strict, true))
  88. {
  89. try
  90. {
  91. var argumentsInstance = _engine.FunctionDeclarationInstantiation(
  92. functionInstance: this,
  93. arguments,
  94. localEnv);
  95. var result = _function.Body.Execute();
  96. var value = result.GetValueOrDefault().Clone();
  97. argumentsInstance?.FunctionWasCalled();
  98. if (result.Type == CompletionType.Throw)
  99. {
  100. ExceptionHelper.ThrowJavaScriptException(_engine, value, result);
  101. }
  102. if (result.Type == CompletionType.Return)
  103. {
  104. return value;
  105. }
  106. }
  107. finally
  108. {
  109. _engine.LeaveExecutionContext();
  110. }
  111. return Undefined;
  112. }
  113. }
  114. /// <summary>
  115. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  116. /// </summary>
  117. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  118. {
  119. var thisArgument = OrdinaryCreateFromConstructor(TypeConverter.ToObject(_engine, newTarget), _engine.Object.PrototypeObject);
  120. var result = Call(thisArgument, arguments).TryCast<ObjectInstance>();
  121. if (!ReferenceEquals(result, null))
  122. {
  123. return result;
  124. }
  125. return thisArgument;
  126. }
  127. private class ObjectInstanceWithConstructor : ObjectInstance
  128. {
  129. private PropertyDescriptor _constructor;
  130. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  131. {
  132. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  133. }
  134. public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  135. {
  136. if (_constructor != null)
  137. {
  138. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Constructor, _constructor);
  139. }
  140. foreach (var entry in base.GetOwnProperties())
  141. {
  142. yield return entry;
  143. }
  144. }
  145. public override PropertyDescriptor GetOwnProperty(JsValue property)
  146. {
  147. if (property == CommonProperties.Constructor)
  148. {
  149. return _constructor ?? PropertyDescriptor.Undefined;
  150. }
  151. return base.GetOwnProperty(property);
  152. }
  153. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  154. {
  155. if (property == CommonProperties.Constructor)
  156. {
  157. _constructor = desc;
  158. }
  159. else
  160. {
  161. base.SetOwnProperty(property, desc);
  162. }
  163. }
  164. public override bool HasOwnProperty(JsValue property)
  165. {
  166. if (property == CommonProperties.Constructor)
  167. {
  168. return _constructor != null;
  169. }
  170. return base.HasOwnProperty(property);
  171. }
  172. public override void RemoveOwnProperty(JsValue property)
  173. {
  174. if (property == CommonProperties.Constructor)
  175. {
  176. _constructor = null;
  177. }
  178. else
  179. {
  180. base.RemoveOwnProperty(property);
  181. }
  182. }
  183. }
  184. }
  185. }