Skip to content

Commit 164a21a

Browse files
committed
Removed required name from decorator variants
1 parent 6901eb4 commit 164a21a

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/decorators.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ interface Metadata {
5656
/**
5757
* Decorates a class as a struct.
5858
*/
59-
export function struct(this: Function | Options | void, name: string, ...options: Options[]) {
59+
export function struct(this: Function | Options | void, ...options: Options[]) {
6060
const opts = options.reduce((acc, opt) => ({ ...acc, ...opt }), {});
6161
if (typeof this == 'object') Object.assign(opts, this);
6262

@@ -126,25 +126,26 @@ export function struct(this: Function | Options | void, name: string, ...options
126126
value,
127127
});
128128

129-
Object.defineProperties(_struct, {
130-
name: fix(name),
131-
size: fix(size),
132-
alignment: fix(opts.alignment),
133-
isUnion: fix(!!opts.isUnion),
134-
fields: fix(fields),
135-
// @ts-expect-error 2511 : Please don't try to create an instance of an abstract struct
136-
get: fix((buffer: ArrayBufferLike, offset: number) => new _struct(buffer, offset)),
137-
set: fix((buffer: ArrayBufferLike, offset: number, value: InstanceType<T>) => {
138-
const source = new Uint8Array(value.buffer, value.byteOffset, size);
139-
const target = new Uint8Array(buffer, offset, size);
140-
if (value.buffer === buffer && value.byteOffset === offset) return;
141-
for (let i = 0; i < size; i++) target[i] = source[i];
142-
}),
143-
[Symbol.toStringTag]: fix(`[struct ${name}]`),
129+
context.addInitializer(function () {
130+
Object.defineProperties(_struct, {
131+
size: fix(size),
132+
alignment: fix(opts.alignment),
133+
isUnion: fix(!!opts.isUnion),
134+
fields: fix(fields),
135+
// @ts-expect-error 2511 : Please don't try to create an instance of an abstract struct
136+
get: fix((buffer: ArrayBufferLike, offset: number) => new _struct(buffer, offset)),
137+
set: fix((buffer: ArrayBufferLike, offset: number, value: InstanceType<T>) => {
138+
const source = new Uint8Array(value.buffer, value.byteOffset, size);
139+
const target = new Uint8Array(buffer, offset, size);
140+
if (value.buffer === buffer && value.byteOffset === offset) return;
141+
for (let i = 0; i < size; i++) target[i] = source[i];
142+
}),
143+
[Symbol.toStringTag]: fix(`[struct ${this.name}]`),
144+
});
145+
146+
registerType(_struct as unknown as Type<InstanceType<T>>);
144147
});
145148

146-
registerType(_struct as unknown as Type<InstanceType<T>>);
147-
148149
return _struct;
149150
};
150151
}
@@ -164,8 +165,8 @@ export interface UnionOptions {
164165
/**
165166
* Decorates a class as a union.
166167
*/
167-
export function union(name: string, options: UnionOptions = {}) {
168-
return struct(name, { ...options, isUnion: true });
168+
export function union(options: UnionOptions = {}) {
169+
return struct({ ...options, isUnion: true });
169170
}
170171

171172
/**

tests/struct-decorators-dynamic.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { encodeASCII } from 'utilium/string.js';
55
import { $from, field, struct, types as t } from '../src/decorators.js';
66
import { sizeof } from '../src/misc.js';
77

8-
@struct.packed('Duck')
8+
@struct.packed()
99
class Duck extends $from.typed(Uint8Array) {
1010
@t.uint8 public accessor name_length: number = 0;
1111
@t.char(64, { countedBy: 'name_length' }) public accessor name!: Uint8Array;
@@ -16,7 +16,7 @@ class Duck extends $from.typed(Uint8Array) {
1616

1717
assert.equal(sizeof(Duck), 77);
1818

19-
@struct.packed('MamaDuck')
19+
@struct.packed()
2020
class MamaDuck extends Duck {
2121
@t.uint16 public accessor n_ducklings: number = 0;
2222

0 commit comments

Comments
 (0)