moonbitlang / core

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

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

Open gmlewis opened 4 months ago

gmlewis commented 4 months ago

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):

peter-jerry-ye commented 4 months ago

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.

gmlewis commented 4 months ago

@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?

nilslice commented 4 months ago

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.

peter-jerry-ye commented 4 months ago

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.

bobzhang commented 4 months ago

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

bhelx commented 4 months ago

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

peter-jerry-ye commented 4 months ago

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