ScriptFunctionInstance.cs 8.2 KB

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