utilium 1.7.12 → 1.7.14

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';
@@ -18,7 +18,7 @@ export function sizeof(type) {
18
18
  }
19
19
  return size;
20
20
  }
21
- // primitive
21
+ // primitive or character
22
22
  if (typeof type == 'string') {
23
23
  primitive.checkValid(type);
24
24
  return (+primitive.normalize(type).match(primitive.regex)[2] / 8);
@@ -29,14 +29,15 @@ export function sizeof(type) {
29
29
  const constructor = isStatic(type) ? type : type.constructor;
30
30
  _polyfill_metadata(constructor);
31
31
  const { struct } = constructor[Symbol.metadata];
32
- if (isStatic(type))
33
- return struct.staticSize;
34
32
  let size = struct.staticSize;
33
+ if (isStatic(type))
34
+ return size;
35
35
  for (const member of struct.members.values()) {
36
36
  if (typeof member.length != 'string')
37
37
  continue;
38
38
  for (let i = 0; i < type[member.length]; i++) {
39
- size += sizeof(type[member.name][i]);
39
+ const value = type[member.name][i];
40
+ size += sizeof(isStruct(value) ? value : member.type);
40
41
  }
41
42
  }
42
43
  return size;
@@ -57,7 +58,8 @@ export function offsetof(type, memberName) {
57
58
  for (const member of struct.members.values()) {
58
59
  if (member.name == memberName)
59
60
  return offset;
60
- offset += sizeof(type[member.name]);
61
+ const value = type[member.name];
62
+ offset += sizeof(isStruct(value) ? value : member.type);
61
63
  }
62
64
  throw new Error('Struct does not have member: ' + memberName);
63
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.12",
3
+ "version": "1.7.14",
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';
@@ -44,7 +45,7 @@ export function sizeof<T extends TypeLike>(type: T | T[]): Size<T> {
44
45
  return size as Size<T>;
45
46
  }
46
47
 
47
- // primitive
48
+ // primitive or character
48
49
  if (typeof type == 'string') {
49
50
  primitive.checkValid(type);
50
51
 
@@ -59,14 +60,16 @@ export function sizeof<T extends TypeLike>(type: T | T[]): Size<T> {
59
60
  _polyfill_metadata(constructor);
60
61
  const { struct } = constructor[Symbol.metadata];
61
62
 
62
- if (isStatic(type)) return struct.staticSize as Size<T>;
63
-
64
63
  let size = struct.staticSize;
65
64
 
65
+ if (isStatic(type)) return size as Size<T>;
66
+
66
67
  for (const member of struct.members.values()) {
67
68
  if (typeof member.length != 'string') continue;
68
69
  for (let i = 0; i < (type as any)[member.length]; i++) {
69
- 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);
70
73
  }
71
74
  }
72
75
 
@@ -95,7 +98,8 @@ export function offsetof(type: StaticLike | InstanceLike, memberName: string): n
95
98
 
96
99
  for (const member of struct.members.values()) {
97
100
  if (member.name == memberName) return offset;
98
- offset += sizeof((type as any)[member.name]);
101
+ const value = (type as any)[member.name];
102
+ offset += sizeof(isStruct(value) ? value : member.type);
99
103
  }
100
104
 
101
105
  throw new Error('Struct does not have member: ' + memberName);