ttulka / as-big

AssemblyScript library for arbitrary-precision decimal arithmetic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to run directly in javascript?

jhomarolo opened this issue · comments

I'm new to webassembly and I have a question about using the lib.

I read your journey of learning webassembly, but I have a question about the possibility of calling the lib after compiled in javascript

inside index.ts:

import { basics0, basics1, basics2, basics3 } from './basics';
import Big from 'as-big/Big';

export { 
    basics0, basics1, basics2, basics3, 
    Big
};

inside index.js

const fs = require("fs");
const loader = require("@assemblyscript/loader");
const wasm = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"), {});
module.exports = wasm.exports;

const { __getString } = wasm.exports;

console.log('ONE #0:', wasm.exports.Big.ONE);

Every function calling as-big must be inside assembly folder and compiled?

Thanks a lot

Yes, it is possible with little AS-loader magic:

const fs = require("fs");
const loader = require("@assemblyscript/loader");
const wasm = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"), {});
module.exports = wasm.exports;

const { __getString, __pin, __unpin, Big } = wasm.exports;

const pt = __pin(Big.TWO);
const big = Big.wrap(pt);
const val = big.toNumber();
const str = __getString(big.toString());
__unpin(pt);

console.log('Big.TWO #1:', val);
console.log('Big.TWO #2:', str);

To get the "pin" functions, you have to compile the Wasm module with the --exportRuntime flag:

npm  run asbuild:optimized -- --exportRuntime

Hope this helps.