GlobalEnvironment.cs 13 KB

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