Skip to content

Commit 0af0aa7

Browse files
committed
rlp: allow manually registering types implementing Encoder/Decoder
in tinygo
1 parent e2007e5 commit 0af0aa7

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

rlp/decode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func makeDecoder(typ reflect.Type, tags rlpstruct.Tags) (dec decoder, err error)
168168
return decodeU256NoPtr, nil
169169
case kind == reflect.Ptr:
170170
return makePtrDecoder(typ, tags)
171-
case reflect.PointerTo(typ).Implements(decoderInterface):
171+
case implementsInterface(reflect.PointerTo(typ), decoderInterface):
172172
return decodeDecoder, nil
173173
case isUint(kind):
174174
return decodeUint, nil
@@ -262,7 +262,7 @@ func decodeU256(s *Stream, val reflect.Value) error {
262262

263263
func makeListDecoder(typ reflect.Type, tag rlpstruct.Tags) (decoder, error) {
264264
etype := typ.Elem()
265-
if etype.Kind() == reflect.Uint8 && !reflect.PointerTo(etype).Implements(decoderInterface) {
265+
if etype.Kind() == reflect.Uint8 && !implementsInterface(reflect.PointerTo(etype), decoderInterface) {
266266
if typ.Kind() == reflect.Array {
267267
return decodeByteArray, nil
268268
}

rlp/encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func makeWriter(typ reflect.Type, ts rlpstruct.Tags) (writer, error) {
151151
return writeU256IntNoPtr, nil
152152
case kind == reflect.Ptr:
153153
return makePtrWriter(typ, ts)
154-
case reflect.PointerTo(typ).Implements(encoderInterface):
154+
case implementsInterface(reflect.PointerTo(typ), encoderInterface):
155155
return makeEncoderWriter(typ), nil
156156
case isUint(kind):
157157
return writeUint, nil
@@ -409,7 +409,7 @@ func makePtrWriter(typ reflect.Type, ts rlpstruct.Tags) (writer, error) {
409409
}
410410

411411
func makeEncoderWriter(typ reflect.Type) writer {
412-
if typ.Implements(encoderInterface) {
412+
if implementsInterface(typ, encoderInterface) {
413413
return func(val reflect.Value, w *encBuffer) error {
414414
return val.Interface().(Encoder).EncodeRLP(w)
415415
}

rlp/reflection.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//go:build !tinygo
18+
19+
package rlp
20+
21+
import "reflect"
22+
23+
func implementsInterface(t reflect.Type, i reflect.Type) bool {
24+
return t.Implements(i)
25+
}

rlp/reflection_tinygo.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//go:build tinygo
18+
19+
package rlp
20+
21+
import "reflect"
22+
23+
var customEncodedTypes = make(map[reflect.Type]bool)
24+
25+
func implementsInterface(t reflect.Type, i reflect.Type) bool {
26+
// reflect implementation of tinygo cannot handle this automatically
27+
// at runtime. So we need custom encoded types to be registered manually.
28+
if i == decoderInterface || i == encoderInterface {
29+
return customEncodedTypes[t]
30+
}
31+
return false
32+
}
33+
34+
// RegisterCustomEncodedType manually registers a type as an implementation of
35+
// Decoder and Encoder interfaces
36+
func RegisterCustomEncodedType(t reflect.Type) {
37+
customEncodedTypes[t] = true
38+
}

rlp/typecache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func rtypeToStructType(typ reflect.Type, rec map[reflect.Type]*rlpstruct.Type) *
199199
t := &rlpstruct.Type{
200200
Name: typ.String(),
201201
Kind: k,
202-
IsEncoder: typ.Implements(encoderInterface),
203-
IsDecoder: typ.Implements(decoderInterface),
202+
IsEncoder: implementsInterface(typ, encoderInterface),
203+
IsDecoder: implementsInterface(typ, decoderInterface),
204204
}
205205
rec[typ] = t
206206
if k == reflect.Array || k == reflect.Slice || k == reflect.Ptr {
@@ -234,5 +234,5 @@ func isUint(k reflect.Kind) bool {
234234
}
235235

236236
func isByte(typ reflect.Type) bool {
237-
return typ.Kind() == reflect.Uint8 && !typ.Implements(encoderInterface)
237+
return typ.Kind() == reflect.Uint8 && !implementsInterface(typ, encoderInterface)
238238
}

0 commit comments

Comments
 (0)