feat(Boolean)

This commit is contained in:
2025-08-05 20:08:01 +03:00
parent 4a5a0e4f3c
commit 5d7534dd44
3 changed files with 37 additions and 4 deletions

View File

@ -12,6 +12,13 @@ export { limits, memcpy, Type, Int, ConstString, ConstArray, ConstDataView, Stru
export function serialize(dv, src, ...types) {
const [type, ...inner_types] = types
if (type == Boolean && typeof src == 'boolean') {
if (dv.byteLength < 1) {
throw new Error('too small buffer')
}
dv.setUint8(0, src ? 1 : 0)
return
}
if (type == Number && typeof src == 'number') {
if (dv.byteLength < 8) {
throw new Error('too small buffer')
@ -68,6 +75,7 @@ export function serialize(dv, src, ...types) {
const frame = new DataView(dv.buffer, dv.byteOffset + 4)
memcpy(frame, src)
return
}
if (type instanceof Type) {
type.serialize(dv, src, ...inner_types)
@ -78,6 +86,9 @@ export function serialize(dv, src, ...types) {
export function parse(dv, ...types) {
const [type, ...inner_types] = types
if (type == Boolean) {
return !!dv.getUint8(0)
}
if (type == Number) {
return dv.getFloat64(0)
}
@ -138,6 +149,9 @@ export function sizeofHead(...args) {
export function sizeof(...args) {
const [arg, ...inner_args] = args
if (arg == Boolean || typeof arg == 'boolean') {
return 1
}
if (arg == Number || typeof arg == 'number') {
return 8
}