ScriptFunctionInstance.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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)
  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. var length = function._hasRestParameter ? _formalParameters.Length - 1 : _formalParameters.Length;
  36. _length = new PropertyDescriptor(JsNumber.Create(length), PropertyFlag.AllForbidden);
  37. var proto = new ObjectInstanceWithConstructor(engine, this)
  38. {
  39. Extensible = true,
  40. Prototype = _engine.Object.PrototypeObject
  41. };
  42. _prototype = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  43. if (_function._name != null)
  44. {
  45. DefineOwnProperty("name", new PropertyDescriptor(_function._name, PropertyFlag.None), false);
  46. }
  47. if (strict)
  48. {
  49. var thrower = engine.Function.ThrowTypeError;
  50. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  51. DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  52. DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  53. }
  54. }
  55. // for example RavenDB wants to inspect this
  56. public IFunction FunctionDeclaration => _function._function;
  57. /// <summary>
  58. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  59. /// </summary>
  60. /// <param name="thisArg"></param>
  61. /// <param name="arguments"></param>
  62. /// <returns></returns>
  63. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  64. {
  65. var strict = Strict || _engine._isStrict;
  66. using (new StrictModeScope(strict, true))
  67. {
  68. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  69. JsValue thisBinding;
  70. if (StrictModeScope.IsStrictModeCode)
  71. {
  72. thisBinding = thisArg;
  73. }
  74. else if (thisArg._type == Types.Undefined || thisArg._type == Types.Null)
  75. {
  76. thisBinding = _engine.Global;
  77. }
  78. else if (thisArg._type != Types.Object)
  79. {
  80. thisBinding = TypeConverter.ToObject(_engine, thisArg);
  81. }
  82. else
  83. {
  84. thisBinding = thisArg;
  85. }
  86. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _scope);
  87. _engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  88. try
  89. {
  90. var argumentInstanceRented = _engine.DeclarationBindingInstantiation(
  91. DeclarationBindingType.FunctionCode,
  92. _function._hoistingScope.FunctionDeclarations,
  93. _function._hoistingScope.VariableDeclarations,
  94. functionInstance: this,
  95. arguments);
  96. var result = _function._body.Execute();
  97. var value = result.GetValueOrDefault();
  98. if (argumentInstanceRented)
  99. {
  100. _engine.ExecutionContext.LexicalEnvironment?._record?.FunctionWasCalled();
  101. _engine.ExecutionContext.VariableEnvironment?._record?.FunctionWasCalled();
  102. }
  103. if (result.Type == CompletionType.Throw)
  104. {
  105. var ex = new JavaScriptException(value).SetCallstack(_engine, result.Location);
  106. throw ex;
  107. }
  108. if (result.Type == CompletionType.Return)
  109. {
  110. return value;
  111. }
  112. }
  113. finally
  114. {
  115. _engine.LeaveExecutionContext();
  116. }
  117. return Undefined;
  118. }
  119. }
  120. /// <summary>
  121. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  122. /// </summary>
  123. /// <param name="arguments"></param>
  124. /// <returns></returns>
  125. public ObjectInstance Construct(JsValue[] arguments)
  126. {
  127. var proto = Get("prototype").TryCast<ObjectInstance>();
  128. var obj = new ObjectInstance(_engine)
  129. {
  130. Extensible = true,
  131. Prototype = proto ?? _engine.Object.PrototypeObject
  132. };
  133. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  134. if (!ReferenceEquals(result, null))
  135. {
  136. return result;
  137. }
  138. return obj;
  139. }
  140. private class ObjectInstanceWithConstructor : ObjectInstance
  141. {
  142. private const string PropertyNameConstructor = "constructor";
  143. private const int PropertyNameConstructorLength = 11;
  144. private PropertyDescriptor _constructor;
  145. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  146. {
  147. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  148. }
  149. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  150. {
  151. if (_constructor != null)
  152. {
  153. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  154. }
  155. foreach (var entry in base.GetOwnProperties())
  156. {
  157. yield return entry;
  158. }
  159. }
  160. public override PropertyDescriptor GetOwnProperty(string propertyName)
  161. {
  162. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  163. {
  164. return _constructor ?? PropertyDescriptor.Undefined;
  165. }
  166. return base.GetOwnProperty(propertyName);
  167. }
  168. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  169. {
  170. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  171. {
  172. _constructor = desc;
  173. }
  174. else
  175. {
  176. base.SetOwnProperty(propertyName, desc);
  177. }
  178. }
  179. public override bool HasOwnProperty(string propertyName)
  180. {
  181. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  182. {
  183. return _constructor != null;
  184. }
  185. return base.HasOwnProperty(propertyName);
  186. }
  187. public override void RemoveOwnProperty(string propertyName)
  188. {
  189. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  190. {
  191. _constructor = null;
  192. }
  193. else
  194. {
  195. base.RemoveOwnProperty(propertyName);
  196. }
  197. }
  198. }
  199. }
  200. }