diff options
author | Ayke van Laethem <[email protected]> | 2024-06-24 17:26:58 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-06-24 21:21:52 +0200 |
commit | 1270a501044c97a4a2aa057f4fa87ef679478953 (patch) | |
tree | d582eedb8c2b5e928e4435a820a251b2bd1cfb4b | |
parent | 868262f4a7f24753e5b8dab28cae59330506bedf (diff) | |
download | tinygo-1270a501044c97a4a2aa057f4fa87ef679478953.tar.gz tinygo-1270a501044c97a4a2aa057f4fa87ef679478953.zip |
machine: use new internal/binary package
The encoding/binary package in Go 1.23 imports the slices package, which
results in circular import when imported from the machine package.
Therefore, encoding/binary cannot be used in the machine package.
This commit fixes that by introducing a new internal/binary package that
is just plain Go code without dependencies. It can be safely used
anywhere (including the runtime if needed).
-rw-r--r-- | loader/goroot.go | 1 | ||||
-rw-r--r-- | src/internal/binary/binary.go | 32 | ||||
-rw-r--r-- | src/machine/machine_atsamd21.go | 2 | ||||
-rw-r--r-- | src/machine/machine_atsamd51.go | 2 | ||||
-rw-r--r-- | src/machine/machine_nrf.go | 2 | ||||
-rw-r--r-- | src/machine/machine_stm32f4.go | 2 | ||||
-rw-r--r-- | src/machine/machine_stm32l4.go | 2 | ||||
-rw-r--r-- | src/machine/machine_stm32wlx.go | 2 | ||||
-rw-r--r-- | src/machine/usb/descriptor/configuration.go | 2 | ||||
-rw-r--r-- | src/machine/usb/descriptor/device.go | 2 | ||||
-rw-r--r-- | src/machine/usb/descriptor/endpoint.go | 2 | ||||
-rw-r--r-- | src/machine/usb/descriptor/hid.go | 2 |
12 files changed, 43 insertions, 10 deletions
diff --git a/loader/goroot.go b/loader/goroot.go index c7341fc7f..7c8d97fa4 100644 --- a/loader/goroot.go +++ b/loader/goroot.go @@ -236,6 +236,7 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool { "device/": false, "examples/": false, "internal/": true, + "internal/binary/": false, "internal/bytealg/": false, "internal/fuzz/": false, "internal/reflectlite/": false, diff --git a/src/internal/binary/binary.go b/src/internal/binary/binary.go new file mode 100644 index 000000000..f3dbf6e9b --- /dev/null +++ b/src/internal/binary/binary.go @@ -0,0 +1,32 @@ +// Package binary is a lightweight replacement package for encoding/binary. +package binary + +// This file contains small helper functions for working with binary data. + +var LittleEndian = littleEndian{} + +type littleEndian struct{} + +// Encode data like encoding/binary.LittleEndian.Uint16. +func (littleEndian) Uint16(b []byte) uint16 { + return uint16(b[0]) | uint16(b[1])<<8 +} + +// Store data like binary.LittleEndian.PutUint16. +func (littleEndian) PutUint16(b []byte, v uint16) { + b[0] = byte(v) + b[1] = byte(v >> 8) +} + +// Append data like binary.LittleEndian.AppendUint16. +func (littleEndian) AppendUint16(b []byte, v uint16) []byte { + return append(b, + byte(v), + byte(v>>8), + ) +} + +// Encode data like encoding/binary.LittleEndian.Uint32. +func (littleEndian) Uint32(b []byte) uint32 { + return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 +} diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go index 755bedf9a..fe67f45a3 100644 --- a/src/machine/machine_atsamd21.go +++ b/src/machine/machine_atsamd21.go @@ -10,8 +10,8 @@ import ( "bytes" "device/arm" "device/sam" - "encoding/binary" "errors" + "internal/binary" "runtime/interrupt" "unsafe" ) diff --git a/src/machine/machine_atsamd51.go b/src/machine/machine_atsamd51.go index fdc368a63..bcaaec721 100644 --- a/src/machine/machine_atsamd51.go +++ b/src/machine/machine_atsamd51.go @@ -10,8 +10,8 @@ import ( "bytes" "device/arm" "device/sam" - "encoding/binary" "errors" + "internal/binary" "runtime/interrupt" "unsafe" ) diff --git a/src/machine/machine_nrf.go b/src/machine/machine_nrf.go index 99c49d3ca..945700711 100644 --- a/src/machine/machine_nrf.go +++ b/src/machine/machine_nrf.go @@ -5,7 +5,7 @@ package machine import ( "bytes" "device/nrf" - "encoding/binary" + "internal/binary" "runtime/interrupt" "unsafe" ) diff --git a/src/machine/machine_stm32f4.go b/src/machine/machine_stm32f4.go index 42193a739..41e1b2204 100644 --- a/src/machine/machine_stm32f4.go +++ b/src/machine/machine_stm32f4.go @@ -6,8 +6,8 @@ package machine import ( "device/stm32" - "encoding/binary" "errors" + "internal/binary" "math/bits" "runtime/interrupt" "runtime/volatile" diff --git a/src/machine/machine_stm32l4.go b/src/machine/machine_stm32l4.go index 856320911..4eb3588f7 100644 --- a/src/machine/machine_stm32l4.go +++ b/src/machine/machine_stm32l4.go @@ -4,8 +4,8 @@ package machine import ( "device/stm32" - "encoding/binary" "errors" + "internal/binary" "runtime/interrupt" "runtime/volatile" "unsafe" diff --git a/src/machine/machine_stm32wlx.go b/src/machine/machine_stm32wlx.go index b27c779fe..84e302a10 100644 --- a/src/machine/machine_stm32wlx.go +++ b/src/machine/machine_stm32wlx.go @@ -6,8 +6,8 @@ package machine import ( "device/stm32" - "encoding/binary" "errors" + "internal/binary" "math/bits" "runtime/interrupt" "runtime/volatile" diff --git a/src/machine/usb/descriptor/configuration.go b/src/machine/usb/descriptor/configuration.go index d9446e673..efb9ab1d2 100644 --- a/src/machine/usb/descriptor/configuration.go +++ b/src/machine/usb/descriptor/configuration.go @@ -1,7 +1,7 @@ package descriptor import ( - "encoding/binary" + "internal/binary" ) const ( diff --git a/src/machine/usb/descriptor/device.go b/src/machine/usb/descriptor/device.go index 48229fbfd..0c3ee92f9 100644 --- a/src/machine/usb/descriptor/device.go +++ b/src/machine/usb/descriptor/device.go @@ -1,7 +1,7 @@ package descriptor import ( - "encoding/binary" + "internal/binary" ) const ( diff --git a/src/machine/usb/descriptor/endpoint.go b/src/machine/usb/descriptor/endpoint.go index affaffa0a..c7fa011fa 100644 --- a/src/machine/usb/descriptor/endpoint.go +++ b/src/machine/usb/descriptor/endpoint.go @@ -1,7 +1,7 @@ package descriptor import ( - "encoding/binary" + "internal/binary" ) var endpointEP1IN = [endpointTypeLen]byte{ diff --git a/src/machine/usb/descriptor/hid.go b/src/machine/usb/descriptor/hid.go index cdd4fc7e5..d8f86a41e 100644 --- a/src/machine/usb/descriptor/hid.go +++ b/src/machine/usb/descriptor/hid.go @@ -2,8 +2,8 @@ package descriptor import ( "bytes" - "encoding/binary" "errors" + "internal/binary" ) var configurationCDCHID = [configurationTypeLen]byte{ |