core icon indicating copy to clipboard operation
core copied to clipboard

Need a way to disable or override `spectest.print_char` in wasm output

Open gmlewis opened this issue 1 year ago • 7 comments

I'm trying to build an Extism PDK for MoonBit here: https://github.com/gmlewis/moonbit-pdk and the resulting wasm binaries always include this reference (converted to WAT):

(import "spectest" "print_char" (func $printc (param $i i32)))

The Extism Host does not provide the function spectest.print_char, so I need to be able to disable it or override/replace it with another function within my package/library.

Here are examples of the resulting builds (using moon build --target wasm):

  • greet.wasm: https://modsurfer.dylibso.com/module?hash=3fc57ca997006043ee3451257c13795bfa84186bc9fe00e7d77a471cea04b71a
  • count-vowels.wasm: https://modsurfer.dylibso.com/module?hash=98d020447ea59fda8f1721df17f6173add472ffc785c53757fe1ffd00d7065d0

gmlewis avatar Jun 01 '24 02:06 gmlewis

This is due to the fact that we would like to print some debug information when the program fails (abort('xxx')), and there's no uniform way to do the output in the Wasm world. We are working on this right now. For example, we are providing panic() that also aborts the program but do not log the error message.

peter-jerry-ye avatar Jun 03 '24 02:06 peter-jerry-ye

@bhelx or @nilslice - do you have any recommendations for a more standardized way for languages to provide debug information that wouldn't require mangling the WAT output before creating the final WASM?

Or do you know of examples for how other languages get around this issue?

gmlewis avatar Jun 03 '24 02:06 gmlewis

I see, thanks!

I know at least AssemblyScript has a flag for the compiler to set the function you want to use, and the assemblyScript PDK provides a similar abort function implementation. e.g.

npx asc example.ts --outFile example.wasm --use abort=example/myAbort

I don't know if that's possible here though without the feature being added to the moonbit compiler.

nilslice avatar Jun 03 '24 02:06 nilslice

I know at least AssemblyScript has a flag for the compiler to set the function you want to use, and the assemblyScript PDK provides a similar abort function implementation. e.g.

npx asc example.ts --outFile example.wasm --use abort=example/myAbort

Yeah, we'll try to support a similar behavior. For the first step though (probably next week's release), we'll replace the abort with panic, so that we do not rely on spectest.print_char unless the user uses println explicitly. Then we'll try to improve the behavior.

peter-jerry-ye avatar Jun 03 '24 04:06 peter-jerry-ye

@peter-jerry-ye can we do it for this week's release?

bobzhang avatar Jun 03 '24 05:06 bobzhang

Thank you for considering making it configurable! I think it will make moonbit more portable.

bhelx avatar Jun 03 '24 12:06 bhelx

@gmlewis It should have been disabled in yesterday's release. Please check it again.

peter-jerry-ye avatar Jun 04 '24 01:06 peter-jerry-ye

$ moon version
moon 0.1.20250305 (4e6af84 2025-03-05)

$ wasmtime -S cli=y --invoke greeting target/wasm/release/build/lunar.wasm
Error: failed to run main module `target/wasm/release/build/lunar.wasm`

Caused by:
    0: failed to instantiate "target/wasm/release/build/lunar.wasm"
    1: unknown import: `spectest::print_char` has not been defined

Seems still have this problem.

TheWaWaR avatar Mar 09 '25 04:03 TheWaWaR

@TheWaWaR Do not use println and you'll be fine.

As of println, we'll try to find a way, i.e. provide virtual package or sth like that.

peter-jerry-ye avatar Mar 10 '25 02:03 peter-jerry-ye

The virtual package is on its way. Closing this issue for now.

peter-jerry-ye avatar Apr 11 '25 07:04 peter-jerry-ye