Skip to content

How to use grain wasm files in Deno #1771

@Mishieck

Description

@Mishieck

Hi,

I would like to write code using Grain, convert the code to WebAssembly, and finally use the WebAssembly code in Deno.

Let's say I have a Grain module is-even.gr with the following code:

let isEven = (value) => value % 2 == 0;

export isEven;

I would like to be able to transpile the code to a wasm file that is compatible with Deno. I would like to use the wasm code as demonstrated in the Deno script below.

const wasmCode = await Deno.readFile('is-even.gr.wasm');
const wasmModule = new WebAssembly.Module(wasmCode);
const wasmInstance = new WebAssembly.Instance(wasmModule, {});
const isEven = wasmInstance.exports.isEven as CallableFunction;
console.log(isEven(2));
console.log(isEven(1));

I've tried to use the wasm file generated using grain is-even.gr, but it doesn't work. I'm getting the following error:

error: Uncaught (in promise) TypeError: WebAssembly.Instance(): Import #0 module="GRAIN$MODULE$runtime/gc" error: module is not an object or function
const wasmInstance = new WebAssembly.Instance(wasmModule, {});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions