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) { export function ConstArray(size) {
const obj = { _size: size } const obj = { _size: size }
Object.setPrototypeOf(obj, ConstArray.prototype) Object.setPrototypeOf(obj, ConstArray.prototype)
obj.new(ConstArray, arguments) obj.new('ConstArray', arguments)
return obj return obj
} }
ConstArray.prototype.serialize = function (dv, src, ...inner_types) { ConstArray.prototype.serialize = function (dv, src, ...inner_types) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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