@thi.ng/wasm-api-bindgen
Advanced tools
Comparing version 0.5.4 to 0.5.5
62
c11.js
@@ -106,34 +106,22 @@ import { topoSort } from "@thi.ng/arrays/topo-sort"; | ||
}, | ||
struct: (struct, coll, acc, opts2) => { | ||
const name = typePrefix + struct.name; | ||
acc.push( | ||
...withIndentation( | ||
[ | ||
`struct ${name} {`, | ||
...__generateFields( | ||
gen, | ||
struct, | ||
coll, | ||
opts2, | ||
typePrefix | ||
) | ||
], | ||
INDENT, | ||
...SCOPES | ||
) | ||
); | ||
}, | ||
union: (union, coll, acc, opts2) => { | ||
const name = typePrefix + union.name; | ||
acc.push( | ||
...withIndentation( | ||
[ | ||
`union ${name} {`, | ||
...__generateFields(gen, union, coll, opts2, typePrefix) | ||
], | ||
INDENT, | ||
...SCOPES | ||
) | ||
); | ||
}, | ||
struct: (struct, coll, acc, opts2) => __structOrUnion( | ||
gen, | ||
struct, | ||
coll, | ||
acc, | ||
opts2, | ||
typePrefix, | ||
INDENT, | ||
SCOPES | ||
), | ||
union: (union, coll, acc, opts2) => __structOrUnion( | ||
gen, | ||
union, | ||
coll, | ||
acc, | ||
opts2, | ||
typePrefix, | ||
INDENT, | ||
SCOPES | ||
), | ||
// funcpointers are emitted in `pre` phase above | ||
@@ -145,2 +133,12 @@ funcptr: () => { | ||
}; | ||
const __structOrUnion = (gen, spec, coll, acc, opts, prefix, indent, scopes) => acc.push( | ||
...withIndentation( | ||
[ | ||
`${spec.type} ${prefix + spec.name} {`, | ||
...__generateFields(gen, spec, coll, opts, prefix) | ||
], | ||
indent, | ||
...scopes | ||
) | ||
); | ||
const __generateFields = (gen, parent, coll, opts, typePrefix) => { | ||
@@ -147,0 +145,0 @@ const res = []; |
# Change Log | ||
- **Last updated**: 2024-05-08T18:24:32Z | ||
- **Last updated**: 2024-06-21T19:34:38Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,11 @@ | ||
### [0.5.5](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api-bindgen@0.5.5) (2024-06-21) | ||
#### ♻️ Refactoring | ||
- dedupe struct/union gens for C/Zig ([be628f7](https://github.com/thi-ng/umbrella/commit/be628f7)) | ||
- update/dedupe user body injection ([40cbf13](https://github.com/thi-ng/umbrella/commit/40cbf13)) | ||
- update TS & Zig code gens | ||
- enforce uniform naming convention of internal functions ([56992b2](https://github.com/thi-ng/umbrella/commit/56992b2)) | ||
## [0.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api-bindgen@0.5.0) (2024-04-22) | ||
@@ -14,0 +23,0 @@ |
@@ -31,3 +31,3 @@ import { SIZEOF } from "@thi.ng/api/typedarray"; | ||
}; | ||
const sizeOf = defmulti( | ||
const __sizeOf = defmulti( | ||
(x) => x.type, | ||
@@ -48,3 +48,3 @@ {}, | ||
} else { | ||
size = isNumeric(field.type) || isBigNumeric(field.type) ? SIZEOF[field.type] : isWasmString(field.type) ? opts.target.sizeBytes * (isStringSlice(opts.stringType) ? 2 : 1) : isOpaque(field.type) ? opts.target.sizeBytes : sizeOf(coll[field.type], coll, align, opts); | ||
size = isNumeric(field.type) || isBigNumeric(field.type) ? SIZEOF[field.type] : isWasmString(field.type) ? opts.target.sizeBytes * (isStringSlice(opts.stringType) ? 2 : 1) : isOpaque(field.type) ? opts.target.sizeBytes : __sizeOf(coll[field.type], coll, align, opts); | ||
if (field.tag == "array" || field.tag === "vec") { | ||
@@ -69,3 +69,3 @@ size *= field.len; | ||
f.__offset = offset; | ||
offset += sizeOf(f, coll, align, opts); | ||
offset += __sizeOf(f, coll, align, opts); | ||
} | ||
@@ -79,3 +79,3 @@ return type.__size = align.size(offset, type.__align); | ||
f.__offset = 0; | ||
maxSize = Math.max(maxSize, sizeOf(f, coll, align, opts)); | ||
maxSize = Math.max(maxSize, __sizeOf(f, coll, align, opts)); | ||
} | ||
@@ -89,3 +89,3 @@ return type.__size = align.size(maxSize, type.__align); | ||
); | ||
const alignOf = defmulti( | ||
const __alignOf = defmulti( | ||
(x) => x.type, | ||
@@ -100,3 +100,3 @@ {}, | ||
} | ||
return field.__align = isPointerLike(field, coll) ? align.align({ type: opts.target.usize }) : isNumeric(field.type) || isBigNumeric(field.type) ? align.align(field) : alignOf( | ||
return field.__align = isPointerLike(field, coll) ? align.align({ type: opts.target.usize }) : isNumeric(field.type) || isBigNumeric(field.type) ? align.align(field) : __alignOf( | ||
coll[field.type], | ||
@@ -118,3 +118,3 @@ coll, | ||
for (let f of type.fields) { | ||
maxAlign = Math.max(maxAlign, alignOf(f, coll, align, opts)); | ||
maxAlign = Math.max(maxAlign, __alignOf(f, coll, align, opts)); | ||
} | ||
@@ -126,3 +126,3 @@ return type.__align = maxAlign; | ||
for (let f of type.fields) { | ||
maxAlign = Math.max(maxAlign, alignOf(f, coll, align, opts)); | ||
maxAlign = Math.max(maxAlign, __alignOf(f, coll, align, opts)); | ||
} | ||
@@ -135,6 +135,6 @@ return type.__align = maxAlign; | ||
if (ptr.rtype !== "void") { | ||
sizeOf(ptr.rtype, coll, align, opts); | ||
__sizeOf(ptr.rtype, coll, align, opts); | ||
} | ||
for (let a of ptr.args) { | ||
alignOf(a, coll, align, opts); | ||
__alignOf(a, coll, align, opts); | ||
} | ||
@@ -147,3 +147,3 @@ return type.__align = align.align({ | ||
); | ||
const prepareType = defmulti( | ||
const __prepareType = defmulti( | ||
(x) => x.type, | ||
@@ -154,4 +154,4 @@ {}, | ||
if (x.__align) return; | ||
alignOf(x, coll, alignImpl, opts); | ||
sizeOf(x, coll, alignImpl, opts); | ||
__alignOf(x, coll, alignImpl, opts); | ||
__sizeOf(x, coll, alignImpl, opts); | ||
}, | ||
@@ -161,3 +161,3 @@ struct: (x, coll, align, opts) => { | ||
const struct = x; | ||
alignOf(struct, coll, align, opts); | ||
__alignOf(struct, coll, align, opts); | ||
if (struct.auto) { | ||
@@ -171,6 +171,6 @@ struct.fields.sort( | ||
if (type) { | ||
prepareType(type, coll, selectAlignment(type), opts); | ||
__prepareType(type, coll, selectAlignment(type), opts); | ||
} | ||
} | ||
sizeOf(struct, coll, align, opts); | ||
__sizeOf(struct, coll, align, opts); | ||
} | ||
@@ -181,3 +181,3 @@ } | ||
for (let id in coll) { | ||
prepareType(coll[id], coll, selectAlignment(coll[id]), opts); | ||
__prepareType(coll[id], coll, selectAlignment(coll[id]), opts); | ||
} | ||
@@ -184,0 +184,0 @@ return coll; |
@@ -1,2 +0,2 @@ | ||
import type { BigType } from "@thi.ng/api"; | ||
import type { BigType, Keys, Nullable } from "@thi.ng/api"; | ||
import type { CodeGenOpts, Field, InjectedBody, Struct, TopLevelType, TypeColl, Union, WasmPrim, WasmPrim32 } from "../api.js"; | ||
@@ -105,2 +105,3 @@ /** | ||
export declare function withIndentation(lines: string[], indent: string, scopeStart: RegExp, scopeEnd: RegExp, level?: number): Generator<string, void, unknown>; | ||
export declare const injectBody: (acc: string[], body: Nullable<string | string[] | InjectedBody>, key?: Keys<InjectedBody>) => number | "" | null | undefined; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -40,2 +40,3 @@ import { isArray } from "@thi.ng/checks/is-array"; | ||
} | ||
const injectBody = (acc, body, key = "impl") => body && acc.push("", ...ensureLines(body, key), ""); | ||
export { | ||
@@ -46,2 +47,3 @@ defaultValue, | ||
enumName, | ||
injectBody, | ||
isBigNumeric, | ||
@@ -48,0 +50,0 @@ isEnum, |
{ | ||
"name": "@thi.ng/wasm-api-bindgen", | ||
"version": "0.5.4", | ||
"version": "0.5.5", | ||
"description": "Polyglot bindings code generators for hybrid JS & WebAssembly projects", | ||
@@ -14,3 +14,3 @@ "type": "module", | ||
}, | ||
"homepage": "https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-bindgen#readme", | ||
"homepage": "https://thi.ng/wasm-api-bindgen", | ||
"funding": [ | ||
@@ -42,21 +42,21 @@ { | ||
"dependencies": { | ||
"@thi.ng/api": "^8.11.2", | ||
"@thi.ng/args": "^2.3.33", | ||
"@thi.ng/arrays": "^2.9.6", | ||
"@thi.ng/binary": "^3.4.25", | ||
"@thi.ng/checks": "^3.6.4", | ||
"@thi.ng/compare": "^2.3.5", | ||
"@thi.ng/defmulti": "^3.0.39", | ||
"@thi.ng/errors": "^2.5.7", | ||
"@thi.ng/file-io": "^2.1.2", | ||
"@thi.ng/logger": "^3.0.12", | ||
"@thi.ng/paths": "^5.1.81", | ||
"@thi.ng/strings": "^3.7.33", | ||
"@thi.ng/wasm-api": "^1.5.4" | ||
"@thi.ng/api": "^8.11.3", | ||
"@thi.ng/args": "^2.3.34", | ||
"@thi.ng/arrays": "^2.9.7", | ||
"@thi.ng/binary": "^3.4.26", | ||
"@thi.ng/checks": "^3.6.5", | ||
"@thi.ng/compare": "^2.3.6", | ||
"@thi.ng/defmulti": "^3.0.40", | ||
"@thi.ng/errors": "^2.5.8", | ||
"@thi.ng/file-io": "^2.1.3", | ||
"@thi.ng/logger": "^3.0.13", | ||
"@thi.ng/paths": "^5.1.82", | ||
"@thi.ng/strings": "^3.7.34", | ||
"@thi.ng/wasm-api": "^1.6.0" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.43.2", | ||
"esbuild": "^0.21.1", | ||
"@microsoft/api-extractor": "^7.47.0", | ||
"esbuild": "^0.21.5", | ||
"typedoc": "^0.25.13", | ||
"typescript": "^5.4.5" | ||
"typescript": "^5.5.2" | ||
}, | ||
@@ -125,3 +125,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "df34b4a9e650cc7323575356de207d78933bdcf3\n" | ||
"gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n" | ||
} |
@@ -10,3 +10,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
> [!NOTE] | ||
> This is one of 192 standalone projects, maintained as part | ||
> This is one of 193 standalone projects, maintained as part | ||
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo | ||
@@ -749,3 +749,3 @@ > and anti-framework. | ||
Package sizes (brotli'd, pre-treeshake): ESM: 5.97 KB | ||
Package sizes (brotli'd, pre-treeshake): ESM: 6.01 KB | ||
@@ -752,0 +752,0 @@ ## Dependencies |
@@ -13,2 +13,3 @@ import { | ||
enumName, | ||
injectBody, | ||
isEnum, | ||
@@ -64,3 +65,3 @@ isFuncPointer, | ||
const strType = __stringImpl(opts2); | ||
const fields = struct.fields.map((f) => generateField(f, coll, opts2)).filter((f) => !!f); | ||
const fields = struct.fields.map((f) => __generateField(f, coll, opts2)).filter((f) => !!f); | ||
const lines = []; | ||
@@ -75,5 +76,3 @@ lines.push( | ||
} | ||
if (struct.body?.ts) { | ||
lines.push("", ...ensureLines(struct.body.ts, "decl")); | ||
} | ||
injectBody(lines, struct.body?.ts, "decl"); | ||
lines.push("}", ""); | ||
@@ -125,5 +124,3 @@ const pointerDecls = fields.filter((f) => isPointer(f.field.tag) && f.field.len !== 0).map((f) => { | ||
} | ||
if (struct.body?.ts) { | ||
lines.push("", ...ensureLines(struct.body.ts, "impl"), ""); | ||
} | ||
injectBody(lines, struct.body?.ts); | ||
lines.push("};", "}", "});", ""); | ||
@@ -191,3 +188,3 @@ acc.push(...withIndentation(lines, indent, ...SCOPES)); | ||
}; | ||
const generateField = (field, coll, opts) => { | ||
const __generateField = (field, coll, opts) => { | ||
if (field.skip) return; | ||
@@ -194,0 +191,0 @@ if (isFuncPointer(field.type, coll) || isOpaque(field.type)) { |
46
zig.js
@@ -8,4 +8,4 @@ import { isNumber } from "@thi.ng/checks"; | ||
defaultValue, | ||
ensureLines, | ||
ensureStringArray, | ||
injectBody, | ||
isOpaque, | ||
@@ -63,32 +63,8 @@ isPadding, | ||
} | ||
if (e.body?.zig) { | ||
lines.push("", ...ensureLines(e.body.zig, "impl"), ""); | ||
} | ||
injectBody(lines, e.body?.zig); | ||
lines.push("};", ""); | ||
acc.push(...withIndentation(lines, INDENT, ...SCOPES)); | ||
}, | ||
struct: (struct, coll, acc, opts2) => { | ||
acc.push( | ||
...withIndentation( | ||
[ | ||
`pub const ${struct.name} = extern struct {`, | ||
...__generateFields(gen, struct, coll, opts2) | ||
], | ||
INDENT, | ||
...SCOPES | ||
) | ||
); | ||
}, | ||
union: (union, coll, acc, opts2) => { | ||
acc.push( | ||
...withIndentation( | ||
[ | ||
`pub const ${union.name} = extern union {`, | ||
...__generateFields(gen, union, coll, opts2) | ||
], | ||
INDENT, | ||
...SCOPES | ||
) | ||
); | ||
}, | ||
struct: (struct, coll, acc, opts2) => __structOrUnion(gen, struct, coll, acc, opts2, INDENT, SCOPES), | ||
union: (union, coll, acc, opts2) => __structOrUnion(gen, union, coll, acc, opts2, INDENT, SCOPES), | ||
funcptr: (ptr, coll, acc, opts2) => { | ||
@@ -110,2 +86,12 @@ const args = ptr.args.map((a) => `${a.name}: ${fieldType(a, ptr, coll, opts2).type}`).join(", "); | ||
}; | ||
const __structOrUnion = (gen, spec, coll, acc, opts, indent, scopes) => acc.push( | ||
...withIndentation( | ||
[ | ||
`pub const ${spec.name} = extern ${spec.type} {`, | ||
...__generateFields(gen, spec, coll, opts) | ||
], | ||
indent, | ||
...scopes | ||
) | ||
); | ||
const __generateFields = (gen, parent, coll, opts) => { | ||
@@ -127,5 +113,3 @@ const res = []; | ||
} | ||
if (parent.body?.zig) { | ||
res.push("", ...ensureLines(parent.body.zig, "impl"), ""); | ||
} | ||
injectBody(res, parent.body?.zig); | ||
res.push("};"); | ||
@@ -132,0 +116,0 @@ if (opts.debug) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127498
2401
Updated@thi.ng/api@^8.11.3
Updated@thi.ng/args@^2.3.34
Updated@thi.ng/arrays@^2.9.7
Updated@thi.ng/binary@^3.4.26
Updated@thi.ng/checks@^3.6.5
Updated@thi.ng/compare@^2.3.6
Updated@thi.ng/defmulti@^3.0.40
Updated@thi.ng/errors@^2.5.8
Updated@thi.ng/file-io@^2.1.3
Updated@thi.ng/logger@^3.0.13
Updated@thi.ng/paths@^5.1.82
Updated@thi.ng/strings@^3.7.34
Updated@thi.ng/wasm-api@^1.6.0