wa-lang.org/wazero@v1.0.2/internal/gojs/util.go (about) 1 package gojs 2 3 import ( 4 "context" 5 "fmt" 6 7 "wa-lang.org/wazero/api" 8 "wa-lang.org/wazero/internal/wasm" 9 ) 10 11 // Debug has unknown use, so stubbed. 12 // 13 // See https://github.com/golang/go/blob/go1.19/src/cmd/link/internal/wasm/asm.go#L133-L138 14 var Debug = stubFunction(functionDebug) 15 16 // stubFunction stubs functions not used in Go's main source tree. 17 func stubFunction(name string) *wasm.HostFunc { 18 return &wasm.HostFunc{ 19 ExportNames: []string{name}, 20 Name: name, 21 ParamTypes: []wasm.ValueType{wasm.ValueTypeI32}, 22 ParamNames: []string{parameterSp}, 23 Code: &wasm.Code{IsHostFunction: true, Body: []byte{wasm.OpcodeUnreachable, wasm.OpcodeEnd}}, 24 } 25 } 26 27 // mustRead is like api.Memory except that it panics if the offset and 28 // byteCount are out of range. 29 func mustRead(ctx context.Context, mem api.Memory, fieldName string, offset, byteCount uint32) []byte { 30 buf, ok := mem.Read(ctx, offset, byteCount) 31 if !ok { 32 panic(fmt.Errorf("out of memory reading %s", fieldName)) 33 } 34 return buf 35 } 36 37 // mustReadUint64Le is like api.Memory except that it panics if the offset 38 // is out of range. 39 func mustReadUint64Le(ctx context.Context, mem api.Memory, fieldName string, offset uint32) uint64 { 40 result, ok := mem.ReadUint64Le(ctx, offset) 41 if !ok { 42 panic(fmt.Errorf("out of memory reading %s", fieldName)) 43 } 44 return result 45 } 46 47 // mustWrite is like api.Memory except that it panics if the offset 48 // is out of range. 49 func mustWrite(ctx context.Context, mem api.Memory, fieldName string, offset uint32, val []byte) { 50 if ok := mem.Write(ctx, offset, val); !ok { 51 panic(fmt.Errorf("out of memory writing %s", fieldName)) 52 } 53 } 54 55 // mustWriteUint64Le is like api.Memory except that it panics if the offset 56 // is out of range. 57 func mustWriteUint64Le(ctx context.Context, mem api.Memory, fieldName string, offset uint32, val uint64) { 58 if ok := mem.WriteUint64Le(ctx, offset, val); !ok { 59 panic(fmt.Errorf("out of memory writing %s", fieldName)) 60 } 61 }