ScriptFunctionInstance.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using Esprima.Ast;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  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. private LexicalEnvironment _localEnv;
  15. /// <summary>
  16. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  17. /// </summary>
  18. public ScriptFunctionInstance(
  19. Engine engine,
  20. IFunction functionDeclaration,
  21. LexicalEnvironment scope,
  22. bool strict)
  23. : this(engine, new JintFunctionDefinition(engine, functionDeclaration), scope, strict)
  24. {
  25. }
  26. internal ScriptFunctionInstance(
  27. Engine engine,
  28. JintFunctionDefinition function,
  29. LexicalEnvironment scope,
  30. bool strict)
  31. : base(engine, function._name, function._parameterNames, scope, strict)
  32. {
  33. _function = function;
  34. _prototype = _engine.Function.PrototypeObject;
  35. _length = new PropertyDescriptor(JsNumber.Create(function._length), PropertyFlag.Configurable);
  36. var proto = new ObjectInstanceWithConstructor(engine, this)
  37. {
  38. _prototype = _engine.Object.PrototypeObject
  39. };
  40. _prototypeDescriptor = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  41. if (strict)
  42. {
  43. DefineOwnProperty(KnownKeys.Caller, engine._getSetThrower);
  44. DefineOwnProperty(KnownKeys.Arguments, engine._getSetThrower);
  45. }
  46. }
  47. // for example RavenDB wants to inspect this
  48. public IFunction FunctionDeclaration => _function._function;
  49. /// <summary>
  50. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  51. /// </summary>
  52. /// <param name="thisArg"></param>
  53. /// <param name="arguments"></param>
  54. /// <returns></returns>
  55. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  56. {
  57. var strict = _strict || _engine._isStrict;
  58. using (new StrictModeScope(strict, true))
  59. {
  60. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  61. JsValue thisBinding;
  62. if (StrictModeScope.IsStrictModeCode)
  63. {
  64. thisBinding = thisArg;
  65. }
  66. else if (thisArg._type == InternalTypes.Undefined || thisArg._type == InternalTypes.Null)
  67. {
  68. thisBinding = _engine.Global;
  69. }
  70. else if (thisArg._type != InternalTypes.Object)
  71. {
  72. thisBinding = TypeConverter.ToObject(_engine, thisArg);
  73. }
  74. else
  75. {
  76. thisBinding = thisArg;
  77. }
  78. var localEnv = _localEnv ?? LexicalEnvironment.NewDeclarativeEnvironment(_engine, _scope);
  79. localEnv.Reset(_scope);
  80. _localEnv = null;
  81. _engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  82. try
  83. {
  84. var argumentInstanceRented = _engine.DeclarationBindingInstantiation(
  85. DeclarationBindingType.FunctionCode,
  86. _function._hoistingScope,
  87. functionInstance: this,
  88. arguments);
  89. var result = _function._body.Execute();
  90. var value = result.GetValueOrDefault();
  91. if (argumentInstanceRented)
  92. {
  93. _engine.ExecutionContext.LexicalEnvironment?._record?.FunctionWasCalled();
  94. _engine.ExecutionContext.VariableEnvironment?._record?.FunctionWasCalled();
  95. }
  96. if (result.Type == CompletionType.Throw)
  97. {
  98. ExceptionHelper.ThrowJavaScriptException(_engine, value, result);
  99. }
  100. if (result.Type == CompletionType.Return)
  101. {
  102. return value;
  103. }
  104. }
  105. finally
  106. {
  107. _engine.LeaveExecutionContext();
  108. _localEnv = localEnv;
  109. }
  110. return Undefined;
  111. }
  112. }
  113. /// <summary>
  114. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  115. /// </summary>
  116. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  117. {
  118. var thisArgument = OrdinaryCreateFromConstructor(TypeConverter.ToObject(_engine, newTarget), _engine.Object.PrototypeObject);
  119. var result = Call(thisArgument, arguments).TryCast<ObjectInstance>();
  120. if (!ReferenceEquals(result, null))
  121. {
  122. return result;
  123. }
  124. return thisArgument;
  125. }
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. private ObjectInstance OrdinaryCreateFromConstructor(ObjectInstance constructor, ObjectInstance intrinsicDefaultProto)
  128. {
  129. var proto = GetPrototypeFromConstructor(constructor, intrinsicDefaultProto);
  130. var obj = new ObjectInstance(_engine)
  131. {
  132. _prototype = proto
  133. };
  134. return obj;
  135. }
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. private static ObjectInstance GetPrototypeFromConstructor(ObjectInstance constructor, ObjectInstance intrinsicDefaultProto)
  138. {
  139. var proto = constructor.Get(KnownKeys.Prototype, constructor) as ObjectInstance;
  140. // If Type(proto) is not Object, then
  141. // Let realm be ? GetFunctionRealm(constructor).
  142. // Set proto to realm's intrinsic object named intrinsicDefaultProto.
  143. return proto ?? intrinsicDefaultProto;
  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<Key, PropertyDescriptor>> GetOwnProperties()
  153. {
  154. if (_constructor != null)
  155. {
  156. yield return new KeyValuePair<Key, PropertyDescriptor>(KnownKeys.Constructor, _constructor);
  157. }
  158. foreach (var entry in base.GetOwnProperties())
  159. {
  160. yield return entry;
  161. }
  162. }
  163. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  164. {
  165. if (propertyName == KnownKeys.Constructor)
  166. {
  167. return _constructor ?? PropertyDescriptor.Undefined;
  168. }
  169. return base.GetOwnProperty(propertyName);
  170. }
  171. protected internal override void SetOwnProperty(in Key propertyName, PropertyDescriptor desc)
  172. {
  173. if (propertyName == KnownKeys.Constructor)
  174. {
  175. _constructor = desc;
  176. }
  177. else
  178. {
  179. base.SetOwnProperty(propertyName, desc);
  180. }
  181. }
  182. public override bool HasOwnProperty(in Key propertyName)
  183. {
  184. if (propertyName == KnownKeys.Constructor)
  185. {
  186. return _constructor != null;
  187. }
  188. return base.HasOwnProperty(propertyName);
  189. }
  190. public override void RemoveOwnProperty(in Key propertyName)
  191. {
  192. if (propertyName == KnownKeys.Constructor)
  193. {
  194. _constructor = null;
  195. }
  196. else
  197. {
  198. base.RemoveOwnProperty(propertyName);
  199. }
  200. }
  201. }
  202. }
  203. }