Skip to content

Classes #3052

Open
Open

Description

@Gouvernathor

Question

Is there a guide or explanation for how classes written in as files are compiled by assemblyscript and usable from there ? Are constructors, static methods, ordinary methods ok and under which conditions ?

The as-bigint package and repo uses them, and apparently it compiles under their obsolete version of assemblyscript and not under current version – and I could not get an explanation for how that's possible.
The as-mpz seems to do the same, and while it uses a more recent version of assemblyscript to do so, the code (especially the built code) is very intricate to understand for a newcomer. In particular, it is very long within a single file and so what makes it work or doesn't work is not easy to understand.
Which is also the reason for guides and documentation to exist: because if retro-engineering is not an efficient way to learn a language.

Activity

  1. JairusSW commented on Oct 21, 2026

    @JairusSW
    Member

    @Gouvernathor are you attempting to expose (via export class ...) to the host?
    Generally, classes behave the exact same way that they would in classic TypeScript. You can also export them and consume as an import (eg. export class Foo {} and import { Foo } from "./foo"), just can't export them from the entry file to be exposed to host

    Which is also the reason for guides and documentation to exist: because if retro-engineering is not an efficient way to learn a language.

    perhaps i'll go and write some tutorials some time

  2. Gouvernathor commented on Oct 22, 2026

    @Gouvernathor
    Author

    So, you're talking about the entry file : does that mean that an AS library can't export/provide a class to other AS code ? Or is it just unable to be exported to JS ?
    I'm not attempting anything right now, I'm trying to understand what's possible to be done.

  3. JairusSW commented on Oct 22, 2026

    @JairusSW
    Member

    Unable to export to js (or whatever host you're using eg wasmtime, wazero, etc)
    Everything else works as expected

  4. dcodeIO commented on Oct 22, 2026

    @dcodeIO
    Member

    Perhaps for context: In early versions of AS, there used to be some rudimentary bindings for classes exported to JS, say, an instance method was exported under the name MyClass#someMethod with parameters (this: i32, ...). That was later dropped however, partly in anticipation of Wasm GC <-> JS interop, and partly because it interfered with dead code elimination because class exports aren't fine-grained and kept all the static and instance members of the class alive via the exports created for each of them, leading to unexpected binary sizes for some users.

    Currently, to get something similar, one would export wrappers instead, for example:

    export function MyClass_SomeMethod(instance: MyClass): i32 {
      return instance.someMethod();
    }

    Or, better, a wrapper that does more than just that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions