ScriptFunctionInstance.cs 7.7 KB

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