GlobalEnvironmentRecord.cs 13 KB

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