moonbitlang / core

MoonBit's Core library
https://moonbitlang.com/
Apache License 2.0
584 stars 72 forks source link

Feature Request: Ability to change `_start` to `_initialize` #520

Closed gmlewis closed 2 months ago

gmlewis commented 3 months ago

In this Discord discussion: https://discord.com/channels/1011124058408112148/1246483784740900874 G4Vi discovered that Extism initializes a WASM module by first calling "_initialize" if it exists.

To address this, I'm planning on compiling to WAT, changing this line:

(export "_start" (func $*init*/126))

to:

(export "_initialize" (func $*init*/126))

and compiling back to WASM, but it would be nice to add a compiler flag so that this name can be chosen during moon build.

bobzhang commented 3 months ago

how does other languages deal with this issue?

CryZe commented 3 months ago

The difference is explained here: WASI Application ABI

One is a WASI 0.1 command and one is a WASI 0.1 reactor.

You can specify it like so in clang (or other LLVM based projects): https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mexec-model

I believe this changed to some degree with WASI 0.2, so you may have to consider that.

gmlewis commented 3 months ago

Closely related to this issue is that I needed to add the wat expression (export "memory" (memory $moonbit.memory)) to get the wasm to work correctly with Extism.

peter-jerry-ye commented 3 months ago

Closely related to this issue is that I needed to add the wat expression (export "memory" (memory $moonbit.memory)) to get the wasm to work correctly with Extism.

You can configure it with "export-memory-name": "custom_memory_name" in the link.wasm of moon.pkg.json

gmlewis commented 3 months ago

You can configure it with "export-memory-name": "custom_memory_name" in the link.wasm of moon.pkg.json

Thank you, @peter-jerry-ye ! That worked perfectly.

peter-jerry-ye commented 2 months ago

The init function will become start function after today's release. If there's anything that relies on the initialized instance, then user can export a public function, such as

pub fn initialize() -> Unit {
}

or

pub fn start() -> Unit {
}

and define the alias in the moon.pkg.json