utilium 1.7.4 → 1.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/struct.js +7 -3
- package/package.json +1 -1
- package/src/struct.ts +9 -5
package/dist/struct.js
CHANGED
@@ -58,9 +58,11 @@ function _memberLength(struct, length, name) {
|
|
58
58
|
}
|
59
59
|
/** Compute the size of a struct including dynamically sized members */
|
60
60
|
function _structSize() {
|
61
|
-
const { staticSize, members } = this[symbol_metadata(this)].struct;
|
61
|
+
const { staticSize, members } = this.constructor[symbol_metadata(this.constructor)].struct;
|
62
62
|
let size = staticSize;
|
63
63
|
for (const [name, { type, length: key }] of members) {
|
64
|
+
if (typeof key != 'string')
|
65
|
+
continue;
|
64
66
|
size += sizeof(type) * _memberLength(this, key, name);
|
65
67
|
}
|
66
68
|
return size;
|
@@ -117,13 +119,15 @@ export function member(type, length) {
|
|
117
119
|
* Serializes a struct into a Uint8Array
|
118
120
|
*/
|
119
121
|
export function serialize(instance) {
|
122
|
+
if (isCustom(instance))
|
123
|
+
return instance[Symbol.serialize]();
|
120
124
|
checkInstance(instance);
|
121
125
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)].struct;
|
122
126
|
const buffer = new Uint8Array(sizeof(instance));
|
123
127
|
const view = new DataView(buffer.buffer);
|
124
128
|
// for unions we should write members in ascending last modified order, but we don't have that info.
|
125
129
|
for (const [name, { type, length: rawLength, offset }] of members) {
|
126
|
-
const length = _memberLength(instance
|
130
|
+
const length = _memberLength(instance, rawLength, name);
|
127
131
|
for (let i = 0; i < (length || 1); i++) {
|
128
132
|
const iOff = offset + sizeof(type) * i;
|
129
133
|
let value = length > 0 ? instance[name][i] : instance[name];
|
@@ -174,7 +178,7 @@ export function deserialize(instance, _buffer) {
|
|
174
178
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)].struct;
|
175
179
|
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
176
180
|
for (const [name, { type, offset, length: rawLength }] of members) {
|
177
|
-
const length = _memberLength(instance
|
181
|
+
const length = _memberLength(instance, rawLength, name);
|
178
182
|
for (let i = 0; i < (length || 1); i++) {
|
179
183
|
let object = length > 0 ? instance[name] : instance;
|
180
184
|
const key = length > 0 ? i : name, iOff = offset + sizeof(type) * i;
|
package/package.json
CHANGED
package/src/struct.ts
CHANGED
@@ -2,6 +2,7 @@ import { toUint8Array } from './buffer.js';
|
|
2
2
|
import * as primitive from './internal/primitives.js';
|
3
3
|
import type {
|
4
4
|
DecoratorContext,
|
5
|
+
Instance,
|
5
6
|
InstanceLike,
|
6
7
|
Member,
|
7
8
|
MemberContext,
|
@@ -66,7 +67,7 @@ export function align(value: number, alignment: number): number {
|
|
66
67
|
* @param name The name of the array field— only used for errors
|
67
68
|
*/
|
68
69
|
function _memberLength<T extends Metadata>(
|
69
|
-
struct:
|
70
|
+
struct: Instance<T>,
|
70
71
|
length: string | number | undefined,
|
71
72
|
name: string
|
72
73
|
): number {
|
@@ -86,12 +87,13 @@ function _memberLength<T extends Metadata>(
|
|
86
87
|
}
|
87
88
|
|
88
89
|
/** Compute the size of a struct including dynamically sized members */
|
89
|
-
function _structSize<T extends Metadata>(this:
|
90
|
-
const { staticSize, members } = this[symbol_metadata(this)].struct;
|
90
|
+
function _structSize<T extends Metadata>(this: Instance<T>) {
|
91
|
+
const { staticSize, members } = this.constructor[symbol_metadata(this.constructor)].struct;
|
91
92
|
|
92
93
|
let size = staticSize;
|
93
94
|
|
94
95
|
for (const [name, { type, length: key }] of members) {
|
96
|
+
if (typeof key != 'string') continue;
|
95
97
|
size += sizeof(type) * _memberLength(this, key, name);
|
96
98
|
}
|
97
99
|
|
@@ -160,6 +162,8 @@ export function member(type: primitive.Valid | ClassLike, length?: number | stri
|
|
160
162
|
* Serializes a struct into a Uint8Array
|
161
163
|
*/
|
162
164
|
export function serialize(instance: unknown): Uint8Array {
|
165
|
+
if (isCustom(instance)) return instance[Symbol.serialize]!();
|
166
|
+
|
163
167
|
checkInstance(instance);
|
164
168
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)].struct;
|
165
169
|
|
@@ -168,7 +172,7 @@ export function serialize(instance: unknown): Uint8Array {
|
|
168
172
|
|
169
173
|
// for unions we should write members in ascending last modified order, but we don't have that info.
|
170
174
|
for (const [name, { type, length: rawLength, offset }] of members) {
|
171
|
-
const length = _memberLength(instance
|
175
|
+
const length = _memberLength(instance, rawLength, name);
|
172
176
|
for (let i = 0; i < (length || 1); i++) {
|
173
177
|
const iOff = offset + sizeof(type) * i;
|
174
178
|
|
@@ -233,7 +237,7 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
|
|
233
237
|
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
234
238
|
|
235
239
|
for (const [name, { type, offset, length: rawLength }] of members) {
|
236
|
-
const length = _memberLength(instance
|
240
|
+
const length = _memberLength(instance, rawLength, name);
|
237
241
|
for (let i = 0; i < (length || 1); i++) {
|
238
242
|
let object = length > 0 ? instance[name] : instance;
|
239
243
|
const key = length > 0 ? i : name,
|