@@ -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/**
0 commit comments