ya-struct 0.0.10 → 0.0.12
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/lib/index.d.ts +2 -1
- package/dist/lib/index.js +3 -1
- package/dist/lib/layout.js +44 -44
- package/dist/lib/parser.d.ts +7 -1
- package/dist/lib/parser.js +6 -0
- package/dist/lib/tests/compile-and-compare.vibe.d.ts +30 -0
- package/dist/lib/tests/compile-and-compare.vibe.js +221 -0
- package/dist/lib/types/array.js +1 -0
- package/dist/lib/types/string.js +1 -0
- package/dist/lib/types/struct.js +1 -0
- package/package.json +12 -7
- package/.editorconfig +0 -8
- package/.github/workflows/ci.yml +0 -23
- package/.github/workflows/npm-publish.yml +0 -28
- package/eslint.config.js +0 -127
- package/lib/bit-buffer.ts +0 -70
- package/lib/common.ts +0 -30
- package/lib/index.ts +0 -21
- package/lib/layout.ts +0 -286
- package/lib/parser.ts +0 -88
- package/lib/types/array.ts +0 -90
- package/lib/types/c-types.ts +0 -222
- package/lib/types/index.ts +0 -122
- package/lib/types/integer.ts +0 -160
- package/lib/types/pointer.ts +0 -27
- package/lib/types/string.ts +0 -56
- package/lib/types/struct.ts +0 -146
- package/lib/types/value.ts +0 -8
- package/package.npm.json +0 -25
- package/samples/basic.ts +0 -40
- package/test/c-structs.ts +0 -399
- package/test/compile-util.ts +0 -92
- package/test/nested-structs.ts +0 -78
- package/tsconfig.json +0 -11
package/lib/types/c-types.ts
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { type TCompiler, type TDataModel, type TAbi } from "../common.ts";
|
|
2
|
-
import { type TFieldType, type TCFieldType, type TPrimitiveBasicFieldType } from "./index.ts";
|
|
3
|
-
|
|
4
|
-
type TCField = TFieldType & { type: "c-type" };
|
|
5
|
-
|
|
6
|
-
type TPartialCTypeMappings = {
|
|
7
|
-
[dataModel in TDataModel]?: {
|
|
8
|
-
[compiler in TCompiler]?: {
|
|
9
|
-
[field in TCFieldType]?: TPrimitiveBasicFieldType
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const cTypeMappings: TPartialCTypeMappings = {
|
|
15
|
-
LP64: {
|
|
16
|
-
gcc: {
|
|
17
|
-
"char": {
|
|
18
|
-
type: "integer",
|
|
19
|
-
sizeInBits: 8,
|
|
20
|
-
fixedAbi: {},
|
|
21
|
-
signed: true
|
|
22
|
-
},
|
|
23
|
-
"unsigned char": {
|
|
24
|
-
type: "integer",
|
|
25
|
-
sizeInBits: 8,
|
|
26
|
-
fixedAbi: {},
|
|
27
|
-
signed: false
|
|
28
|
-
},
|
|
29
|
-
"short": {
|
|
30
|
-
type: "integer",
|
|
31
|
-
sizeInBits: 16,
|
|
32
|
-
fixedAbi: {},
|
|
33
|
-
signed: true
|
|
34
|
-
},
|
|
35
|
-
"unsigned short": {
|
|
36
|
-
type: "integer",
|
|
37
|
-
sizeInBits: 16,
|
|
38
|
-
fixedAbi: {},
|
|
39
|
-
signed: false
|
|
40
|
-
},
|
|
41
|
-
"int": {
|
|
42
|
-
type: "integer",
|
|
43
|
-
sizeInBits: 32,
|
|
44
|
-
fixedAbi: {},
|
|
45
|
-
signed: true
|
|
46
|
-
},
|
|
47
|
-
"unsigned int": {
|
|
48
|
-
type: "integer",
|
|
49
|
-
sizeInBits: 32,
|
|
50
|
-
fixedAbi: {},
|
|
51
|
-
signed: false
|
|
52
|
-
},
|
|
53
|
-
"long": {
|
|
54
|
-
type: "integer",
|
|
55
|
-
sizeInBits: 64,
|
|
56
|
-
fixedAbi: {},
|
|
57
|
-
signed: true
|
|
58
|
-
},
|
|
59
|
-
"unsigned long": {
|
|
60
|
-
type: "integer",
|
|
61
|
-
sizeInBits: 64,
|
|
62
|
-
fixedAbi: {},
|
|
63
|
-
signed: false
|
|
64
|
-
},
|
|
65
|
-
"long long": {
|
|
66
|
-
type: "integer",
|
|
67
|
-
sizeInBits: 64,
|
|
68
|
-
fixedAbi: {},
|
|
69
|
-
signed: true
|
|
70
|
-
},
|
|
71
|
-
"unsigned long long": {
|
|
72
|
-
type: "integer",
|
|
73
|
-
sizeInBits: 64,
|
|
74
|
-
fixedAbi: {},
|
|
75
|
-
signed: false
|
|
76
|
-
},
|
|
77
|
-
"float": {
|
|
78
|
-
type: "float",
|
|
79
|
-
sizeInBits: 32,
|
|
80
|
-
fixedAbi: {}
|
|
81
|
-
},
|
|
82
|
-
"double": {
|
|
83
|
-
type: "float",
|
|
84
|
-
sizeInBits: 64,
|
|
85
|
-
fixedAbi: {}
|
|
86
|
-
},
|
|
87
|
-
"long double": {
|
|
88
|
-
type: "float",
|
|
89
|
-
sizeInBits: 128,
|
|
90
|
-
fixedAbi: {}
|
|
91
|
-
},
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
ILP32: {
|
|
96
|
-
gcc: {
|
|
97
|
-
"char": {
|
|
98
|
-
type: "integer",
|
|
99
|
-
sizeInBits: 8,
|
|
100
|
-
fixedAbi: {},
|
|
101
|
-
signed: true
|
|
102
|
-
},
|
|
103
|
-
"unsigned char": {
|
|
104
|
-
type: "integer",
|
|
105
|
-
sizeInBits: 8,
|
|
106
|
-
fixedAbi: {},
|
|
107
|
-
signed: false
|
|
108
|
-
},
|
|
109
|
-
"short": {
|
|
110
|
-
type: "integer",
|
|
111
|
-
sizeInBits: 16,
|
|
112
|
-
fixedAbi: {},
|
|
113
|
-
signed: true
|
|
114
|
-
},
|
|
115
|
-
"unsigned short": {
|
|
116
|
-
type: "integer",
|
|
117
|
-
sizeInBits: 16,
|
|
118
|
-
fixedAbi: {},
|
|
119
|
-
signed: false
|
|
120
|
-
},
|
|
121
|
-
"int": {
|
|
122
|
-
type: "integer",
|
|
123
|
-
sizeInBits: 32,
|
|
124
|
-
fixedAbi: {},
|
|
125
|
-
signed: true
|
|
126
|
-
},
|
|
127
|
-
"unsigned int": {
|
|
128
|
-
type: "integer",
|
|
129
|
-
sizeInBits: 32,
|
|
130
|
-
fixedAbi: {},
|
|
131
|
-
signed: false
|
|
132
|
-
},
|
|
133
|
-
"long": {
|
|
134
|
-
type: "integer",
|
|
135
|
-
sizeInBits: 32,
|
|
136
|
-
fixedAbi: {},
|
|
137
|
-
signed: true
|
|
138
|
-
},
|
|
139
|
-
"unsigned long": {
|
|
140
|
-
type: "integer",
|
|
141
|
-
sizeInBits: 32,
|
|
142
|
-
fixedAbi: {},
|
|
143
|
-
signed: false
|
|
144
|
-
},
|
|
145
|
-
"long long": {
|
|
146
|
-
type: "integer",
|
|
147
|
-
sizeInBits: 64,
|
|
148
|
-
fixedAbi: {},
|
|
149
|
-
signed: true
|
|
150
|
-
},
|
|
151
|
-
"unsigned long long": {
|
|
152
|
-
type: "integer",
|
|
153
|
-
sizeInBits: 64,
|
|
154
|
-
fixedAbi: {},
|
|
155
|
-
signed: false
|
|
156
|
-
},
|
|
157
|
-
"float": {
|
|
158
|
-
type: "float",
|
|
159
|
-
sizeInBits: 32,
|
|
160
|
-
fixedAbi: {}
|
|
161
|
-
},
|
|
162
|
-
"double": {
|
|
163
|
-
type: "float",
|
|
164
|
-
sizeInBits: 64,
|
|
165
|
-
fixedAbi: {}
|
|
166
|
-
},
|
|
167
|
-
"long double": {
|
|
168
|
-
type: "float",
|
|
169
|
-
sizeInBits: 128,
|
|
170
|
-
fixedAbi: {}
|
|
171
|
-
},
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
const createCTypeNormalizer = ({
|
|
177
|
-
abi
|
|
178
|
-
}: {
|
|
179
|
-
abi: TAbi
|
|
180
|
-
}) => {
|
|
181
|
-
|
|
182
|
-
const fieldMappings = cTypeMappings[abi.dataModel]?.[abi.compiler];
|
|
183
|
-
if (!fieldMappings) {
|
|
184
|
-
throw Error(`no c-type mappings for data model ${abi.dataModel} and compiler ${abi.compiler}`);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const normalize = ({ cField }: { cField: TCField }): TPrimitiveBasicFieldType => {
|
|
188
|
-
|
|
189
|
-
const mapping = fieldMappings[cField.cType];
|
|
190
|
-
if (mapping === undefined) {
|
|
191
|
-
throw Error(`no c-type mapping for c-type "${cField.cType}" for data model ${abi.dataModel} and compiler ${abi.compiler}`);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const mappingFixedAbi = mapping.fixedAbi;
|
|
195
|
-
const fieldFixedAbi = cField.fixedAbi;
|
|
196
|
-
|
|
197
|
-
const fixedAbi = {
|
|
198
|
-
...mappingFixedAbi,
|
|
199
|
-
...fieldFixedAbi
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
Object.keys(fixedAbi).forEach((keyAsString) => {
|
|
203
|
-
const key = keyAsString as keyof TAbi;
|
|
204
|
-
if (mappingFixedAbi[key] !== undefined && fieldFixedAbi[key] !== undefined) {
|
|
205
|
-
throw Error(`conflicting fixed ABI property for key ${key} between c-type mapping and c-field`);
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
...mapping,
|
|
211
|
-
fixedAbi
|
|
212
|
-
};
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
return {
|
|
216
|
-
normalize
|
|
217
|
-
};
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
export {
|
|
221
|
-
createCTypeNormalizer
|
|
222
|
-
};
|
package/lib/types/index.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import type { TAbi } from "../common.ts";
|
|
2
|
-
|
|
3
|
-
type TCFieldType =
|
|
4
|
-
"char" | "unsigned char" |
|
|
5
|
-
"short" | "unsigned short" |
|
|
6
|
-
"int" | "unsigned int" |
|
|
7
|
-
"long" | "unsigned long" |
|
|
8
|
-
"long long" | "unsigned long long" |
|
|
9
|
-
"float" | "double" | "long double";
|
|
10
|
-
|
|
11
|
-
type TFieldType = {
|
|
12
|
-
readonly type: "integer";
|
|
13
|
-
readonly sizeInBits: number;
|
|
14
|
-
readonly signed: boolean;
|
|
15
|
-
readonly fixedAbi: Partial<TAbi>;
|
|
16
|
-
} | {
|
|
17
|
-
readonly type: "float";
|
|
18
|
-
readonly sizeInBits: number;
|
|
19
|
-
readonly fixedAbi: Partial<TAbi>;
|
|
20
|
-
} | {
|
|
21
|
-
readonly type: "pointer";
|
|
22
|
-
readonly fixedAbi: Partial<TAbi>;
|
|
23
|
-
} | {
|
|
24
|
-
readonly type: "array";
|
|
25
|
-
readonly elementType: TFieldType;
|
|
26
|
-
readonly length: number;
|
|
27
|
-
} | {
|
|
28
|
-
readonly type: "struct";
|
|
29
|
-
readonly fields: readonly { readonly name: string; readonly definition: TFieldType }[];
|
|
30
|
-
readonly packed: boolean;
|
|
31
|
-
readonly fixedAbi: Partial<TAbi>;
|
|
32
|
-
} | {
|
|
33
|
-
readonly type: "string";
|
|
34
|
-
readonly charSizeInBits: number;
|
|
35
|
-
readonly nullTerminatorMandatory: boolean;
|
|
36
|
-
readonly length: number;
|
|
37
|
-
} | {
|
|
38
|
-
readonly type: "c-type";
|
|
39
|
-
readonly cType: TCFieldType;
|
|
40
|
-
readonly fixedAbi: Partial<TAbi>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
type TBasicFieldType = Exclude<TFieldType, { type: "c-type" }>;
|
|
44
|
-
type TPrimitiveBasicFieldType = Exclude<TBasicFieldType, { type: "array" } | { type: "struct" } | { type: "string" }>;
|
|
45
|
-
|
|
46
|
-
const Int16: TFieldType = {
|
|
47
|
-
type: "integer",
|
|
48
|
-
sizeInBits: 16,
|
|
49
|
-
signed: true,
|
|
50
|
-
fixedAbi: {}
|
|
51
|
-
} as const;
|
|
52
|
-
|
|
53
|
-
const UInt16: TFieldType = {
|
|
54
|
-
type: "integer",
|
|
55
|
-
sizeInBits: 16,
|
|
56
|
-
signed: false,
|
|
57
|
-
fixedAbi: {}
|
|
58
|
-
} as const;
|
|
59
|
-
|
|
60
|
-
const Int32: TFieldType = {
|
|
61
|
-
type: "integer",
|
|
62
|
-
sizeInBits: 32,
|
|
63
|
-
signed: true,
|
|
64
|
-
fixedAbi: {}
|
|
65
|
-
} as const;
|
|
66
|
-
|
|
67
|
-
const Int64: TFieldType = {
|
|
68
|
-
type: "integer",
|
|
69
|
-
sizeInBits: 64,
|
|
70
|
-
signed: true,
|
|
71
|
-
fixedAbi: {}
|
|
72
|
-
} as const;
|
|
73
|
-
|
|
74
|
-
const UInt32: TFieldType = {
|
|
75
|
-
type: "integer",
|
|
76
|
-
sizeInBits: 32,
|
|
77
|
-
signed: false,
|
|
78
|
-
fixedAbi: {}
|
|
79
|
-
} as const;
|
|
80
|
-
|
|
81
|
-
const UInt64: TFieldType = {
|
|
82
|
-
type: "integer",
|
|
83
|
-
sizeInBits: 64,
|
|
84
|
-
signed: false,
|
|
85
|
-
fixedAbi: {}
|
|
86
|
-
} as const;
|
|
87
|
-
|
|
88
|
-
const ascii = ({ length }: { length: number }) => {
|
|
89
|
-
return {
|
|
90
|
-
type: "string",
|
|
91
|
-
charSizeInBits: 8,
|
|
92
|
-
nullTerminatorMandatory: true,
|
|
93
|
-
length
|
|
94
|
-
} as const;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const pointer: TFieldType = {
|
|
98
|
-
type: "pointer",
|
|
99
|
-
fixedAbi: {}
|
|
100
|
-
} as const;
|
|
101
|
-
|
|
102
|
-
export type {
|
|
103
|
-
TBasicFieldType,
|
|
104
|
-
TFieldType,
|
|
105
|
-
TCFieldType,
|
|
106
|
-
TPrimitiveBasicFieldType
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const types = {
|
|
110
|
-
Int16,
|
|
111
|
-
UInt16,
|
|
112
|
-
Int32,
|
|
113
|
-
Int64,
|
|
114
|
-
UInt32,
|
|
115
|
-
UInt64,
|
|
116
|
-
ascii,
|
|
117
|
-
pointer
|
|
118
|
-
} as const;
|
|
119
|
-
|
|
120
|
-
export {
|
|
121
|
-
types
|
|
122
|
-
};
|
package/lib/types/integer.ts
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import type { TEndianness } from "../common.ts";
|
|
2
|
-
import type { TValueParser } from "./value.ts";
|
|
3
|
-
|
|
4
|
-
type TIntegerParser = TValueParser<bigint>;
|
|
5
|
-
|
|
6
|
-
const createIntegerParser = ({
|
|
7
|
-
sizeInBits,
|
|
8
|
-
signed,
|
|
9
|
-
endianness
|
|
10
|
-
}: {
|
|
11
|
-
sizeInBits: number,
|
|
12
|
-
signed: boolean,
|
|
13
|
-
endianness: TEndianness
|
|
14
|
-
}): TIntegerParser => {
|
|
15
|
-
|
|
16
|
-
// eslint-disable-next-line max-statements, complexity
|
|
17
|
-
const parse: TIntegerParser["parse"] = ({ data, offsetInBits }) => {
|
|
18
|
-
if (data.length < sizeInBits / 8) {
|
|
19
|
-
throw Error("BUG: not enough data for integer parsing");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (offsetInBits !== 0) {
|
|
23
|
-
throw Error("unaligned data in integer parser, not supported yet");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const view = new DataView(data.buffer, data.byteOffset);
|
|
27
|
-
|
|
28
|
-
if (sizeInBits === 8) {
|
|
29
|
-
if (signed) {
|
|
30
|
-
return BigInt(view.getInt8(0));
|
|
31
|
-
} else {
|
|
32
|
-
return BigInt(view.getUint8(0));
|
|
33
|
-
}
|
|
34
|
-
} else if (sizeInBits === 16) {
|
|
35
|
-
if (signed) {
|
|
36
|
-
if (endianness === "little") {
|
|
37
|
-
return BigInt(view.getInt16(0, true));
|
|
38
|
-
} else {
|
|
39
|
-
return BigInt(view.getInt16(0, false));
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
// eslint-disable-next-line no-lonely-if
|
|
43
|
-
if (endianness === "little") {
|
|
44
|
-
return BigInt(view.getUint16(0, true));
|
|
45
|
-
} else {
|
|
46
|
-
return BigInt(view.getUint16(0, false));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
} else if (sizeInBits === 32) {
|
|
50
|
-
if (signed) {
|
|
51
|
-
if (endianness === "little") {
|
|
52
|
-
return BigInt(view.getInt32(0, true));
|
|
53
|
-
} else {
|
|
54
|
-
return BigInt(view.getInt32(0, false));
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
// eslint-disable-next-line no-lonely-if
|
|
58
|
-
if (endianness === "little") {
|
|
59
|
-
return BigInt(view.getUint32(0, true));
|
|
60
|
-
} else {
|
|
61
|
-
return BigInt(view.getUint32(0, false));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
} else if (sizeInBits === 64) {
|
|
65
|
-
if (signed) {
|
|
66
|
-
if (endianness === "little") {
|
|
67
|
-
return view.getBigInt64(0, true);
|
|
68
|
-
} else {
|
|
69
|
-
return view.getBigInt64(0, false);
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
// eslint-disable-next-line no-lonely-if
|
|
73
|
-
if (endianness === "little") {
|
|
74
|
-
return view.getBigUint64(0, true);
|
|
75
|
-
} else {
|
|
76
|
-
return view.getBigUint64(0, false);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
throw Error("not implemented yet");
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// eslint-disable-next-line max-statements, complexity
|
|
85
|
-
const format: TIntegerParser["format"] = ({ value, target, offsetInBits }) => {
|
|
86
|
-
if (offsetInBits !== 0) {
|
|
87
|
-
throw Error("unaligned data in integer formatter, not supported yet");
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (typeof value !== "bigint") {
|
|
91
|
-
throw Error("invalid value type for integer formatter");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const view = new DataView(target.buffer, target.byteOffset);
|
|
95
|
-
|
|
96
|
-
if (sizeInBits === 8) {
|
|
97
|
-
if (signed) {
|
|
98
|
-
view.setInt8(0, Number(value));
|
|
99
|
-
} else {
|
|
100
|
-
view.setUint8(0, Number(value));
|
|
101
|
-
}
|
|
102
|
-
} else if (sizeInBits === 16) {
|
|
103
|
-
if (signed) {
|
|
104
|
-
if (endianness === "little") {
|
|
105
|
-
view.setInt16(0, Number(value), true);
|
|
106
|
-
} else {
|
|
107
|
-
view.setInt16(0, Number(value), false);
|
|
108
|
-
}
|
|
109
|
-
} else {
|
|
110
|
-
// eslint-disable-next-line no-lonely-if
|
|
111
|
-
if (endianness === "little") {
|
|
112
|
-
view.setUint16(0, Number(value), true);
|
|
113
|
-
} else {
|
|
114
|
-
view.setUint16(0, Number(value), false);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
} else if (sizeInBits === 32) {
|
|
118
|
-
if (signed) {
|
|
119
|
-
if (endianness === "little") {
|
|
120
|
-
view.setInt32(0, Number(value), true);
|
|
121
|
-
} else {
|
|
122
|
-
view.setInt32(0, Number(value), false);
|
|
123
|
-
}
|
|
124
|
-
} else {
|
|
125
|
-
// eslint-disable-next-line no-lonely-if
|
|
126
|
-
if (endianness === "little") {
|
|
127
|
-
view.setUint32(0, Number(value), true);
|
|
128
|
-
} else {
|
|
129
|
-
view.setUint32(0, Number(value), false);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
} else if (sizeInBits === 64) {
|
|
133
|
-
if (signed) {
|
|
134
|
-
if (endianness === "little") {
|
|
135
|
-
view.setBigInt64(0, value, true);
|
|
136
|
-
} else {
|
|
137
|
-
view.setBigInt64(0, value, false);
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
// eslint-disable-next-line no-lonely-if
|
|
141
|
-
if (endianness === "little") {
|
|
142
|
-
view.setBigUint64(0, value, true);
|
|
143
|
-
} else {
|
|
144
|
-
view.setBigUint64(0, value, false);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
throw Error("not implemented yet");
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
return {
|
|
153
|
-
parse,
|
|
154
|
-
format
|
|
155
|
-
};
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
export {
|
|
159
|
-
createIntegerParser
|
|
160
|
-
};
|
package/lib/types/pointer.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { TEndianness } from "../common.ts";
|
|
2
|
-
import { createIntegerParser } from "./integer.ts";
|
|
3
|
-
import type { TValueParser } from "./value.ts";
|
|
4
|
-
|
|
5
|
-
type TPointerParser = TValueParser<bigint>;
|
|
6
|
-
|
|
7
|
-
const createPointerParser = ({ sizeInBits, endianness }: { sizeInBits: number, endianness: TEndianness }): TPointerParser => {
|
|
8
|
-
|
|
9
|
-
const integerParser = createIntegerParser({ sizeInBits, signed: false, endianness });
|
|
10
|
-
|
|
11
|
-
const parse: TPointerParser["parse"] = ({ data, offsetInBits }) => {
|
|
12
|
-
return integerParser.parse({ data, offsetInBits });
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const format: TPointerParser["format"] = ({ value, target, offsetInBits }) => {
|
|
16
|
-
return integerParser.format({ value, target, offsetInBits });
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
parse,
|
|
21
|
-
format
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export {
|
|
26
|
-
createPointerParser
|
|
27
|
-
};
|
package/lib/types/string.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { TValueParser } from "./value.ts";
|
|
2
|
-
|
|
3
|
-
type TStringParser = TValueParser<string>;
|
|
4
|
-
|
|
5
|
-
const createStringParser = ({ length }: { length: number }): TStringParser => {
|
|
6
|
-
|
|
7
|
-
const decoder = new TextDecoder();
|
|
8
|
-
const encoder = new TextEncoder();
|
|
9
|
-
|
|
10
|
-
const parse: TStringParser["parse"] = ({ data, offsetInBits }) => {
|
|
11
|
-
if (offsetInBits !== 0) {
|
|
12
|
-
throw Error("unaligned data in string parser, not supported yet");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (data.length < length) {
|
|
16
|
-
throw Error("not enough data for string parsing");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const stringData = data.subarray(0, length);
|
|
20
|
-
const nullTerminatorIndex = stringData.indexOf(0);
|
|
21
|
-
|
|
22
|
-
if (nullTerminatorIndex < 0) {
|
|
23
|
-
return decoder.decode(stringData);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return decoder.decode(stringData.subarray(0, nullTerminatorIndex));
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const format: TStringParser["format"] = ({ value, target, offsetInBits }) => {
|
|
30
|
-
if (offsetInBits !== 0) {
|
|
31
|
-
throw Error("unaligned data in string formatter, not supported yet");
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (target.length < length) {
|
|
35
|
-
throw Error("not enough space in target for string formatting");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const encoded = encoder.encode(value);
|
|
39
|
-
|
|
40
|
-
if (encoded.length + 1 > length) {
|
|
41
|
-
throw Error("string too long to fit in target");
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
target.set(encoded, 0);
|
|
45
|
-
target[encoded.length] = 0;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
parse,
|
|
50
|
-
format
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export {
|
|
55
|
-
createStringParser
|
|
56
|
-
};
|