ya-struct 0.0.5 → 0.0.6

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.
Files changed (61) hide show
  1. package/.editorconfig +8 -0
  2. package/.github/workflows/ci.yml +23 -0
  3. package/.github/workflows/npm-publish.yml +28 -0
  4. package/README.md +150 -18
  5. package/dist/lib/common.d.ts +14 -0
  6. package/dist/lib/common.js +30 -0
  7. package/dist/lib/index.d.ts +5 -0
  8. package/dist/lib/index.js +21 -0
  9. package/dist/lib/layout.d.ts +48 -0
  10. package/dist/lib/layout.js +262 -0
  11. package/dist/lib/parser.d.ts +51 -0
  12. package/dist/lib/parser.js +87 -0
  13. package/dist/lib/types/array.d.ts +10 -0
  14. package/dist/lib/types/array.js +90 -0
  15. package/dist/lib/types/c-types.d.ts +13 -0
  16. package/dist/lib/types/c-types.js +222 -0
  17. package/dist/lib/types/index.d.ts +93 -0
  18. package/dist/lib/types/index.js +122 -0
  19. package/dist/lib/types/integer.d.ts +9 -0
  20. package/dist/lib/types/integer.js +160 -0
  21. package/dist/lib/types/pointer.d.ts +8 -0
  22. package/dist/lib/types/pointer.js +27 -0
  23. package/dist/lib/types/string.d.ts +6 -0
  24. package/dist/lib/types/string.js +56 -0
  25. package/dist/lib/types/struct.d.ts +11 -0
  26. package/dist/lib/types/struct.js +113 -0
  27. package/dist/lib/types/value.d.ts +12 -0
  28. package/dist/lib/types/value.js +8 -0
  29. package/eslint.config.js +127 -0
  30. package/lib/bit-buffer.ts +70 -0
  31. package/lib/common.ts +30 -0
  32. package/lib/index.ts +21 -0
  33. package/lib/layout.ts +262 -0
  34. package/lib/parser.ts +87 -0
  35. package/lib/types/array.ts +90 -0
  36. package/lib/types/c-types.ts +222 -0
  37. package/lib/types/index.ts +122 -0
  38. package/lib/types/integer.ts +160 -0
  39. package/lib/types/pointer.ts +27 -0
  40. package/lib/types/string.ts +56 -0
  41. package/lib/types/struct.ts +113 -0
  42. package/lib/types/value.ts +8 -0
  43. package/package.json +19 -15
  44. package/package.npm.json +25 -0
  45. package/samples/basic.ts +40 -0
  46. package/test/c-structs.ts +399 -0
  47. package/test/compile-util.ts +92 -0
  48. package/tsconfig.json +10 -0
  49. package/.eslintrc +0 -92
  50. package/.github/workflows/CI.yml +0 -39
  51. package/.prettierrc +0 -5
  52. package/lib/builder.js +0 -62
  53. package/lib/index.js +0 -159
  54. package/lib/marshaller.js +0 -88
  55. package/lib/refbuf.js +0 -11
  56. package/lib/types/basic.js +0 -200
  57. package/lib/types/ctypes.js +0 -160
  58. package/test/abi.js +0 -203
  59. package/test/basic.js +0 -92
  60. package/test/ctypes.js +0 -166
  61. package/test/ref.js +0 -35
