GlobalEnvironmentRecord.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. /// https://tc39.es/ecma262/#sec-global-environment-records
  12. /// </summary>
  13. public sealed class GlobalEnvironmentRecord : EnvironmentRecord
  14. {
  15. private readonly ObjectInstance _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, ObjectInstance 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 is not 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.Realm, 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.Realm, 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.Realm);
  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.Realm);
  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. if (_global is GlobalObject globalObject)
  130. {
  131. // fast inlined path as we know we target global
  132. if (!globalObject.Set(name.Key, value) && strict)
  133. {
  134. ExceptionHelper.ThrowTypeError(_engine.Realm);
  135. }
  136. }
  137. else
  138. {
  139. _objectRecord.SetMutableBinding(name, value ,strict);
  140. }
  141. }
  142. }
  143. public override JsValue GetBindingValue(string name, bool strict)
  144. {
  145. return _declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name)
  146. ? _declarativeRecord.GetBindingValue(name, strict)
  147. : _objectRecord.GetBindingValue(name, strict);
  148. }
  149. public override bool DeleteBinding(string name)
  150. {
  151. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  152. {
  153. return _declarativeRecord.DeleteBinding(name);
  154. }
  155. if (_global.HasOwnProperty(name))
  156. {
  157. var status = _objectRecord.DeleteBinding(name);
  158. if (status)
  159. {
  160. _varNames.Remove(name);
  161. }
  162. return status;
  163. }
  164. return true;
  165. }
  166. public override bool HasThisBinding()
  167. {
  168. return true;
  169. }
  170. public override bool HasSuperBinding()
  171. {
  172. return false;
  173. }
  174. public override JsValue WithBaseObject()
  175. {
  176. return Undefined;
  177. }
  178. public override JsValue GetThisBinding()
  179. {
  180. return _global;
  181. }
  182. public bool HasVarDeclaration(string name)
  183. {
  184. return _varNames.Contains(name);
  185. }
  186. public bool HasLexicalDeclaration(string name)
  187. {
  188. return _declarativeRecord.HasBinding(name);
  189. }
  190. public bool HasRestrictedGlobalProperty(string name)
  191. {
  192. var existingProp = _global.GetOwnProperty(name);
  193. if (existingProp == PropertyDescriptor.Undefined)
  194. {
  195. return false;
  196. }
  197. return !existingProp.Configurable;
  198. }
  199. public bool CanDeclareGlobalVar(string name)
  200. {
  201. if (_global._properties.ContainsKey(name))
  202. {
  203. return true;
  204. }
  205. return _global.Extensible;
  206. }
  207. public bool CanDeclareGlobalFunction(string name)
  208. {
  209. if (!_global._properties.TryGetValue(name, out var existingProp)
  210. || existingProp == PropertyDescriptor.Undefined)
  211. {
  212. return _global.Extensible;
  213. }
  214. if (existingProp.Configurable)
  215. {
  216. return true;
  217. }
  218. if (existingProp.IsDataDescriptor() && existingProp.Writable && existingProp.Enumerable)
  219. {
  220. return true;
  221. }
  222. return false;
  223. }
  224. public void CreateGlobalVarBinding(string name, bool canBeDeleted)
  225. {
  226. var hasProperty = _global.HasOwnProperty(name);
  227. if (!hasProperty && _global.Extensible)
  228. {
  229. _objectRecord.CreateMutableBindingAndInitialize(name, Undefined, canBeDeleted);
  230. }
  231. _varNames.Add(name);
  232. }
  233. public void CreateGlobalFunctionBinding(string name, JsValue value, bool canBeDeleted)
  234. {
  235. var existingProp = _global.GetOwnProperty(name);
  236. PropertyDescriptor desc;
  237. if (existingProp == PropertyDescriptor.Undefined || existingProp.Configurable)
  238. {
  239. desc = new PropertyDescriptor(value, true, true, canBeDeleted);
  240. }
  241. else
  242. {
  243. desc = new PropertyDescriptor(value, PropertyFlag.None);
  244. }
  245. _global.DefinePropertyOrThrow(name, desc);
  246. _global.Set(name, value, false);
  247. _varNames.Add(name);
  248. }
  249. internal override string[] GetAllBindingNames()
  250. {
  251. // JT: Rather than introduce a new method for the debugger, I'm reusing this one,
  252. // which - in spite of the very general name - is actually only used by the debugger
  253. // at this point.
  254. return _global.GetOwnProperties().Select(x => x.Key.ToString())
  255. .Concat(_declarativeRecord.GetAllBindingNames()).ToArray();
  256. }
  257. public override bool Equals(JsValue other)
  258. {
  259. return ReferenceEquals(_objectRecord, other);
  260. }
  261. }
  262. }