This commit is contained in:
2025-08-06 16:22:19 +03:00
parent 16789a942e
commit 40dc6a2637
20 changed files with 250 additions and 183 deletions

View File

@ -1,12 +1,12 @@
import { isHeadless, parse, serialize, sizeof, sizeofHead, Type } from "."
import { isHeadless, parse, serialize, sizeof, sizeofHead, Type } from '.'
export function Struct(type_obj) {
const obj = {}
Object.setPrototypeOf(obj, Struct.prototype)
obj._info_by_key = new Map
obj._info_by_key = new Map()
obj._headless = false
const arg = {}
let offset = 0
@ -18,7 +18,7 @@ export function Struct(type_obj) {
types = [value]
}
arg[key] = types.map(type => type.name)
arg[key] = types.map((type) => type.name)
const headless = isHeadless(...types)
obj._info_by_key.set(key, {
@ -35,12 +35,12 @@ export function Struct(type_obj) {
return obj
}
Struct.prototype.serialize = function(dv, src) {
Struct.prototype.serialize = function (dv, src) {
let data_offset = this._size
for (const [key, value] of Object.entries(src)) {
const info = this._info_by_key.get(key)
if (info.headless) {
dv.setUint32(info.offset, data_offset)
const frame = new DataView(dv.buffer, dv.byteOffset + data_offset)
@ -52,10 +52,10 @@ Struct.prototype.serialize = function(dv, src) {
}
}
}
Struct.prototype.parse = function(dv) {
Struct.prototype.parse = function (dv) {
const res = {}
for (const [key, info] of this._info_by_key.entries()) {
for (const [key, info] of this._info_by_key.entries()) {
if (info.headless) {
const data_offset = dv.getUint32(info.offset)
const frame = new DataView(dv.buffer, dv.byteOffset + data_offset)
@ -68,10 +68,10 @@ Struct.prototype.parse = function(dv) {
return res
}
Struct.prototype.isHeadless = function() {
Struct.prototype.isHeadless = function () {
return this._headless
}
Struct.prototype.sizeof = function(arg) {
Struct.prototype.sizeof = function (arg) {
if (this._headless) {
if (arg === undefined) {
throw new Error('unknown size of ' + this)