GlobalEnvironment.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Runtime.CompilerServices;
  3. using Jint.Native;
  4. using Jint.Native.Global;
  5. using Jint.Native.Object;
  6. using Jint.Runtime.Descriptors;
  7. namespace Jint.Runtime.Environments
  8. {
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-global-environment-records
  11. /// </summary>
  12. internal sealed class GlobalEnvironment : Environment
  13. {
  14. /// <summary>
  15. /// A sealed class for global usage.
  16. /// </summary>
  17. internal sealed class GlobalDeclarativeEnvironment : DeclarativeEnvironment
  18. {
  19. public GlobalDeclarativeEnvironment(Engine engine) : base(engine)
  20. {
  21. }
  22. }
  23. internal readonly ObjectInstance _global;
  24. // we expect it to be GlobalObject, but need to allow to something host-defined, like Window
  25. private readonly GlobalObject? _globalObject;
  26. // Environment records are needed by debugger
  27. internal readonly GlobalDeclarativeEnvironment _declarativeRecord;
  28. private readonly HashSet<Key> _varNames = [];
  29. public GlobalEnvironment(Engine engine, ObjectInstance global) : base(engine)
  30. {
  31. _global = global;
  32. _globalObject = global as GlobalObject;
  33. _declarativeRecord = new GlobalDeclarativeEnvironment(engine);
  34. }
  35. public ObjectInstance GlobalThisValue => _global;
  36. internal override bool HasBinding(Key name)
  37. {
  38. if (_declarativeRecord.HasBinding(name))
  39. {
  40. return true;
  41. }
  42. if (_globalObject is not null)
  43. {
  44. return _globalObject.HasProperty(name);
  45. }
  46. return _global.HasProperty(new JsString(name));
  47. }
  48. internal override bool HasBinding(BindingName name)
  49. {
  50. if (_declarativeRecord.HasBinding(name))
  51. {
  52. return true;
  53. }
  54. if (_globalObject is not null)
  55. {
  56. return _globalObject.HasProperty(name.Key);
  57. }
  58. return _global.HasProperty(name.Value);
  59. }
  60. internal override bool TryGetBinding(
  61. BindingName name,
  62. bool strict,
  63. out Binding binding,
  64. [NotNullWhen(true)] out JsValue? value)
  65. {
  66. if (_declarativeRecord._dictionary is not null && _declarativeRecord.TryGetBinding(name, strict, out binding, out value))
  67. {
  68. return true;
  69. }
  70. // we unwrap by name
  71. binding = default;
  72. value = default;
  73. // normal case is to find
  74. if (_global._properties!._dictionary.TryGetValue(name.Key, out var property)
  75. && property != PropertyDescriptor.Undefined)
  76. {
  77. value = ObjectInstance.UnwrapJsValue(property, _global);
  78. return true;
  79. }
  80. if (_global._prototype is not null)
  81. {
  82. return TryGetBindingForGlobalParent(name, out value);
  83. }
  84. return false;
  85. }
  86. [MethodImpl(MethodImplOptions.NoInlining)]
  87. private bool TryGetBindingForGlobalParent(
  88. BindingName name,
  89. [NotNullWhen(true)] out JsValue? value)
  90. {
  91. value = default;
  92. var parent = _global._prototype!;
  93. var property = parent.GetOwnProperty(name.Value);
  94. if (property == PropertyDescriptor.Undefined)
  95. {
  96. return false;
  97. }
  98. value = ObjectInstance.UnwrapJsValue(property, _global);
  99. return true;
  100. }
  101. /// <summary>
  102. /// https://tc39.es/ecma262/#sec-global-environment-records-createmutablebinding-n-d
  103. /// </summary>
  104. internal override void CreateMutableBinding(Key name, bool canBeDeleted = false)
  105. {
  106. if (_declarativeRecord.HasBinding(name))
  107. {
  108. ThrowAlreadyDeclaredException(name);
  109. }
  110. _declarativeRecord.CreateMutableBinding(name, canBeDeleted);
  111. }
  112. /// <summary>
  113. /// https://tc39.es/ecma262/#sec-global-environment-records-createimmutablebinding-n-s
  114. /// </summary>
  115. internal override void CreateImmutableBinding(Key name, bool strict = true)
  116. {
  117. if (_declarativeRecord.HasBinding(name))
  118. {
  119. ThrowAlreadyDeclaredException(name);
  120. }
  121. _declarativeRecord.CreateImmutableBinding(name, strict);
  122. }
  123. [MethodImpl(MethodImplOptions.NoInlining)]
  124. private void ThrowAlreadyDeclaredException(Key name)
  125. {
  126. ExceptionHelper.ThrowTypeError(_engine.Realm, $"{name} has already been declared");
  127. }
  128. internal override void InitializeBinding(Key name, JsValue value)
  129. {
  130. if (_declarativeRecord.HasBinding(name))
  131. {
  132. _declarativeRecord.InitializeBinding(name, value);
  133. }
  134. else
  135. {
  136. _global._properties![name].Value = value;
  137. }
  138. }
  139. internal override void SetMutableBinding(Key name, JsValue value, bool strict)
  140. {
  141. if (_declarativeRecord.HasBinding(name))
  142. {
  143. _declarativeRecord.SetMutableBinding(name, value, strict);
  144. }
  145. else
  146. {
  147. if (_globalObject is not null)
  148. {
  149. // fast inlined path as we know we target global
  150. if (!_globalObject.SetFromMutableBinding(name, value, strict) && strict)
  151. {
  152. ExceptionHelper.ThrowTypeError(_engine.Realm);
  153. }
  154. }
  155. else
  156. {
  157. SetMutableBindingUnlikely(name, value, strict);
  158. }
  159. }
  160. }
  161. internal override void SetMutableBinding(BindingName name, JsValue value, bool strict)
  162. {
  163. if (_declarativeRecord.HasBinding(name))
  164. {
  165. _declarativeRecord.SetMutableBinding(name, value, strict);
  166. }
  167. else
  168. {
  169. if (_globalObject is not null)
  170. {
  171. // fast inlined path as we know we target global
  172. if (!_globalObject.SetFromMutableBinding(name.Key, value, strict) && strict)
  173. {
  174. ExceptionHelper.ThrowTypeError(_engine.Realm);
  175. }
  176. }
  177. else
  178. {
  179. SetMutableBindingUnlikely(name.Key, value, strict);
  180. }
  181. }
  182. }
  183. private void SetMutableBindingUnlikely(Key name, JsValue value, bool strict)
  184. {
  185. // see ObjectEnvironmentRecord.SetMutableBinding
  186. var jsString = new JsString(name.Name);
  187. if (strict && !_global.HasProperty(jsString))
  188. {
  189. ExceptionHelper.ThrowReferenceNameError(_engine.Realm, name);
  190. }
  191. _global.Set(jsString, value);
  192. }
  193. internal override JsValue GetBindingValue(Key name, bool strict)
  194. {
  195. if (_declarativeRecord.HasBinding(name))
  196. {
  197. return _declarativeRecord.GetBindingValue(name, strict);
  198. }
  199. // see ObjectEnvironmentRecord.GetBindingValue
  200. var desc = PropertyDescriptor.Undefined;
  201. if (_globalObject is not null)
  202. {
  203. if (_globalObject._properties?.TryGetValue(name, out desc) == false)
  204. {
  205. desc = PropertyDescriptor.Undefined;
  206. }
  207. }
  208. else
  209. {
  210. desc = _global.GetOwnProperty(name.Name);
  211. }
  212. if (strict && desc == PropertyDescriptor.Undefined)
  213. {
  214. ExceptionHelper.ThrowReferenceNameError(_engine.Realm, name);
  215. }
  216. return ObjectInstance.UnwrapJsValue(desc, _global);
  217. }
  218. internal override bool DeleteBinding(Key name)
  219. {
  220. if (_declarativeRecord.HasBinding(name))
  221. {
  222. return _declarativeRecord.DeleteBinding(name);
  223. }
  224. if (_global.HasOwnProperty(name.Name))
  225. {
  226. var status = _global.Delete(name.Name);
  227. if (status)
  228. {
  229. _varNames.Remove(name);
  230. }
  231. return status;
  232. }
  233. return true;
  234. }
  235. internal override bool HasThisBinding() => true;
  236. internal override bool HasSuperBinding() => false;
  237. internal override JsValue WithBaseObject() => Undefined;
  238. internal override JsValue GetThisBinding() => _global;
  239. internal bool HasVarDeclaration(Key name) => _varNames.Contains(name);
  240. internal bool HasLexicalDeclaration(Key name) => _declarativeRecord.HasBinding(name);
  241. internal bool HasRestrictedGlobalProperty(Key name)
  242. {
  243. if (_globalObject is not null)
  244. {
  245. return _globalObject._properties?.TryGetValue(name, out var desc) == true
  246. && !desc.Configurable;
  247. }
  248. var existingProp = _global.GetOwnProperty(name.Name);
  249. if (existingProp == PropertyDescriptor.Undefined)
  250. {
  251. return false;
  252. }
  253. return !existingProp.Configurable;
  254. }
  255. public bool CanDeclareGlobalVar(Key name)
  256. {
  257. if (_global._properties!.ContainsKey(name))
  258. {
  259. return true;
  260. }
  261. return _global.Extensible;
  262. }
  263. public bool CanDeclareGlobalFunction(Key name)
  264. {
  265. if (!_global._properties!.TryGetValue(name, out var existingProp)
  266. || existingProp == PropertyDescriptor.Undefined)
  267. {
  268. return _global.Extensible;
  269. }
  270. if (existingProp.Configurable)
  271. {
  272. return true;
  273. }
  274. if (existingProp.IsDataDescriptor() && existingProp.Writable && existingProp.Enumerable)
  275. {
  276. return true;
  277. }
  278. return false;
  279. }
  280. public void CreateGlobalVarBinding(Key name, bool canBeDeleted)
  281. {
  282. if (_global.Extensible && _global._properties!.TryAdd(name, new PropertyDescriptor(Undefined, canBeDeleted
  283. ? PropertyFlag.ConfigurableEnumerableWritable | PropertyFlag.MutableBinding
  284. : PropertyFlag.NonConfigurable | PropertyFlag.MutableBinding)))
  285. {
  286. _varNames.Add(name);
  287. }
  288. }
  289. internal void CreateGlobalVarBindings(List<Key> names, bool canBeDeleted)
  290. {
  291. if (!_global.Extensible)
  292. {
  293. return;
  294. }
  295. for (var i = 0; i < names.Count; i++)
  296. {
  297. var name = names[i];
  298. _global._properties!.TryAdd(name, new PropertyDescriptor(Undefined, canBeDeleted
  299. ? PropertyFlag.ConfigurableEnumerableWritable | PropertyFlag.MutableBinding
  300. : PropertyFlag.NonConfigurable | PropertyFlag.MutableBinding));
  301. _varNames.Add(name);
  302. }
  303. }
  304. /// <summary>
  305. /// https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  306. /// </summary>
  307. public void CreateGlobalFunctionBinding(Key name, JsValue value, bool canBeDeleted)
  308. {
  309. var jsString = new JsString(name);
  310. var existingProp = _global.GetOwnProperty(jsString);
  311. PropertyDescriptor desc;
  312. if (existingProp == PropertyDescriptor.Undefined || existingProp.Configurable)
  313. {
  314. desc = new PropertyDescriptor(value, true, true, canBeDeleted);
  315. }
  316. else
  317. {
  318. desc = new PropertyDescriptor(value, PropertyFlag.None);
  319. }
  320. _global.DefinePropertyOrThrow(jsString, desc);
  321. _global.Set(jsString, value, false);
  322. _varNames.Add(name);
  323. }
  324. internal override bool HasBindings()
  325. {
  326. return _declarativeRecord.HasBindings() || _globalObject?._properties?.Count > 0 || _global._properties?.Count > 0;
  327. }
  328. internal override string[] GetAllBindingNames()
  329. {
  330. // JT: Rather than introduce a new method for the debugger, I'm reusing this one,
  331. // which - in spite of the very general name - is actually only used by the debugger
  332. // at this point.
  333. var names = new List<string>(_global._properties?.Count ?? 0 + _declarativeRecord._dictionary?.Count ?? 0);
  334. foreach (var name in _global.GetOwnProperties())
  335. {
  336. names.Add(name.Key.ToString());
  337. }
  338. foreach (var name in _declarativeRecord.GetAllBindingNames())
  339. {
  340. names.Add(name);
  341. }
  342. return names.ToArray();
  343. }
  344. public override bool Equals(JsValue? other)
  345. {
  346. return ReferenceEquals(this, other);
  347. }
  348. }
  349. }