@yume-chan/struct 0.0.17 → 0.0.18
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/CHANGELOG.json +12 -0
- package/CHANGELOG.md +8 -1
- package/LICENSE +1 -1
- package/esm/basic/definition.d.ts +4 -4
- package/esm/basic/definition.d.ts.map +1 -1
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.d.ts +7 -7
- package/esm/basic/field-value.d.ts.map +1 -1
- package/esm/basic/field-value.js +2 -2
- package/esm/basic/field-value.js.map +1 -1
- package/esm/basic/stream.d.ts +1 -1
- package/esm/basic/stream.d.ts.map +1 -1
- package/esm/basic/struct-value.d.ts +2 -2
- package/esm/basic/struct-value.d.ts.map +1 -1
- package/esm/basic/struct-value.js +10 -3
- package/esm/basic/struct-value.js.map +1 -1
- package/esm/struct.d.ts +19 -19
- package/esm/struct.d.ts.map +1 -1
- package/esm/struct.js +10 -8
- package/esm/struct.js.map +1 -1
- package/esm/sync-promise.d.ts.map +1 -1
- package/esm/sync-promise.js +4 -4
- package/esm/sync-promise.js.map +1 -1
- package/esm/types/bigint.d.ts +4 -4
- package/esm/types/bigint.d.ts.map +1 -1
- package/esm/types/bigint.js +6 -6
- package/esm/types/bigint.js.map +1 -1
- package/esm/types/buffer/base.d.ts +4 -4
- package/esm/types/buffer/base.d.ts.map +1 -1
- package/esm/types/buffer/base.js +5 -6
- package/esm/types/buffer/base.js.map +1 -1
- package/esm/types/buffer/fixed-length.d.ts.map +1 -1
- package/esm/types/buffer/fixed-length.js +0 -1
- package/esm/types/buffer/fixed-length.js.map +1 -1
- package/esm/types/buffer/variable-length.d.ts +7 -7
- package/esm/types/buffer/variable-length.d.ts.map +1 -1
- package/esm/types/buffer/variable-length.js +4 -4
- package/esm/types/buffer/variable-length.js.map +1 -1
- package/esm/types/number.d.ts +16 -22
- package/esm/types/number.d.ts.map +1 -1
- package/esm/types/number.js +89 -40
- package/esm/types/number.js.map +1 -1
- package/esm/utils.d.ts +6 -6
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +1 -8
- package/esm/utils.js.map +1 -1
- package/package.json +12 -9
- package/src/basic/definition.ts +12 -9
- package/src/basic/field-value.ts +18 -15
- package/src/basic/stream.ts +1 -1
- package/src/basic/struct-value.ts +17 -11
- package/src/struct.ts +207 -201
- package/src/sync-promise.ts +32 -12
- package/src/types/bigint.ts +58 -31
- package/src/types/buffer/base.ts +53 -35
- package/src/types/buffer/fixed-length.ts +3 -8
- package/src/types/buffer/variable-length.ts +41 -24
- package/src/types/number.ts +131 -79
- package/src/utils.ts +30 -8
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
package/src/basic/field-value.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
1
|
+
import { type StructFieldDefinition } from "./definition.js";
|
|
2
|
+
import { type StructOptions } from "./options.js";
|
|
3
|
+
import { type StructValue } from "./struct-value.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* A field value defines how to serialize a field.
|
|
@@ -9,8 +9,12 @@ import type { StructValue } from "./struct-value.js";
|
|
|
9
9
|
* helpful for the serialization process.
|
|
10
10
|
*/
|
|
11
11
|
export abstract class StructFieldValue<
|
|
12
|
-
TDefinition extends StructFieldDefinition<
|
|
13
|
-
|
|
12
|
+
TDefinition extends StructFieldDefinition<
|
|
13
|
+
any,
|
|
14
|
+
any,
|
|
15
|
+
any
|
|
16
|
+
> = StructFieldDefinition<any, any, any>
|
|
17
|
+
> {
|
|
14
18
|
/** Gets the definition associated with this runtime value */
|
|
15
19
|
public readonly definition: TDefinition;
|
|
16
20
|
|
|
@@ -21,17 +25,19 @@ export abstract class StructFieldValue<
|
|
|
21
25
|
public readonly struct: StructValue;
|
|
22
26
|
|
|
23
27
|
public get hasCustomAccessors(): boolean {
|
|
24
|
-
return
|
|
25
|
-
this.
|
|
28
|
+
return (
|
|
29
|
+
this.get !== StructFieldValue.prototype.get ||
|
|
30
|
+
this.set !== StructFieldValue.prototype.set
|
|
31
|
+
);
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
protected value: TDefinition[
|
|
34
|
+
protected value: TDefinition["TValue"];
|
|
29
35
|
|
|
30
36
|
public constructor(
|
|
31
37
|
definition: TDefinition,
|
|
32
38
|
options: Readonly<StructOptions>,
|
|
33
39
|
struct: StructValue,
|
|
34
|
-
value: TDefinition[
|
|
40
|
+
value: TDefinition["TValue"]
|
|
35
41
|
) {
|
|
36
42
|
this.definition = definition;
|
|
37
43
|
this.options = options;
|
|
@@ -51,22 +57,19 @@ export abstract class StructFieldValue<
|
|
|
51
57
|
/**
|
|
52
58
|
* When implemented in derived classes, reads current field's value.
|
|
53
59
|
*/
|
|
54
|
-
public get(): TDefinition[
|
|
60
|
+
public get(): TDefinition["TValue"] {
|
|
55
61
|
return this.value;
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
/**
|
|
59
65
|
* When implemented in derived classes, updates current field's value.
|
|
60
66
|
*/
|
|
61
|
-
public set(value: TDefinition[
|
|
67
|
+
public set(value: TDefinition["TValue"]): void {
|
|
62
68
|
this.value = value;
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
/**
|
|
66
72
|
* When implemented in derived classes, serializes this field into `dataView` at `offset`
|
|
67
73
|
*/
|
|
68
|
-
public abstract serialize(
|
|
69
|
-
dataView: DataView,
|
|
70
|
-
offset: number,
|
|
71
|
-
): void;
|
|
74
|
+
public abstract serialize(dataView: DataView, offset: number): void;
|
|
72
75
|
}
|
package/src/basic/stream.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type StructFieldValue } from "./field-value.js";
|
|
2
2
|
|
|
3
3
|
export const STRUCT_VALUE_SYMBOL = Symbol("struct-value");
|
|
4
4
|
|
|
@@ -6,25 +6,27 @@ export const STRUCT_VALUE_SYMBOL = Symbol("struct-value");
|
|
|
6
6
|
* A struct value is a map between keys in a struct and their field values.
|
|
7
7
|
*/
|
|
8
8
|
export class StructValue {
|
|
9
|
-
/** @internal */ readonly fieldValues: Record<
|
|
9
|
+
/** @internal */ readonly fieldValues: Record<
|
|
10
|
+
PropertyKey,
|
|
11
|
+
StructFieldValue
|
|
12
|
+
> = {};
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Gets the result struct value object
|
|
13
16
|
*/
|
|
14
17
|
public readonly value: Record<PropertyKey, unknown>;
|
|
15
18
|
|
|
16
|
-
public constructor(prototype:
|
|
19
|
+
public constructor(prototype: object) {
|
|
17
20
|
// PERF: `Object.create(extra)` is 50% faster
|
|
18
21
|
// than `Object.defineProperties(this.value, extra)`
|
|
19
|
-
this.value = Object.create(prototype)
|
|
22
|
+
this.value = Object.create(prototype) as Record<PropertyKey, unknown>;
|
|
20
23
|
|
|
21
24
|
// PERF: `Object.defineProperty` is slow
|
|
22
25
|
// but we need it to be non-enumerable
|
|
23
|
-
Object.defineProperty(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
);
|
|
26
|
+
Object.defineProperty(this.value, STRUCT_VALUE_SYMBOL, {
|
|
27
|
+
enumerable: false,
|
|
28
|
+
value: this,
|
|
29
|
+
});
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -42,8 +44,12 @@ export class StructValue {
|
|
|
42
44
|
Object.defineProperty(this.value, name, {
|
|
43
45
|
configurable: true,
|
|
44
46
|
enumerable: true,
|
|
45
|
-
get() {
|
|
46
|
-
|
|
47
|
+
get() {
|
|
48
|
+
return fieldValue.get();
|
|
49
|
+
},
|
|
50
|
+
set(v) {
|
|
51
|
+
fieldValue.set(v);
|
|
52
|
+
},
|
|
47
53
|
});
|
|
48
54
|
} else {
|
|
49
55
|
this.value[name] = fieldValue.get();
|