mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 07:23:39 +00:00
internal/wasm-tools, internal/cm: udpate to go.bytecodealliance.org@v0.6.1 and cm@v0.2.1
This commit is contained in:
+11
-6
@@ -1,7 +1,5 @@
|
||||
package cm
|
||||
|
||||
import "errors"
|
||||
|
||||
// CaseUnmarshaler returns an function that can unmarshal text into
|
||||
// [variant] or [enum] case T.
|
||||
//
|
||||
@@ -33,8 +31,7 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
|
||||
if len(text) == 0 {
|
||||
return errEmpty
|
||||
}
|
||||
s := string(text)
|
||||
c, ok := m[s]
|
||||
c, ok := m[string(text)]
|
||||
if !ok {
|
||||
return errNoMatchingCase
|
||||
}
|
||||
@@ -46,6 +43,14 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
|
||||
const linearScanThreshold = 16
|
||||
|
||||
var (
|
||||
errEmpty = errors.New("empty text")
|
||||
errNoMatchingCase = errors.New("no matching case")
|
||||
errEmpty = &stringError{"empty text"}
|
||||
errNoMatchingCase = &stringError{"no matching case"}
|
||||
)
|
||||
|
||||
type stringError struct {
|
||||
err string
|
||||
}
|
||||
|
||||
func (err *stringError) Error() string {
|
||||
return err.err
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -62,57 +60,3 @@ func (l list[T]) Data() *T {
|
||||
func (l list[T]) Len() uintptr {
|
||||
return l.len
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (l list[T]) MarshalJSON() ([]byte, error) {
|
||||
if l.len == 0 {
|
||||
return []byte("[]"), nil
|
||||
}
|
||||
|
||||
s := l.Slice()
|
||||
var zero T
|
||||
if unsafe.Sizeof(zero) == 1 {
|
||||
// The default Go json.Encoder will marshal []byte as base64.
|
||||
// We override that behavior so all int types have the same serialization format.
|
||||
// []uint8{1,2,3} -> [1,2,3]
|
||||
// []uint32{1,2,3} -> [1,2,3]
|
||||
return json.Marshal(sliceOf(s))
|
||||
}
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
type slice[T any] []entry[T]
|
||||
|
||||
func sliceOf[S ~[]E, E any](s S) slice[E] {
|
||||
return *(*slice[E])(unsafe.Pointer(&s))
|
||||
}
|
||||
|
||||
type entry[T any] [1]T
|
||||
|
||||
func (v entry[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(v[0])
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
func (l *list[T]) UnmarshalJSON(data []byte) error {
|
||||
if bytes.Equal(data, nullLiteral) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var s []T
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.data = unsafe.SliceData([]T(s))
|
||||
l.len = uintptr(len(s))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// nullLiteral is the JSON representation of a null literal.
|
||||
// By convention, to approximate the behavior of Unmarshal itself,
|
||||
// Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
|
||||
// See https://pkg.go.dev/encoding/json#Unmarshaler for more information.
|
||||
var nullLiteral = []byte("null")
|
||||
|
||||
Reference in New Issue
Block a user