Skip to content

Commit

Permalink
Add ability to add function aliasses for Emscripten
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Bobbeldijk <[email protected]>
  • Loading branch information
jerbob92 committed Jul 19, 2024
1 parent c077eab commit f3c7add
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions internal/emscripten/emscripten.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,35 @@ func (v *InvokeFunc) Call(ctx context.Context, mod api.Module, stack []uint64) {
}
}

// Some methods got renamed in Emscripten, this map is to support programs
// compiled with a newer Emscripten version.
var exportedFunctionAliases = map[string][]string{
"stackSave": {"emscripten_stack_get_current"},
"stackRestore": {"_emscripten_stack_restore"},
}

// maybeCallOrPanic calls a given function if it is exported, otherwise panics.
//
// This ensures if the given name is exported before calling it. In other words, this explicitly checks if an api.Function
// returned by api.Module.ExportedFunction is not nil. This is necessary because directly calling a method which is
// potentially nil interface can be fatal on some platforms due to a bug? in Go/QEMU.
// See https://github.com/tetratelabs/wazero/issues/1621
func callOrPanic(ctx context.Context, m api.Module, name string, stack []uint64) {
if f := m.ExportedFunction(name); f != nil {
err := f.CallWithStack(ctx, stack)
var exportedFunction api.Function
functionNames := []string{name}
if aliases, ok := exportedFunctionAliases[name]; ok {
functionNames = append(functionNames, aliases...)
}

for _, functionName := range functionNames {
if f := m.ExportedFunction(functionName); f != nil {
exportedFunction = f
break
}
}

if exportedFunction != nil {
err := exportedFunction.CallWithStack(ctx, stack)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit f3c7add

Please sign in to comment.