ScriptFunctionInstance.cs 8.1 KB

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