ScriptFunctionInstance.cs 8.0 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.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. _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. var thrower = engine.Function.ThrowTypeError;
  45. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  46. DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  47. DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  48. }
  49. }
  50. // for example RavenDB wants to inspect this
  51. public IFunction FunctionDeclaration => _function._function;
  52. /// <summary>
  53. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  54. /// </summary>
  55. /// <param name="thisArg"></param>
  56. /// <param name="arguments"></param>
  57. /// <returns></returns>
  58. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  59. {
  60. var strict = Strict || _engine._isStrict;
  61. using (new StrictModeScope(strict, true))
  62. {
  63. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  64. JsValue thisBinding;
  65. if (StrictModeScope.IsStrictModeCode)
  66. {
  67. thisBinding = thisArg;
  68. }
  69. else if (thisArg._type == Types.Undefined || thisArg._type == Types.Null)
  70. {
  71. thisBinding = _engine.Global;
  72. }
  73. else if (thisArg._type != Types.Object)
  74. {
  75. thisBinding = TypeConverter.ToObject(_engine, thisArg);
  76. }
  77. else
  78. {
  79. thisBinding = thisArg;
  80. }
  81. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _scope);
  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. var ex = new JavaScriptException(value).SetCallstack(_engine, result.Location);
  100. throw ex;
  101. }
  102. if (result.Type == CompletionType.Return)
  103. {
  104. return value;
  105. }
  106. }
  107. finally
  108. {
  109. _engine.LeaveExecutionContext();
  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("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 const string PropertyNameConstructor = "constructor";
  137. private const int PropertyNameConstructorLength = 11;
  138. private PropertyDescriptor _constructor;
  139. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  140. {
  141. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  142. }
  143. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  144. {
  145. if (_constructor != null)
  146. {
  147. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  148. }
  149. foreach (var entry in base.GetOwnProperties())
  150. {
  151. yield return entry;
  152. }
  153. }
  154. public override PropertyDescriptor GetOwnProperty(string propertyName)
  155. {
  156. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  157. {
  158. return _constructor ?? PropertyDescriptor.Undefined;
  159. }
  160. return base.GetOwnProperty(propertyName);
  161. }
  162. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  163. {
  164. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  165. {
  166. _constructor = desc;
  167. }
  168. else
  169. {
  170. base.SetOwnProperty(propertyName, desc);
  171. }
  172. }
  173. public override bool HasOwnProperty(string propertyName)
  174. {
  175. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  176. {
  177. return _constructor != null;
  178. }
  179. return base.HasOwnProperty(propertyName);
  180. }
  181. public override void RemoveOwnProperty(string propertyName)
  182. {
  183. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  184. {
  185. _constructor = null;
  186. }
  187. else
  188. {
  189. base.RemoveOwnProperty(propertyName);
  190. }
  191. }
  192. }
  193. }
  194. }