GlobalObject.Properties.cs 17 KB

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