diff --git a/src/ConstArray.js b/src/ConstArray.js index 89a08da..e73faca 100644 --- a/src/ConstArray.js +++ b/src/ConstArray.js @@ -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) { diff --git a/src/ConstDataView.js b/src/ConstDataView.js index ef32ce2..42721d8 100644 --- a/src/ConstDataView.js +++ b/src/ConstDataView.js @@ -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) { diff --git a/src/ConstString.js b/src/ConstString.js index 164cc72..69e1537 100644 --- a/src/ConstString.js +++ b/src/ConstString.js @@ -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) { diff --git a/src/Int.js b/src/Int.js index 83428b5..fb981ab 100644 --- a/src/Int.js +++ b/src/Int.js @@ -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': diff --git a/src/Sequence.js b/src/Sequence.js index 98c3a16..969fa1d 100644 --- a/src/Sequence.js +++ b/src/Sequence.js @@ -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 diff --git a/src/Struct.js b/src/Struct.js index 1a89d03..75224f5 100644 --- a/src/Struct.js +++ b/src/Struct.js @@ -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 diff --git a/src/Type.js b/src/Type.js index 97331a9..b30d554 100644 --- a/src/Type.js +++ b/src/Type.js @@ -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') { diff --git a/test/Struct.test.js b/test/Struct.test.js index d93973a..761a5fb 100644 --- a/test/Struct.test.js +++ b/test/Struct.test.js @@ -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) })