2
0

Intrinsics.cs 15 KB

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