Intrinsics.cs 14 KB

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