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/struct.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import type { TEndianness } from "../common.ts";
|
|
2
|
-
import type { TLayoutedField } from "../layout.ts";
|
|
3
|
-
import { createIntegerParser } from "./integer.ts";
|
|
4
|
-
import { createPointerParser } from "./pointer.ts";
|
|
5
|
-
import { createStringParser } from "./string.ts";
|
|
6
|
-
import { createArrayParser } from "./array.ts";
|
|
7
|
-
import type { TValueParser } from "./value.ts";
|
|
8
|
-
import type { TFieldType } from "./index.ts";
|
|
9
|
-
|
|
10
|
-
type TStructParser = TValueParser<Record<string, unknown>>;
|
|
11
|
-
|
|
12
|
-
const subData = ({ data, offsetInBits, sizeInBits }: { data: Uint8Array, offsetInBits: number, sizeInBits: number }) => {
|
|
13
|
-
return {
|
|
14
|
-
data: new Uint8Array(data.buffer, data.byteOffset + Math.floor(offsetInBits / 8), Math.ceil(sizeInBits / 8)),
|
|
15
|
-
offsetInBits: offsetInBits % 8
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const createStructParser = ({
|
|
20
|
-
layoutedFields,
|
|
21
|
-
structOffsetInBits,
|
|
22
|
-
endianness
|
|
23
|
-
}: {
|
|
24
|
-
layoutedFields: (TLayoutedField & { type: "struct" })["fields"];
|
|
25
|
-
structOffsetInBits: number;
|
|
26
|
-
endianness: TEndianness
|
|
27
|
-
}): TStructParser => {
|
|
28
|
-
|
|
29
|
-
// eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any
|
|
30
|
-
const fieldParsers: TValueParser<any>[] = layoutedFields.map((field) => {
|
|
31
|
-
if (field.definition.type === "integer") {
|
|
32
|
-
return createIntegerParser({
|
|
33
|
-
sizeInBits: field.definition.sizeInBits,
|
|
34
|
-
signed: field.definition.signed,
|
|
35
|
-
endianness
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (field.definition.type === "pointer") {
|
|
40
|
-
return createPointerParser({
|
|
41
|
-
sizeInBits: field.definition.sizeInBits,
|
|
42
|
-
endianness
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (field.definition.type === "struct") {
|
|
47
|
-
return createStructParser({
|
|
48
|
-
layoutedFields: field.definition.fields,
|
|
49
|
-
structOffsetInBits: field.definition.offsetInBits,
|
|
50
|
-
endianness
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (field.definition.type === "string") {
|
|
55
|
-
return createStringParser({
|
|
56
|
-
length: field.definition.length,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (field.definition.type === "array") {
|
|
61
|
-
return createArrayParser({
|
|
62
|
-
// TODO: cast should not be necessary
|
|
63
|
-
elementType: field.definition.elementType as TFieldType,
|
|
64
|
-
endianness,
|
|
65
|
-
length: field.definition.length
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
throw Error("not implemented yet");
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const parse: TStructParser["parse"] = ({ data, offsetInBits }) => {
|
|
73
|
-
if (offsetInBits !== 0) {
|
|
74
|
-
throw Error("unaligned struct parsing not supported yet");
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// eslint-disable-next-line prefer-const
|
|
78
|
-
let result: Record<string, unknown> = {};
|
|
79
|
-
|
|
80
|
-
layoutedFields.forEach((field, idx) => {
|
|
81
|
-
const fieldParser = fieldParsers[idx];
|
|
82
|
-
|
|
83
|
-
// field definition is absolute, subtracting struct offset gives bit offset inside struct
|
|
84
|
-
// adding data bit offset gives bit offset inside provided data
|
|
85
|
-
const offsetToProvidedDataInBits = field.definition.offsetInBits - structOffsetInBits + offsetInBits;
|
|
86
|
-
|
|
87
|
-
const offsetInBitsInByte = field.definition.offsetInBits % 8;
|
|
88
|
-
if (offsetInBitsInByte !== 0) {
|
|
89
|
-
throw Error("not implemented yet: unaligned field parsing");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const {
|
|
93
|
-
data: fieldData,
|
|
94
|
-
offsetInBits: fieldOffsetInBits
|
|
95
|
-
} = subData({
|
|
96
|
-
data,
|
|
97
|
-
offsetInBits: offsetToProvidedDataInBits,
|
|
98
|
-
sizeInBits: field.definition.sizeInBits
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
result[field.name] = fieldParser.parse({ data: fieldData, offsetInBits: fieldOffsetInBits });
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
return result;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const format: TStructParser["format"] = ({ value, target, offsetInBits }) => {
|
|
108
|
-
if (offsetInBits % 8 !== 0) {
|
|
109
|
-
throw Error("unaligned struct formatting not supported yet");
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
layoutedFields.forEach((field, idx) => {
|
|
113
|
-
const fieldValue = value[field.name];
|
|
114
|
-
const fieldParser = fieldParsers[idx];
|
|
115
|
-
|
|
116
|
-
const offsetInBitsInByte = field.definition.offsetInBits % 8;
|
|
117
|
-
if (offsetInBitsInByte !== 0) {
|
|
118
|
-
throw Error("not implemented yet: unaligned field formatting");
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const {
|
|
122
|
-
data: fieldTarget,
|
|
123
|
-
offsetInBits: fieldOffsetInBits
|
|
124
|
-
} = subData({
|
|
125
|
-
data: target,
|
|
126
|
-
offsetInBits: field.definition.offsetInBits - structOffsetInBits + offsetInBits,
|
|
127
|
-
sizeInBits: field.definition.sizeInBits
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
fieldParser.format({ value: fieldValue, target: fieldTarget, offsetInBits: fieldOffsetInBits });
|
|
132
|
-
} catch (ex) {
|
|
133
|
-
throw Error(`failed to format field "${field.name}"`, { cause: ex });
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
parse,
|
|
140
|
-
format
|
|
141
|
-
};
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
export {
|
|
145
|
-
createStructParser
|
|
146
|
-
};
|
package/lib/types/value.ts
DELETED
package/package.npm.json
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ya-struct",
|
|
3
|
-
"description": "Yet Another Node.js Structure API",
|
|
4
|
-
"main": "dist/lib/index.js",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/k13-engineering/node-ya-struct.git"
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"structure",
|
|
11
|
-
"struct",
|
|
12
|
-
"abi",
|
|
13
|
-
"endian",
|
|
14
|
-
"alignment",
|
|
15
|
-
"c",
|
|
16
|
-
"c++",
|
|
17
|
-
"layout"
|
|
18
|
-
],
|
|
19
|
-
"author": "Simon Kadisch",
|
|
20
|
-
"license": "LGPL 2.1",
|
|
21
|
-
"bugs": {
|
|
22
|
-
"url": "https://github.com/k13-engineering/node-ya-struct/issues"
|
|
23
|
-
},
|
|
24
|
-
"homepage": "https://github.com/k13-engineering/node-ya-struct#readme"
|
|
25
|
-
}
|
package/samples/basic.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { define, types } from "../lib/index.ts";
|
|
2
|
-
|
|
3
|
-
const def = define({
|
|
4
|
-
definition: {
|
|
5
|
-
type: "struct",
|
|
6
|
-
packed: false,
|
|
7
|
-
fixedAbi: {},
|
|
8
|
-
fields: [
|
|
9
|
-
{ name: "a", definition: types.Int16 },
|
|
10
|
-
{ name: "b", definition: types.UInt16 },
|
|
11
|
-
{ name: "c", definition: types.UInt32 },
|
|
12
|
-
]
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const parser = def.parser({
|
|
17
|
-
abi: {
|
|
18
|
-
endianness: "little",
|
|
19
|
-
dataModel: "LP64",
|
|
20
|
-
compiler: "gcc",
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const value: ReturnType<typeof parser.parse> = {
|
|
25
|
-
a: 0n,
|
|
26
|
-
b: 1n,
|
|
27
|
-
c: 2n,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
console.log("value", value);
|
|
31
|
-
|
|
32
|
-
const formatted = parser.format({
|
|
33
|
-
value
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
console.log("formatted", formatted);
|
|
37
|
-
|
|
38
|
-
const reparsed = parser.parse({ data: formatted });
|
|
39
|
-
|
|
40
|
-
console.log("reparsed", reparsed);
|
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
|
-
};
|