GlobalEnvironmentRecord.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.Value);
  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.Value);
  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) && 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) && 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. public override bool DeleteBinding(string name)
  210. {
  211. if (_declarativeRecord.HasBinding(name))
  212. {
  213. return _declarativeRecord.DeleteBinding(name);
  214. }
  215. if (_global.HasOwnProperty(name))
  216. {
  217. var status = _global.Delete(name);
  218. if (status)
  219. {
  220. _varNames.Remove(name);
  221. }
  222. return status;
  223. }
  224. return true;
  225. }
  226. public override bool HasThisBinding()
  227. {
  228. return true;
  229. }
  230. public override bool HasSuperBinding()
  231. {
  232. return false;
  233. }
  234. public override JsValue WithBaseObject()
  235. {
  236. return Undefined;
  237. }
  238. public override JsValue GetThisBinding()
  239. {
  240. return _global;
  241. }
  242. public bool HasVarDeclaration(string name)
  243. {
  244. return _varNames.Contains(name);
  245. }
  246. public bool HasLexicalDeclaration(string name)
  247. {
  248. return _declarativeRecord.HasBinding(name);
  249. }
  250. public bool HasRestrictedGlobalProperty(string name)
  251. {
  252. if (_globalObject is not null)
  253. {
  254. return _globalObject._properties?.TryGetValue(name, out var desc) == true
  255. && !desc.Configurable;
  256. }
  257. var existingProp = _global.GetOwnProperty(name);
  258. if (existingProp == PropertyDescriptor.Undefined)
  259. {
  260. return false;
  261. }
  262. return !existingProp.Configurable;
  263. }
  264. public bool CanDeclareGlobalVar(string name)
  265. {
  266. if (_global._properties!.ContainsKey(name))
  267. {
  268. return true;
  269. }
  270. return _global.Extensible;
  271. }
  272. public bool CanDeclareGlobalFunction(string name)
  273. {
  274. if (!_global._properties!.TryGetValue(name, out var existingProp)
  275. || existingProp == PropertyDescriptor.Undefined)
  276. {
  277. return _global.Extensible;
  278. }
  279. if (existingProp.Configurable)
  280. {
  281. return true;
  282. }
  283. if (existingProp.IsDataDescriptor() && existingProp.Writable && existingProp.Enumerable)
  284. {
  285. return true;
  286. }
  287. return false;
  288. }
  289. public void CreateGlobalVarBinding(string name, bool canBeDeleted)
  290. {
  291. Key key = name;
  292. if (!_global._properties!.ContainsKey(key) && _global.Extensible)
  293. {
  294. _global._properties[key] = new PropertyDescriptor(Undefined, canBeDeleted
  295. ? PropertyFlag.ConfigurableEnumerableWritable | PropertyFlag.MutableBinding
  296. : PropertyFlag.NonConfigurable | PropertyFlag.MutableBinding);
  297. }
  298. _varNames.Add(name);
  299. }
  300. /// <summary>
  301. /// https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  302. /// </summary>
  303. public void CreateGlobalFunctionBinding(string name, JsValue value, bool canBeDeleted)
  304. {
  305. var jsString = new JsString(name);
  306. var existingProp = _global.GetOwnProperty(jsString);
  307. PropertyDescriptor desc;
  308. if (existingProp == PropertyDescriptor.Undefined || existingProp.Configurable)
  309. {
  310. desc = new PropertyDescriptor(value, true, true, canBeDeleted);
  311. }
  312. else
  313. {
  314. desc = new PropertyDescriptor(value, PropertyFlag.None);
  315. }
  316. _global.DefinePropertyOrThrow(jsString, desc);
  317. _global.Set(jsString, value, false);
  318. _varNames.Add(name);
  319. }
  320. public sealed override bool HasBindings()
  321. {
  322. return _declarativeRecord.HasBindings() || _globalObject?._properties?.Count > 0 || _global._properties?.Count > 0;
  323. }
  324. internal override string[] GetAllBindingNames()
  325. {
  326. // JT: Rather than introduce a new method for the debugger, I'm reusing this one,
  327. // which - in spite of the very general name - is actually only used by the debugger
  328. // at this point.
  329. var names = new List<string>(_global._properties?.Count ?? 0 + _declarativeRecord._dictionary?.Count ?? 0);
  330. foreach (var name in _global.GetOwnProperties())
  331. {
  332. names.Add(name.Key.ToString());
  333. }
  334. foreach (var name in _declarativeRecord.GetAllBindingNames())
  335. {
  336. names.Add(name);
  337. }
  338. return names.ToArray();
  339. }
  340. public override bool Equals(JsValue? other)
  341. {
  342. return ReferenceEquals(this, other);
  343. }
  344. }
  345. }