Intrinsics.cs 12 KB

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