Skip to content

Commit 7d133cc

Browse files
authored
Implement portable variants of TypedArray.wrap (#1309)
1 parent 40f889e commit 7d133cc

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

NOTICE

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ under the licensing terms detailed in LICENSE:
2121
* Vladimir Tikhonov <[email protected]>
2222
* Duncan Uszkay <[email protected]>
2323
24+
* Julien Letellier <[email protected]>
2425

2526
Portions of this software are derived from third-party works licensed under
2627
the following terms:

std/portable/index.d.ts

+45
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,48 @@ declare function unmanaged(constructor: Function): void;
303303

304304
/** Environmental tracing function. */
305305
declare function trace(msg: string, n?: i32, a0?: f64, a1?: f64, a2?: f64, a3?: f64, a4?: f64): void;
306+
307+
declare interface Int8ArrayConstructor {
308+
/** Equivalent to calling `new Int8Array` with multiple arguments. */
309+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Int8Array;
310+
}
311+
312+
declare interface Uint8ArrayConstructor {
313+
/** Equivalent to calling `new Uint8Array` with multiple arguments. */
314+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint8Array;
315+
}
316+
317+
declare interface Uint8ClampedArrayConstructor {
318+
/** Equivalent to calling `new Uint8ClampedArray` with multiple arguments. */
319+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint8ClampedArray;
320+
}
321+
322+
declare interface Int16ArrayConstructor {
323+
/** Equivalent to calling `new Int16Array` with multiple arguments. */
324+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Int16Array;
325+
}
326+
327+
declare interface Uint16ArrayConstructor {
328+
/** Equivalent to calling `new Uint16Array` with multiple arguments. */
329+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint16Array;
330+
}
331+
332+
declare interface Int32ArrayConstructor {
333+
/** Equivalent to calling `new Int32Array` with multiple arguments. */
334+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Int32Array;
335+
}
336+
337+
declare interface Uint32ArrayConstructor {
338+
/** Equivalent to calling `new Uint32Array` with multiple arguments. */
339+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint32Array;
340+
}
341+
342+
declare interface Float32ArrayConstructor {
343+
/** Equivalent to calling `new Float32Array` with multiple arguments. */
344+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Float32Array;
345+
}
346+
347+
declare interface Float64ArrayConstructor {
348+
/** Equivalent to calling `new Float64Array` with multiple arguments. */
349+
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Float64Array;
350+
}

std/portable/index.js

+54
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,57 @@ globalScope["trace"] = function(message, n) {
313313
if (n) message += Array.prototype.slice.call(arguments, 2, 2 + n);
314314
console.error("trace: " + message);
315315
};
316+
317+
Object.defineProperty(Int8Array, "wrap", {
318+
value: function wrap(buffer, byteOffset, length) {
319+
return new Int8Array(buffer, byteOffset, length);
320+
}
321+
});
322+
323+
Object.defineProperty(Uint8Array, "wrap", {
324+
value: function wrap(buffer, byteOffset, length) {
325+
return new Uint8Array(buffer, byteOffset, length);
326+
}
327+
});
328+
329+
Object.defineProperty(Uint8ClampedArray, "wrap", {
330+
value: function wrap(buffer, byteOffset, length) {
331+
return new Uint8ClampedArray(buffer, byteOffset, length);
332+
}
333+
});
334+
335+
Object.defineProperty(Int16Array, "wrap", {
336+
value: function wrap(buffer, byteOffset, length) {
337+
return new Int16Array(buffer, byteOffset, length);
338+
}
339+
});
340+
341+
Object.defineProperty(Uint16Array, "wrap", {
342+
value: function wrap(buffer, byteOffset, length) {
343+
return new Uint16Array(buffer, byteOffset, length);
344+
}
345+
});
346+
347+
Object.defineProperty(Int32Array, "wrap", {
348+
value: function wrap(buffer, byteOffset, length) {
349+
return new Int32Array(buffer, byteOffset, length);
350+
}
351+
});
352+
353+
Object.defineProperty(Uint32Array, "wrap", {
354+
value: function wrap(buffer, byteOffset, length) {
355+
return new Uint32Array(buffer, byteOffset, length);
356+
}
357+
});
358+
359+
Object.defineProperty(Float32Array, "wrap", {
360+
value: function wrap(buffer, byteOffset, length) {
361+
return new Float32Array(buffer, byteOffset, length);
362+
}
363+
});
364+
365+
Object.defineProperty(Float64Array, "wrap", {
366+
value: function wrap(buffer, byteOffset, length) {
367+
return new Float64Array(buffer, byteOffset, length);
368+
}
369+
});

0 commit comments

Comments
 (0)