Intrinsics.cs 10 KB

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