|
1 | 1 | import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
|
2 |
| -import { E_INDEXOUTOFRANGE } from "./util/error"; |
| 2 | +import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH } from "./util/error"; |
3 | 3 | import { idof } from "./builtins";
|
4 | 4 | import { ArrayBufferView } from "./arraybuffer";
|
5 | 5 |
|
@@ -95,6 +95,10 @@ export class Int8Array extends ArrayBufferView {
|
95 | 95 | reverse(): this {
|
96 | 96 | return REVERSE<this, i8>(this);
|
97 | 97 | }
|
| 98 | + |
| 99 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Int8Array { |
| 100 | + return WRAP<Int8Array, i8>(buffer, byteOffset, length); |
| 101 | + } |
98 | 102 | }
|
99 | 103 |
|
100 | 104 | export class Uint8Array extends ArrayBufferView {
|
@@ -189,6 +193,10 @@ export class Uint8Array extends ArrayBufferView {
|
189 | 193 | reverse(): this {
|
190 | 194 | return REVERSE<this, u8>(this);
|
191 | 195 | }
|
| 196 | + |
| 197 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Uint8Array { |
| 198 | + return WRAP<Uint8Array, u8>(buffer, byteOffset, length); |
| 199 | + } |
192 | 200 | }
|
193 | 201 |
|
194 | 202 | export class Uint8ClampedArray extends ArrayBufferView {
|
@@ -283,6 +291,10 @@ export class Uint8ClampedArray extends ArrayBufferView {
|
283 | 291 | reverse(): this {
|
284 | 292 | return REVERSE<this, u8>(this);
|
285 | 293 | }
|
| 294 | + |
| 295 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Uint8ClampedArray { |
| 296 | + return WRAP<Uint8ClampedArray, u8>(buffer, byteOffset, length); |
| 297 | + } |
286 | 298 | }
|
287 | 299 |
|
288 | 300 | export class Int16Array extends ArrayBufferView {
|
@@ -377,6 +389,10 @@ export class Int16Array extends ArrayBufferView {
|
377 | 389 | reverse(): this {
|
378 | 390 | return REVERSE<this, i16>(this);
|
379 | 391 | }
|
| 392 | + |
| 393 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Int16Array { |
| 394 | + return WRAP<Int16Array, i16>(buffer, byteOffset, length); |
| 395 | + } |
380 | 396 | }
|
381 | 397 |
|
382 | 398 | export class Uint16Array extends ArrayBufferView {
|
@@ -471,6 +487,10 @@ export class Uint16Array extends ArrayBufferView {
|
471 | 487 | reverse(): this {
|
472 | 488 | return REVERSE<this, u16>(this);
|
473 | 489 | }
|
| 490 | + |
| 491 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Uint16Array { |
| 492 | + return WRAP<Uint16Array, u16>(buffer, byteOffset, length); |
| 493 | + } |
474 | 494 | }
|
475 | 495 |
|
476 | 496 | export class Int32Array extends ArrayBufferView {
|
@@ -565,6 +585,10 @@ export class Int32Array extends ArrayBufferView {
|
565 | 585 | reverse(): this {
|
566 | 586 | return REVERSE<this, i32>(this);
|
567 | 587 | }
|
| 588 | + |
| 589 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Int32Array { |
| 590 | + return WRAP<Int32Array, i32>(buffer, byteOffset, length); |
| 591 | + } |
568 | 592 | }
|
569 | 593 |
|
570 | 594 | export class Uint32Array extends ArrayBufferView {
|
@@ -659,6 +683,10 @@ export class Uint32Array extends ArrayBufferView {
|
659 | 683 | reverse(): this {
|
660 | 684 | return REVERSE<this, u32>(this);
|
661 | 685 | }
|
| 686 | + |
| 687 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Uint32Array { |
| 688 | + return WRAP<Uint32Array, u32>(buffer, byteOffset, length); |
| 689 | + } |
662 | 690 | }
|
663 | 691 |
|
664 | 692 | export class Int64Array extends ArrayBufferView {
|
@@ -753,6 +781,10 @@ export class Int64Array extends ArrayBufferView {
|
753 | 781 | reverse(): this {
|
754 | 782 | return REVERSE<this, i64>(this);
|
755 | 783 | }
|
| 784 | + |
| 785 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Int64Array { |
| 786 | + return WRAP<Int64Array, i64>(buffer, byteOffset, length); |
| 787 | + } |
756 | 788 | }
|
757 | 789 |
|
758 | 790 | export class Uint64Array extends ArrayBufferView {
|
@@ -847,6 +879,10 @@ export class Uint64Array extends ArrayBufferView {
|
847 | 879 | reverse(): this {
|
848 | 880 | return REVERSE<this, u64>(this);
|
849 | 881 | }
|
| 882 | + |
| 883 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Uint64Array { |
| 884 | + return WRAP<Uint64Array, u64>(buffer, byteOffset, length); |
| 885 | + } |
850 | 886 | }
|
851 | 887 |
|
852 | 888 | export class Float32Array extends ArrayBufferView {
|
@@ -941,6 +977,10 @@ export class Float32Array extends ArrayBufferView {
|
941 | 977 | reverse(): this {
|
942 | 978 | return REVERSE<this, f32>(this);
|
943 | 979 | }
|
| 980 | + |
| 981 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Float32Array { |
| 982 | + return WRAP<Float32Array, f32>(buffer, byteOffset, length); |
| 983 | + } |
944 | 984 | }
|
945 | 985 |
|
946 | 986 | export class Float64Array extends ArrayBufferView {
|
@@ -1035,6 +1075,10 @@ export class Float64Array extends ArrayBufferView {
|
1035 | 1075 | reverse(): this {
|
1036 | 1076 | return REVERSE<this, f64>(this);
|
1037 | 1077 | }
|
| 1078 | + |
| 1079 | + static wrap(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Float64Array { |
| 1080 | + return WRAP<Float64Array, f64>(buffer, byteOffset, length); |
| 1081 | + } |
1038 | 1082 | }
|
1039 | 1083 |
|
1040 | 1084 | // @ts-ignore: decorator
|
@@ -1260,3 +1304,35 @@ function REVERSE<TArray extends ArrayBufferView, T>(array: TArray): TArray {
|
1260 | 1304 | }
|
1261 | 1305 | return array;
|
1262 | 1306 | }
|
| 1307 | + |
| 1308 | +// @ts-ignore: decorator |
| 1309 | +@inline |
| 1310 | +function WRAP<TArray extends ArrayBufferView, T>(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): TArray { |
| 1311 | + var bufferByteLength = buffer.byteLength; |
| 1312 | + if (<u32>byteOffset >= <u32>bufferByteLength) { |
| 1313 | + throw new RangeError(E_INDEXOUTOFRANGE); |
| 1314 | + } |
| 1315 | + var byteLength: i32; |
| 1316 | + if (length < 0) { |
| 1317 | + if (length == -1) { |
| 1318 | + const mask = <i32>(1 << alignof<T>() - 1); |
| 1319 | + if (buffer.byteLength & mask) { |
| 1320 | + throw new RangeError(E_INVALIDLENGTH); |
| 1321 | + } else { |
| 1322 | + byteLength = buffer.byteLength; |
| 1323 | + } |
| 1324 | + } else { |
| 1325 | + throw new RangeError(E_INVALIDLENGTH); |
| 1326 | + } |
| 1327 | + } else { |
| 1328 | + byteLength = length << alignof<T>(); |
| 1329 | + } |
| 1330 | + if (byteOffset + byteLength > buffer.byteLength) { |
| 1331 | + throw new RangeError(E_INVALIDLENGTH); |
| 1332 | + } |
| 1333 | + var out = changetype<TArray>(__alloc(offsetof<TArray>(), idof<TArray>())); |
| 1334 | + out.data = buffer; |
| 1335 | + out.dataLength = byteLength; |
| 1336 | + out.dataStart = changetype<usize>(buffer) + <usize>byteOffset; |
| 1337 | + return out; |
| 1338 | +} |
0 commit comments