@thi.ng/wasm-api-bindgen
Advanced tools
Comparing version 0.6.0 to 0.7.0
62
api.d.ts
@@ -46,3 +46,3 @@ import type { BigType, FloatType, Fn, Fn2, IObjectOf, NumOrString } from "@thi.ng/api"; | ||
*/ | ||
type: "enum" | "funcptr" | "struct" | "union"; | ||
type: "enum" | "ext" | "funcptr" | "struct" | "union"; | ||
/** | ||
@@ -68,2 +68,13 @@ * Optional object of user provided source codes to be injected into the | ||
} | ||
export interface External extends TopLevelType { | ||
type: "ext"; | ||
/** | ||
* Type alignment (in bytes) | ||
*/ | ||
align: number; | ||
/** | ||
* Type's byte size | ||
*/ | ||
size: number; | ||
} | ||
export interface Struct extends TopLevelType { | ||
@@ -357,18 +368,55 @@ type: "struct"; | ||
/** | ||
* Docstring codegen | ||
* Codegen for docstrings. | ||
* | ||
* @param doc | ||
* @param acc | ||
* @param opts | ||
* @param topLevel | ||
*/ | ||
doc: (doc: string | string[], acc: string[], opts: CodeGenOpts, topLevel?: boolean) => void; | ||
doc(doc: string | string[], acc: string[], opts: CodeGenOpts, topLevel?: boolean): void; | ||
/** | ||
* Codegen for enum types. | ||
* | ||
* @param type | ||
* @param coll | ||
* @param acc | ||
* @param opts | ||
*/ | ||
enum: (type: Enum, coll: TypeColl, acc: string[], opts: CodeGenOpts) => void; | ||
enum(type: Enum, coll: TypeColl, acc: string[], opts: CodeGenOpts): void; | ||
/** | ||
* Codegen for external types. | ||
* | ||
* @param type | ||
* @param coll | ||
* @param acc | ||
* @param opts | ||
*/ | ||
ext(type: External, coll: TypeColl, acc: string[], opts: CodeGenOpts): void; | ||
/** | ||
* Code gen for function pointers. | ||
* | ||
* @param type | ||
* @param coll | ||
* @param acc | ||
* @param opts | ||
*/ | ||
funcptr(type: FuncPointer, coll: TypeColl, acc: string[], opts: CodeGenOpts): void; | ||
/** | ||
* Codegen for struct types. | ||
* | ||
* @param type | ||
* @param coll | ||
* @param acc | ||
* @param opts | ||
*/ | ||
struct: (type: Struct, coll: TypeColl, acc: string[], opts: CodeGenOpts) => void; | ||
struct(type: Struct, coll: TypeColl, acc: string[], opts: CodeGenOpts): void; | ||
/** | ||
* Codegen for union types. | ||
* | ||
* @param type | ||
* @param coll | ||
* @param acc | ||
* @param opts | ||
*/ | ||
union: (type: Union, coll: TypeColl, acc: string[], opts: CodeGenOpts) => void; | ||
funcptr: (type: FuncPointer, coll: TypeColl, acc: string[], opts: CodeGenOpts) => void; | ||
union(type: Union, coll: TypeColl, acc: string[], opts: CodeGenOpts): void; | ||
} | ||
@@ -375,0 +423,0 @@ export interface WasmTarget { |
58
c11.js
@@ -52,6 +52,9 @@ import { topoSort } from "@thi.ng/arrays/topo-sort"; | ||
if (!prim) continue; | ||
res.push(__sliceDef(prim, typePrefix, capitalize(id))); | ||
res.push( | ||
...__sliceDef(prim, typePrefix, capitalize(id), coll) | ||
); | ||
} | ||
for (let id of __declOrder(coll)) { | ||
const type = coll[id]; | ||
const name = __prefixedName(typePrefix, type.name, coll); | ||
if (type.type == "funcptr") { | ||
@@ -61,11 +64,12 @@ res.push( | ||
); | ||
} else { | ||
res.push( | ||
` | ||
typedef ${type.type} ${typePrefix}${type.name} ${typePrefix}${type.name};` | ||
); | ||
} else if (type.type !== "ext") { | ||
res.push(` | ||
typedef ${type.type} ${name} ${name};`); | ||
} | ||
if (slices.has(id)) { | ||
const [ptr, name] = id === "opaque" ? ["void*", "Opaque"] : [typePrefix + id, capitalize(id)]; | ||
res.push(__sliceDef(ptr, typePrefix, name)); | ||
const [ptr, name2] = id === "opaque" ? ["void*", "Opaque"] : [ | ||
__prefixedName(typePrefix, id, coll), | ||
capitalize(id) | ||
]; | ||
res.push(...__sliceDef(ptr, typePrefix, name2, coll)); | ||
} | ||
@@ -85,2 +89,8 @@ } | ||
}, | ||
ext: (e, _, acc) => { | ||
acc.push( | ||
`// external type: ${e.name} (size: ${e.size}, align: ${e.align}) | ||
` | ||
); | ||
}, | ||
enum: (e, _, acc, opts2) => { | ||
@@ -242,3 +252,3 @@ if (!(e.tag === "i32" || e.tag === "u32")) { | ||
const $const = isConst ? "const " : ""; | ||
type = PRIM_ALIASES[type] || prefix + type; | ||
type = PRIM_ALIASES[type] || __prefixedName(prefix, type, coll); | ||
switch (classifier) { | ||
@@ -256,3 +266,3 @@ case "ptr": | ||
case "enumSlice": | ||
type = `${prefix}${$isConst}${capitalize(f.type)}Slice`; | ||
type = __sliceName(prefix, f.type, coll, isConst); | ||
decl = `${type} ${f.name}`; | ||
@@ -284,3 +294,3 @@ break; | ||
const __funcptr = (ptr, coll, opts, typePrefix) => { | ||
const name = typePrefix + ptr.name; | ||
const name = __prefixedName(typePrefix, ptr.name, coll); | ||
const args = ptr.args.map((a) => __fieldType(a, coll, opts, typePrefix).decl).join(", "); | ||
@@ -295,7 +305,27 @@ const rtype = ptr.rtype === "void" ? ptr.rtype : __fieldType( | ||
}; | ||
const __sliceDef = (ptr, prefix, name) => ` | ||
typedef struct { ${ptr}* ptr; size_t len; } ${prefix}${name}Slice; | ||
typedef struct { const ${ptr}* ptr; size_t len; } ${prefix}Const${name}Slice;`; | ||
const __sliceDef = (ptr, prefix, name, coll) => [ | ||
"", | ||
`typedef struct { ${ptr}* ptr; size_t len; } ${__sliceName( | ||
prefix, | ||
name, | ||
coll, | ||
false | ||
)};`, | ||
`typedef struct { const ${ptr}* ptr; size_t len; } ${__sliceName( | ||
prefix, | ||
name, | ||
coll, | ||
true | ||
)};` | ||
]; | ||
const __sliceName = (prefix, name, coll, isConst) => `${__prefixedName( | ||
prefix, | ||
capitalize(name), | ||
coll, | ||
isConst ? "Const" : "" | ||
)}Slice`; | ||
const __prefixedName = (prefix, name, coll, qualifier = "") => __prefix(prefix, name, coll) + qualifier + name; | ||
const __prefix = (prefix, name, coll) => coll[name]?.type !== "ext" ? prefix : ""; | ||
export { | ||
C11 | ||
}; |
# Change Log | ||
- **Last updated**: 2024-08-18T14:11:34Z | ||
- **Last updated**: 2024-08-21T13:14:29Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,16 @@ | ||
## [0.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api-bindgen@0.7.0) (2024-08-21) | ||
#### 🚀 Features | ||
- add support for external types ([af1a44d](https://github.com/thi-ng/umbrella/commit/af1a44d)) | ||
- update ICodeGen interface & all impls (C, TS, Zig) | ||
- update JSON schema ([e7bd5a9](https://github.com/thi-ng/umbrella/commit/e7bd5a9)) | ||
#### 🩹 Bug fixes | ||
- update type prefix handling in C11 codegen ([5b6fcc2](https://github.com/thi-ng/umbrella/commit/5b6fcc2)) | ||
- don't add prefix for external types | ||
- add naming convention helper fns | ||
## [0.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api-bindgen@0.6.0) (2024-08-18) | ||
@@ -14,0 +28,0 @@ |
@@ -20,2 +20,3 @@ import { | ||
import { | ||
isExternal, | ||
isOpaque, | ||
@@ -93,3 +94,3 @@ isPadding, | ||
if (!(spec.name && spec.type)) invalidSpec(path); | ||
if (!["enum", "funcptr", "struct", "union"].includes(spec.type)) | ||
if (!["enum", "ext", "funcptr", "struct", "union"].includes(spec.type)) | ||
invalidSpec(path, `${spec.name} type: ${spec.type}`); | ||
@@ -115,3 +116,3 @@ if (coll[spec.name]) invalidSpec(path, `duplicate name: ${spec.name}`); | ||
for (let f of spec.fields || spec.args) { | ||
if (!(isPadding(f) || isWasmPrim(f.type) || isSizeT(f.type) || isOpaque(f.type) || isWasmString(f.type) || coll[f.type])) { | ||
if (!(isPadding(f) || isWasmPrim(f.type) || isSizeT(f.type) || isOpaque(f.type) || isWasmString(f.type) || isExternal(f.type, coll) || coll[f.type])) { | ||
invalidSpec( | ||
@@ -118,0 +119,0 @@ spec.__path, |
@@ -57,2 +57,3 @@ import { SIZEOF } from "@thi.ng/api/typedarray"; | ||
}, | ||
ext: (type) => type.__size = type.size, | ||
enum: (type) => { | ||
@@ -103,2 +104,6 @@ if (type.__size) return type.__size; | ||
}, | ||
ext: (type) => { | ||
const e = type; | ||
return e.__align = e.align; | ||
}, | ||
enum: (type, _, align) => { | ||
@@ -105,0 +110,0 @@ const e = type; |
@@ -27,2 +27,3 @@ import type { BigType, Keys, Nullable } from "@thi.ng/api"; | ||
export declare const isEnum: (type: string, coll: TypeColl) => boolean; | ||
export declare const isExternal: (type: string, coll: TypeColl) => boolean; | ||
export declare const isSlice: (x: Field["tag"]) => x is "slice"; | ||
@@ -29,0 +30,0 @@ export declare const isOpaque: (x: string) => x is "opaque"; |
@@ -15,2 +15,3 @@ import { isArray } from "@thi.ng/checks/is-array"; | ||
const isEnum = (type, coll) => coll[type]?.type === "enum"; | ||
const isExternal = (type, coll) => coll[type]?.type === "ext"; | ||
const isSlice = (x) => x === "slice"; | ||
@@ -50,2 +51,3 @@ const isOpaque = (x) => x === "opaque"; | ||
isEnum, | ||
isExternal, | ||
isFuncPointer, | ||
@@ -52,0 +54,0 @@ isNumeric, |
{ | ||
"name": "@thi.ng/wasm-api-bindgen", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Polyglot bindings code generators (TS/JS, Zig, C11) for hybrid WebAssembly projects", | ||
@@ -128,3 +128,3 @@ "type": "module", | ||
}, | ||
"gitHead": "f6e26ea1142525171de5d36b9c3119f2782bb437\n" | ||
"gitHead": "b9a3f06e1792b11b305ca0d4049b043c3a1763ce\n" | ||
} |
@@ -21,2 +21,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
- [Supported data types](#supported-data-types) | ||
- [External types](#external-types) | ||
- [Struct/union field types](#structunion-field-types) | ||
@@ -35,2 +36,4 @@ - [Primitives](#primitives) | ||
- [CLI generator](#cli-generator) | ||
- [Configuration](#configuration) | ||
- [Additional code injection](#additional-code-injection) | ||
- [Example usage](#example-usage) | ||
@@ -100,2 +103,3 @@ - [Type definitions](#type-definitions) | ||
- [`EnumValue`](https://docs.thi.ng/umbrella/wasm-api-bindgen/interfaces/EnumValue.html) (individual enum value spec) | ||
- [`External`](https://docs.thi.ng/umbrella/wasm-api-bindgen/interfaces/External.html) (stub for external types) | ||
- [`Field`](https://docs.thi.ng/umbrella/wasm-api-bindgen/interfaces/Field.html) (individual spec for values contained in structs/unions) | ||
@@ -107,2 +111,26 @@ - [`FuncPointer`](https://docs.thi.ng/umbrella/wasm-api-bindgen/interfaces/FuncPointer.html) | ||
### External types | ||
The code generators support external types for which only alignment and size | ||
needs to be known & specified (both in bytes), like so: | ||
```json | ||
{ | ||
"name": "TypeName", | ||
"type": "ext", | ||
"size": 28, | ||
"align": 4, | ||
} | ||
``` | ||
Once defined, these types can be used just like any others in type specs. | ||
However, since these definitions will not have any form of code generation | ||
themselves and only exist to aid the computation of alignments & sizes of other | ||
types, it's the user's responsibility to provide necessary | ||
[preludes/imports](#additional-code-injection) themselves. | ||
Note: Any optionally configured [type prefix for the C11 code | ||
generator](https://docs.thi.ng/umbrella/wasm-api-bindgen/interfaces/C11Opts.html#typePrefix) | ||
will **not** be used for external types! | ||
### Struct/union field types | ||
@@ -515,2 +543,4 @@ | ||
## Configuration | ||
The structure of the config file is as follows (all optional): | ||
@@ -534,2 +564,4 @@ | ||
### Additional code injection | ||
All code generators have support for custom code prologues & epilogues which can | ||
@@ -917,3 +949,3 @@ be specified via the above options. These config options exist for both non-CLI | ||
Package sizes (brotli'd, pre-treeshake): ESM: 6.07 KB | ||
Package sizes (brotli'd, pre-treeshake): ESM: 6.20 KB | ||
@@ -920,0 +952,0 @@ ## Dependencies |
@@ -11,2 +11,3 @@ { | ||
{ "$ref": "#/$defs/enum" }, | ||
{ "$ref": "#/$defs/ext" }, | ||
{ "$ref": "#/$defs/funcptr" }, | ||
@@ -59,2 +60,22 @@ { "$ref": "#/$defs/struct" }, | ||
}, | ||
"ext": { | ||
"type": "object", | ||
"properties": { | ||
"doc": { "$ref": "#/$defs/doc" }, | ||
"name": { "$ref": "#/$defs/name" }, | ||
"type": { | ||
"const": "ext", | ||
"description": "This spec describes an external type." | ||
}, | ||
"align": { | ||
"type": "integer", | ||
"description": "Type alignment (in bytes)" | ||
}, | ||
"size": { | ||
"type": "integer", | ||
"description": "Type size (in bytes)" | ||
} | ||
}, | ||
"required": ["name", "type", "align", "size"] | ||
}, | ||
"funcptr": { | ||
@@ -61,0 +82,0 @@ "type": "object", |
@@ -54,2 +54,8 @@ import { | ||
}, | ||
ext: (e, _, acc) => { | ||
acc.push( | ||
`// external type: ${e.name} (size: ${e.size}, align: ${e.align}) | ||
` | ||
); | ||
}, | ||
enum: (e, _, acc, opts2) => { | ||
@@ -56,0 +62,0 @@ const res = []; |
@@ -48,2 +48,8 @@ import { isNumber } from "@thi.ng/checks"; | ||
}, | ||
ext: (e, _, acc) => { | ||
acc.push( | ||
`// external type: ${e.name} (size: ${e.size}, align: ${e.align}) | ||
` | ||
); | ||
}, | ||
enum: (e, _, acc, opts2) => { | ||
@@ -50,0 +56,0 @@ const lines = []; |
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
140671
2557
1001