AssemblyScript loader experiments (#13)

This commit is contained in:
Ashwin Phatak 2021-09-23 16:45:23 +05:30 committed by nabarun
parent 889e96572d
commit 9e6f6b4519
3 changed files with 48 additions and 6 deletions

View File

@ -37,3 +37,15 @@ import {
export function callGraphAPI (): void {
log.debug('hello {}', ['world']);
}
export class Foo {
static getFoo (): Foo {
return new Foo();
}
getString (): string {
return 'hello world!';
}
}
export const FooID = idof<Foo>();

View File

@ -22,8 +22,8 @@
"scripts": {
"lint": "eslint .",
"build": "tsc",
"asbuild:debug": "asc assembly/index.ts --lib ./node_modules --target debug",
"asbuild:release": "asc assembly/index.ts --lib ./node_modules --target release",
"asbuild:debug": "asc assembly/index.ts --lib ./node_modules --exportRuntime --target debug",
"asbuild:release": "asc assembly/index.ts --lib ./node_modules --exportRuntime --target release",
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
"test": "yarn asbuild:debug && mocha src/**/*.test.ts"
},

View File

@ -3,13 +3,43 @@
//
import path from 'path';
import { expect } from 'chai';
import { instantiate } from './index';
const WASM_FILE_PATH = '../build/debug.wasm';
it('should execute exported function', async () => {
const filePath = path.resolve(__dirname, WASM_FILE_PATH);
const { exports } = await instantiate(filePath);
exports.callGraphAPI();
describe('wasm loader tests', () => {
let exports: any;
before(async () => {
const filePath = path.resolve(__dirname, WASM_FILE_PATH);
const instance = await instantiate(filePath);
exports = instance.exports;
});
it('should execute exported function', async () => {
const { callGraphAPI } = exports;
callGraphAPI();
});
it('should use a class/instance created in wasm from JS', async () => {
const { Foo, __getString, __pin, __unpin } = exports;
const fooPtr = __pin(Foo.getFoo());
const foo = Foo.wrap(fooPtr);
const strPtr = foo.getString();
expect(__getString(strPtr)).to.equal('hello world!');
__unpin(fooPtr);
});
it('should instantiate a class in wasm from JS', async () => {
const { Foo, FooID, __getString, __new, __pin, __unpin } = exports;
const newFooPtr = __pin(__new(FooID));
const newFoo = Foo.wrap(newFooPtr);
const newStrPtr = newFoo.getString();
expect(__getString(newStrPtr)).to.equal('hello world!');
__unpin(newFooPtr);
});
});