ya-struct 0.0.11 → 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
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { define } from "./parser.js";
|
|
2
2
|
import { types } from "./types/index.js";
|
|
3
|
+
import { compileAndCompare } from "./tests/compile-and-compare.vibe.js";
|
|
3
4
|
import type { TAbi, TCompiler, TDataModel, TEndianness } from "./common.js";
|
|
4
|
-
export { define, types };
|
|
5
|
+
export { define, types, compileAndCompare };
|
|
5
6
|
export type { TAbi, TEndianness, TCompiler, TDataModel };
|
package/dist/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { define } from "./parser.js";
|
|
2
2
|
import { types } from "./types/index.js";
|
|
3
|
+
import { compileAndCompare } from "./tests/compile-and-compare.vibe.js";
|
|
3
4
|
|
|
4
5
|
/*import type {
|
|
5
6
|
TAbi,
|
|
@@ -10,7 +11,8 @@ import { types } from "./types/index.js";
|
|
|
10
11
|
|
|
11
12
|
export {
|
|
12
13
|
define,
|
|
13
|
-
types
|
|
14
|
+
types,
|
|
15
|
+
compileAndCompare
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
/*export type {
|
package/dist/lib/parser.d.ts
CHANGED
package/dist/lib/parser.js
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { TAbi } from "../common.js";
|
|
2
|
+
import type { TFieldType } from "../types/index.js";
|
|
3
|
+
type TLayoutError = {
|
|
4
|
+
type: "size-mismatch";
|
|
5
|
+
fieldPath: string;
|
|
6
|
+
expectedSizeInBits: number;
|
|
7
|
+
actualSizeInBits: number;
|
|
8
|
+
} | {
|
|
9
|
+
type: "offset-mismatch";
|
|
10
|
+
fieldPath: string;
|
|
11
|
+
expectedOffsetInBits: number;
|
|
12
|
+
actualOffsetInBits: number;
|
|
13
|
+
};
|
|
14
|
+
type TCompileAndCompareResult = {
|
|
15
|
+
layoutErrors: TLayoutError[];
|
|
16
|
+
};
|
|
17
|
+
declare const compileAndCompare: ({ structDefinition, abi, globalCode, cStructName, compileAndRun }: {
|
|
18
|
+
structDefinition: Extract<TFieldType, {
|
|
19
|
+
type: "struct";
|
|
20
|
+
}>;
|
|
21
|
+
abi: TAbi;
|
|
22
|
+
globalCode: string;
|
|
23
|
+
cStructName: string;
|
|
24
|
+
compileAndRun: ({ sourceCode }: {
|
|
25
|
+
sourceCode: string;
|
|
26
|
+
}) => Promise<{
|
|
27
|
+
output: string;
|
|
28
|
+
}>;
|
|
29
|
+
}) => Promise<TCompileAndCompareResult>;
|
|
30
|
+
export { compileAndCompare };
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/*import type { TAbi } from "../common.ts";*/
|
|
2
|
+
/*import type { TLayoutedField } from "../layout.ts";*/
|
|
3
|
+
import { define } from "../parser.js";
|
|
4
|
+
/*import type { TFieldType } from "../types/index.ts";*/
|
|
5
|
+
|
|
6
|
+
/*type TLayoutError = {
|
|
7
|
+
type: "size-mismatch",
|
|
8
|
+
fieldPath: string;
|
|
9
|
+
expectedSizeInBits: number;
|
|
10
|
+
actualSizeInBits: number;
|
|
11
|
+
} | {
|
|
12
|
+
type: "offset-mismatch",
|
|
13
|
+
fieldPath: string;
|
|
14
|
+
expectedOffsetInBits: number;
|
|
15
|
+
actualOffsetInBits: number;
|
|
16
|
+
};*/
|
|
17
|
+
|
|
18
|
+
/*type TCompileAndCompareResult = {
|
|
19
|
+
layoutErrors: TLayoutError[];
|
|
20
|
+
};*/
|
|
21
|
+
|
|
22
|
+
/*type TFlatField = {
|
|
23
|
+
fieldPath: string;
|
|
24
|
+
offsetInBits: number;
|
|
25
|
+
sizeInBits: number;
|
|
26
|
+
isStruct: boolean;
|
|
27
|
+
};*/
|
|
28
|
+
|
|
29
|
+
/*type TStructLayoutedFields =
|
|
30
|
+
(TLayoutedField & { type: "struct" })["fields"];*/
|
|
31
|
+
|
|
32
|
+
const flattenLayout = ({
|
|
33
|
+
fields,
|
|
34
|
+
parentPath
|
|
35
|
+
}/*: {
|
|
36
|
+
fields: TStructLayoutedFields;
|
|
37
|
+
parentPath: string;
|
|
38
|
+
}*/)/*: TFlatField[]*/ => {
|
|
39
|
+
return fields.flatMap((field) => {
|
|
40
|
+
const fieldPath = parentPath
|
|
41
|
+
? `${parentPath}.${field.name}`
|
|
42
|
+
: field.name;
|
|
43
|
+
|
|
44
|
+
const isStruct = field.definition.type === "struct";
|
|
45
|
+
|
|
46
|
+
const entry/*: TFlatField*/ = {
|
|
47
|
+
fieldPath,
|
|
48
|
+
offsetInBits: field.definition.offsetInBits,
|
|
49
|
+
sizeInBits: field.definition.sizeInBits,
|
|
50
|
+
isStruct,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
if (isStruct) {
|
|
54
|
+
return [
|
|
55
|
+
entry,
|
|
56
|
+
...flattenLayout({
|
|
57
|
+
fields: field.definition.fields,
|
|
58
|
+
parentPath: fieldPath,
|
|
59
|
+
}),
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return [entry];
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const generateSourceCode = ({
|
|
68
|
+
globalCode,
|
|
69
|
+
cStructName,
|
|
70
|
+
flatFields,
|
|
71
|
+
}/*: {
|
|
72
|
+
globalCode: string;
|
|
73
|
+
cStructName: string;
|
|
74
|
+
flatFields: TFlatField[];
|
|
75
|
+
}*/) => {
|
|
76
|
+
|
|
77
|
+
const printStatements = flatFields.map((f, idx) => {
|
|
78
|
+
if (f.isStruct) {
|
|
79
|
+
const offsetExpr =
|
|
80
|
+
`offsetof(struct ${cStructName}, ${f.fieldPath}) * CHAR_BIT`;
|
|
81
|
+
const sizeExpr =
|
|
82
|
+
`sizeof(((struct ${cStructName}*)0)->${f.fieldPath}) * CHAR_BIT`;
|
|
83
|
+
|
|
84
|
+
return ` printf("%zu %zu\\n", `
|
|
85
|
+
+ `(size_t)(${offsetExpr}), (size_t)(${sizeExpr}));`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Use memory scanning to detect actual bit offset and size.
|
|
89
|
+
// This works for both regular fields and bitfields.
|
|
90
|
+
return ` {
|
|
91
|
+
struct ${cStructName} __s${idx};
|
|
92
|
+
memset(&__s${idx}, 0, sizeof(__s${idx}));
|
|
93
|
+
memset(&__s${idx}.${f.fieldPath}, 0xFF, sizeof(__s${idx}.${f.fieldPath}));
|
|
94
|
+
unsigned char *__p${idx} = (unsigned char *)&__s${idx};
|
|
95
|
+
size_t __off${idx} = 0, __sz${idx} = 0;
|
|
96
|
+
int __found${idx} = 0;
|
|
97
|
+
for (size_t __i = 0; __i < sizeof(struct ${cStructName}) * CHAR_BIT; __i++) {
|
|
98
|
+
if (__p${idx}[__i / CHAR_BIT] & (1u << (__i % CHAR_BIT))) {
|
|
99
|
+
if (!__found${idx}) { __off${idx} = __i; __found${idx} = 1; }
|
|
100
|
+
__sz${idx}++;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
printf("%zu %zu\\n", __off${idx}, __sz${idx});
|
|
104
|
+
}`;
|
|
105
|
+
}).join("\n");
|
|
106
|
+
|
|
107
|
+
return `#include <stdio.h>
|
|
108
|
+
#include <stddef.h>
|
|
109
|
+
#include <limits.h>
|
|
110
|
+
#include <string.h>
|
|
111
|
+
${globalCode}
|
|
112
|
+
|
|
113
|
+
int main(void) {
|
|
114
|
+
${printStatements}
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
`;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const parseCOutput = ({
|
|
121
|
+
output,
|
|
122
|
+
}/*: {
|
|
123
|
+
output: string;
|
|
124
|
+
}*/)/*: { offset: number; size: number }[]*/ => {
|
|
125
|
+
return output.trim().split("\n").map((line) => {
|
|
126
|
+
const parts = line.trim().split(" ");
|
|
127
|
+
return {
|
|
128
|
+
offset: parseInt(parts[0], 10),
|
|
129
|
+
size: parseInt(parts[1], 10),
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const compareLayouts = ({
|
|
135
|
+
flatFields,
|
|
136
|
+
cFields,
|
|
137
|
+
}/*: {
|
|
138
|
+
flatFields: TFlatField[];
|
|
139
|
+
cFields: { offset: number; size: number }[];
|
|
140
|
+
}*/)/*: TLayoutError[]*/ => {
|
|
141
|
+
return flatFields.flatMap((field, idx) => {
|
|
142
|
+
const cField = cFields[idx];
|
|
143
|
+
if (!cField) {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const sizeMismatch/*: TLayoutError[]*/ =
|
|
148
|
+
cField.size === field.sizeInBits
|
|
149
|
+
? []
|
|
150
|
+
: [{
|
|
151
|
+
type: "size-mismatch" /*as const*/,
|
|
152
|
+
fieldPath: field.fieldPath,
|
|
153
|
+
expectedSizeInBits: cField.size,
|
|
154
|
+
actualSizeInBits: field.sizeInBits,
|
|
155
|
+
}];
|
|
156
|
+
|
|
157
|
+
const offsetMismatch/*: TLayoutError[]*/ =
|
|
158
|
+
cField.offset === field.offsetInBits
|
|
159
|
+
? []
|
|
160
|
+
: [{
|
|
161
|
+
type: "offset-mismatch" /*as const*/,
|
|
162
|
+
fieldPath: field.fieldPath,
|
|
163
|
+
expectedOffsetInBits: cField.offset,
|
|
164
|
+
actualOffsetInBits: field.offsetInBits,
|
|
165
|
+
}];
|
|
166
|
+
|
|
167
|
+
return [...sizeMismatch, ...offsetMismatch];
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const compileAndCompare = async ({
|
|
172
|
+
structDefinition,
|
|
173
|
+
abi,
|
|
174
|
+
|
|
175
|
+
globalCode,
|
|
176
|
+
cStructName,
|
|
177
|
+
|
|
178
|
+
compileAndRun
|
|
179
|
+
}/*: {
|
|
180
|
+
structDefinition: Extract<TFieldType, { type: "struct" }>;
|
|
181
|
+
abi: TAbi;
|
|
182
|
+
|
|
183
|
+
globalCode: string;
|
|
184
|
+
cStructName: string;
|
|
185
|
+
|
|
186
|
+
compileAndRun: ({ sourceCode }: { sourceCode: string }) => Promise<{
|
|
187
|
+
output: string;
|
|
188
|
+
}>;
|
|
189
|
+
}*/)/*: Promise<TCompileAndCompareResult>*/ => {
|
|
190
|
+
|
|
191
|
+
const def = define({ definition: structDefinition });
|
|
192
|
+
const parser = def.parser({ abi });
|
|
193
|
+
const layoutResult = parser.layout;
|
|
194
|
+
|
|
195
|
+
if (layoutResult.type !== "struct") {
|
|
196
|
+
throw Error("expected struct layout");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const flatFields = flattenLayout({
|
|
200
|
+
fields: layoutResult.fields,
|
|
201
|
+
parentPath: "",
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const sourceCode = generateSourceCode({
|
|
205
|
+
globalCode,
|
|
206
|
+
cStructName,
|
|
207
|
+
flatFields,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const { output } = await compileAndRun({ sourceCode });
|
|
211
|
+
|
|
212
|
+
const cFields = parseCOutput({ output });
|
|
213
|
+
|
|
214
|
+
const layoutErrors = compareLayouts({ flatFields, cFields });
|
|
215
|
+
|
|
216
|
+
return { layoutErrors };
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export {
|
|
220
|
+
compileAndCompare
|
|
221
|
+
};
|
package/package.json
CHANGED