GlobalObject.Properties.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Jint.Collections;
  2. using Jint.Runtime.Descriptors;
  3. using Jint.Runtime.Descriptors.Specialized;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Global;
  6. public partial class GlobalObject
  7. {
  8. private static readonly Key propertyAggregateError = "AggregateError";
  9. private static readonly Key propertyArray = "Array";
  10. private static readonly Key propertyArrayBuffer = "ArrayBuffer";
  11. private static readonly Key propertyAtomics = "Atomics";
  12. private static readonly Key propertyBigInt = "BigInt";
  13. private static readonly Key propertyBigInt64Array = "BigInt64Array";
  14. private static readonly Key propertyBigUint64Array = "BigUint64Array";
  15. private static readonly Key propertyBoolean = "Boolean";
  16. private static readonly Key propertyDataView = "DataView";
  17. private static readonly Key propertyDate = "Date";
  18. private static readonly Key propertyError = "Error";
  19. private static readonly Key propertyEvalError = "EvalError";
  20. private static readonly Key propertyFinalizationRegistry = "FinalizationRegistry";
  21. private static readonly Key propertyFloat32Array = "Float32Array";
  22. private static readonly Key propertyFloat64Array = "Float64Array";
  23. private static readonly Key propertyFunction = "Function";
  24. private static readonly Key propertyGeneratorFunction = "Generator";
  25. private static readonly Key propertyInt16Array = "Int16Array";
  26. private static readonly Key propertyInt32Array = "Int32Array";
  27. private static readonly Key propertyInt8Array = "Int8Array";
  28. //private static readonly Key propertyIntl = "Intl";
  29. private static readonly Key propertyJSON = "JSON";
  30. private static readonly Key propertyMap = "Map";
  31. private static readonly Key propertyMath = "Math";
  32. private static readonly Key propertyNumber = "Number";
  33. private static readonly Key propertyObject = "Object";
  34. private static readonly Key propertyPromise = "Promise";
  35. private static readonly Key propertyProxy = "Proxy";
  36. private static readonly Key propertyRangeError = "RangeError";
  37. private static readonly Key propertyReferenceError = "ReferenceError";
  38. private static readonly Key propertyReflect = "Reflect";
  39. private static readonly Key propertyRegExp = "RegExp";
  40. private static readonly Key propertySet = "Set";
  41. private static readonly Key propertyShadowRealm = "ShadowRealm";
  42. private static readonly Key propertySharedArrayBuffer = "SharedArrayBuffer";
  43. private static readonly Key propertyString = "String";
  44. private static readonly Key propertySymbol = "Symbol";
  45. private static readonly Key propertySyntaxError = "SyntaxError";
  46. private static readonly Key propertyTypeError = "TypeError";
  47. private static readonly Key propertyTypedArray = "TypedArray";
  48. private static readonly Key propertyURIError = "URIError";
  49. private static readonly Key propertyUint16Array = "Uint16Array";
  50. private static readonly Key propertyUint32Array = "Uint32Array";
  51. private static readonly Key propertyUint8Array = "Uint8Array";
  52. private static readonly Key propertyUint8ClampedArray = "Uint8ClampedArray";
  53. private static readonly Key propertyWeakMap = "WeakMap";
  54. private static readonly Key propertyWeakRef = "WeakRef";
  55. private static readonly Key propertyWeakSet = "WeakSet";
  56. private static readonly Key propertyNaN = "NaN";
  57. private static readonly Key propertyInfinity = "Infinity";
  58. private static readonly Key propertyUndefined = "undefined";
  59. private static readonly Key propertyParseInt = "parseInt";
  60. private static readonly Key propertyParseFloat = "parseFloat";
  61. private static readonly Key propertyIsNaN = "isNaN";
  62. private static readonly Key propertyIsFinite = "isFinite";
  63. private static readonly Key propertyDecodeURI = "decodeURI";
  64. private static readonly Key propertyDecodeURIComponent = "decodeURIComponent";
  65. private static readonly Key propertyEncodeURI = "encodeURI";
  66. private static readonly Key propertyEncodeURIComponent = "encodeURIComponent";
  67. private static readonly Key propertyEscape = "escape";
  68. private static readonly Key propertyUnescape = "unescape";
  69. private static readonly Key propertyGlobalThis = "globalThis";
  70. private static readonly Key propertyEval = "eval";
  71. private static readonly Key propertyToString = "toString";
  72. private static readonly PropertyDescriptor _propertyDescriptorNan = new(JsNumber.DoubleNaN, PropertyFlag.AllForbidden);
  73. private static readonly PropertyDescriptor _propertyDescriptorPositiveInfinity = new(JsNumber.DoublePositiveInfinity, PropertyFlag.AllForbidden);
  74. private static readonly PropertyDescriptor _propertyDescriptorUndefined = new(Undefined, PropertyFlag.AllForbidden);
  75. protected override void Initialize()
  76. {
  77. const PropertyFlag LengthFlags = PropertyFlag.Configurable;
  78. const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  79. var properties = new StringDictionarySlim<PropertyDescriptor>(64);
  80. properties.AddDangerous(propertyAggregateError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.AggregateError, PropertyFlags));
  81. properties.AddDangerous(propertyArray, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Array, PropertyFlags));
  82. properties.AddDangerous(propertyArrayBuffer, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.ArrayBuffer, PropertyFlags));
  83. properties.AddDangerous(propertyAtomics, new LazyPropertyDescriptor(this, static state => Undefined, PropertyFlags));
  84. properties.AddDangerous(propertyBigInt, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.BigInt, PropertyFlags));
  85. properties.AddDangerous(propertyBigInt64Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.BigInt64Array, PropertyFlags));
  86. properties.AddDangerous(propertyBigUint64Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.BigUint64Array, PropertyFlags));
  87. properties.AddDangerous(propertyBoolean, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Boolean, PropertyFlags));
  88. properties.AddDangerous(propertyDataView, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.DataView, PropertyFlags));
  89. properties.AddDangerous(propertyDate, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Date, PropertyFlags));
  90. properties.AddDangerous(propertyError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Error, PropertyFlags));
  91. properties.AddDangerous(propertyEvalError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.EvalError, PropertyFlags));
  92. properties.AddDangerous(propertyFinalizationRegistry, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.FinalizationRegistry, PropertyFlags));
  93. properties.AddDangerous(propertyFloat32Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Float32Array, PropertyFlags));
  94. properties.AddDangerous(propertyFloat64Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Float64Array, PropertyFlags));
  95. properties.AddDangerous(propertyFunction, new PropertyDescriptor(_realm.Intrinsics.Function, PropertyFlags));
  96. properties.AddDangerous(propertyGeneratorFunction, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.GeneratorFunction, PropertyFlags));
  97. properties.AddDangerous(propertyInt16Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Int16Array, PropertyFlags));
  98. properties.AddDangerous(propertyInt32Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Int32Array, PropertyFlags));
  99. properties.AddDangerous(propertyInt8Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Int8Array, PropertyFlags));
  100. // TODO properties.AddDapropertygerous(propertyIntl, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Intl, propertyFlags));
  101. properties.AddDangerous(propertyJSON, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Json, PropertyFlags));
  102. properties.AddDangerous(propertyMap, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Map, PropertyFlags));
  103. properties.AddDangerous(propertyMath, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Math, PropertyFlags));
  104. properties.AddDangerous(propertyNumber, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Number, PropertyFlags));
  105. properties.AddDangerous(propertyObject, new PropertyDescriptor(_realm.Intrinsics.Object, PropertyFlags));
  106. properties.AddDangerous(propertyPromise, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Promise, PropertyFlags));
  107. properties.AddDangerous(propertyProxy, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Proxy, PropertyFlags));
  108. properties.AddDangerous(propertyRangeError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.RangeError, PropertyFlags));
  109. properties.AddDangerous(propertyReferenceError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.ReferenceError, PropertyFlags));
  110. properties.AddDangerous(propertyReflect, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Reflect, PropertyFlags));
  111. properties.AddDangerous(propertyRegExp, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.RegExp, PropertyFlags));
  112. properties.AddDangerous(propertySet, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Set, PropertyFlags));
  113. properties.AddDangerous(propertyShadowRealm, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.ShadowRealm, PropertyFlags));
  114. properties.AddDangerous(propertySharedArrayBuffer, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.SharedArrayBuffer, PropertyFlags));
  115. properties.AddDangerous(propertyString, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.String, PropertyFlags));
  116. properties.AddDangerous(propertySymbol, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Symbol, PropertyFlags));
  117. properties.AddDangerous(propertySyntaxError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.SyntaxError, PropertyFlags));
  118. properties.AddDangerous(propertyTypeError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.TypeError, PropertyFlags));
  119. properties.AddDangerous(propertyTypedArray, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.TypedArray, PropertyFlags));
  120. properties.AddDangerous(propertyURIError, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.UriError, PropertyFlags));
  121. properties.AddDangerous(propertyUint16Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Uint16Array, PropertyFlags));
  122. properties.AddDangerous(propertyUint32Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Uint32Array, PropertyFlags));
  123. properties.AddDangerous(propertyUint8Array, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Uint8Array, PropertyFlags));
  124. properties.AddDangerous(propertyUint8ClampedArray, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Uint8ClampedArray, PropertyFlags));
  125. properties.AddDangerous(propertyWeakMap, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.WeakMap, PropertyFlags));
  126. properties.AddDangerous(propertyWeakRef, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.WeakRef, PropertyFlags));
  127. properties.AddDangerous(propertyWeakSet, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.WeakSet, PropertyFlags));
  128. properties.AddDangerous(propertyNaN, _propertyDescriptorNan);
  129. properties.AddDangerous(propertyInfinity, _propertyDescriptorPositiveInfinity);
  130. properties.AddDangerous(propertyUndefined, _propertyDescriptorUndefined);
  131. properties.AddDangerous(propertyParseInt, new LazyPropertyDescriptor(this, static state => new ClrFunction(((GlobalObject) state!)._engine, "parseInt", ParseInt, 2, LengthFlags), PropertyFlags));
  132. properties.AddDangerous(propertyParseFloat, new LazyPropertyDescriptor(this, static state => new ClrFunction(((GlobalObject) state!)._engine, "parseFloat", ParseFloat, 1, LengthFlags), PropertyFlags));
  133. properties.AddDangerous(propertyIsNaN, new LazyPropertyDescriptor(this, static state => new ClrFunction(((GlobalObject) state!)._engine, "isNaN", IsNaN, 1, LengthFlags), PropertyFlags));
  134. properties.AddDangerous(propertyIsFinite, new LazyPropertyDescriptor(this, static state => new ClrFunction(((GlobalObject) state!)._engine, "isFinite", IsFinite, 1, LengthFlags), PropertyFlags));
  135. properties.AddDangerous(propertyDecodeURI, new LazyPropertyDescriptor(this, static state =>
  136. {
  137. var global = (GlobalObject) state!;
  138. return new ClrFunction(global._engine, "decodeURI", global.DecodeUri, 1, LengthFlags);
  139. }, PropertyFlags));
  140. properties.AddDangerous(propertyDecodeURIComponent, new LazyPropertyDescriptor(this, static state =>
  141. {
  142. var global = (GlobalObject) state!;
  143. return new ClrFunction(global._engine, "decodeURIComponent", global.DecodeUriComponent, 1, LengthFlags);
  144. }, PropertyFlags));
  145. properties.AddDangerous(propertyEncodeURI, new LazyPropertyDescriptor(this, static state =>
  146. {
  147. var global = (GlobalObject) state!;
  148. return new ClrFunction(global._engine, "encodeURI", global.EncodeUri, 1, LengthFlags);
  149. }, PropertyFlags));
  150. properties.AddDangerous(propertyEncodeURIComponent, new LazyPropertyDescriptor(this, static state =>
  151. {
  152. var global = (GlobalObject) state!;
  153. return new ClrFunction(global._engine, "encodeURIComponent", global.EncodeUriComponent, 1, LengthFlags);
  154. }, PropertyFlags));
  155. properties.AddDangerous(propertyEscape, new LazyPropertyDescriptor(this, static state =>
  156. {
  157. var global = (GlobalObject) state!;
  158. return new ClrFunction(global._engine, "escape", global.Escape, 1, LengthFlags);
  159. }, PropertyFlags));
  160. properties.AddDangerous(propertyUnescape, new LazyPropertyDescriptor(this, static state =>
  161. {
  162. var global = (GlobalObject) state!;
  163. return new ClrFunction(global._engine, "unescape", global.Unescape, 1, LengthFlags);
  164. }, PropertyFlags));
  165. properties.AddDangerous(propertyGlobalThis, new PropertyDescriptor(this, PropertyFlags));
  166. properties.AddDangerous(propertyEval, new LazyPropertyDescriptor(this, static state => ((GlobalObject) state!)._realm.Intrinsics.Eval, PropertyFlag.Configurable | PropertyFlag.Writable));
  167. // toString is not mentioned or actually required in spec, but some tests rely on it
  168. properties.AddDangerous(propertyToString, new LazyPropertyDescriptor(this, static state =>
  169. {
  170. var global = (GlobalObject) state!;
  171. return new ClrFunction(global._engine, "toString", global.ToStringString, 1);
  172. }, PropertyFlags));
  173. SetProperties(properties);
  174. }
  175. }