GlobalEnvironment.cs 13 KB

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