@@ -0,0 +1,399 @@
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
+ });
@@ -0,0 +1,92 @@
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
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "NodeNext",
4
+ "allowImportingTsExtensions": true,
5
+ "moduleResolution": "NodeNext",
6
+ "noEmit": true,
7
+ "esModuleInterop": true,
8
+ "strict": true
9
+ }
10
+ }
package/.eslintrc DELETED
@@ -1,92 +0,0 @@
1
- {
2
- "parserOptions": {
3
- "ecmaVersion": 2021,
4
- "sourceType": "module",
5
- "requireConfigFile": false
6
- },
7
- "env": {
8
- "node": true,
9
- "es2020": true
10
- },
11
- "rules": {
12
- "global-require": "off",
13
- "quote-props": ["warn", "consistent-as-needed"],
14
- "comma-dangle": ["error", {"arrays": "always-multiline", "objects": "always-multiline", "functions": "never"}],
15
- "id-length": ["error", {"min": 3, "properties": "always", "exceptions": ["i", "os", "fs"]}],
16
- "quotes": ["error", "double", { "allowTemplateLiterals": true }],
17
- "no-plusplus": "error",
18
- "no-nested-ternary": "error",
19
- "no-multiple-empty-lines": "error",
20
- "no-inline-comments": "error",
21
- "no-lonely-if": "error",
22
- "no-array-constructor": "error",
23
- "no-delete-var": "error",
24
- "no-param-reassign": "error",
25
- "no-return-assign": "error",
26
- "no-import-assign": "error",
27
- "no-multi-assign": "error",
28
- "keyword-spacing": "error",
29
- "max-len": [ "warn", { "code": 140 } ],
30
- "max-params": ["error", 4],
31
- "max-statements": ["error", 15],
32
- "no-loss-of-precision": "error",
33
- "no-unreachable-loop": "error",
34
- "require-atomic-updates": "error",
35
- "complexity": ["error", 4],
36
- "max-statements-per-line": ["error", { "max": 1 }],
37
- "no-tabs": "error",
38
- "no-underscore-dangle": "error",
39
- "no-negated-condition": "error",
40
- "no-use-before-define": "error",
41
- "no-shadow": "error",
42
- "no-labels": "error",
43
- "no-throw-literal": "error",
44
- "default-case": "error",
45
- "default-case-last": "error",
46
- "no-caller": "error",
47
- "no-eval": "error",
48
- "no-implied-eval": "error",
49
- "no-new": "error",
50
- "no-new-func": "error",
51
- "no-new-object": "error",
52
- "no-new-wrappers": "error",
53
- "no-useless-concat": "error",
54
- "no-unused-vars": ["error", {"ignoreRestSiblings": true}],
55
- "array-bracket-newline": ["error", "consistent"],
56
- "func-names": ["error", "never"],
57
- "func-style": ["error", "expression", { "allowArrowFunctions": true }],
58
- "max-depth": ["error", 4],
59
- "arrow-parens": "error",
60
- "no-confusing-arrow": "error",
61
- "prefer-const": "error",
62
- "rest-spread-spacing": ["error", "never"],
63
- "template-curly-spacing": ["error", "never"],
64
- "prefer-rest-params": "error",
65
- "prefer-spread": "error",
66
- "prefer-template": "error",
67
- "object-shorthand": ["error", "properties"],
68
- "no-var": "error",
69
- "no-useless-computed-key": "error",
70
- "array-callback-return": "error",
71
- "consistent-return": "error",
72
- "dot-notation": "error",
73
- "eqeqeq": "error",
74
- "no-eq-null": "error",
75
- "no-implicit-coercion": "error",
76
- "no-multi-spaces": "error",
77
- "no-proto": "error",
78
- "yoda": "error",
79
- "indent": ["error", 2],
80
- "object-curly-spacing": [ "error", "always" ],
81
- "object-curly-newline": ["error", { "consistent": true, "multiline": true }],
82
- "space-before-blocks": "error",
83
- "space-before-function-paren": [ "error", "always" ],
84
- "spaced-comment": "error",
85
- "no-whitespace-before-property": "error",
86
- "brace-style": ["error", "1tbs", { "allowSingleLine": false }],
87
- "eol-last": ["error", "always"],
88
- "func-call-spacing": ["error", "never"],
89
- "semi": ["error", "always"]
90
- },
91
- "extends": "eslint:recommended"
92
- }
@@ -1,39 +0,0 @@
1
- name: CI
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- tests:
7
- runs-on: ubuntu-latest
8
- name: tests
9
-
10
- strategy:
11
- matrix:
12
- arch: [ amd64, arm64v8, arm32v7 ]
13
- node: [ 15, 16 ]
14
- fail-fast: false
15
-
16
- steps:
17
- - uses: actions/checkout@v2
18
- - uses: actions/setup-node@v2-beta
19
- with:
20
- node-version: '15'
21
- - uses: docker/setup-qemu-action@v1
22
- - name: Install dependencies
23
- run: npm install
24
- - name: Run tests
25
- run: node_modules/.bin/archibald -p -a ${{ matrix.arch }} -n ${{ matrix.node }} .
26
-
27
- linter:
28
- runs-on: ubuntu-latest
29
- name: linter
30
-
31
- steps:
32
- - uses: actions/checkout@v2
33
- - uses: actions/setup-node@v2-beta
34
- with:
35
- node-version: '15'
36
- - name: Install dependencies
37
- run: npm install
38
- - name: Verify code with ESLint
39
- run: npm run eslint
package/.prettierrc DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "trailingComma": "es5",
3
- "tabWidth": 2,
4
- "singleQuote": false
5
- }
package/lib/builder.js DELETED
@@ -1,62 +0,0 @@
1
- import ctypes from "./types/ctypes.js";
2
- import basicTypes from "./types/basic.js";
3
- import marshallerFactory from "./marshaller.js";
4
-
5
- const createFieldsViaBuilder = ({
6
- builder,
7
- abi: { endianness = "LE", dataModel = "LP64", compiler = "gcc" } = {},
8
- }) => {
9
- let fieldDefinitions = {};
10
-
11
- let currentOffset = 0;
12
-
13
- const fieldBuilderForAbi = ({ abi }) => {
14
- let result = {};
15
- Object.keys(abi).forEach((typeName) => {
16
- result = {
17
- ...result,
18
- [typeName]: (name) => {
19
- const def = abi[typeName]({ offset: currentOffset });
20
-
21
- fieldDefinitions = {
22
- ...fieldDefinitions,
23
- [name]: def,
24
- };
25
-
26
- currentOffset = def.offset + def.size;
27
- },
28
- };
29
- });
30
-
31
- return result;
32
- };
33
-
34
- const basicTypesAbi = basicTypes.abi({ dataModel, compiler, endianness });
35
- const cTypesAbi = ctypes.abi({ dataModel, compiler, endianness });
36
-
37
- const field = {
38
- ...fieldBuilderForAbi({ abi: basicTypesAbi }),
39
- CTypes: fieldBuilderForAbi({ abi: cTypesAbi }),
40
- };
41
-
42
- builder({ field });
43
-
44
- const size = currentOffset;
45
-
46
- const { marshal, unmarshal } = marshallerFactory.create({
47
- fieldDefinitions,
48
- size,
49
- });
50
-
51
- return {
52
- fields: fieldDefinitions,
53
- size,
54
-
55
- marshal,
56
- unmarshal,
57
- };
58
- };
59
-
60
- export default {
61
- createFieldsViaBuilder,
62
- };