Skip to content

Automatically start main on import. #1939

Open
@daxpedda

Description

@daxpedda

Motivation

Especially after #1843, I think it would make a lot of sense that on importing the js file, it would immediately start init and therefore execute main.

This would solve a couple of issues:

  • Less boilerplate: <script src="wasm.js" type="module"></script> would be enough.
    • No need to call init.
    • No need to preload the js file (the wasm still needs to be preloaded).
  • Can be directly used with Worker, because it starts itself.
  • It should only start itself when main or wasm_bindgen(start) is used in rust, which should be pretty intuitive.
  • Behaving like other output modes, bundler and nodejs already do that.

Proposed Solution

Call init after its definition if needs_manual_start is true in cli-support.
Additionally we should add a flag to disable this functionality.

Alternatives

  • Potentially we could always call init to start loading the wasm file, even if there is no start function.
  • Hide this functionality behind a flag instead of making it default and disabling it by a flag.

Additional Context

I am afraid there was some reason that this functionality wasn't implemented already, I just can't figure out why.

EDIT: Just started to implement this when I found the other output modes already do that, only no-modules and web don't.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions