Skip to content

Commit 5cf6e2a

Browse files
committed
fix: Make locals array resize dynamically
1 parent c4e0054 commit 5cf6e2a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

internal/engine/interpreter/interpreter.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,22 @@ func (ce *callEngine) callGoFunc(ctx context.Context, m *wasm.ModuleInstance, f
704704
}
705705
}
706706

707+
func ensureSize[T any](s []T, index int) []T {
708+
709+
if index < len(s) {
710+
return s
711+
}
712+
713+
x := math.Log2(float64(index))
714+
715+
newSize := math.Pow(2, x+1)
716+
717+
newSlice := make([]T, int(newSize))
718+
copy(newSlice, s)
719+
720+
return newSlice
721+
}
722+
707723
func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance, f *function) {
708724
frame := &callFrame{f: f, base: len(ce.stack)}
709725
moduleInst := f.moduleInstance
@@ -719,7 +735,7 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance
719735
bodyLen := uint64(len(body))
720736

721737
// TODO: Figure out a way to resolve how many local variables we need
722-
locals := make([]uint64, 10000)
738+
locals := make([]uint64, 1024)
723739

724740
initialOffset := frame.f.parent.offsetsInWasmBinary[frame.pc]
725741

@@ -945,6 +961,8 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance
945961
ce.stack[index] = value
946962

947963
localIndex := int(op.U2)
964+
965+
locals = ensureSize(locals, localIndex)
948966
locals[localIndex] = value
949967

950968
}

0 commit comments

Comments
 (0)