fix(names), feat(Struct): serialize validation

This commit is contained in:
2025-08-09 17:44:19 +03:00
parent 9c5e13f91c
commit 46284a8d2a
8 changed files with 14 additions and 9 deletions

View File

@ -11,7 +11,7 @@ import {
export function ConstArray(size) {
const obj = { _size: size }
Object.setPrototypeOf(obj, ConstArray.prototype)
obj.new(ConstArray, arguments)
obj.new('ConstArray', arguments)
return obj
}
ConstArray.prototype.serialize = function (dv, src, ...inner_types) {

View File

@ -3,7 +3,7 @@ import { memcpy, Type } from '.'
export function ConstDataView(size) {
const obj = { _size: size }
Object.setPrototypeOf(obj, ConstDataView.prototype)
obj.new(ConstDataView, arguments)
obj.new('ConstDataView', arguments)
return obj
}
ConstDataView.prototype.serialize = function (dv, src) {

View File

@ -4,7 +4,7 @@ import { Type } from './Type'
export function ConstString(size) {
const obj = { _size: size }
Object.setPrototypeOf(obj, ConstString.prototype)
obj.new(ConstString, arguments)
obj.new('ConstString', arguments)
return obj
}
ConstString.prototype.serialize = function (dv, src) {

View File

@ -3,7 +3,7 @@ import { Type, limits } from '.'
export function Int(bits, sign) {
const obj = { _size: bits / 8 }
Object.setPrototypeOf(obj, Int.prototype)
obj.new(Int, arguments)
obj.new('Int', arguments)
switch (sign) {
case 'signed':

View File

@ -4,7 +4,7 @@ import { Type } from './Type'
export function Sequence(types) {
const obj = { _types: types, _headless: false, _sizeof: 0 }
Object.setPrototypeOf(obj, Sequence.prototype)
obj.new(Sequence, [
obj.new('Sequence', [
types.map((arg) => {
if (typeof arg == 'function') {
return arg.name

View File

@ -37,7 +37,7 @@ export function Struct(type_obj) {
}
obj._size = offset
obj.new(Struct, [arg])
obj.new('Struct', [arg])
return obj
}
@ -47,6 +47,9 @@ Struct.prototype.serialize = function (dv, src) {
for (const [key, value] of Object.entries(src)) {
const info = this._info_by_key.get(key)
if (info == undefined) {
throw new Error(this._name + " doesn't have key " + key)
}
if (info.headless) {
dv.setUint32(info.offset, data_offset)
const frame = new DataView(dv.buffer, dv.byteOffset + data_offset)
@ -80,7 +83,7 @@ Struct.prototype.isHeadless = function () {
Struct.prototype.sizeof = function (arg) {
if (this._headless) {
if (arg === undefined) {
throw new Error('unknown size of ' + this)
throw new Error('unknown size of ' + this._name)
}
let size = this._size

View File

@ -3,8 +3,8 @@ export function Type() {
Object.setPrototypeOf(obj, Type.prototype)
return obj
}
Type.prototype.new = function (func, args) {
this._name = func.name
Type.prototype.new = function (name, args) {
this._name = name
if (args !== undefined) {
const str_args = Array.from(args).map((arg) => {
if (typeof arg == 'function') {

View File

@ -40,6 +40,8 @@ describe(Struct.name, () => {
const dv = sizedDataView(sizeof(Vector2))
expect(16).toEqual(dv.byteLength)
expect(() => serialize(dv, { x: 1, z: 2 }, Vector2)).toThrow()
serialize(dv, vector2, Vector2)
expectDataViewEqual(dv, vector2_dv)
})