GlobalEnvironment.cs 11 KB

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