cbor icon indicating copy to clipboard operation
cbor copied to clipboard

bug: complete TinyGo workaround for encode

Open seedhammer opened this issue 5 months ago • 4 comments

What version of fxamacker/cbor are you using?

feature/cbor-tinygo-beta

Does this issue reproduce with the latest release?

N/A

What OS and CPU architecture are you using (go env)?

TinyGo v0.38.0

What did you do?

Using the Marshal method to encode a Go value into CBOR.

What did you expect to see?

No panics.

What did you see instead?

On TinyGo, I sometimes see "reflect: unimplemented: AssignableTo with interface" panics. Not always, presumably because of build time optimizations by TinyGo.

FWIW, I've hacked together a change on top of the feature/cbor/cbor-tinygo-beta branch that seems to fix my issues: https://github.com/seedhammer/cbor/commit/1e5ed28b4fb5e7dad61e03d61d59530848d5152b. It just blindly replaces calls to reflect.Type.Implements with the implements function specialized for TinyGo.

seedhammer avatar Jul 19 '25 08:07 seedhammer

@seedhammer thanks for opening this issue!

I think this problem might get fixed at TinyGo:

  • https://github.com/tinygo-org/tinygo/issues/4277

This year, new comments in the TinyGo issue mention the same problem with encoding/json too:

FWIW, I've hacked together a change on top of the feature/cbor/cbor-tinygo-beta branch that seems to fix my issues: https://github.com/seedhammer/cbor/commit/1e5ed28b4fb5e7dad61e03d61d59530848d5152b. It just blindly replaces calls to reflect.Type.Implements with the implements function specialized for TinyGo.

That is actually very interesting! I'm not familiar with the lowercase implements function specialized for TinyGo. Can you share more info or link to docs about that function? 🙏

fxamacker avatar Jul 21 '25 01:07 fxamacker

Uh, yeah, I can't find any source defining implements in TinyGo and yet @seedhammer's patch makes my use of cbor work...

progrium avatar Jul 24 '25 23:07 progrium

Mystery solved: https://github.com/fxamacker/cbor/blob/feature/cbor-tinygo-beta/decode_tinygo.go#L22

It returns false.

progrium avatar Jul 24 '25 23:07 progrium

That is actually very interesting! I'm not familiar with the lowercase implements function specialized for TinyGo. Can you share more info or link to docs about that function? 🙏

Uh, yeah, I can't find any source defining implements in TinyGo and yet @seedhammer's patch makes my use of cbor work...

Mystery solved: https://github.com/fxamacker/cbor/blob/feature/cbor-tinygo-beta/decode_tinygo.go#L22

It returns false.

Thanks for solving the mystery!

Yeah, this is funny. I forgot about the one-liner I wrote on Sep 8, 2024 to return false.

It might be OK to use this workaround as long as the application doesn't need to implement:

  • cbor.Marhsaler
  • cbor.Unmarshaler
  • or other supported interfaces like binary.Marshaler, etc.

It is unfortunate this issue hasn't been fixed yet at TinyGo:

  • https://github.com/tinygo-org/tinygo/issues/4277

I'll leave this issue open for now.

fxamacker avatar Jul 28 '25 00:07 fxamacker