Skip to content

Commit

Permalink
Implement portable variants of TypedArray.wrap (AssemblyScript#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
jletellier authored May 29, 2020
1 parent 40f889e commit 7d133cc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ under the licensing terms detailed in LICENSE:
* Vladimir Tikhonov <reg@tikhonov.by>
* Duncan Uszkay <duncan.uszkay@shopify.com>
* Surma <surma@surma.dev>
* Julien Letellier <letellier.julien@gmail.com>

Portions of this software are derived from third-party works licensed under
the following terms:
Expand Down
45 changes: 45 additions & 0 deletions std/portable/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,48 @@ declare function unmanaged(constructor: Function): void;

/** Environmental tracing function. */
declare function trace(msg: string, n?: i32, a0?: f64, a1?: f64, a2?: f64, a3?: f64, a4?: f64): void;

declare interface Int8ArrayConstructor {
/** Equivalent to calling `new Int8Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Int8Array;
}

declare interface Uint8ArrayConstructor {
/** Equivalent to calling `new Uint8Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint8Array;
}

declare interface Uint8ClampedArrayConstructor {
/** Equivalent to calling `new Uint8ClampedArray` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint8ClampedArray;
}

declare interface Int16ArrayConstructor {
/** Equivalent to calling `new Int16Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Int16Array;
}

declare interface Uint16ArrayConstructor {
/** Equivalent to calling `new Uint16Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint16Array;
}

declare interface Int32ArrayConstructor {
/** Equivalent to calling `new Int32Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Int32Array;
}

declare interface Uint32ArrayConstructor {
/** Equivalent to calling `new Uint32Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Uint32Array;
}

declare interface Float32ArrayConstructor {
/** Equivalent to calling `new Float32Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Float32Array;
}

declare interface Float64ArrayConstructor {
/** Equivalent to calling `new Float64Array` with multiple arguments. */
wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): Float64Array;
}
54 changes: 54 additions & 0 deletions std/portable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,57 @@ globalScope["trace"] = function(message, n) {
if (n) message += Array.prototype.slice.call(arguments, 2, 2 + n);
console.error("trace: " + message);
};

Object.defineProperty(Int8Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Int8Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Uint8Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Uint8Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Uint8ClampedArray, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Uint8ClampedArray(buffer, byteOffset, length);
}
});

Object.defineProperty(Int16Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Int16Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Uint16Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Uint16Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Int32Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Int32Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Uint32Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Uint32Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Float32Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Float32Array(buffer, byteOffset, length);
}
});

Object.defineProperty(Float64Array, "wrap", {
value: function wrap(buffer, byteOffset, length) {
return new Float64Array(buffer, byteOffset, length);
}
});

0 comments on commit 7d133cc

Please sign in to comment.