Skip to content

Commit

Permalink
use unsafe.Slice in Memory.UnsafeData (bytecodealliance#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Oct 31, 2023
1 parent 915992a commit 976f938
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/bytecodealliance/wasmtime-go/v14

go 1.18
go 1.19

require github.com/stretchr/testify v1.8.0

Expand Down
7 changes: 1 addition & 6 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@ func (mem *Memory) Data(store Storelike) unsafe.Pointer {
// `m` alive for long enough while you're using the `[]byte` slice. If the
// `[]byte` slice is used after `m` is GC'd then that is undefined behavior.
func (mem *Memory) UnsafeData(store Storelike) []byte {
// see https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
const MaxLen = 1 << 32
length := mem.DataSize(store)
if length >= MaxLen {
panic("memory is too big")
}
return (*[MaxLen]byte)(mem.Data(store))[:length:length]
return unsafe.Slice((*byte)(mem.Data(store)), length)
}

// DataSize returns the size, in bytes, that `Data()` is valid for
Expand Down

0 comments on commit 976f938

Please sign in to comment.