janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.38k stars 217 forks source link

Add buffer/push-* sized int and float #1359

Closed pnelson closed 6 months ago

pnelson commented 6 months ago

I suspect many binary protocols need to encode/decode floats to/from big/little endian. Here is a first draft, should it be included in the standard library.

I am not tied to the binary module as I'm aware there exists buffer/* functions that are similar and it could possibly be bundled into the existing buffer module. I was concerned about the naming because buffer/push-word already exists and the signature is different with no control over endianness. I thought it might be confusing to add buffer/push-uint32 in that case - but maybe not.

Additionally, binary/write-float32 and binary/write-float64 could potentially be removed if we implement something like math/float32-bits and math/float64-bits respectively for constructing sized uints from the underlying bits and then lean on binary/write-uint32 and binary/write-uint64 to write to the buffer.

I just wanted to start these discussions and fix any outstanding bugs that may be present.

bakpakin commented 6 months ago

This should almost certainly go in the buffer module, but otherwise seems reasonable.

The ffi module and peg module have some of this functionality but with other limitations. I would prefer if this were just folded into the buffer module as "binary" isn't particularly descriptive for a module name.