Intrinsics.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 IteratorPrototype _iteratorPrototype;
  54. private MathInstance _math;
  55. private JsonInstance _json;
  56. private SymbolConstructor _symbol;
  57. private RegExpConstructor _regExp;
  58. private RegExpStringIteratorPrototype _regExpStringIteratorPrototype;
  59. private NumberConstructor _number;
  60. private StringConstructor _string;
  61. private StringIteratorPrototype _stringIteratorPrototype;
  62. private MapConstructor _map;
  63. private MapIteratorPrototype _mapIteratorPrototype;
  64. private SetConstructor _set;
  65. private SetIteratorPrototype _setIteratorPrototype;
  66. private ArrayConstructor _array;
  67. private ArrayIteratorPrototype _arrayIteratorPrototype;
  68. private BooleanConstructor _boolean;
  69. private ArrayBufferConstructor _arrayBufferConstructor;
  70. private DataViewConstructor _dataView;
  71. private IntrinsicTypedArrayConstructor _typedArray;
  72. private Int8ArrayConstructor _int8Array;
  73. private Uint8ArrayConstructor _uint8Array;
  74. private Uint8ClampedArrayConstructor _uint8ClampedArray;
  75. private Int16ArrayConstructor _int16Array;
  76. private Uint16ArrayConstructor _uint16Array;
  77. private Int32ArrayConstructor _int32Array;
  78. private Uint32ArrayConstructor _uint32Array;
  79. private BigInt64ArrayConstructor _bigInt64Array;
  80. private BigUint64ArrayConstructor _bigUint64Array;
  81. private Float32ArrayConstructor _float32Array;
  82. private Float64ArrayConstructor _float64Array;
  83. internal Intrinsics(Engine engine, Realm realm)
  84. {
  85. _engine = engine;
  86. _realm = realm;
  87. // we need to transfer state currently to some initialization, would otherwise require quite the
  88. // ClrFunctionInstance constructor refactoring
  89. _engine._originalIntrinsics = this;
  90. Object = new ObjectConstructor(engine, realm);
  91. Function = new FunctionConstructor(engine, realm, Object.PrototypeObject);
  92. // this is implementation dependent, and only to pass some unit tests
  93. Object._prototype = Function.PrototypeObject;
  94. }
  95. public ObjectConstructor Object { get; }
  96. public FunctionConstructor Function { get; }
  97. public ArrayConstructor Array =>
  98. _array ??= new ArrayConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  99. internal ArrayIteratorPrototype ArrayIteratorPrototype =>
  100. _arrayIteratorPrototype ??= new ArrayIteratorPrototype(_engine, _realm, Object.PrototypeObject);
  101. public DataViewConstructor DataView =>
  102. _dataView ??= new DataViewConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  103. public ArrayBufferConstructor ArrayBuffer =>
  104. _arrayBufferConstructor ??= new ArrayBufferConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  105. internal IntrinsicTypedArrayConstructor TypedArray =>
  106. _typedArray ??= new IntrinsicTypedArrayConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject, "TypedArray");
  107. public Int8ArrayConstructor Int8Array =>
  108. _int8Array ??= new Int8ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  109. public Uint8ArrayConstructor Uint8Array =>
  110. _uint8Array ??= new Uint8ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  111. public Uint8ClampedArrayConstructor Uint8ClampedArray =>
  112. _uint8ClampedArray ??= new Uint8ClampedArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  113. public Int16ArrayConstructor Int16Array =>
  114. _int16Array ??= new Int16ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  115. public Uint16ArrayConstructor Uint16Array =>
  116. _uint16Array ??= new Uint16ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  117. public Int32ArrayConstructor Int32Array =>
  118. _int32Array ??= new Int32ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  119. public Uint32ArrayConstructor Uint32Array =>
  120. _uint32Array ??= new Uint32ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  121. public BigInt64ArrayConstructor BigInt64Array =>
  122. _bigInt64Array ??= new BigInt64ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  123. public BigUint64ArrayConstructor BigUint64Array =>
  124. _bigUint64Array ??= new BigUint64ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  125. public Float32ArrayConstructor Float32Array =>
  126. _float32Array ??= new Float32ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  127. public Float64ArrayConstructor Float64Array =>
  128. _float64Array ??= new Float64ArrayConstructor(_engine, _realm, TypedArray, TypedArray.PrototypeObject);
  129. public MapConstructor Map =>
  130. _map ??= new MapConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  131. internal MapIteratorPrototype MapIteratorPrototype =>
  132. _mapIteratorPrototype ??= new MapIteratorPrototype(_engine, _realm, Object.PrototypeObject);
  133. public SetConstructor Set =>
  134. _set ??= new SetConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  135. internal SetIteratorPrototype SetIteratorPrototype =>
  136. _setIteratorPrototype ??= new SetIteratorPrototype(_engine, _realm, Object.PrototypeObject);
  137. public WeakMapConstructor WeakMap =>
  138. _weakMap ??= new WeakMapConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  139. public WeakSetConstructor WeakSet =>
  140. _weakSet ??= new WeakSetConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  141. public PromiseConstructor Promise =>
  142. _promise ??= new PromiseConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  143. internal IteratorPrototype IteratorPrototype =>
  144. _iteratorPrototype ??= new IteratorPrototype(_engine, _realm, null, Object.PrototypeObject);
  145. public StringConstructor String =>
  146. _string ??= new StringConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  147. internal StringIteratorPrototype StringIteratorPrototype =>
  148. _stringIteratorPrototype ??= new StringIteratorPrototype(_engine, _realm, Object.PrototypeObject);
  149. public RegExpConstructor RegExp =>
  150. _regExp ??= new RegExpConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  151. internal RegExpStringIteratorPrototype RegExpStringIteratorPrototype =>
  152. _regExpStringIteratorPrototype ??= new RegExpStringIteratorPrototype(_engine, _realm, Object.PrototypeObject);
  153. public BooleanConstructor Boolean =>
  154. _boolean ??= new BooleanConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  155. public NumberConstructor Number =>
  156. _number ??= new NumberConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  157. public DateConstructor Date =>
  158. _date ??= new DateConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  159. public MathInstance Math =>
  160. _math ??= new MathInstance(_engine, Object.PrototypeObject);
  161. public JsonInstance Json =>
  162. _json ??= new JsonInstance(_engine, _realm, Object.PrototypeObject);
  163. public ProxyConstructor Proxy =>
  164. _proxy ??= new ProxyConstructor(_engine, _realm);
  165. public ReflectInstance Reflect =>
  166. _reflect ??= new ReflectInstance(_engine, _realm, Object.PrototypeObject);
  167. public SymbolConstructor Symbol =>
  168. _symbol ??= new SymbolConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject);
  169. public EvalFunctionInstance Eval =>
  170. _eval ??= new EvalFunctionInstance(_engine, _realm, Function.PrototypeObject);
  171. public ErrorConstructor Error =>
  172. _error ??= new ErrorConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject, _errorFunctionName);
  173. public ErrorConstructor EvalError =>
  174. _evalError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _evalErrorFunctionName);
  175. public ErrorConstructor SyntaxError =>
  176. _syntaxError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _syntaxErrorFunctionName);
  177. public ErrorConstructor TypeError =>
  178. _typeError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _typeErrorFunctionName);
  179. public ErrorConstructor RangeError =>
  180. _rangeError ??=
  181. new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _rangeErrorFunctionName);
  182. public ErrorConstructor ReferenceError
  183. => _referenceError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _referenceErrorFunctionName);
  184. public ErrorConstructor UriError =>
  185. _uriError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _uriErrorFunctionName);
  186. }
  187. }