AssmeblyScript function doesn't get called

208 Views Asked by At

I have the following AssmemblyScript module:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function printNum(a: string): f64 {
  return parseFloat(a);
}

Which is generated with asc assembly/index.ts --target release

I'm then consuming that in my host TypeScript files:

import fs from "fs";
import loader from "@assemblyscript/loader";
import { join } from "path";

const wasmModule = loader.instantiateSync(fs.readFileSync(join(__dirname, "..", "/build/untouched.wasm")), {});
module.exports = wasmModule.exports;

And I can call add just fine:

const { add, printNum } = require("./wasm");
console.log(add(1, 2)); // correctly prints 3

However, trying to call printNum doesn't crash my Node process, but the process exits successfully, without the code being called:

    const [row] = await db.getWhere({});
    console.log("balance", row.balance, typeof row.balance); // balance 123456789123456789.123456789123456789 string
    try {
      const result = printNum(row.balance);
      console.log("result", result);
    } catch (error) {
      console.log(error);
    }
    console.log(add(1, 2));

    console.log("done");

The printNum function is never called, or it's called and internally errors? I don't receive any error in the catch, and the subsequent two console.logs are never called

If I comment out the whole printNum try/catch, those next two console.logs are executed

Where do I begin debugging this? Or can anyone spot any errors in my AssemblyScript printNum method itself that might cause this?

1

There are 1 best solutions below

4
On

printNum takes a pointer to a string. To create a new string use __newString and pass the returned pointer to printNum.

const { add, printNum, __newString } = require("./wasm");
const result = printNum(__newString(row.balance));

Reference