ScriptFunctionInstance.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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(KnownKeys.Caller, engine._getSetThrower, false);
  44. DefineOwnProperty(KnownKeys.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(KnownKeys.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 PropertyDescriptor _constructor;
  133. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  134. {
  135. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  136. }
  137. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  138. {
  139. if (_constructor != null)
  140. {
  141. yield return new KeyValuePair<string, PropertyDescriptor>(KnownKeys.Constructor, _constructor);
  142. }
  143. foreach (var entry in base.GetOwnProperties())
  144. {
  145. yield return entry;
  146. }
  147. }
  148. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  149. {
  150. if (propertyName == KnownKeys.Constructor)
  151. {
  152. return _constructor ?? PropertyDescriptor.Undefined;
  153. }
  154. return base.GetOwnProperty(propertyName);
  155. }
  156. protected internal override void SetOwnProperty(in Key propertyName, PropertyDescriptor desc)
  157. {
  158. if (propertyName == KnownKeys.Constructor)
  159. {
  160. _constructor = desc;
  161. }
  162. else
  163. {
  164. base.SetOwnProperty(propertyName, desc);
  165. }
  166. }
  167. public override bool HasOwnProperty(in Key propertyName)
  168. {
  169. if (propertyName == KnownKeys.Constructor)
  170. {
  171. return _constructor != null;
  172. }
  173. return base.HasOwnProperty(propertyName);
  174. }
  175. public override void RemoveOwnProperty(in Key propertyName)
  176. {
  177. if (propertyName == KnownKeys.Constructor)
  178. {
  179. _constructor = null;
  180. }
  181. else
  182. {
  183. base.RemoveOwnProperty(propertyName);
  184. }
  185. }
  186. }
  187. }
  188. }