utilium 1.7.14 → 1.7.16

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
+ if (isInstance(member) && member.constructor[Symbol.metadata].struct.isDynamic) {
37
+ if (struct.isUnion)
38
+ size = Math.max(size, sizeof(member));
39
+ else
40
+ size += sizeof(member);
41
+ continue;
42
+ }
36
43
  if (typeof member.length != 'string')
37
44
  continue;
45
+ let subSize = 0;
38
46
  for (let i = 0; i < type[member.length]; i++) {
39
47
  const value = type[member.name][i];
40
- size += sizeof(isStruct(value) ? value : member.type);
48
+ subSize += sizeof(isStruct(value) ? value : 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);
100
- isDynamic ||= typeof length == 'string';
111
+ const memberSize = typeof length == 'string' || (isStatic(type) && type[Symbol.metadata].struct.isDynamic)
112
+ ? 0
113
+ : sizeof(type) * (length || 1);
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.14",
3
+ "version": "1.7.16",
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
+ if (isInstance(member) && member.constructor[Symbol.metadata].struct.isDynamic) {
69
+ if (struct.isUnion) size = Math.max(size, sizeof(member));
70
+ else size += sizeof(member);
71
+ continue;
72
+ }
73
+
68
74
  if (typeof member.length != 'string') continue;
75
+
76
+ let subSize = 0;
77
+
69
78
  for (let i = 0; i < (type as any)[member.length]; i++) {
70
79
  const value = (type as any)[member.name][i];
71
80
 
72
- size += sizeof(isStruct(value) ? value : member.type);
81
+ subSize += sizeof(isStruct(value) ? value : 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,15 +162,25 @@ export function struct(options: Partial<Options> = {}) {
149
162
  decl,
150
163
  });
151
164
 
152
- const memberSize = typeof length == 'string' ? 0 : sizeof(type) * (length || 1);
153
- isDynamic ||= typeof length == 'string';
165
+ const memberSize =
166
+ typeof length == 'string' || (isStatic(type) && type[Symbol.metadata].struct.isDynamic)
167
+ ? 0
168
+ : sizeof(type) * (length || 1);
169
+
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);
156
173
 
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
  };