Skip to content

Commit af17d55

Browse files
committed
feature: add buildmode=legacy to support existing base of users who expected the
older behavior for wasi modules to not return an exit code as if they were reactors. See #4726 for some details on what this is intended to address. Signed-off-by: deadprogram <[email protected]>
1 parent d04eea7 commit af17d55

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

builder/build.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,12 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
667667
ldflags = append(ldflags, "--no-entry")
668668
}
669669

670+
if config.Options.BuildMode == "legacy" {
671+
if !strings.HasPrefix(config.Triple(), "wasm32-") {
672+
return result, fmt.Errorf("buildmode legacy is only supported on wasm")
673+
}
674+
}
675+
670676
// Add compiler-rt dependency if needed. Usually this is a simple load from
671677
// a cache.
672678
if config.Target.RTLib == "compiler-rt" {

compileopts/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
var (
11-
validBuildModeOptions = []string{"default", "c-shared"}
11+
validBuildModeOptions = []string{"default", "c-shared", "legacy"}
1212
validGCOptions = []string{"none", "leaking", "conservative", "custom", "precise"}
1313
validSchedulerOptions = []string{"none", "tasks", "asyncify"}
1414
validSerialOptions = []string{"none", "uart", "usb", "rtt"}

compiler/symbol.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ func (c *compilerContext) getFunctionInfo(f *ssa.Function) functionInfo {
283283
info.wasmName = "_start"
284284
info.exported = true
285285
}
286+
if info.linkName == "runtime.wasmEntryLegacy" && c.BuildMode == "legacy" {
287+
info.linkName = "_start"
288+
info.wasmName = "_start"
289+
info.exported = true
290+
}
286291

287292
// Check for //go: pragmas, which may change the link name (among others).
288293
c.parsePragmas(&info, f)

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ func main() {
15011501
var tags buildutil.TagsFlag
15021502
flag.Var(&tags, "tags", "a space-separated list of extra build tags")
15031503
target := flag.String("target", "", "chip/board name or JSON target specification file")
1504-
buildMode := flag.String("buildmode", "", "build mode to use (default, c-shared)")
1504+
buildMode := flag.String("buildmode", "", "build mode to use (default, c-shared, legacy)")
15051505
var stackSize uint64
15061506
flag.Func("stack-size", "goroutine stack size (if unknown at compile time)", func(s string) error {
15071507
size, err := bytesize.Parse(s)

src/runtime/runtime_wasmentry.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func wasmEntryReactor() {
5151
}
5252
}
5353

54+
// This is the _start entry point, when using -buildmode=legacy.
55+
func wasmEntryLegacy() {
56+
// These need to be initialized early so that the heap can be initialized.
57+
initializeCalled = true
58+
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
59+
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
60+
run()
61+
}
62+
5463
// Whether the runtime was initialized by a call to _initialize or _start.
5564
var initializeCalled bool
5665

0 commit comments

Comments
 (0)