GlobalEnvironmentRecord.cs 10 KB

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