Intrinsics.cs 14 KB

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