feat(Struct)

This commit is contained in:
2025-08-03 23:18:27 +03:00
parent 72f287a4e4
commit 38f0d922aa
15 changed files with 410 additions and 150 deletions

33
src/ConstString.js Normal file
View File

@ -0,0 +1,33 @@
import { memcpy } from "./mem"
import { Type } from "./Type"
export function ConstString(size) {
const obj = { _size: size }
Object.setPrototypeOf(obj, ConstString.prototype)
obj.new(ConstString, arguments)
return obj
}
ConstString.prototype.serialize = function(dv, src) {
const encoder = new TextEncoder('utf-8')
const encoded = new DataView(encoder.encode(src).buffer, 0, this._size)
if (dv.byteLength < encoded.byteLength) {
throw new Error('too small buffer')
}
memcpy(dv, encoded)
return
}
ConstString.prototype.parse = function(dv) {
const frame = new DataView(dv.buffer, dv.byteOffset, this._size)
const decoder = new TextDecoder('utf-8')
return decoder.decode(frame)
}
ConstString.prototype.isHeadless = function() {
return false
}
ConstString.prototype.sizeof = function() {
return this._size
}
Object.setPrototypeOf(ConstString.prototype, Type.prototype)
Object.freeze(ConstString.prototype)