Skip to content

Reading integer arrays with loader into JavaScript #418

@JamesLMilner

Description

@JamesLMilner

Hi there, firstly thanks for this great project, I'm really enjoying getting to dive into it. I've been looking at how to talk to JavaScript in the browser and came across the loader which seems like a nice developer experience. I've been fine with numbers, booleans, strings but I am currently struggling to get arrays over from WASM to browser side JavaScript.

Here are my files:

// AS

import "allocator/tlsf";

export { memory };

export function getIntArray(): i32[] {
	let arr: i32[] = [1,2,3,4,5];
	return arr;
}
// TS (Browser)

import { instantiateBuffer, ASUtil } from "assemblyscript/lib/loader/";

interface Test extends ASUtil {
    getIntArray: () => number;
}

// Fetch and instantiate the module
fetch("build/optimized.wasm")
    .then(response => response.arrayBuffer())
    .then(buffer => {
        const wasmModule: Test = instantiateBuffer(new Uint8Array(buffer));
        console.log(wasmModule.getArray(Int32Array, wasmModule.getIntArray()));
    })
    .catch(err => {
        console.error("Failed to load WASM: " + err.message);
        console.error(err.stack);
    });

Running this in the browser returns the error on the wasmModule.getArray(Int32Array, wasmModule.getIntArray()) line:

attempting to construct out-of-bounds TypedArray on ArrayBuffer

I've been able to get Strings working (the other loader type that requires you to explicitly export { memory }. I am not sure if there's something I'm missing for arrays?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions