tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

wasm-unknown target shouldn't set `--import-memory` by default

Open jmillikin opened this issue 1 year ago • 1 comments
trafficstars

The wasm-unknown target invokes wasm-ld with the --import-memory option, which causes the following import to be emitted in the output:

(import "env" "memory" (memory $mimport$0 2))

This is undesirable to have as default behavior for wasm-unknown, because it introduces a mandatory import into what is otherwise a freestanding module.

If the --import-memory were to be removed from targets/wasm-unknown.json ldflags, then wasm-ld would emit this instead:

;; Declare and export the Memory, which requires no special import binding in the host
(memory $0 2)
(export "memory" (memory $0))

This option appears to have been added as part of a larger change in https://github.com/tinygo-org/tinygo/pull/4125/commits/2cd966d6d577df17cfde3869ee7e0dbb4da9307f, but it's unclear from the commit message why --import-memory was added. Based on discussion in the PR, I think it was simply to reduce the number of bytes in an absolute bare-bones "hello world" module, which IMO is not a compelling reason to have it set by default.

jmillikin avatar Jun 30 '24 11:06 jmillikin

Because wasm-unknown by default doesn't target a particular platform, it's difficult to say whether memory should be imported or exported by default. @deadprogram what do environments like wazero expect? Memory, or no memory? Or do they support both?

aykevl avatar Jun 30 '24 11:06 aykevl

I think @jmillikin is probably correct in what the default should be.

deadprogram avatar Jul 17 '24 18:07 deadprogram

See https://github.com/tinygo-org/tinygo/pull/4414 for a correction.

deadprogram avatar Aug 16 '24 15:08 deadprogram