utilium 1.7.13 → 1.7.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  import { toUint8Array } from './buffer.js';
2
2
  import { _debugLog } from './debugging.js';
3
3
  import * as primitive from './internal/primitives.js';
4
- import { _polyfill_metadata, checkInstance, checkStruct, initMetadata, isCustom, isInstance, isStatic, } from './internal/struct.js';
4
+ import { _polyfill_metadata, checkInstance, checkStruct, initMetadata, isCustom, isInstance, isStatic, isStruct, } from './internal/struct.js';
5
5
  import { _throw } from './misc.js';
6
6
  import { capitalize } from './string.js';
7
7
  export * as Struct from './internal/struct.js';
@@ -20,9 +20,6 @@ export function sizeof(type) {
20
20
  }
21
21
  // primitive or character
22
22
  if (typeof type == 'string') {
23
- // Single character inside string, since sizeof(string) -> sizeof(string[0]) -> here
24
- if (type.length == 1)
25
- return 1;
26
23
  primitive.checkValid(type);
27
24
  return (+primitive.normalize(type).match(primitive.regex)[2] / 8);
28
25
  }
@@ -32,14 +29,15 @@ export function sizeof(type) {
32
29
  const constructor = isStatic(type) ? type : type.constructor;
33
30
  _polyfill_metadata(constructor);
34
31
  const { struct } = constructor[Symbol.metadata];
35
- if (isStatic(type))
36
- return struct.staticSize;
37
32
  let size = struct.staticSize;
33
+ if (isStatic(type))
34
+ return size;
38
35
  for (const member of struct.members.values()) {
39
36
  if (typeof member.length != 'string')
40
37
  continue;
41
38
  for (let i = 0; i < type[member.length]; i++) {
42
- size += sizeof(type[member.name][i]);
39
+ const value = type[member.name][i];
40
+ size += sizeof(isStruct(value) ? value : member.type);
43
41
  }
44
42
  }
45
43
  return size;
@@ -60,7 +58,8 @@ export function offsetof(type, memberName) {
60
58
  for (const member of struct.members.values()) {
61
59
  if (member.name == memberName)
62
60
  return offset;
63
- offset += sizeof(type[member.name]);
61
+ const value = type[member.name];
62
+ offset += sizeof(isStruct(value) ? value : member.type);
64
63
  }
65
64
  throw new Error('Struct does not have member: ' + memberName);
66
65
  }
@@ -98,7 +97,7 @@ export function struct(options = {}) {
98
97
  decl,
99
98
  });
100
99
  const memberSize = typeof length == 'string' ? 0 : sizeof(type) * (length || 1);
101
- isDynamic ||= typeof length == 'string';
100
+ isDynamic ||= isStatic(type) ? type[Symbol.metadata].struct.isDynamic : typeof length == 'string';
102
101
  staticSize = options.isUnion ? Math.max(staticSize, memberSize) : staticSize + memberSize;
103
102
  staticSize = align(staticSize, options.align || 1);
104
103
  _debugLog('define', target.name + '.' + name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.13",
3
+ "version": "1.7.15",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/struct.ts CHANGED
@@ -22,6 +22,7 @@ import {
22
22
  isCustom,
23
23
  isInstance,
24
24
  isStatic,
25
+ isStruct,
25
26
  } from './internal/struct.js';
26
27
  import { _throw } from './misc.js';
27
28
  import { capitalize } from './string.js';
@@ -46,9 +47,6 @@ export function sizeof<T extends TypeLike>(type: T | T[]): Size<T> {
46
47
 
47
48
  // primitive or character
48
49
  if (typeof type == 'string') {
49
- // Single character inside string, since sizeof(string) -> sizeof(string[0]) -> here
50
- if (type.length == 1) return 1 as Size<T>;
51
-
52
50
  primitive.checkValid(type);
53
51
 
54
52
  return (+primitive.normalize(type).match(primitive.regex)![2] / 8) as Size<T>;
@@ -62,14 +60,16 @@ export function sizeof<T extends TypeLike>(type: T | T[]): Size<T> {
62
60
  _polyfill_metadata(constructor);
63
61
  const { struct } = constructor[Symbol.metadata];
64
62
 
65
- if (isStatic(type)) return struct.staticSize as Size<T>;
66
-
67
63
  let size = struct.staticSize;
68
64
 
65
+ if (isStatic(type)) return size as Size<T>;
66
+
69
67
  for (const member of struct.members.values()) {
70
68
  if (typeof member.length != 'string') continue;
71
69
  for (let i = 0; i < (type as any)[member.length]; i++) {
72
- size += sizeof((type as any)[member.name][i]);
70
+ const value = (type as any)[member.name][i];
71
+
72
+ size += sizeof(isStruct(value) ? value : member.type);
73
73
  }
74
74
  }
75
75
 
@@ -98,7 +98,8 @@ export function offsetof(type: StaticLike | InstanceLike, memberName: string): n
98
98
 
99
99
  for (const member of struct.members.values()) {
100
100
  if (member.name == memberName) return offset;
101
- offset += sizeof((type as any)[member.name]);
101
+ const value = (type as any)[member.name];
102
+ offset += sizeof(isStruct(value) ? value : member.type);
102
103
  }
103
104
 
104
105
  throw new Error('Struct does not have member: ' + memberName);
@@ -149,7 +150,7 @@ export function struct(options: Partial<Options> = {}) {
149
150
  });
150
151
 
151
152
  const memberSize = typeof length == 'string' ? 0 : sizeof(type) * (length || 1);
152
- isDynamic ||= typeof length == 'string';
153
+ isDynamic ||= isStatic(type) ? type[Symbol.metadata].struct.isDynamic : typeof length == 'string';
153
154
  staticSize = options.isUnion ? Math.max(staticSize, memberSize) : staticSize + memberSize;
154
155
  staticSize = align(staticSize, options.align || 1);
155
156