Intrinsics.cs 13 KB

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