Intrinsics.cs 13 KB

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