fix writeByteArray type
This commit is contained in:
@@ -25,12 +25,12 @@ DataView.prototype.readU32 = function () {
|
||||
DataView.prototype.readU64 = function () {
|
||||
return this.getUint64(0);
|
||||
};
|
||||
const isDataView = (x: DataView | number[]): x is DataView => {
|
||||
return x instanceof DataView;
|
||||
const isU8Array = (x: Uint8Array | number[]): x is Uint8Array => {
|
||||
return x instanceof Uint8Array;
|
||||
};
|
||||
|
||||
DataView.prototype.writeByteArray = function (arr) {
|
||||
const len = isDataView(arr) ? arr.byteLength : arr.length;
|
||||
const len = isU8Array(arr) ? arr.byteLength : arr.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
this.setUint8(i, arr[i]);
|
||||
}
|
||||
@@ -45,7 +45,8 @@ DataView.prototype.readByteArray = function (len) {
|
||||
|
||||
const Memory = {
|
||||
alloc: (len: number) => new DataView(new ArrayBuffer(len + 1)),
|
||||
copy: (outbuf: DataView, inbuf: DataView, len: number) => outbuf.writeByteArray(inbuf.readByteArray(len)),
|
||||
copy: (outbuf: DataView, inbuf: DataView, len: number) =>
|
||||
outbuf.writeByteArray(new Uint8Array(inbuf.readByteArray(len).buffer)),
|
||||
};
|
||||
|
||||
declare global {
|
||||
@@ -56,7 +57,7 @@ declare global {
|
||||
readU32(): undefined;
|
||||
readU64(): undefined;
|
||||
readU8(): undefined;
|
||||
writeByteArray(arr: DataView | number[]): undefined;
|
||||
writeByteArray(arr: Uint8Array | number[]): undefined;
|
||||
writeU16(n: number): undefined;
|
||||
writeU32(n: number): undefined;
|
||||
writeU64(n: number): undefined;
|
||||
|
||||
Reference in New Issue
Block a user