utilium 1.7.15 → 1.7.17

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.
@@ -41,7 +41,10 @@ export interface Metadata {
41
41
  options: Partial<Options>;
42
42
  members: Map<string, Member>;
43
43
  staticSize: number;
44
+ /** Whether the struct is dynamically sized */
44
45
  isDynamic: boolean;
46
+ /** Whether the struct is a union */
47
+ isUnion: boolean;
45
48
  }
46
49
  type _DecoratorMetadata<T extends Metadata = Metadata> = DecoratorMetadata & {
47
50
  struct?: T;
package/dist/struct.js CHANGED
@@ -33,12 +33,24 @@ export function sizeof(type) {
33
33
  if (isStatic(type))
34
34
  return size;
35
35
  for (const member of struct.members.values()) {
36
+ const value = type[member.name];
37
+ if (isInstance(value) && value.constructor[Symbol.metadata].struct.isDynamic) {
38
+ if (struct.isUnion)
39
+ size = Math.max(size, sizeof(value));
40
+ else
41
+ size += sizeof(value);
42
+ continue;
43
+ }
36
44
  if (typeof member.length != 'string')
37
45
  continue;
46
+ let subSize = 0;
38
47
  for (let i = 0; i < type[member.length]; i++) {
39
- const value = type[member.name][i];
40
- size += sizeof(isStruct(value) ? value : member.type);
48
+ subSize += sizeof(isStruct(value[i]) ? value[i] : member.type);
41
49
  }
50
+ if (struct.isUnion)
51
+ size = Math.max(size, subSize);
52
+ else
53
+ size += subSize;
42
54
  }
43
55
  return size;
44
56
  }
@@ -96,13 +108,21 @@ export function struct(options = {}) {
96
108
  length,
97
109
  decl,
98
110
  });
99
- const memberSize = typeof length == 'string' ? 0 : sizeof(type) * (length || 1);
111
+ const memberSize = typeof length == 'string' || (isStatic(type) && type[Symbol.metadata].struct.isDynamic)
112
+ ? 0
113
+ : sizeof(type) * (length || 1);
100
114
  isDynamic ||= isStatic(type) ? type[Symbol.metadata].struct.isDynamic : typeof length == 'string';
101
115
  staticSize = options.isUnion ? Math.max(staticSize, memberSize) : staticSize + memberSize;
102
116
  staticSize = align(staticSize, options.align || 1);
103
117
  _debugLog('define', target.name + '.' + name);
104
118
  }
105
- context.metadata.struct = { options, members, staticSize, isDynamic };
119
+ context.metadata.struct = {
120
+ options,
121
+ members,
122
+ staticSize,
123
+ isDynamic,
124
+ isUnion: options.isUnion ?? false,
125
+ };
106
126
  return target;
107
127
  };
108
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.15",
3
+ "version": "1.7.17",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -63,7 +63,12 @@ export interface Metadata {
63
63
  options: Partial<Options>;
64
64
  members: Map<string, Member>;
65
65
  staticSize: number;
66
+
67
+ /** Whether the struct is dynamically sized */
66
68
  isDynamic: boolean;
69
+
70
+ /** Whether the struct is a union */
71
+ isUnion: boolean;
67
72
  }
68
73
 
69
74
  type _DecoratorMetadata<T extends Metadata = Metadata> = DecoratorMetadata & {
package/src/struct.ts CHANGED
@@ -65,12 +65,24 @@ export function sizeof<T extends TypeLike>(type: T | T[]): Size<T> {
65
65
  if (isStatic(type)) return size as Size<T>;
66
66
 
67
67
  for (const member of struct.members.values()) {
68
+ const value = (type as any)[member.name];
69
+
70
+ if (isInstance(value) && value.constructor[Symbol.metadata].struct.isDynamic) {
71
+ if (struct.isUnion) size = Math.max(size, sizeof(value));
72
+ else size += sizeof(value);
73
+ continue;
74
+ }
75
+
68
76
  if (typeof member.length != 'string') continue;
69
- for (let i = 0; i < (type as any)[member.length]; i++) {
70
- const value = (type as any)[member.name][i];
71
77
 
72
- size += sizeof(isStruct(value) ? value : member.type);
78
+ let subSize = 0;
79
+
80
+ for (let i = 0; i < (type as any)[member.length]; i++) {
81
+ subSize += sizeof(isStruct(value[i]) ? value[i] : member.type);
73
82
  }
83
+
84
+ if (struct.isUnion) size = Math.max(size, subSize);
85
+ else size += subSize;
74
86
  }
75
87
 
76
88
  return size as Size<T>;
@@ -98,6 +110,7 @@ export function offsetof(type: StaticLike | InstanceLike, memberName: string): n
98
110
 
99
111
  for (const member of struct.members.values()) {
100
112
  if (member.name == memberName) return offset;
113
+
101
114
  const value = (type as any)[member.name];
102
115
  offset += sizeof(isStruct(value) ? value : member.type);
103
116
  }
@@ -149,7 +162,11 @@ export function struct(options: Partial<Options> = {}) {
149
162
  decl,
150
163
  });
151
164
 
152
- const memberSize = typeof length == 'string' ? 0 : sizeof(type) * (length || 1);
165
+ const memberSize =
166
+ typeof length == 'string' || (isStatic(type) && type[Symbol.metadata].struct.isDynamic)
167
+ ? 0
168
+ : sizeof(type) * (length || 1);
169
+
153
170
  isDynamic ||= isStatic(type) ? type[Symbol.metadata].struct.isDynamic : typeof length == 'string';
154
171
  staticSize = options.isUnion ? Math.max(staticSize, memberSize) : staticSize + memberSize;
155
172
  staticSize = align(staticSize, options.align || 1);
@@ -157,7 +174,13 @@ export function struct(options: Partial<Options> = {}) {
157
174
  _debugLog('define', target.name + '.' + name);
158
175
  }
159
176
 
160
- context.metadata.struct = { options, members, staticSize, isDynamic } satisfies Metadata;
177
+ context.metadata.struct = {
178
+ options,
179
+ members,
180
+ staticSize,
181
+ isDynamic,
182
+ isUnion: options.isUnion ?? false,
183
+ } satisfies Metadata;
161
184
 
162
185
  return target;
163
186
  };