How to export classes from AssemblyScript?
Asked Answered
S

2

6

I am trying to port my simple collision detection library from JavaScript to WebAssembly for speed. After looking up languages that compile to WASM, AssemblyScript seemed to be perfect as I only needed to add types to my JS file. The whole library is a Class and after adding types I tried to compile it but it does not compile properly. For example, compiling this using command 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! in the console but it did not return the initiated class, instead I got this: [Number (Internref): 18624].

So I serched on the internet for solution and found this. But the accepted solution there is to use AssemblyScript Loader, which is deprecated. I also know about as-bind but it states that it wraps around AssemblyScript Loader so, indirectly, it is also deprecated. So how can I export classes from AssemblyScript?

Soapsuds answered 18/4, 2022 at 3:13 Comment(2)
You don't export classes, simple as that. You can only export functions that will call methods on an instance passed to them.Decoration
@Decoration I suppose I can split my class into an object containing all the properties, and all the methods as independent functions that modify and use the values in the object. But I need to run multiple instances of the library so I think I will handle raw calculations in WASM and a JS class over it for other things. Thanks for clarifying!Soapsuds
O
2

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. I believe it is planned to have functions named like Vec3#constructor Vec3#magnitude ect..

Exporting classes from WebAssembly to JavaScript with Assemblyscript?

Overstay answered 28/4, 2022 at 19:49 Comment(3)
I gave up on trying to export classes from AssemblyScript. I now just handle raw calculations in AssemblyScript and a wrapper class in JS. Thanks for trying to help me though.Soapsuds
Honestly, I would do the same in practice.Overstay
I am trying to understand the use case for AssemblyScript, as there are no classes, no interfaces, no closures, no destructuring...?Grimmett
T
1

A short update regarding this question:

I also tried this using AssemblyScript 0.27.22 and ran into the same issue. According to the manual (https://www.assemblyscript.org/compiler.html#host-bindings) complex objects are returned as reference (pointer) while for simple objects it is possible to return a copy of them.

class Person {
  firstname: string;
  lastname: string;
}

export function createPerson(): Person {
  return {firstname: 'AFirstName', lastname: 'ALastName'};
}

I used this example in my Typescript file and logged the result in my JS/HTML file, it should look like this: "Generate person: {firstname: 'AFirstName', lastname: 'ALastName'}"

While this is still not perfect it is a step to a better type/object support.

Transducer answered 20/12, 2023 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.