SharedArrayBufferPrototype.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using Jint.Collections;
  2. using Jint.Native.Object;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.SharedArrayBuffer;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-properties-of-the-sharedarraybuffer-prototype-object
  10. /// </summary>
  11. internal sealed class SharedArrayBufferPrototype : Prototype
  12. {
  13. private readonly SharedArrayBufferConstructor _constructor;
  14. internal SharedArrayBufferPrototype(
  15. Engine engine,
  16. SharedArrayBufferConstructor constructor,
  17. ObjectPrototype objectPrototype) : base(engine, engine.Realm)
  18. {
  19. _prototype = objectPrototype;
  20. _constructor = constructor;
  21. }
  22. protected override void Initialize()
  23. {
  24. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  25. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  26. {
  27. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  28. [KnownKeys.Constructor] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  29. ["growable"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get growable", Growable, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  30. ["grow"] = new PropertyDescriptor(new ClrFunction(_engine, "grow", Grow, 1, lengthFlags), PropertyFlag.NonEnumerable),
  31. ["maxByteLength"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get maxByteLength", MaxByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  32. ["slice"] = new PropertyDescriptor(new ClrFunction(Engine, "slice", Slice, 2, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  33. };
  34. SetProperties(properties);
  35. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("SharedArrayBuffer", PropertyFlag.Configurable) };
  36. SetSymbols(symbols);
  37. }
  38. /// <summary>
  39. /// https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.bytelength
  40. /// </summary>
  41. private JsNumber ByteLength(JsValue thisObj, JsCallArguments arguments)
  42. {
  43. var o = thisObj as JsSharedArrayBuffer;
  44. if (o is null || !o.IsSharedArrayBuffer)
  45. {
  46. ExceptionHelper.ThrowTypeError(_realm, "Method prototype.byteLength called on incompatible receiver " + thisObj);
  47. }
  48. return JsNumber.Create(o.ArrayBufferByteLength);
  49. }
  50. /// <summary>
  51. /// https://tc39.es/ecma262/#sec-sharedarraybuffer.prototype.slice
  52. /// </summary>
  53. private JsSharedArrayBuffer Slice(JsValue thisObj, JsCallArguments arguments)
  54. {
  55. var o = thisObj as JsSharedArrayBuffer;
  56. if (o is null || !o.IsSharedArrayBuffer)
  57. {
  58. ExceptionHelper.ThrowTypeError(_realm, "Method prototype.slice called on incompatible receiver " + thisObj);
  59. }
  60. o.AssertNotDetached();
  61. var start = arguments.At(0);
  62. var end = arguments.At(1);
  63. var len = o.ArrayBufferByteLength;
  64. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  65. var first = relativeStart switch
  66. {
  67. double.NegativeInfinity => 0,
  68. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  69. _ => (int) System.Math.Min(relativeStart, len)
  70. };
  71. double relativeEnd;
  72. if (end.IsUndefined())
  73. {
  74. relativeEnd = len;
  75. }
  76. else
  77. {
  78. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  79. }
  80. var final = relativeEnd switch
  81. {
  82. double.NegativeInfinity => 0,
  83. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  84. _ => (int) System.Math.Min(relativeEnd, len)
  85. };
  86. var newLen = System.Math.Max(final - first, 0);
  87. var ctor = SpeciesConstructor(o, _realm.Intrinsics.SharedArrayBuffer);
  88. var bufferInstance = Construct(ctor, [JsNumber.Create(newLen)]) as JsSharedArrayBuffer;
  89. if (bufferInstance is null)
  90. {
  91. ExceptionHelper.ThrowTypeError(_realm);
  92. }
  93. if (!bufferInstance.IsSharedArrayBuffer)
  94. {
  95. ExceptionHelper.ThrowTypeError(_realm);
  96. }
  97. if (bufferInstance.IsDetachedBuffer)
  98. {
  99. ExceptionHelper.ThrowTypeError(_realm);
  100. }
  101. if (ReferenceEquals(bufferInstance, o))
  102. {
  103. ExceptionHelper.ThrowTypeError(_realm);
  104. }
  105. if (bufferInstance.ArrayBufferByteLength < newLen)
  106. {
  107. ExceptionHelper.ThrowTypeError(_realm);
  108. }
  109. // NOTE: Side-effects of the above steps may have detached O.
  110. if (bufferInstance.IsDetachedBuffer)
  111. {
  112. ExceptionHelper.ThrowTypeError(_realm);
  113. }
  114. var fromBuf = o.ArrayBufferData!;
  115. var toBuf = bufferInstance.ArrayBufferData!;
  116. System.Array.Copy(fromBuf, first, toBuf, 0, newLen);
  117. return bufferInstance;
  118. }
  119. /// <summary>
  120. /// https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.growable
  121. /// </summary>
  122. private JsValue Growable(JsValue thisObject, JsCallArguments arguments)
  123. {
  124. var o = thisObject as JsSharedArrayBuffer;
  125. if (o is null || !o.IsSharedArrayBuffer)
  126. {
  127. ExceptionHelper.ThrowTypeError(_realm, "Method SharedArrayBuffer.prototype.growable called on incompatible receiver " + thisObject);
  128. }
  129. return !o.IsFixedLengthArrayBuffer;
  130. }
  131. /// <summary>
  132. /// https://tc39.es/ecma262/#sec-sharedarraybuffer.prototype.grow
  133. /// </summary>
  134. private JsValue Grow(JsValue thisObject, JsCallArguments arguments)
  135. {
  136. var o = thisObject as JsSharedArrayBuffer;
  137. if (o is null || !o.IsSharedArrayBuffer)
  138. {
  139. ExceptionHelper.ThrowTypeError(_realm, "Method SharedArrayBuffer.prototype.grow called on incompatible receiver " + thisObject);
  140. }
  141. var newLength = arguments.At(0);
  142. var newByteLength = TypeConverter.ToIndex(_realm, newLength);
  143. o.AssertNotDetached();
  144. o.Resize(newByteLength);
  145. return Undefined;
  146. }
  147. /// <summary>
  148. /// https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.maxbytelength
  149. /// </summary>
  150. private JsValue MaxByteLength(JsValue thisObject, JsCallArguments arguments)
  151. {
  152. var o = thisObject as JsSharedArrayBuffer;
  153. if (o is null || !o.IsSharedArrayBuffer)
  154. {
  155. ExceptionHelper.ThrowTypeError(_realm, "Method SharedArrayBuffer.prototype.maxByteLength called on incompatible receiver " + thisObject);
  156. }
  157. if (o.IsDetachedBuffer)
  158. {
  159. return JsNumber.PositiveZero;
  160. }
  161. long length = o.IsFixedLengthArrayBuffer
  162. ? o.ArrayBufferByteLength
  163. : o._arrayBufferMaxByteLength.GetValueOrDefault();
  164. return length;
  165. }
  166. }