我在AS和WA领域还很陌生,我目前的疑问是如何从JavaScript获取字符串和数组。
index.js
const fs = require("fs");
const abort = () => { throw new Error('....') };
const compiled = new WebAssembly.Module(fs.readFileSync("aaa.wasm"));
const instance = new WebAssembly.Instance(compiled, { env: { abort } })
const lib = instance.exports
console.log(' say', ':', lib.say("oi"))
console.log(' say', ':', lib.say("hi"))
console.log(' add', ':', lib.add(100, 100))
console.log('teste', ':', lib.teste())
索引
import 'allocator/buddy';
export function say(hello: string): string {
return hello + " world"
}
export function add(a: i32, b: i32): i32 {
return a + b;
}
export function teste(): string {
let x = "ZZZZZZZZZZZZ"
return x;
}
copile并使用nodejs运行
$ asc assembly\index.ts -o aaa.wasm
$ node index.js
我得到什么(我怀疑这些整数是内存指针,对吗?)
say : 3136
say : 3168
add : 200
teste : 84
我所期望的
say : oi world
say : hi world
add : 200
teste : ZZZZZZZZZZZZ
对不起,我的英语不好,我来自巴西,葡萄牙语是我的主要语言,还有tks的帮助。
我阅读了加载程序文档并尝试成功使用它,
难道我做错了什么?
我需要通过npm安装加载程序吗?
索引
const loader = require("@assemblyscript/loader");
const file = fs.readFileSync("aaa.wasm")
const fs = require("fs");
const myModule = loader.instatiateBuffer(file, {});
错误
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module '@assemblyscript/loader'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:\terminal\user\personal\wasmTs\index.js:1:78)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
加载程序尚未在npm上。 现在,尝试:
const loader = require("assemblyscript/lib/loader");
哇,这行得通!!!
我在加载器文档中发现了一个错误,在示例中是写const myModule = loader.instatiateBuffer(fs.readFileSync("myModule.wasm"), myImports);
而不是insta n tiateBuffer。
非常感谢你的帮助! ^^
只是将我的测试发布给其他冒险家程序员作为参考/示例:
索引
import "allocator/tlsf";
export { memory };
export function say(hello: string): string {
return hello + " world"
}
export function add(a: i32, b: i32): i32 {
return a + b;
}
export function teste(): string {
let x = "ZZZZZZZZZZZZ"
return x;
}
export class X {
teste(): String {
return "Aew";
}
}
index.js
const loader = require("./assemblyscript/lib/loader/index");
const fs = require("fs");
const file = fs.readFileSync("index.wasm")
const lib = loader.instantiateBuffer(file, {});
const x = new lib.X()
console.log(' say :' , lib.getString(lib.say(lib.newString("oi"))))
console.log(' say :' , lib.getString(lib.say(lib.newString("hi"))))
console.log(' add :' , lib.add(100, 100))
console.log(' teste :' , lib.getString(lib.teste()))
console.log('method :' , lib.getString(x.teste()))
编译并运行
λ asc index.ts -o index.wasm
λ node index.js
结果
say : oi world
say : hi world
add : 200
teste : ZZZZZZZZZZZZ
method : Aew
由于尚未收到任何回复,因此暂时关闭此问题。 如有必要,请随时重新打开!
最有用的评论
只是将我的测试发布给其他冒险家程序员作为参考/示例:
索引
index.js
编译并运行
结果