简体   繁体   English

如何从 AssemblyScript 导出类?

[英]How to export classes from AssemblyScript?

I am trying to port my simple collision detection library from JavaScript to WebAssembly for speed.我正在尝试将我的简单碰撞检测库从 JavaScript 移植到 WebAssembly 以提高速度。 After looking up languages that compile to WASM, AssemblyScript seemed to be perfect as I only needed to add types to my JS file.在查找了可编译为 WASM 的语言后,AssemblyScript 似乎是完美的,因为我只需要将类型添加到我的 JS 文件中。 The whole library is a Class and after adding types I tried to compile it but it does not compile properly.整个库是Class ,在添加类型后,我尝试编译它,但编译不正确。 For example, compiling this using command npx asc path/to/main.ts -o wasm.wasm --exportRuntime -t wasm.wat --bindings esm :例如,使用命令npx asc path/to/main.ts -o wasm.wasm --exportRuntime -t wasm.wat --bindings esm

export class Test {
    constructor() {
        console.log('Successful!');
    }
};

Resulted in this error:导致此错误:

WARNING AS235: Only variables, functions and enums become WebAssembly module exports.

 export class Test {
              ~~~~
 in main.ts(1,14)

After seeing the error I tried to fix it by doing:看到错误后,我尝试通过以下方式修复它:

class Test {
    constructor() {
        console.log('Successful!');
    }
};

export function getTest(): Test {
    return Test;
};

But that resulted in another error:但这导致了另一个错误:

ERROR AS234: Expression does not compile to a value at runtime.

     return Test;
            ~~~~
 in main.ts(8,12)

FAILURE 1 compile error(s)

So I tried to do this:所以我试着这样做:

class Test {
    constructor() {
        console.log('Successful!');
    }
};

export function getTest(): Test {
    return new Test();
};

That compiled successfully and after calling getTest from JavaScript I got an output Successful!编译成功,从 JavaScript 调用getTest后,我得到了 output Successful! in the console but it did not return the initiated class, instead I got this: [Number (Internref): 18624] .在控制台中,但它没有返回启动的 class,而是我得到了这个: [Number (Internref): 18624]

So I serched on the inte.net for solution and found this .所以我在 inte.net 上搜索解决方案并找到了这个 But the accepted solution there is to use AssemblyScript Loader , which is deprecated.但是公认的解决方案是使用AssemblyScript Loader ,它已被弃用。 I also know about as-bind but it states that it wraps around AssemblyScript Loader so, indirectly, it is also deprecated.我也知道as-bind但它声明它环绕 AssemblyScript Loader 因此,间接地,它也被弃用了。 So how can I export classes from AssemblyScript?那么如何从 AssemblyScript 中导出类呢?

So, there currently is no way to export classes.所以,目前没有办法导出类。 However, you can call methods on a class from JavaScript or call a get or set method to modify data.但是,您可以从 JavaScript 调用 class 的方法或调用 get 或 set 方法来修改数据。 I believe it is planned to have functions named like Vec3#constructor Vec3#magnitude ect..我相信计划将函数命名为Vec3#constructor Vec3#magnitude等。

Exporting classes from WebAssembly to JavaScript with Assemblyscript? 使用 Assemblyscript 将类从 WebAssembly 导出到 JavaScript?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM