GlobalEnvironmentRecord.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native;
  5. using Jint.Native.Global;
  6. using Jint.Native.Object;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Runtime.Environments
  9. {
  10. /// <summary>
  11. /// http://www.ecma-international.org/ecma-262/6.0/#sec-global-environment-records
  12. /// </summary>
  13. internal sealed class GlobalEnvironmentRecord : EnvironmentRecord
  14. {
  15. private readonly GlobalObject _global;
  16. private readonly DeclarativeEnvironmentRecord _declarativeRecord;
  17. private readonly ObjectEnvironmentRecord _objectRecord;
  18. private readonly HashSet<string> _varNames = new HashSet<string>();
  19. public GlobalEnvironmentRecord(Engine engine, GlobalObject global) : base(engine)
  20. {
  21. _global = global;
  22. _objectRecord = new ObjectEnvironmentRecord(engine, global, provideThis: false, withEnvironment: false);
  23. _declarativeRecord = new DeclarativeEnvironmentRecord(engine);
  24. }
  25. public ObjectInstance GlobalThisValue => _global;
  26. public override bool HasBinding(string name)
  27. {
  28. return (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name)) || _objectRecord.HasBinding(name);
  29. }
  30. internal override bool TryGetBinding(
  31. in BindingName name,
  32. bool strict,
  33. out Binding binding,
  34. out JsValue value)
  35. {
  36. if (_declarativeRecord._hasBindings &&
  37. _declarativeRecord.TryGetBinding(name, strict, out binding, out value))
  38. {
  39. return true;
  40. }
  41. // we unwrap by name
  42. binding = default;
  43. value = default;
  44. // normal case is to find
  45. if (_global._properties._dictionary.TryGetValue(name.Key, out var property)
  46. && property != PropertyDescriptor.Undefined)
  47. {
  48. value = ObjectInstance.UnwrapJsValue(property, _global);
  49. return true;
  50. }
  51. return TryGetBindingForGlobalParent(name, out value, property);
  52. }
  53. [MethodImpl(MethodImplOptions.NoInlining)]
  54. private bool TryGetBindingForGlobalParent(
  55. in BindingName name,
  56. out JsValue value,
  57. PropertyDescriptor property)
  58. {
  59. value = default;
  60. var parent = _global._prototype;
  61. if (parent != null)
  62. {
  63. property = parent.GetOwnProperty(name.StringValue);
  64. }
  65. if (property == PropertyDescriptor.Undefined)
  66. {
  67. return false;
  68. }
  69. value = ObjectInstance.UnwrapJsValue(property, _global);
  70. return true;
  71. }
  72. /// <summary>
  73. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2.2
  74. /// </summary>
  75. public override void CreateMutableBinding(string name, bool canBeDeleted = false)
  76. {
  77. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  78. {
  79. ExceptionHelper.ThrowTypeError(_engine, name + " has already been declared");
  80. }
  81. _declarativeRecord.CreateMutableBinding(name, canBeDeleted);
  82. }
  83. public override void CreateImmutableBinding(string name, bool strict = true)
  84. {
  85. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  86. {
  87. ExceptionHelper.ThrowTypeError(_engine, name + " has already been declared");
  88. }
  89. _declarativeRecord.CreateImmutableBinding(name, strict);
  90. }
  91. public override void InitializeBinding(string name, JsValue value)
  92. {
  93. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  94. {
  95. _declarativeRecord.InitializeBinding(name, value);
  96. }
  97. else
  98. {
  99. if (!_global.Set(name, value))
  100. {
  101. ExceptionHelper.ThrowTypeError(_engine);
  102. }
  103. }
  104. }
  105. public override void SetMutableBinding(string name, JsValue value, bool strict)
  106. {
  107. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  108. {
  109. _declarativeRecord.SetMutableBinding(name, value, strict);
  110. }
  111. else
  112. {
  113. // fast inlined path as we know we target global, otherwise would be
  114. // _objectRecord.SetMutableBinding(name, value, strict);
  115. if (!_global.Set(name, value) && strict)
  116. {
  117. ExceptionHelper.ThrowTypeError(_engine);
  118. }
  119. }
  120. }
  121. internal override void SetMutableBinding(in BindingName name, JsValue value, bool strict)
  122. {
  123. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name.Key.Name))
  124. {
  125. _declarativeRecord.SetMutableBinding(name.Key.Name, value, strict);
  126. }
  127. else
  128. {
  129. // fast inlined path as we know we target global, otherwise would be
  130. // _objectRecord.SetMutableBinding(name, value, strict);
  131. if (!_global.Set(name.Key, value) && strict)
  132. {
  133. ExceptionHelper.ThrowTypeError(_engine);
  134. }
  135. }
  136. }
  137. public override JsValue GetBindingValue(string name, bool strict)
  138. {
  139. return _declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name)
  140. ? _declarativeRecord.GetBindingValue(name, strict)
  141. : _objectRecord.GetBindingValue(name, strict);
  142. }
  143. public override bool DeleteBinding(string name)
  144. {
  145. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  146. {
  147. return _declarativeRecord.DeleteBinding(name);
  148. }
  149. if (_global.HasOwnProperty(name))
  150. {
  151. var status = _objectRecord.DeleteBinding(name);
  152. if (status)
  153. {
  154. _varNames.Remove(name);
  155. }
  156. return status;
  157. }
  158. return true;
  159. }
  160. public override bool HasThisBinding()
  161. {
  162. return true;
  163. }
  164. public override bool HasSuperBinding()
  165. {
  166. return false;
  167. }
  168. public override JsValue WithBaseObject()
  169. {
  170. return Undefined;
  171. }
  172. public override JsValue GetThisBinding()
  173. {
  174. return _global;
  175. }
  176. public bool HasVarDeclaration(string name)
  177. {
  178. return _varNames.Contains(name);
  179. }
  180. public bool HasLexicalDeclaration(string name)
  181. {
  182. return _declarativeRecord.HasBinding(name);
  183. }
  184. public bool HasRestrictedGlobalProperty(string name)
  185. {
  186. var existingProp = _global.GetOwnProperty(name);
  187. if (existingProp == PropertyDescriptor.Undefined)
  188. {
  189. return false;
  190. }
  191. return !existingProp.Configurable;
  192. }
  193. public bool CanDeclareGlobalVar(string name)
  194. {
  195. if (_global._properties.ContainsKey(name))
  196. {
  197. return true;
  198. }
  199. return _global.Extensible;
  200. }
  201. public bool CanDeclareGlobalFunction(string name)
  202. {
  203. if (!_global._properties.TryGetValue(name, out var existingProp)
  204. || existingProp == PropertyDescriptor.Undefined)
  205. {
  206. return _global.Extensible;
  207. }
  208. if (existingProp.Configurable)
  209. {
  210. return true;
  211. }
  212. if (existingProp.IsDataDescriptor() && existingProp.Writable && existingProp.Enumerable)
  213. {
  214. return true;
  215. }
  216. return false;
  217. }
  218. public void CreateGlobalVarBinding(string name, bool canBeDeleted)
  219. {
  220. var hasProperty = _global.HasOwnProperty(name);
  221. if (!hasProperty && _global.Extensible)
  222. {
  223. _objectRecord.CreateMutableBindingAndInitialize(name, Undefined, canBeDeleted);
  224. }
  225. _varNames.Add(name);
  226. }
  227. public void CreateGlobalFunctionBinding(string name, JsValue value, bool canBeDeleted)
  228. {
  229. var existingProp = _global.GetOwnProperty(name);
  230. PropertyDescriptor desc;
  231. if (existingProp == PropertyDescriptor.Undefined || existingProp.Configurable)
  232. {
  233. desc = new PropertyDescriptor(value, true, true, canBeDeleted);
  234. }
  235. else
  236. {
  237. desc = new PropertyDescriptor(value, PropertyFlag.None);
  238. }
  239. _global.DefinePropertyOrThrow(name, desc);
  240. _global.Set(name, value, false);
  241. _varNames.Add(name);
  242. }
  243. internal override string[] GetAllBindingNames()
  244. {
  245. // JT: Rather than introduce a new method for the debugger, I'm reusing this one,
  246. // which - in spite of the very general name - is actually only used by the debugger
  247. // at this point.
  248. return _global.GetOwnProperties().Select(x => x.Key.ToString())
  249. .Concat(_declarativeRecord.GetAllBindingNames()).ToArray();
  250. }
  251. public override bool Equals(JsValue other)
  252. {
  253. return ReferenceEquals(_objectRecord, other);
  254. }
  255. }
  256. }