ya-struct 0.0.9 → 0.0.11
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/layout.js +44 -44
- package/dist/lib/parser.d.ts +6 -1
- package/dist/lib/parser.js +3 -0
- package/dist/lib/types/array.js +1 -0
- package/dist/lib/types/string.js +1 -0
- package/dist/lib/types/struct.d.ts +2 -1
- package/dist/lib/types/struct.js +38 -4
- 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 -87
- 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 -113
- 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/tsconfig.json +0 -10
package/test/c-structs.ts
DELETED
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
import { type TCompiler, type TDataModel } from "../lib/common.ts";
|
|
2
|
-
import { define } from "../lib/parser.ts";
|
|
3
|
-
import { type TCFieldType, type TFieldType } from "../lib/types/index.ts";
|
|
4
|
-
import { determineCCompilerStructLayout } from "./compile-util.ts";
|
|
5
|
-
import nodeAssert from "node:assert";
|
|
6
|
-
|
|
7
|
-
type TStructDefinition = TFieldType & { type: "struct" };
|
|
8
|
-
|
|
9
|
-
const simpleStructDefinitionToCCode = ({
|
|
10
|
-
definition,
|
|
11
|
-
structName,
|
|
12
|
-
packed
|
|
13
|
-
}: {
|
|
14
|
-
definition: TStructDefinition,
|
|
15
|
-
structName: string,
|
|
16
|
-
packed: boolean
|
|
17
|
-
}) => {
|
|
18
|
-
|
|
19
|
-
const fieldDefinitions = definition.fields.map((field) => {
|
|
20
|
-
|
|
21
|
-
switch (field.definition.type) {
|
|
22
|
-
case "c-type": {
|
|
23
|
-
return `${field.definition.cType} ${field.name};`;
|
|
24
|
-
}
|
|
25
|
-
case "pointer": {
|
|
26
|
-
return `void* ${field.name};`;
|
|
27
|
-
}
|
|
28
|
-
case "string": {
|
|
29
|
-
return `char ${field.name}[${field.definition.length}];`;
|
|
30
|
-
}
|
|
31
|
-
default: {
|
|
32
|
-
throw Error(`unsupported field type "${field.definition.type}"`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
return `
|
|
38
|
-
struct ${structName} {
|
|
39
|
-
${fieldDefinitions.join("\n")}
|
|
40
|
-
} ${packed ? "__attribute__((__packed__))" : ""};
|
|
41
|
-
`;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const defineStructTestFor = ({
|
|
45
|
-
packed,
|
|
46
|
-
dataModel,
|
|
47
|
-
compiler,
|
|
48
|
-
fields,
|
|
49
|
-
structName,
|
|
50
|
-
}: {
|
|
51
|
-
packed: boolean,
|
|
52
|
-
dataModel: TDataModel,
|
|
53
|
-
compiler: TCompiler,
|
|
54
|
-
fields: TStructDefinition["fields"],
|
|
55
|
-
structName: string
|
|
56
|
-
}) => {
|
|
57
|
-
it(`should work for ${structName}, packed=${packed ? "true" : "false"}, dataModel=${dataModel}, compiler=${compiler}`, async () => {
|
|
58
|
-
const definition: TStructDefinition = {
|
|
59
|
-
type: "struct",
|
|
60
|
-
fields,
|
|
61
|
-
fixedAbi: {},
|
|
62
|
-
packed
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const cStructName = "MyStruct1";
|
|
66
|
-
|
|
67
|
-
const cDefinition = simpleStructDefinitionToCCode({
|
|
68
|
-
definition,
|
|
69
|
-
structName: cStructName,
|
|
70
|
-
packed
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const cLayout = await determineCCompilerStructLayout({
|
|
74
|
-
definitions: cDefinition,
|
|
75
|
-
structName: cStructName,
|
|
76
|
-
fieldNames: definition.fields.map((f) => f.name),
|
|
77
|
-
bits: dataModel === "LP64" ? 64 : 32
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const def = define({
|
|
81
|
-
definition
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const parser = def.parser({
|
|
85
|
-
abi: {
|
|
86
|
-
compiler,
|
|
87
|
-
dataModel,
|
|
88
|
-
endianness: "little"
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
nodeAssert.strictEqual(parser.layout.type, "struct");
|
|
93
|
-
|
|
94
|
-
let ourLayout = {};
|
|
95
|
-
|
|
96
|
-
parser.layout.fields.forEach((fieldLayout) => {
|
|
97
|
-
const correspondinCLayout = cLayout[fieldLayout.name];
|
|
98
|
-
nodeAssert.ok(correspondinCLayout !== undefined);
|
|
99
|
-
|
|
100
|
-
nodeAssert.ok(fieldLayout.definition.offsetInBits % 8 === 0);
|
|
101
|
-
nodeAssert.ok(fieldLayout.definition.sizeInBits % 8 === 0);
|
|
102
|
-
|
|
103
|
-
ourLayout = {
|
|
104
|
-
...ourLayout,
|
|
105
|
-
[fieldLayout.name]: {
|
|
106
|
-
offset: fieldLayout.definition.offsetInBits / 8,
|
|
107
|
-
length: fieldLayout.definition.sizeInBits / 8
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
nodeAssert.deepStrictEqual(ourLayout, cLayout);
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
type TSingleStructField = TStructDefinition["fields"][0];
|
|
117
|
-
|
|
118
|
-
const cField = ({ name, cType }: { name: string; cType: TCFieldType }): TSingleStructField => {
|
|
119
|
-
return {
|
|
120
|
-
name,
|
|
121
|
-
definition: {
|
|
122
|
-
type: "c-type",
|
|
123
|
-
cType,
|
|
124
|
-
fixedAbi: {}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
describe("c-structs", () => {
|
|
130
|
-
|
|
131
|
-
const dataModels: TDataModel[] = [
|
|
132
|
-
"LP64",
|
|
133
|
-
"ILP32"
|
|
134
|
-
];
|
|
135
|
-
|
|
136
|
-
const compilers: TCompiler[] = [
|
|
137
|
-
"gcc"
|
|
138
|
-
];
|
|
139
|
-
|
|
140
|
-
const fieldsDefinitions: { structName: string, fields: TStructDefinition["fields"] }[] = [
|
|
141
|
-
{
|
|
142
|
-
structName: "definition #1",
|
|
143
|
-
fields: [
|
|
144
|
-
cField({ name: "a", cType: "char" }),
|
|
145
|
-
cField({ name: "b", cType: "int" }),
|
|
146
|
-
]
|
|
147
|
-
},
|
|
148
|
-
// {
|
|
149
|
-
// fields: [
|
|
150
|
-
// cField({ name: "a", cType: "double" }),
|
|
151
|
-
// cField({ name: "b", cType: "int" }),
|
|
152
|
-
// cField({ name: "c", cType: "char" }),
|
|
153
|
-
// ]
|
|
154
|
-
// }
|
|
155
|
-
{
|
|
156
|
-
structName: "definition #2",
|
|
157
|
-
fields: [
|
|
158
|
-
cField({ name: "a", cType: "char" }),
|
|
159
|
-
cField({ name: "b", cType: "char" }),
|
|
160
|
-
cField({ name: "c", cType: "char" }),
|
|
161
|
-
]
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
structName: "definition #3",
|
|
165
|
-
fields: [
|
|
166
|
-
cField({ name: "a", cType: "unsigned char" }),
|
|
167
|
-
cField({ name: "b", cType: "unsigned char" }),
|
|
168
|
-
cField({ name: "c", cType: "unsigned char" }),
|
|
169
|
-
]
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
structName: "definition #4",
|
|
173
|
-
fields: [
|
|
174
|
-
cField({ name: "a", cType: "short" }),
|
|
175
|
-
cField({ name: "b", cType: "short" }),
|
|
176
|
-
cField({ name: "c", cType: "short" }),
|
|
177
|
-
]
|
|
178
|
-
},
|
|
179
|
-
{
|
|
180
|
-
structName: "definition #5",
|
|
181
|
-
fields: [
|
|
182
|
-
cField({ name: "a", cType: "unsigned short" }),
|
|
183
|
-
cField({ name: "b", cType: "unsigned short" }),
|
|
184
|
-
cField({ name: "c", cType: "unsigned short" }),
|
|
185
|
-
]
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
structName: "definition #6",
|
|
189
|
-
fields: [
|
|
190
|
-
cField({ name: "a", cType: "int" }),
|
|
191
|
-
cField({ name: "b", cType: "int" }),
|
|
192
|
-
cField({ name: "c", cType: "int" }),
|
|
193
|
-
]
|
|
194
|
-
},
|
|
195
|
-
{
|
|
196
|
-
structName: "definition #7",
|
|
197
|
-
fields: [
|
|
198
|
-
cField({ name: "a", cType: "unsigned int" }),
|
|
199
|
-
cField({ name: "b", cType: "unsigned int" }),
|
|
200
|
-
cField({ name: "c", cType: "unsigned int" }),
|
|
201
|
-
]
|
|
202
|
-
},
|
|
203
|
-
{
|
|
204
|
-
structName: "definition #8",
|
|
205
|
-
fields: [
|
|
206
|
-
cField({ name: "a", cType: "long" }),
|
|
207
|
-
cField({ name: "b", cType: "long" }),
|
|
208
|
-
cField({ name: "c", cType: "long" }),
|
|
209
|
-
]
|
|
210
|
-
},
|
|
211
|
-
{
|
|
212
|
-
structName: "definition #9",
|
|
213
|
-
fields: [
|
|
214
|
-
cField({ name: "a", cType: "unsigned long" }),
|
|
215
|
-
cField({ name: "b", cType: "unsigned long" }),
|
|
216
|
-
cField({ name: "c", cType: "unsigned long" }),
|
|
217
|
-
]
|
|
218
|
-
},
|
|
219
|
-
{
|
|
220
|
-
structName: "definition #10",
|
|
221
|
-
fields: [
|
|
222
|
-
cField({ name: "a", cType: "long long" }),
|
|
223
|
-
cField({ name: "b", cType: "long long" }),
|
|
224
|
-
cField({ name: "c", cType: "long long" }),
|
|
225
|
-
]
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
structName: "definition #11",
|
|
229
|
-
fields: [
|
|
230
|
-
cField({ name: "a", cType: "unsigned long long" }),
|
|
231
|
-
cField({ name: "b", cType: "unsigned long long" }),
|
|
232
|
-
cField({ name: "c", cType: "unsigned long long" }),
|
|
233
|
-
]
|
|
234
|
-
},
|
|
235
|
-
{
|
|
236
|
-
structName: "definition #12",
|
|
237
|
-
fields: [
|
|
238
|
-
cField({ name: "a", cType: "char" }),
|
|
239
|
-
cField({ name: "b", cType: "short" }),
|
|
240
|
-
cField({ name: "c", cType: "int" }),
|
|
241
|
-
cField({ name: "d", cType: "long" }),
|
|
242
|
-
cField({ name: "e", cType: "long long" }),
|
|
243
|
-
]
|
|
244
|
-
},
|
|
245
|
-
{
|
|
246
|
-
structName: "definition #13",
|
|
247
|
-
fields: [
|
|
248
|
-
cField({ name: "a", cType: "unsigned char" }),
|
|
249
|
-
cField({ name: "b", cType: "unsigned short" }),
|
|
250
|
-
cField({ name: "c", cType: "unsigned int" }),
|
|
251
|
-
cField({ name: "d", cType: "unsigned long" }),
|
|
252
|
-
cField({ name: "e", cType: "unsigned long long" }),
|
|
253
|
-
]
|
|
254
|
-
},
|
|
255
|
-
{
|
|
256
|
-
structName: "definition #14",
|
|
257
|
-
fields: [
|
|
258
|
-
cField({ name: "a", cType: "long long" }),
|
|
259
|
-
cField({ name: "b", cType: "long" }),
|
|
260
|
-
cField({ name: "c", cType: "int" }),
|
|
261
|
-
cField({ name: "d", cType: "short" }),
|
|
262
|
-
cField({ name: "e", cType: "char" }),
|
|
263
|
-
]
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
structName: "definition #15",
|
|
267
|
-
fields: [
|
|
268
|
-
cField({ name: "a", cType: "unsigned long long" }),
|
|
269
|
-
cField({ name: "b", cType: "unsigned long" }),
|
|
270
|
-
cField({ name: "c", cType: "unsigned int" }),
|
|
271
|
-
cField({ name: "d", cType: "unsigned short" }),
|
|
272
|
-
cField({ name: "e", cType: "unsigned char" }),
|
|
273
|
-
]
|
|
274
|
-
},
|
|
275
|
-
|
|
276
|
-
{
|
|
277
|
-
structName: "definition #16",
|
|
278
|
-
fields: [
|
|
279
|
-
cField({ name: "a", cType: "int" }),
|
|
280
|
-
{
|
|
281
|
-
name: "b",
|
|
282
|
-
definition: {
|
|
283
|
-
type: "string",
|
|
284
|
-
charSizeInBits: 8,
|
|
285
|
-
nullTerminatorMandatory: true,
|
|
286
|
-
length: 9
|
|
287
|
-
}
|
|
288
|
-
},
|
|
289
|
-
cField({ name: "c", cType: "int" }),
|
|
290
|
-
]
|
|
291
|
-
},
|
|
292
|
-
|
|
293
|
-
{
|
|
294
|
-
structName: "definition #17",
|
|
295
|
-
fields: [
|
|
296
|
-
cField({ name: "a", cType: "char" }),
|
|
297
|
-
{
|
|
298
|
-
name: "b",
|
|
299
|
-
definition: {
|
|
300
|
-
type: "pointer",
|
|
301
|
-
fixedAbi: {}
|
|
302
|
-
}
|
|
303
|
-
},
|
|
304
|
-
cField({ name: "c", cType: "unsigned long" }),
|
|
305
|
-
]
|
|
306
|
-
},
|
|
307
|
-
|
|
308
|
-
{
|
|
309
|
-
structName: "definition #18",
|
|
310
|
-
fields: [
|
|
311
|
-
cField({ name: "a", cType: "char" }),
|
|
312
|
-
{
|
|
313
|
-
name: "b",
|
|
314
|
-
definition: {
|
|
315
|
-
type: "pointer",
|
|
316
|
-
fixedAbi: {}
|
|
317
|
-
}
|
|
318
|
-
},
|
|
319
|
-
cField({ name: "c", cType: "unsigned long long" }),
|
|
320
|
-
]
|
|
321
|
-
},
|
|
322
|
-
|
|
323
|
-
{
|
|
324
|
-
structName: "definition #19",
|
|
325
|
-
fields: [
|
|
326
|
-
cField({ name: "a", cType: "char" }),
|
|
327
|
-
{
|
|
328
|
-
name: "b",
|
|
329
|
-
definition: {
|
|
330
|
-
type: "pointer",
|
|
331
|
-
fixedAbi: {}
|
|
332
|
-
}
|
|
333
|
-
},
|
|
334
|
-
cField({ name: "c", cType: "unsigned int" }),
|
|
335
|
-
]
|
|
336
|
-
},
|
|
337
|
-
|
|
338
|
-
{
|
|
339
|
-
structName: "definition #20",
|
|
340
|
-
fields: [
|
|
341
|
-
cField({ name: "a", cType: "unsigned long" }),
|
|
342
|
-
{
|
|
343
|
-
name: "b",
|
|
344
|
-
definition: {
|
|
345
|
-
type: "pointer",
|
|
346
|
-
fixedAbi: {}
|
|
347
|
-
}
|
|
348
|
-
},
|
|
349
|
-
cField({ name: "c", cType: "unsigned int" }),
|
|
350
|
-
]
|
|
351
|
-
},
|
|
352
|
-
|
|
353
|
-
{
|
|
354
|
-
structName: "definition #21",
|
|
355
|
-
fields: [
|
|
356
|
-
{
|
|
357
|
-
name: "a",
|
|
358
|
-
definition: {
|
|
359
|
-
type: "pointer",
|
|
360
|
-
fixedAbi: {}
|
|
361
|
-
}
|
|
362
|
-
},
|
|
363
|
-
{
|
|
364
|
-
name: "b",
|
|
365
|
-
definition: {
|
|
366
|
-
type: "pointer",
|
|
367
|
-
fixedAbi: {}
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
|
-
{
|
|
371
|
-
name: "c",
|
|
372
|
-
definition: {
|
|
373
|
-
type: "pointer",
|
|
374
|
-
fixedAbi: {}
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
]
|
|
378
|
-
}
|
|
379
|
-
];
|
|
380
|
-
|
|
381
|
-
fieldsDefinitions.forEach(({ fields, structName }) => {
|
|
382
|
-
[
|
|
383
|
-
false,
|
|
384
|
-
true
|
|
385
|
-
].forEach((packed) => {
|
|
386
|
-
dataModels.forEach((dataModel) => {
|
|
387
|
-
compilers.forEach((compiler) => {
|
|
388
|
-
defineStructTestFor({
|
|
389
|
-
packed,
|
|
390
|
-
dataModel,
|
|
391
|
-
compiler,
|
|
392
|
-
fields,
|
|
393
|
-
structName
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
});
|
|
399
|
-
});
|
package/test/compile-util.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import tmp from "tmp-promise";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
import child_process from "node:child_process";
|
|
5
|
-
import assert from "node:assert";
|
|
6
|
-
|
|
7
|
-
const exec = ({ command }: { command: string }) => {
|
|
8
|
-
return new Promise<{ stdout: string }>((resolve, reject) => {
|
|
9
|
-
child_process.exec(command, (err, stdout, stderr) => {
|
|
10
|
-
if (err) {
|
|
11
|
-
reject(Error(`run failed: ${stderr}`, { cause: err }));
|
|
12
|
-
} else {
|
|
13
|
-
resolve({ stdout });
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const compileAndRun = async ({ code, bits }: { code: string, bits: 64 | 32 }) => {
|
|
20
|
-
return await tmp.withDir(async (dir) => {
|
|
21
|
-
const sourceFile = path.resolve(dir.path, "main.c");
|
|
22
|
-
const binaryFile = path.resolve(dir.path, "main.out");
|
|
23
|
-
|
|
24
|
-
await fs.promises.writeFile(sourceFile, code);
|
|
25
|
-
try {
|
|
26
|
-
await exec({ command: `gcc -m${bits} "${sourceFile}" -o "${binaryFile}"` });
|
|
27
|
-
try {
|
|
28
|
-
return await exec({ command: `"${binaryFile}"` });
|
|
29
|
-
} finally {
|
|
30
|
-
await fs.promises.unlink(binaryFile);
|
|
31
|
-
}
|
|
32
|
-
} finally {
|
|
33
|
-
await fs.promises.unlink(sourceFile);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const determineCCompilerStructLayout = async ({
|
|
39
|
-
definitions,
|
|
40
|
-
structName,
|
|
41
|
-
fieldNames,
|
|
42
|
-
bits
|
|
43
|
-
}: {
|
|
44
|
-
definitions: string,
|
|
45
|
-
structName: string,
|
|
46
|
-
fieldNames: string[],
|
|
47
|
-
bits: 64 | 32
|
|
48
|
-
}) => {
|
|
49
|
-
const code = `
|
|
50
|
-
|
|
51
|
-
${definitions}
|
|
52
|
-
|
|
53
|
-
#include <stdio.h>
|
|
54
|
-
|
|
55
|
-
int main(int argc, char* argv[]) {
|
|
56
|
-
printf("{ \\"fields\\": {");
|
|
57
|
-
${fieldNames.map((field, idx) => {
|
|
58
|
-
|
|
59
|
-
let lines = [`printf("\\"${field}\\": { \\"offset\\": %i, \\"length\\": %i }",
|
|
60
|
-
__builtin_offsetof(struct ${structName}, ${field}),
|
|
61
|
-
sizeof(((struct ${structName}*) 0)->${field}));`];
|
|
62
|
-
|
|
63
|
-
if (idx < fieldNames.length - 1) {
|
|
64
|
-
lines = [...lines, ` printf(", "); `];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return lines.join("\n");
|
|
68
|
-
}).join("\n")}
|
|
69
|
-
printf("} }");
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
|
74
|
-
`;
|
|
75
|
-
|
|
76
|
-
const { stdout } = await compileAndRun({ code, bits });
|
|
77
|
-
|
|
78
|
-
const parsed = JSON.parse(stdout.trim());
|
|
79
|
-
|
|
80
|
-
const fields = parsed.fields;
|
|
81
|
-
Object.keys(fields).forEach((fieldName) => {
|
|
82
|
-
assert.ok(typeof fields[fieldName].offset === "number");
|
|
83
|
-
assert.ok(typeof fields[fieldName].length === "number");
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
return fields as { [key: string]: { offset: number; length: number } };
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export {
|
|
90
|
-
compileAndRun,
|
|
91
|
-
determineCCompilerStructLayout
|
|
92
|
-
};
|