Intrinsics.cs 14 KB

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