ArrayBufferPrototype.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(3, 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. ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  31. };
  32. SetProperties(properties);
  33. var symbols = new SymbolDictionary(1)
  34. {
  35. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable)
  36. };
  37. SetSymbols(symbols);
  38. }
  39. /// <summary>
  40. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
  41. /// </summary>
  42. private JsValue ByteLength(JsValue thisObj, JsValue[] arguments)
  43. {
  44. var o = thisObj as ArrayBufferInstance;
  45. if (o is null)
  46. {
  47. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.byteLength called on incompatible receiver " + thisObj);
  48. }
  49. if (o.IsSharedArrayBuffer)
  50. {
  51. ExceptionHelper.ThrowTypeError(_realm);
  52. }
  53. if (o.IsDetachedBuffer)
  54. {
  55. return JsNumber.PositiveZero;
  56. }
  57. return JsNumber.Create(o.ArrayBufferByteLength);
  58. }
  59. /// <summary>
  60. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
  61. /// </summary>
  62. private JsValue Slice(JsValue thisObj, JsValue[] arguments)
  63. {
  64. var o = thisObj as ArrayBufferInstance;
  65. if (o is null)
  66. {
  67. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.slice called on incompatible receiver " + thisObj);
  68. }
  69. if (o.IsSharedArrayBuffer)
  70. {
  71. ExceptionHelper.ThrowTypeError(_realm);
  72. }
  73. o.AssertNotDetached();
  74. var start = arguments.At(0);
  75. var end = arguments.At(1);
  76. var len = o.ArrayBufferByteLength;
  77. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  78. var first = relativeStart switch
  79. {
  80. double.NegativeInfinity => 0,
  81. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  82. _ => (int) System.Math.Min(relativeStart, len)
  83. };
  84. double relativeEnd;
  85. if (end.IsUndefined())
  86. {
  87. relativeEnd = len;
  88. }
  89. else
  90. {
  91. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  92. }
  93. var final = relativeEnd switch
  94. {
  95. double.NegativeInfinity => 0,
  96. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  97. _ => (int) System.Math.Min(relativeEnd, len)
  98. };
  99. var newLen = System.Math.Max(final - first, 0);
  100. var ctor = SpeciesConstructor(o, _realm.Intrinsics.ArrayBuffer);
  101. var bufferInstance = Construct(ctor, new JsValue[] { JsNumber.Create(newLen) }) as ArrayBufferInstance;
  102. if (bufferInstance is null)
  103. {
  104. ExceptionHelper.ThrowTypeError(_realm);
  105. }
  106. if (bufferInstance.IsSharedArrayBuffer)
  107. {
  108. ExceptionHelper.ThrowTypeError(_realm);
  109. }
  110. if (bufferInstance.IsDetachedBuffer)
  111. {
  112. ExceptionHelper.ThrowTypeError(_realm);
  113. }
  114. if (ReferenceEquals(bufferInstance, o))
  115. {
  116. ExceptionHelper.ThrowTypeError(_realm);
  117. }
  118. if (bufferInstance.ArrayBufferByteLength < newLen)
  119. {
  120. ExceptionHelper.ThrowTypeError(_realm);
  121. }
  122. // NOTE: Side-effects of the above steps may have detached O.
  123. if (bufferInstance.IsDetachedBuffer)
  124. {
  125. ExceptionHelper.ThrowTypeError(_realm);
  126. }
  127. var fromBuf = o.ArrayBufferData;
  128. var toBuf = bufferInstance.ArrayBufferData;
  129. System.Array.Copy(fromBuf, first, toBuf, 0, newLen);
  130. return bufferInstance;
  131. }
  132. }
  133. }