Intrinsics.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using Jint.Native;
  2. using Jint.Native.AggregateError;
  3. using Jint.Native.Array;
  4. using Jint.Native.ArrayBuffer;
  5. using Jint.Native.AsyncFunction;
  6. using Jint.Native.Atomics;
  7. using Jint.Native.BigInt;
  8. using Jint.Native.Boolean;
  9. using Jint.Native.DataView;
  10. using Jint.Native.Date;
  11. using Jint.Native.Disposable;
  12. using Jint.Native.Error;
  13. using Jint.Native.FinalizationRegistry;
  14. using Jint.Native.Function;
  15. using Jint.Native.Generator;
  16. using Jint.Native.Iterator;
  17. using Jint.Native.Json;
  18. using Jint.Native.Map;
  19. using Jint.Native.Math;
  20. using Jint.Native.Number;
  21. using Jint.Native.Object;
  22. using Jint.Native.Promise;
  23. using Jint.Native.Proxy;
  24. using Jint.Native.Reflect;
  25. using Jint.Native.RegExp;
  26. using Jint.Native.Set;
  27. using Jint.Native.ShadowRealm;
  28. using Jint.Native.SharedArrayBuffer;
  29. using Jint.Native.String;
  30. using Jint.Native.SuppressedError;
  31. using Jint.Native.Symbol;
  32. using Jint.Native.TypedArray;
  33. using Jint.Native.WeakMap;
  34. using Jint.Native.WeakRef;
  35. using Jint.Native.WeakSet;
  36. namespace Jint.Runtime;
  37. public sealed partial class Intrinsics
  38. {
  39. private static readonly JsString _errorFunctionName = new("Error");
  40. private static readonly JsString _evalErrorFunctionName = new("EvalError");
  41. private static readonly JsString _rangeErrorFunctionName = new("RangeError");
  42. private static readonly JsString _referenceErrorFunctionName = new("ReferenceError");
  43. private static readonly JsString _syntaxErrorFunctionName = new("SyntaxError");
  44. private static readonly JsString _typeErrorFunctionName = new("TypeError");
  45. private static readonly JsString _uriErrorFunctionName = new("URIError");
  46. private readonly Engine _engine;
  47. private readonly Realm _realm;
  48. // lazy properties
  49. private ThrowTypeError? _throwTypeError;
  50. private AggregateErrorConstructor? _aggregateError;
  51. private SuppressedErrorConstructor? _suppressedError;
  52. private ErrorConstructor? _error;
  53. private ErrorConstructor? _evalError;
  54. private ErrorConstructor? _rangeError;
  55. private ErrorConstructor? _referenceError;
  56. private ErrorConstructor? _syntaxError;
  57. private ErrorConstructor? _typeError;
  58. private ErrorConstructor? _uriError;
  59. private WeakMapConstructor? _weakMap;
  60. private WeakSetConstructor? _weakSet;
  61. private WeakRefConstructor? _weakRef;
  62. private PromiseConstructor? _promise;
  63. private ProxyConstructor? _proxy;
  64. private ReflectInstance? _reflect;
  65. private EvalFunction? _eval;
  66. private DateConstructor? _date;
  67. private IteratorPrototype? _iteratorPrototype;
  68. private MathInstance? _math;
  69. private JsonInstance? _json;
  70. private SymbolConstructor? _symbol;
  71. private AsyncGeneratorFunctionConstructor? _asyncGeneratorFunction;
  72. private GeneratorFunctionConstructor? _generatorFunction;
  73. private RegExpConstructor? _regExp;
  74. private RegExpStringIteratorPrototype? _regExpStringIteratorPrototype;
  75. private NumberConstructor? _number;
  76. private BigIntConstructor? _bigInt;
  77. private StringConstructor? _string;
  78. private StringIteratorPrototype? _stringIteratorPrototype;
  79. private MapConstructor? _map;
  80. private MapIteratorPrototype? _mapIteratorPrototype;
  81. private SetConstructor? _set;
  82. private SetIteratorPrototype? _setIteratorPrototype;
  83. private ArrayConstructor? _array;
  84. private ArrayIteratorPrototype? _arrayIteratorPrototype;
  85. private AtomicsInstance? _atomics;
  86. private BooleanConstructor? _boolean;
  87. private ArrayBufferConstructor? _arrayBufferConstructor;
  88. private SharedArrayBufferConstructor? _sharedArrayBufferConstructor;
  89. private DataViewConstructor? _dataView;
  90. private AsyncFunctionConstructor? _asyncFunction;
  91. private FinalizationRegistryConstructor? _finalizationRegistry;
  92. private IntrinsicTypedArrayConstructor? _typedArray;
  93. private Int8ArrayConstructor? _int8Array;
  94. private Uint8ArrayConstructor? _uint8Array;
  95. private Uint8ClampedArrayConstructor? _uint8ClampedArray;
  96. private Int16ArrayConstructor? _int16Array;
  97. private Uint16ArrayConstructor? _uint16Array;
  98. private Int32ArrayConstructor? _int32Array;
  99. private Uint32ArrayConstructor? _uint32Array;
  100. private BigInt64ArrayConstructor? _bigInt64Array;
  101. private BigUint64ArrayConstructor? _bigUint64Array;
  102. private Float16ArrayConstructor? _float16Array;
  103. private Float32ArrayConstructor? _float32Array;
  104. private Float64ArrayConstructor? _float64Array;
  105. private ShadowRealmConstructor? _shadowRealm;
  106. private AsyncDisposableStackConstructor? _asyncDisposableStack;
  107. private DisposableStackConstructor? _disposableStack;
  108. internal Intrinsics(Engine engine, Realm realm)
  109. {
  110. _engine = engine;
  111. _realm = realm;
  112. // we need to transfer state currently to some initialization, would otherwise require quite the
  113. // ClrFunctionInstance constructor refactoring
  114. _engine._originalIntrinsics = this;
  115. Object = new ObjectConstructor(engine, realm);
  116. Function = new FunctionConstructor(engine, realm, Object.PrototypeObject);
  117. // this is implementation dependent, and only to pass some unit tests
  118. Object._prototype = Function.PrototypeObject;
  119. }
  120. public ObjectConstructor Object { get; }
  121. public FunctionConstructor Function { get; }
  122. internal FinalizationRegistryConstructor FinalizationRegistry =>
  123. _finalizationRegistry ??= new FinalizationRegistryConstructor(_engine, _realm, Function, Object.PrototypeObject);
  124. internal AsyncFunctionConstructor AsyncFunction =>
  125. _asyncFunction ??= new AsyncFunctionConstructor(_engine, _realm, Function);
  126. public ArrayConstructor Array =>
  127. _array ??= new ArrayConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  128. internal AtomicsInstance Atomics =>
  129. _atomics ??= new AtomicsInstance(_engine, _realm, Object.PrototypeObject);
  130. internal AggregateErrorConstructor AggregateError =>
  131. _aggregateError ??= new AggregateErrorConstructor(_engine, _realm, Error);
  132. internal SuppressedErrorConstructor SuppressedError =>
  133. _suppressedError ??= new SuppressedErrorConstructor(_engine, _realm, Error);
  134. internal ArrayIteratorPrototype ArrayIteratorPrototype =>
  135. _arrayIteratorPrototype ??= new ArrayIteratorPrototype(_engine, _realm, this.IteratorPrototype);
  136. internal DataViewConstructor DataView =>
  137. _dataView ??= new DataViewConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  138. public ArrayBufferConstructor ArrayBuffer =>
  139. _arrayBufferConstructor ??= new ArrayBufferConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  140. internal SharedArrayBufferConstructor SharedArrayBuffer =>
  141. _sharedArrayBufferConstructor ??= new SharedArrayBufferConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  142. internal IntrinsicTypedArrayConstructor TypedArray =>
  143. _typedArray ??= new IntrinsicTypedArrayConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject, "TypedArray");
  144. public Int8ArrayConstructor Int8Array =>
  145. _int8Array ??= new Int8ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  146. public Uint8ArrayConstructor Uint8Array =>
  147. _uint8Array ??= new Uint8ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  148. public Uint8ClampedArrayConstructor Uint8ClampedArray =>
  149. _uint8ClampedArray ??= new Uint8ClampedArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  150. public Int16ArrayConstructor Int16Array =>
  151. _int16Array ??= new Int16ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  152. public Uint16ArrayConstructor Uint16Array =>
  153. _uint16Array ??= new Uint16ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  154. public Int32ArrayConstructor Int32Array =>
  155. _int32Array ??= new Int32ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  156. public Uint32ArrayConstructor Uint32Array =>
  157. _uint32Array ??= new Uint32ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  158. public BigInt64ArrayConstructor BigInt64Array =>
  159. _bigInt64Array ??= new BigInt64ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  160. public BigUint64ArrayConstructor BigUint64Array =>
  161. _bigUint64Array ??= new BigUint64ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  162. public Float16ArrayConstructor Float16Array =>
  163. _float16Array ??= new Float16ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  164. public Float32ArrayConstructor Float32Array =>
  165. _float32Array ??= new Float32ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  166. public Float64ArrayConstructor Float64Array =>
  167. _float64Array ??= new Float64ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  168. public MapConstructor Map =>
  169. _map ??= new MapConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  170. internal MapIteratorPrototype MapIteratorPrototype =>
  171. _mapIteratorPrototype ??= new MapIteratorPrototype(_engine, _realm, IteratorPrototype);
  172. public SetConstructor Set =>
  173. _set ??= new SetConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  174. internal SetIteratorPrototype SetIteratorPrototype =>
  175. _setIteratorPrototype ??= new SetIteratorPrototype(_engine, _realm, IteratorPrototype);
  176. internal WeakMapConstructor WeakMap =>
  177. _weakMap ??= new WeakMapConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  178. internal WeakSetConstructor WeakSet =>
  179. _weakSet ??= new WeakSetConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  180. internal WeakRefConstructor WeakRef =>
  181. _weakRef ??= new WeakRefConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  182. internal PromiseConstructor Promise =>
  183. _promise ??= new PromiseConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  184. internal IteratorPrototype IteratorPrototype =>
  185. _iteratorPrototype ??= new IteratorPrototype(_engine, _realm, Object.PrototypeObject);
  186. internal StringConstructor String =>
  187. _string ??= new StringConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  188. internal StringIteratorPrototype StringIteratorPrototype =>
  189. _stringIteratorPrototype ??= new StringIteratorPrototype(_engine, _realm, IteratorPrototype);
  190. public RegExpConstructor RegExp =>
  191. _regExp ??= new RegExpConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  192. internal RegExpStringIteratorPrototype RegExpStringIteratorPrototype =>
  193. _regExpStringIteratorPrototype ??= new RegExpStringIteratorPrototype(_engine, _realm, IteratorPrototype);
  194. internal BooleanConstructor Boolean =>
  195. _boolean ??= new BooleanConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  196. internal NumberConstructor Number =>
  197. _number ??= new NumberConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  198. internal BigIntConstructor BigInt =>
  199. _bigInt ??= new BigIntConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  200. internal DateConstructor Date =>
  201. _date ??= new DateConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  202. internal MathInstance Math =>
  203. _math ??= new MathInstance(_engine, Object.PrototypeObject);
  204. internal JsonInstance Json =>
  205. _json ??= new JsonInstance(_engine, _realm, Object.PrototypeObject);
  206. internal ProxyConstructor Proxy =>
  207. _proxy ??= new ProxyConstructor(_engine, _realm);
  208. internal ReflectInstance Reflect =>
  209. _reflect ??= new ReflectInstance(_engine, _realm, Object.PrototypeObject);
  210. internal SymbolConstructor Symbol =>
  211. _symbol ??= new SymbolConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  212. public ShadowRealmConstructor ShadowRealm =>
  213. _shadowRealm ??= new ShadowRealmConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  214. internal GeneratorFunctionConstructor GeneratorFunction =>
  215. _generatorFunction ??= new GeneratorFunctionConstructor(_engine, _realm, Function.PrototypeObject, IteratorPrototype);
  216. internal AsyncGeneratorFunctionConstructor AsyncGeneratorFunction =>
  217. _asyncGeneratorFunction ??= new AsyncGeneratorFunctionConstructor(_engine, _realm, AsyncFunction.PrototypeObject, IteratorPrototype);
  218. public EvalFunction Eval =>
  219. _eval ??= new EvalFunction(_engine, _realm, Function.PrototypeObject);
  220. public ErrorConstructor Error =>
  221. _error ??= new ErrorConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject, _errorFunctionName, static intrinsics => intrinsics.Error.PrototypeObject);
  222. internal ErrorConstructor EvalError =>
  223. _evalError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _evalErrorFunctionName, static intrinsics => intrinsics.EvalError.PrototypeObject);
  224. internal ErrorConstructor SyntaxError =>
  225. _syntaxError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _syntaxErrorFunctionName, static intrinsics => intrinsics.SyntaxError.PrototypeObject);
  226. public ErrorConstructor TypeError =>
  227. _typeError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _typeErrorFunctionName, static intrinsics => intrinsics.TypeError.PrototypeObject);
  228. internal ErrorConstructor RangeError =>
  229. _rangeError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _rangeErrorFunctionName, static intrinsics => intrinsics.RangeError.PrototypeObject);
  230. internal ErrorConstructor ReferenceError =>
  231. _referenceError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _referenceErrorFunctionName, static intrinsics => intrinsics.ReferenceError.PrototypeObject);
  232. internal ErrorConstructor UriError =>
  233. _uriError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _uriErrorFunctionName, static intrinsics => intrinsics.UriError.PrototypeObject);
  234. internal ThrowTypeError ThrowTypeError =>
  235. _throwTypeError ??= new ThrowTypeError(_engine, _engine.Realm) { _prototype = _engine.Realm.Intrinsics.Function.PrototypeObject };
  236. internal AsyncDisposableStackConstructor AsyncDisposableStack =>
  237. _asyncDisposableStack ??= new AsyncDisposableStackConstructor(_engine, _engine.Realm) { _prototype = _engine.Realm.Intrinsics.Function.PrototypeObject };
  238. internal DisposableStackConstructor DisposableStack =>
  239. _disposableStack ??= new DisposableStackConstructor(_engine, _engine.Realm) { _prototype = _engine.Realm.Intrinsics.Function.PrototypeObject };
  240. }