Holds wasm.Code as values on wasm.Module (#1243)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
@@ -10,10 +10,10 @@ import (
|
||||
"github.com/tetratelabs/wazero/internal/wasm"
|
||||
)
|
||||
|
||||
func decodeCode(r *bytes.Reader, codeSectionStart uint64) (*wasm.Code, error) {
|
||||
func decodeCode(r *bytes.Reader, codeSectionStart uint64, ret *wasm.Code) (err error) {
|
||||
ss, _, err := leb128.DecodeUint32(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get the size of code: %w", err)
|
||||
return fmt.Errorf("get the size of code: %w", err)
|
||||
}
|
||||
remaining := int64(ss)
|
||||
|
||||
@@ -21,9 +21,9 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64) (*wasm.Code, error) {
|
||||
ls, bytesRead, err := leb128.DecodeUint32(r)
|
||||
remaining -= int64(bytesRead)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get the size locals: %v", err)
|
||||
return fmt.Errorf("get the size locals: %v", err)
|
||||
} else if remaining < 0 {
|
||||
return nil, io.EOF
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
var nums []uint64
|
||||
@@ -34,9 +34,9 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64) (*wasm.Code, error) {
|
||||
n, bytesRead, err = leb128.DecodeUint32(r)
|
||||
remaining -= int64(bytesRead) + 1 // +1 for the subsequent ReadByte
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read n of locals: %v", err)
|
||||
return fmt.Errorf("read n of locals: %v", err)
|
||||
} else if remaining < 0 {
|
||||
return nil, io.EOF
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
sum += uint64(n)
|
||||
@@ -44,19 +44,19 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64) (*wasm.Code, error) {
|
||||
|
||||
b, err := r.ReadByte()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read type of local: %v", err)
|
||||
return fmt.Errorf("read type of local: %v", err)
|
||||
}
|
||||
switch vt := b; vt {
|
||||
case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64,
|
||||
wasm.ValueTypeFuncref, wasm.ValueTypeExternref, wasm.ValueTypeV128:
|
||||
types = append(types, vt)
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid local type: 0x%x", vt)
|
||||
return fmt.Errorf("invalid local type: 0x%x", vt)
|
||||
}
|
||||
}
|
||||
|
||||
if sum > math.MaxUint32 {
|
||||
return nil, fmt.Errorf("too many locals: %d", sum)
|
||||
return fmt.Errorf("too many locals: %d", sum)
|
||||
}
|
||||
|
||||
var localTypes []wasm.ValueType
|
||||
@@ -70,12 +70,15 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64) (*wasm.Code, error) {
|
||||
bodyOffsetInCodeSection := codeSectionStart - uint64(r.Len())
|
||||
body := make([]byte, remaining)
|
||||
if _, err = io.ReadFull(r, body); err != nil {
|
||||
return nil, fmt.Errorf("read body: %w", err)
|
||||
return fmt.Errorf("read body: %w", err)
|
||||
}
|
||||
|
||||
if endIndex := len(body) - 1; endIndex < 0 || body[endIndex] != wasm.OpcodeEnd {
|
||||
return nil, fmt.Errorf("expr not end with OpcodeEnd")
|
||||
return fmt.Errorf("expr not end with OpcodeEnd")
|
||||
}
|
||||
|
||||
return &wasm.Code{Body: body, LocalTypes: localTypes, BodyOffsetInCodeSection: bodyOffsetInCodeSection}, nil
|
||||
ret.BodyOffsetInCodeSection = bodyOffsetInCodeSection
|
||||
ret.LocalTypes = localTypes
|
||||
ret.Body = body
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -161,20 +161,19 @@ func decodeElementSection(r *bytes.Reader, enabledFeatures api.CoreFeatures) ([]
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func decodeCodeSection(r *bytes.Reader) ([]*wasm.Code, error) {
|
||||
func decodeCodeSection(r *bytes.Reader) ([]wasm.Code, error) {
|
||||
codeSectionStart := uint64(r.Len())
|
||||
vs, _, err := leb128.DecodeUint32(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get size of vector: %w", err)
|
||||
}
|
||||
|
||||
result := make([]*wasm.Code, vs)
|
||||
result := make([]wasm.Code, vs)
|
||||
for i := uint32(0); i < vs; i++ {
|
||||
c, err := decodeCode(r, codeSectionStart)
|
||||
err = decodeCode(r, codeSectionStart, &result[i])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %d-th code segment: %v", i, err)
|
||||
}
|
||||
result[i] = c
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user