ScriptFunctionInstance.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Environments;
  7. using Jint.Runtime.Interpreter;
  8. namespace Jint.Native.Function
  9. {
  10. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  11. {
  12. internal readonly JintFunctionDefinition _function;
  13. private LexicalEnvironment _localEnv;
  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. Extensible = true;
  34. Prototype = _engine.Function.PrototypeObject;
  35. _length = new PropertyDescriptor(JsNumber.Create(function._length), PropertyFlag.Configurable);
  36. var proto = new ObjectInstanceWithConstructor(engine, this)
  37. {
  38. Extensible = true,
  39. Prototype = _engine.Object.PrototypeObject
  40. };
  41. _prototype = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  42. if (strict)
  43. {
  44. DefineOwnProperty(KnownKeys.Caller, engine._getSetThrower, false);
  45. DefineOwnProperty(KnownKeys.Arguments, engine._getSetThrower, false);
  46. }
  47. }
  48. // for example RavenDB wants to inspect this
  49. public IFunction FunctionDeclaration => _function._function;
  50. /// <summary>
  51. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  52. /// </summary>
  53. /// <param name="thisArg"></param>
  54. /// <param name="arguments"></param>
  55. /// <returns></returns>
  56. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  57. {
  58. var strict = _strict || _engine._isStrict;
  59. using (new StrictModeScope(strict, true))
  60. {
  61. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  62. JsValue thisBinding;
  63. if (StrictModeScope.IsStrictModeCode)
  64. {
  65. thisBinding = thisArg;
  66. }
  67. else if (thisArg._type == InternalTypes.Undefined || thisArg._type == InternalTypes.Null)
  68. {
  69. thisBinding = _engine.Global;
  70. }
  71. else if (thisArg._type != InternalTypes.Object)
  72. {
  73. thisBinding = TypeConverter.ToObject(_engine, thisArg);
  74. }
  75. else
  76. {
  77. thisBinding = thisArg;
  78. }
  79. var localEnv = _localEnv ?? LexicalEnvironment.NewDeclarativeEnvironment(_engine, _scope);
  80. localEnv.Reset(_scope);
  81. _localEnv = null;
  82. _engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  83. try
  84. {
  85. var argumentInstanceRented = _engine.DeclarationBindingInstantiation(
  86. DeclarationBindingType.FunctionCode,
  87. _function._hoistingScope,
  88. functionInstance: this,
  89. arguments);
  90. var result = _function._body.Execute();
  91. var value = result.GetValueOrDefault();
  92. if (argumentInstanceRented)
  93. {
  94. _engine.ExecutionContext.LexicalEnvironment?._record?.FunctionWasCalled();
  95. _engine.ExecutionContext.VariableEnvironment?._record?.FunctionWasCalled();
  96. }
  97. if (result.Type == CompletionType.Throw)
  98. {
  99. ExceptionHelper.ThrowJavaScriptException(_engine, value, result);
  100. }
  101. if (result.Type == CompletionType.Return)
  102. {
  103. return value;
  104. }
  105. }
  106. finally
  107. {
  108. _engine.LeaveExecutionContext();
  109. _localEnv = localEnv;
  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. /// <param name="arguments"></param>
  118. /// <returns></returns>
  119. public ObjectInstance Construct(JsValue[] arguments)
  120. {
  121. var proto = Get(KnownKeys.Prototype).TryCast<ObjectInstance>();
  122. var obj = new ObjectInstance(_engine)
  123. {
  124. Extensible = true,
  125. Prototype = proto ?? _engine.Object.PrototypeObject
  126. };
  127. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  128. if (!ReferenceEquals(result, null))
  129. {
  130. return result;
  131. }
  132. return obj;
  133. }
  134. private class ObjectInstanceWithConstructor : ObjectInstance
  135. {
  136. private PropertyDescriptor _constructor;
  137. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  138. {
  139. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  140. }
  141. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  142. {
  143. if (_constructor != null)
  144. {
  145. yield return new KeyValuePair<string, PropertyDescriptor>(KnownKeys.Constructor, _constructor);
  146. }
  147. foreach (var entry in base.GetOwnProperties())
  148. {
  149. yield return entry;
  150. }
  151. }
  152. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  153. {
  154. if (propertyName == KnownKeys.Constructor)
  155. {
  156. return _constructor ?? PropertyDescriptor.Undefined;
  157. }
  158. return base.GetOwnProperty(propertyName);
  159. }
  160. protected internal override void SetOwnProperty(in Key propertyName, PropertyDescriptor desc)
  161. {
  162. if (propertyName == KnownKeys.Constructor)
  163. {
  164. _constructor = desc;
  165. }
  166. else
  167. {
  168. base.SetOwnProperty(propertyName, desc);
  169. }
  170. }
  171. public override bool HasOwnProperty(in Key propertyName)
  172. {
  173. if (propertyName == KnownKeys.Constructor)
  174. {
  175. return _constructor != null;
  176. }
  177. return base.HasOwnProperty(propertyName);
  178. }
  179. public override void RemoveOwnProperty(in Key propertyName)
  180. {
  181. if (propertyName == KnownKeys.Constructor)
  182. {
  183. _constructor = null;
  184. }
  185. else
  186. {
  187. base.RemoveOwnProperty(propertyName);
  188. }
  189. }
  190. }
  191. }
  192. }