ScriptFunctionInstance.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. namespace Jint.Native.Function
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
  14. {
  15. private readonly IFunction _functionDeclaration;
  16. /// <summary>
  17. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  18. /// </summary>
  19. /// <param name="engine"></param>
  20. /// <param name="functionDeclaration"></param>
  21. /// <param name="scope"></param>
  22. /// <param name="strict"></param>
  23. public ScriptFunctionInstance(
  24. Engine engine,
  25. IFunction functionDeclaration,
  26. LexicalEnvironment scope,
  27. bool strict)
  28. : base(engine, functionDeclaration.Id?.Name ?? "", GetParameterNames(functionDeclaration), scope, strict)
  29. {
  30. _functionDeclaration = functionDeclaration;
  31. Extensible = true;
  32. Prototype = _engine.Function.PrototypeObject;
  33. _length = new PropertyDescriptor(JsNumber.Create(_formalParameters.Length), PropertyFlag.AllForbidden);
  34. var proto = new ObjectInstanceWithConstructor(engine, this)
  35. {
  36. Extensible = true,
  37. Prototype = _engine.Object.PrototypeObject
  38. };
  39. _prototype = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable);
  40. if (_functionDeclaration.Id != null)
  41. {
  42. DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, PropertyFlag.None), false);
  43. }
  44. if (strict)
  45. {
  46. var thrower = engine.Function.ThrowTypeError;
  47. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  48. DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  49. DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  50. }
  51. }
  52. private static string[] GetParameterNames(IFunction functionDeclaration)
  53. {
  54. var list = functionDeclaration.Params;
  55. var count = list.Count;
  56. if (count == 0)
  57. {
  58. return System.ArrayExt.Empty<string>();
  59. }
  60. var names = new string[count];
  61. for (var i = 0; i < count; ++i)
  62. {
  63. names[i] = ((Identifier) list[i]).Name;
  64. }
  65. return names;
  66. }
  67. /// <summary>
  68. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
  69. /// </summary>
  70. /// <param name="thisArg"></param>
  71. /// <param name="arguments"></param>
  72. /// <returns></returns>
  73. public override JsValue Call(JsValue thisArg, JsValue[] arguments)
  74. {
  75. var strict = Strict || _engine._isStrict;
  76. using (new StrictModeScope(strict, true))
  77. {
  78. // setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
  79. JsValue thisBinding;
  80. if (StrictModeScope.IsStrictModeCode)
  81. {
  82. thisBinding = thisArg;
  83. }
  84. else if (thisArg._type == Types.Undefined || thisArg._type == Types.Null)
  85. {
  86. thisBinding = _engine.Global;
  87. }
  88. else if (thisArg._type != Types.Object)
  89. {
  90. thisBinding = TypeConverter.ToObject(_engine, thisArg);
  91. }
  92. else
  93. {
  94. thisBinding = thisArg;
  95. }
  96. var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _scope);
  97. _engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
  98. try
  99. {
  100. var argumentInstanceRented = _engine.DeclarationBindingInstantiation(
  101. DeclarationBindingType.FunctionCode,
  102. _functionDeclaration.HoistingScope.FunctionDeclarations,
  103. _functionDeclaration.HoistingScope.VariableDeclarations,
  104. this,
  105. arguments);
  106. var result = _engine.ExecuteStatement(_functionDeclaration.Body);
  107. var value = result.GetValueOrDefault();
  108. if (argumentInstanceRented)
  109. {
  110. _engine.ExecutionContext.LexicalEnvironment?._record?.FunctionWasCalled();
  111. _engine.ExecutionContext.VariableEnvironment?._record?.FunctionWasCalled();
  112. }
  113. if (result.Type == CompletionType.Throw)
  114. {
  115. var ex = new JavaScriptException(value).SetCallstack(_engine, result.Location);
  116. throw ex;
  117. }
  118. if (result.Type == CompletionType.Return)
  119. {
  120. return value;
  121. }
  122. }
  123. finally
  124. {
  125. _engine.LeaveExecutionContext();
  126. }
  127. return Undefined;
  128. }
  129. }
  130. /// <summary>
  131. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.2
  132. /// </summary>
  133. /// <param name="arguments"></param>
  134. /// <returns></returns>
  135. public ObjectInstance Construct(JsValue[] arguments)
  136. {
  137. var proto = Get("prototype").TryCast<ObjectInstance>();
  138. var obj = new ObjectInstance(_engine)
  139. {
  140. Extensible = true,
  141. Prototype = proto ?? _engine.Object.PrototypeObject
  142. };
  143. var result = Call(obj, arguments).TryCast<ObjectInstance>();
  144. if (!ReferenceEquals(result, null))
  145. {
  146. return result;
  147. }
  148. return obj;
  149. }
  150. private class ObjectInstanceWithConstructor : ObjectInstance
  151. {
  152. private const string PropertyNameConstructor = "constructor";
  153. private const int PropertyNameConstructorLength = 11;
  154. private PropertyDescriptor _constructor;
  155. public ObjectInstanceWithConstructor(Engine engine, ObjectInstance thisObj) : base(engine)
  156. {
  157. _constructor = new PropertyDescriptor(thisObj, PropertyFlag.NonEnumerable);
  158. }
  159. public override IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  160. {
  161. if (_constructor != null)
  162. {
  163. yield return new KeyValuePair<string, PropertyDescriptor>(PropertyNameConstructor, _constructor);
  164. }
  165. foreach (var entry in base.GetOwnProperties())
  166. {
  167. yield return entry;
  168. }
  169. }
  170. public override PropertyDescriptor GetOwnProperty(string propertyName)
  171. {
  172. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  173. {
  174. return _constructor ?? PropertyDescriptor.Undefined;
  175. }
  176. return base.GetOwnProperty(propertyName);
  177. }
  178. protected internal override void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  179. {
  180. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  181. {
  182. _constructor = desc;
  183. }
  184. else
  185. {
  186. base.SetOwnProperty(propertyName, desc);
  187. }
  188. }
  189. public override bool HasOwnProperty(string propertyName)
  190. {
  191. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  192. {
  193. return _constructor != null;
  194. }
  195. return base.HasOwnProperty(propertyName);
  196. }
  197. public override void RemoveOwnProperty(string propertyName)
  198. {
  199. if (propertyName.Length == PropertyNameConstructorLength && propertyName == PropertyNameConstructor)
  200. {
  201. _constructor = null;
  202. }
  203. else
  204. {
  205. base.RemoveOwnProperty(propertyName);
  206. }
  207. }
  208. }
  209. }
  210. }