ArrayBufferPrototype.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.ArrayBuffer
  8. {
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-properties-of-the-arraybuffer-prototype-object
  11. /// </summary>
  12. public sealed class ArrayBufferPrototype : ObjectInstance
  13. {
  14. private readonly Realm _realm;
  15. private readonly ArrayBufferConstructor _constructor;
  16. internal ArrayBufferPrototype(
  17. Engine engine,
  18. Realm realm,
  19. ArrayBufferConstructor constructor,
  20. ObjectPrototype objectPrototype) : base(engine, 0)
  21. {
  22. _prototype = objectPrototype;
  23. _realm = realm;
  24. _constructor = constructor;
  25. }
  26. protected override void Initialize()
  27. {
  28. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  29. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  30. {
  31. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  32. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  33. ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  34. };
  35. SetProperties(properties);
  36. var symbols = new SymbolDictionary(1)
  37. {
  38. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable)
  39. };
  40. SetSymbols(symbols);
  41. }
  42. /// <summary>
  43. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
  44. /// </summary>
  45. private JsValue ByteLength(JsValue thisObj, JsValue[] arguments)
  46. {
  47. var o = thisObj as ArrayBufferInstance;
  48. if (o is null)
  49. {
  50. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.byteLength called on incompatible receiver " + thisObj);
  51. }
  52. if (o.IsSharedArrayBuffer)
  53. {
  54. ExceptionHelper.ThrowTypeError(_realm);
  55. }
  56. if (o.IsDetachedBuffer)
  57. {
  58. return JsNumber.PositiveZero;
  59. }
  60. return JsNumber.Create(o.ArrayBufferByteLength);
  61. }
  62. /// <summary>
  63. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
  64. /// </summary>
  65. private JsValue Slice(JsValue thisObj, JsValue[] arguments)
  66. {
  67. var o = thisObj as ArrayBufferInstance;
  68. if (o is null)
  69. {
  70. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.slice called on incompatible receiver " + thisObj);
  71. }
  72. if (o.IsSharedArrayBuffer)
  73. {
  74. ExceptionHelper.ThrowTypeError(_realm);
  75. }
  76. o.AssertNotDetached();
  77. var start = arguments.At(0);
  78. var end = arguments.At(1);
  79. var len = o.ArrayBufferByteLength;
  80. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  81. var first = relativeStart switch
  82. {
  83. double.NegativeInfinity => 0,
  84. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  85. _ => (int) System.Math.Min(relativeStart, len)
  86. };
  87. double relativeEnd;
  88. if (end.IsUndefined())
  89. {
  90. relativeEnd = len;
  91. }
  92. else
  93. {
  94. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  95. }
  96. var final = relativeEnd switch
  97. {
  98. double.NegativeInfinity => 0,
  99. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  100. _ => (int) System.Math.Min(relativeEnd, len)
  101. };
  102. var newLen = System.Math.Max(final - first, 0);
  103. var ctor = SpeciesConstructor(o, _realm.Intrinsics.ArrayBuffer);
  104. var bufferInstance = Construct(ctor, new JsValue[] { JsNumber.Create(newLen) }) as ArrayBufferInstance;
  105. if (bufferInstance is null)
  106. {
  107. ExceptionHelper.ThrowTypeError(_realm);
  108. }
  109. if (bufferInstance.IsSharedArrayBuffer)
  110. {
  111. ExceptionHelper.ThrowTypeError(_realm);
  112. }
  113. if (bufferInstance.IsDetachedBuffer)
  114. {
  115. ExceptionHelper.ThrowTypeError(_realm);
  116. }
  117. if (ReferenceEquals(bufferInstance, o))
  118. {
  119. ExceptionHelper.ThrowTypeError(_realm);
  120. }
  121. if (bufferInstance.ArrayBufferByteLength < newLen)
  122. {
  123. ExceptionHelper.ThrowTypeError(_realm);
  124. }
  125. // NOTE: Side-effects of the above steps may have detached O.
  126. if (bufferInstance.IsDetachedBuffer)
  127. {
  128. ExceptionHelper.ThrowTypeError(_realm);
  129. }
  130. var fromBuf = o.ArrayBufferData;
  131. var toBuf = bufferInstance.ArrayBufferData;
  132. System.Array.Copy(fromBuf, first, toBuf, 0, newLen);
  133. return bufferInstance;
  134. }
  135. /// <summary>
  136. /// https://tc39.es/ecma262/#sec-copydatablockbytes
  137. /// </summary>
  138. /// <remarks>
  139. /// Here only to support algorithm of shared view buffer.
  140. /// </remarks>
  141. private void CopyDataBlockBytes(byte[] toBlock, int toIndex, byte[] fromBlock, int fromIndex, int count)
  142. {
  143. var fromSize = Length;
  144. var toSize = Length;
  145. bool isSharedDataBlock = false;
  146. while (count > 0)
  147. {
  148. if (isSharedDataBlock)
  149. {
  150. /*
  151. i. Let execution be the [[CandidateExecution]] field of the surrounding agent's Agent Record.
  152. ii. Let eventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is AgentSignifier().
  153. iii. Let bytes be a List whose sole element is a nondeterministically chosen byte value.
  154. iv. NOTE: In implementations, bytes is the result of a non-atomic read instruction on the underlying hardware. The nondeterminism is a semantic prescription of the memory model to describe observable behaviour of hardware with weak consistency.
  155. v. Let readEvent be ReadSharedMemory { [[Order]]: Unordered, [[NoTear]]: true, [[Block]]: fromBlock, [[ByteIndex]]: fromIndex, [[ElementSize]]: 1 }.
  156. vi. Append readEvent to eventList.
  157. vii. Append Chosen Value Record { [[Event]]: readEvent, [[ChosenValue]]: bytes } to execution.[[ChosenValues]].
  158. viii. If toBlock is a Shared Data Block, then
  159. 1. Append WriteSharedMemory { [[Order]]: Unordered, [[NoTear]]: true, [[Block]]: toBlock, [[ByteIndex]]: toIndex, [[ElementSize]]: 1, [[Payload]]: bytes } to eventList.
  160. ix. Else,
  161. 1. Set toBlock[toIndex] to bytes[0].
  162. */
  163. ExceptionHelper.ThrowNotImplementedException();
  164. }
  165. else
  166. {
  167. toBlock[toIndex] = fromBlock[fromIndex];
  168. }
  169. toIndex++;
  170. fromIndex++;
  171. count--;
  172. }
  173. }
  174. }
  175. }