ArrayBufferPrototype.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. internal sealed class ArrayBufferPrototype : Prototype
  13. {
  14. private readonly ArrayBufferConstructor _constructor;
  15. internal ArrayBufferPrototype(
  16. Engine engine,
  17. ArrayBufferConstructor constructor,
  18. ObjectPrototype objectPrototype) : base(engine, engine.Realm)
  19. {
  20. _prototype = objectPrototype;
  21. _constructor = constructor;
  22. }
  23. protected override void Initialize()
  24. {
  25. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  26. var properties = new PropertyDictionary(4, checkExistingKeys: false)
  27. {
  28. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  29. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  30. ["detached"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get detached", Detached, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  31. ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  32. };
  33. SetProperties(properties);
  34. var symbols = new SymbolDictionary(1)
  35. {
  36. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable)
  37. };
  38. SetSymbols(symbols);
  39. }
  40. private JsValue Detached(JsValue thisObj, JsValue[] arguments)
  41. {
  42. var o = thisObj as ArrayBufferInstance;
  43. if (o is null)
  44. {
  45. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.detached called on incompatible receiver " + thisObj);
  46. }
  47. if (o.IsSharedArrayBuffer)
  48. {
  49. ExceptionHelper.ThrowTypeError(_realm);
  50. }
  51. return o.IsDetachedBuffer;
  52. }
  53. /// <summary>
  54. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
  55. /// </summary>
  56. private JsValue ByteLength(JsValue thisObj, JsValue[] arguments)
  57. {
  58. var o = thisObj as ArrayBufferInstance;
  59. if (o is null)
  60. {
  61. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.byteLength called on incompatible receiver " + thisObj);
  62. }
  63. if (o.IsSharedArrayBuffer)
  64. {
  65. ExceptionHelper.ThrowTypeError(_realm);
  66. }
  67. if (o.IsDetachedBuffer)
  68. {
  69. return JsNumber.PositiveZero;
  70. }
  71. return JsNumber.Create(o.ArrayBufferByteLength);
  72. }
  73. /// <summary>
  74. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
  75. /// </summary>
  76. private JsValue Slice(JsValue thisObj, JsValue[] arguments)
  77. {
  78. var o = thisObj as ArrayBufferInstance;
  79. if (o is null)
  80. {
  81. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.slice called on incompatible receiver " + thisObj);
  82. }
  83. if (o.IsSharedArrayBuffer)
  84. {
  85. ExceptionHelper.ThrowTypeError(_realm);
  86. }
  87. o.AssertNotDetached();
  88. var start = arguments.At(0);
  89. var end = arguments.At(1);
  90. var len = o.ArrayBufferByteLength;
  91. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  92. var first = relativeStart switch
  93. {
  94. double.NegativeInfinity => 0,
  95. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  96. _ => (int) System.Math.Min(relativeStart, len)
  97. };
  98. double relativeEnd;
  99. if (end.IsUndefined())
  100. {
  101. relativeEnd = len;
  102. }
  103. else
  104. {
  105. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  106. }
  107. var final = relativeEnd switch
  108. {
  109. double.NegativeInfinity => 0,
  110. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  111. _ => (int) System.Math.Min(relativeEnd, len)
  112. };
  113. var newLen = System.Math.Max(final - first, 0);
  114. var ctor = SpeciesConstructor(o, _realm.Intrinsics.ArrayBuffer);
  115. var bufferInstance = Construct(ctor, new JsValue[] { JsNumber.Create(newLen) }) as ArrayBufferInstance;
  116. if (bufferInstance is null)
  117. {
  118. ExceptionHelper.ThrowTypeError(_realm);
  119. }
  120. if (bufferInstance.IsSharedArrayBuffer)
  121. {
  122. ExceptionHelper.ThrowTypeError(_realm);
  123. }
  124. if (bufferInstance.IsDetachedBuffer)
  125. {
  126. ExceptionHelper.ThrowTypeError(_realm);
  127. }
  128. if (ReferenceEquals(bufferInstance, o))
  129. {
  130. ExceptionHelper.ThrowTypeError(_realm);
  131. }
  132. if (bufferInstance.ArrayBufferByteLength < newLen)
  133. {
  134. ExceptionHelper.ThrowTypeError(_realm);
  135. }
  136. // NOTE: Side-effects of the above steps may have detached O.
  137. if (bufferInstance.IsDetachedBuffer)
  138. {
  139. ExceptionHelper.ThrowTypeError(_realm);
  140. }
  141. var fromBuf = o.ArrayBufferData;
  142. var toBuf = bufferInstance.ArrayBufferData;
  143. System.Array.Copy(fromBuf!, first, toBuf!, 0, newLen);
  144. return bufferInstance;
  145. }
  146. }
  147. }