ScriptFunctionInstance.cs 7.9 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. 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. }
  110. return Undefined;
  111. }
  112. }
  113. /// <summary>
  114. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  115. /// </summary>
  116. /// <param name="arguments"></param>
  117. /// <returns></returns>
  118. public ObjectInstance Construct(JsValue[] arguments)
  119. {
  120. var proto = Get("prototype").TryCast<ObjectInstance>();
  121. var obj = new ObjectInstance(_engine)
  122. {
  123. Extensible = true,
  124. Prototype = proto ?? _engine.Object.PrototypeObject
  125. };
  126. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  127. if (!ReferenceEquals(result, null))
  128. {
  129. return result;
  130. }
  131. return obj;
  132. }
  133. private class ObjectInstanceWithConstructor : ObjectInstance
  134. {
  135. private const string PropertyNameConstructor = "constructor";
  136. private const int PropertyNameConstructorLength = 11;
  137. private PropertyDescriptor _constructor;
  138. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  139. {
  140. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  141. }
  142. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  143. {
  144. if (_constructor != null)
  145. {
  146. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  147. }
  148. foreach (var entry in base.GetOwnProperties())
  149. {
  150. yield return entry;
  151. }
  152. }
  153. public override PropertyDescriptor GetOwnProperty(string propertyName)
  154. {
  155. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  156. {
  157. return _constructor ?? PropertyDescriptor.Undefined;
  158. }
  159. return base.GetOwnProperty(propertyName);
  160. }
  161. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  162. {
  163. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  164. {
  165. _constructor = desc;
  166. }
  167. else
  168. {
  169. base.SetOwnProperty(propertyName, desc);
  170. }
  171. }
  172. public override bool HasOwnProperty(string propertyName)
  173. {
  174. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  175. {
  176. return _constructor != null;
  177. }
  178. return base.HasOwnProperty(propertyName);
  179. }
  180. public override void RemoveOwnProperty(string propertyName)
  181. {
  182. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  183. {
  184. _constructor = null;
  185. }
  186. else
  187. {
  188. base.RemoveOwnProperty(propertyName);
  189. }
  190. }
  191. }
  192. }
  193. }