GlobalEnvironmentRecord.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native;
  5. using Jint.Native.Global;
  6. using Jint.Native.Object;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Runtime.Environments
  9. {
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-global-environment-records
  12. /// </summary>
  13. public sealed class GlobalEnvironmentRecord : EnvironmentRecord
  14. {
  15. private readonly ObjectInstance _global;
  16. // Environment records are needed by debugger
  17. internal readonly DeclarativeEnvironmentRecord _declarativeRecord;
  18. internal readonly ObjectEnvironmentRecord _objectRecord;
  19. private readonly HashSet<string> _varNames = new HashSet<string>();
  20. public GlobalEnvironmentRecord(Engine engine, ObjectInstance global) : base(engine)
  21. {
  22. _global = global;
  23. _objectRecord = new ObjectEnvironmentRecord(engine, global, provideThis: false, withEnvironment: false);
  24. _declarativeRecord = new DeclarativeEnvironmentRecord(engine);
  25. }
  26. public ObjectInstance GlobalThisValue => _global;
  27. public override bool HasBinding(string name)
  28. {
  29. return (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name)) || _objectRecord.HasBinding(name);
  30. }
  31. internal override bool TryGetBinding(
  32. in BindingName name,
  33. bool strict,
  34. out Binding binding,
  35. out JsValue value)
  36. {
  37. if (_declarativeRecord._hasBindings &&
  38. _declarativeRecord.TryGetBinding(name, strict, out binding, out value))
  39. {
  40. return true;
  41. }
  42. // we unwrap by name
  43. binding = default;
  44. value = default;
  45. // normal case is to find
  46. if (_global._properties._dictionary.TryGetValue(name.Key, out var property)
  47. && property != PropertyDescriptor.Undefined)
  48. {
  49. value = ObjectInstance.UnwrapJsValue(property, _global);
  50. return true;
  51. }
  52. return TryGetBindingForGlobalParent(name, out value, property);
  53. }
  54. [MethodImpl(MethodImplOptions.NoInlining)]
  55. private bool TryGetBindingForGlobalParent(
  56. in BindingName name,
  57. out JsValue value,
  58. PropertyDescriptor property)
  59. {
  60. value = default;
  61. var parent = _global._prototype;
  62. if (parent is not null)
  63. {
  64. property = parent.GetOwnProperty(name.StringValue);
  65. }
  66. if (property == PropertyDescriptor.Undefined)
  67. {
  68. return false;
  69. }
  70. value = ObjectInstance.UnwrapJsValue(property, _global);
  71. return true;
  72. }
  73. /// <summary>
  74. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2.2
  75. /// </summary>
  76. public override void CreateMutableBinding(string name, bool canBeDeleted = false)
  77. {
  78. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  79. {
  80. ExceptionHelper.ThrowTypeError(_engine.Realm, name + " has already been declared");
  81. }
  82. _declarativeRecord.CreateMutableBinding(name, canBeDeleted);
  83. }
  84. public override void CreateImmutableBinding(string name, bool strict = true)
  85. {
  86. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  87. {
  88. ExceptionHelper.ThrowTypeError(_engine.Realm, name + " has already been declared");
  89. }
  90. _declarativeRecord.CreateImmutableBinding(name, strict);
  91. }
  92. public override void InitializeBinding(string name, JsValue value)
  93. {
  94. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  95. {
  96. _declarativeRecord.InitializeBinding(name, value);
  97. }
  98. else
  99. {
  100. if (!_global.Set(name, value))
  101. {
  102. ExceptionHelper.ThrowTypeError(_engine.Realm);
  103. }
  104. }
  105. }
  106. public override void SetMutableBinding(string name, JsValue value, bool strict)
  107. {
  108. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  109. {
  110. _declarativeRecord.SetMutableBinding(name, value, strict);
  111. }
  112. else
  113. {
  114. // fast inlined path as we know we target global, otherwise would be
  115. // _objectRecord.SetMutableBinding(name, value, strict);
  116. var property = JsString.Create(name);
  117. if (strict && !_global.HasProperty(property))
  118. {
  119. ExceptionHelper.ThrowReferenceError(_engine.Realm, name);
  120. }
  121. _global.Set(property, value);
  122. }
  123. }
  124. internal override void SetMutableBinding(in BindingName name, JsValue value, bool strict)
  125. {
  126. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name.Key.Name))
  127. {
  128. _declarativeRecord.SetMutableBinding(name.Key.Name, value, strict);
  129. }
  130. else
  131. {
  132. if (_global is GlobalObject globalObject)
  133. {
  134. // fast inlined path as we know we target global
  135. if (!globalObject.Set(name.Key, value) && strict)
  136. {
  137. ExceptionHelper.ThrowTypeError(_engine.Realm);
  138. }
  139. }
  140. else
  141. {
  142. _objectRecord.SetMutableBinding(name, value ,strict);
  143. }
  144. }
  145. }
  146. public override JsValue GetBindingValue(string name, bool strict)
  147. {
  148. return _declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name)
  149. ? _declarativeRecord.GetBindingValue(name, strict)
  150. : _objectRecord.GetBindingValue(name, strict);
  151. }
  152. internal override bool TryGetBindingValue(string name, bool strict, out JsValue value)
  153. {
  154. return _declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name)
  155. ? _declarativeRecord.TryGetBindingValue(name, strict, out value)
  156. : _objectRecord.TryGetBindingValue(name, strict, out value);
  157. }
  158. public override bool DeleteBinding(string name)
  159. {
  160. if (_declarativeRecord._hasBindings && _declarativeRecord.HasBinding(name))
  161. {
  162. return _declarativeRecord.DeleteBinding(name);
  163. }
  164. if (_global.HasOwnProperty(name))
  165. {
  166. var status = _objectRecord.DeleteBinding(name);
  167. if (status)
  168. {
  169. _varNames.Remove(name);
  170. }
  171. return status;
  172. }
  173. return true;
  174. }
  175. public override bool HasThisBinding()
  176. {
  177. return true;
  178. }
  179. public override bool HasSuperBinding()
  180. {
  181. return false;
  182. }
  183. public override JsValue WithBaseObject()
  184. {
  185. return Undefined;
  186. }
  187. public override JsValue GetThisBinding()
  188. {
  189. return _global;
  190. }
  191. public bool HasVarDeclaration(string name)
  192. {
  193. return _varNames.Contains(name);
  194. }
  195. public bool HasLexicalDeclaration(string name)
  196. {
  197. return _declarativeRecord.HasBinding(name);
  198. }
  199. public bool HasRestrictedGlobalProperty(string name)
  200. {
  201. var existingProp = _global.GetOwnProperty(name);
  202. if (existingProp == PropertyDescriptor.Undefined)
  203. {
  204. return false;
  205. }
  206. return !existingProp.Configurable;
  207. }
  208. public bool CanDeclareGlobalVar(string name)
  209. {
  210. if (_global._properties.ContainsKey(name))
  211. {
  212. return true;
  213. }
  214. return _global.Extensible;
  215. }
  216. public bool CanDeclareGlobalFunction(string name)
  217. {
  218. if (!_global._properties.TryGetValue(name, out var existingProp)
  219. || existingProp == PropertyDescriptor.Undefined)
  220. {
  221. return _global.Extensible;
  222. }
  223. if (existingProp.Configurable)
  224. {
  225. return true;
  226. }
  227. if (existingProp.IsDataDescriptor() && existingProp.Writable && existingProp.Enumerable)
  228. {
  229. return true;
  230. }
  231. return false;
  232. }
  233. public void CreateGlobalVarBinding(string name, bool canBeDeleted)
  234. {
  235. var hasProperty = _global.HasOwnProperty(name);
  236. if (!hasProperty && _global.Extensible)
  237. {
  238. _objectRecord.CreateMutableBindingAndInitialize(name, Undefined, canBeDeleted);
  239. }
  240. _varNames.Add(name);
  241. }
  242. /// <summary>
  243. /// https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  244. /// </summary>
  245. public void CreateGlobalFunctionBinding(string name, JsValue value, bool canBeDeleted)
  246. {
  247. var existingProp = _global.GetOwnProperty(name);
  248. PropertyDescriptor desc;
  249. if (existingProp == PropertyDescriptor.Undefined || existingProp.Configurable)
  250. {
  251. desc = new PropertyDescriptor(value, true, true, canBeDeleted);
  252. }
  253. else
  254. {
  255. desc = new PropertyDescriptor(value, PropertyFlag.None);
  256. }
  257. _global.DefinePropertyOrThrow(name, desc);
  258. _global.Set(name, value, false);
  259. _varNames.Add(name);
  260. }
  261. internal override string[] GetAllBindingNames()
  262. {
  263. // JT: Rather than introduce a new method for the debugger, I'm reusing this one,
  264. // which - in spite of the very general name - is actually only used by the debugger
  265. // at this point.
  266. return _global.GetOwnProperties().Select(x => x.Key.ToString())
  267. .Concat(_declarativeRecord.GetAllBindingNames()).ToArray();
  268. }
  269. public override bool Equals(JsValue other)
  270. {
  271. return ReferenceEquals(_objectRecord, other);
  272. }
  273. }
  274. }