From 079491f5b038c6b49104715116e9afe1fe7e70f2 Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Fri, 9 Jul 2021 19:15:27 -0400 Subject: [PATCH 1/3] Add __newArrayBuffer() to the loader. This will facilitate passing binary strings more easily to AssemblyScript from the host environment. Co-authored-by: Max Graey --- NOTICE | 1 + lib/loader/index.d.ts | 2 ++ lib/loader/index.js | 12 ++++++++++++ lib/loader/tests/index.js | 11 +++++++++++ 4 files changed, 26 insertions(+) diff --git a/NOTICE b/NOTICE index 7eb4e46b07..4a8e5ad578 100644 --- a/NOTICE +++ b/NOTICE @@ -41,6 +41,7 @@ under the licensing terms detailed in LICENSE: * Ryan Pivovar * Roman F. <70765447+romdotdog@users.noreply.github.com> * Joe Pea +* Felipe Gasper Portions of this software are derived from third-party works licensed under the following terms: diff --git a/lib/loader/index.d.ts b/lib/loader/index.d.ts index 3098da6002..ce2dc2ef3a 100644 --- a/lib/loader/index.d.ts +++ b/lib/loader/index.d.ts @@ -86,6 +86,8 @@ export interface ASUtil { __instanceof(ptr: number, baseId: number): boolean; /** Allocates a new string in the module's memory and returns a reference (pointer) to it. */ __newString(str: string): number; + /** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */ + __newArrayBuffer(buf: ArrayBuffer): number; /** Allocates a new array in the module's memory and returns a reference (pointer) to it. */ __newArray(id: number, values: ArrayLike): number; diff --git a/lib/loader/index.js b/lib/loader/index.js index e7b24f74a9..f0bfca90fa 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -150,6 +150,18 @@ function postInstantiate(extendedExports, instance) { extendedExports.__newString = __newString; + /** Allocates a new ArrayBuffer in the module's memory and returns its pointer. */ + function __newArrayBuffer(buf) { + if (buf == null) return 0; + const bufview = new Uint8Array(buf); + const ptr = __new(bufview.length, ARRAYBUFFER_ID); + const U8 = new Uint8Array(memory.buffer); + U8.set(bufview, ptr); + return ptr; + } + + extendedExports.__newArrayBuffer = __newArrayBuffer; + /** Reads a string from the module's memory by its pointer. */ function __getString(ptr) { if (!ptr) return null; diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index aac09f91ae..655c509559 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -36,6 +36,17 @@ function test(file) { assert.strictEqual(exports.strlen(ref), str.length); } + // should be able to allocate and work with a new small ArrayBuffer + { + let u8arr = new Uint8Array([1, 2, 3, 4]); + let buf = u8arr.buffer; + let ref = exports.__newArrayBuffer(buf); + let got = exports.__getArrayBuffer(ref); + let gotu8arr = new Uint8Array(got); + + assert.deepStrictEqual(gotu8arr, u8arr); + } + // should be able to allocate and work with a new big string { let str = ` From b60e654ac5b3702bd78f70d2886ed7a5e7d0c432 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 15 Jul 2021 15:50:32 +0300 Subject: [PATCH 2/3] Update lib/loader/tests/index.js --- lib/loader/tests/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index 655c509559..1f59445aa6 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -38,13 +38,11 @@ function test(file) { // should be able to allocate and work with a new small ArrayBuffer { - let u8arr = new Uint8Array([1, 2, 3, 4]); - let buf = u8arr.buffer; - let ref = exports.__newArrayBuffer(buf); - let got = exports.__getArrayBuffer(ref); - let gotu8arr = new Uint8Array(got); + let input = new Uint8Array([1, 2, 3, 4]); + let bufPtr = exports.__newArrayBuffer(input.buffer); + let output = new Uint8Array(exports.__getArrayBuffer(bufPtr)); - assert.deepStrictEqual(gotu8arr, u8arr); + assert.deepStrictEqual(output, input); } // should be able to allocate and work with a new big string From c3114499f1438ebc67eae1db3c52917a8372ceae Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 15 Jul 2021 15:53:02 +0300 Subject: [PATCH 3/3] Update NOTICE --- NOTICE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NOTICE b/NOTICE index 4a8e5ad578..6b4450d592 100644 --- a/NOTICE +++ b/NOTICE @@ -41,7 +41,7 @@ under the licensing terms detailed in LICENSE: * Ryan Pivovar * Roman F. <70765447+romdotdog@users.noreply.github.com> * Joe Pea -* Felipe Gasper +* Felipe Gasper Portions of this software are derived from third-party works licensed under the following terms: