@thi.ng/wasm-api 0.16.0 → 0.17.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-10-28T19:08:39Z
3
+ - **Last updated**: 2022-10-30T12:26:06Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,28 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [0.17.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.17.1) (2022-10-30)
13
+
14
+ #### ♻️ Refactoring
15
+
16
+ - update zig codegen to ignore uppercase enum config ([3258f91](https://github.com/thi-ng/umbrella/commit/3258f91))
17
+ - idiomatic zig enums are lowercase
18
+ - update config docs
19
+
20
+ ## [0.17.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.17.0) (2022-10-29)
21
+
22
+ #### 🚀 Features
23
+
24
+ - update default value format & handling ([c7752fb](https://github.com/thi-ng/umbrella/commit/c7752fb))
25
+ - update Field.default to support lang-specific alts
26
+ - add defaultValue() helper
27
+ - update zig codegen
28
+
29
+ #### 🩹 Bug fixes
30
+
31
+ - fix idempotence checks for self-referential types ([2c3f670](https://github.com/thi-ng/umbrella/commit/2c3f670))
32
+ - update prepareType()
33
+
12
34
  ## [0.16.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.16.0) (2022-10-28)
13
35
 
14
36
  #### 🚀 Features
package/api.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { BigType, FloatType, Fn, Fn2, IDeref, ILength, IObjectOf } from "@thi.ng/api";
1
+ import type { BigType, FloatType, Fn, Fn2, IDeref, ILength, IObjectOf, NumOrString } from "@thi.ng/api";
2
2
  import type { Pow2 } from "@thi.ng/binary";
3
3
  import type { WasmBridge } from "./bridge.js";
4
4
  export declare const PKG_NAME = "@thi.ng/wasm-api";
@@ -224,7 +224,7 @@ export declare type WasmPrim32 = Exclude<WasmPrim, BigType>;
224
224
  export declare type ReadonlyWasmString = IDeref<string> & ILength & {
225
225
  readonly addr: number;
226
226
  };
227
- export declare type TypeColl = Record<string, TopLevelType>;
227
+ export declare type TypeColl = IObjectOf<TopLevelType>;
228
228
  export interface TypeInfo {
229
229
  /**
230
230
  * Auto-computed size (in bytes)
@@ -370,10 +370,14 @@ export interface Field extends TypeInfo {
370
370
  */
371
371
  len?: number;
372
372
  /**
373
- * Currently only supported for {@link ZIG} scalar & string values,
374
- * otherwise ignored!
373
+ * Currently only supported for {@link ZIG}, otherwise ignored!
374
+ *
375
+ * @remarks
376
+ * The object form allows for different default values per language (in
377
+ * theory). So if given as object, the keys refer to the lang ID and the
378
+ * values as the defaults for those languages.
375
379
  */
376
- default?: number;
380
+ default?: NumOrString | IObjectOf<NumOrString>;
377
381
  /**
378
382
  * If defined and > 0, the field will be considered for padding purposes
379
383
  * only and the value provided is the number of bytes used. All other config
@@ -59,6 +59,14 @@ export declare const stringFields: (fields: Field[]) => Field[];
59
59
  * @internal
60
60
  */
61
61
  export declare const enumName: (opts: CodeGenOpts, name: string) => string;
62
+ /**
63
+ * Returns given field's default value (or undefined). The `lang` ID is required
64
+ * to obtain the language specific value if the default is given as object.
65
+ *
66
+ * @param f
67
+ * @param lang
68
+ */
69
+ export declare const defaultValue: (f: Field, lang: string) => import("@thi.ng/api").NumOrString | undefined;
62
70
  /**
63
71
  * Takes an array of strings or splits given string into lines, word wraps and
64
72
  * then prefixes each line with given `width` and `prefix`. Returns array of new
package/codegen/utils.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { isArray } from "@thi.ng/checks/is-array";
2
+ import { isPlainObject } from "@thi.ng/checks/is-plain-object";
2
3
  import { isString } from "@thi.ng/checks/is-string";
3
4
  import { split } from "@thi.ng/strings/split";
4
5
  import { wordWrapLine, wordWrapLines } from "@thi.ng/strings/word-wrap";
@@ -61,6 +62,18 @@ export const stringFields = (fields) => fields.filter((f) => isWasmString(f.type
61
62
  * @internal
62
63
  */
63
64
  export const enumName = (opts, name) => opts.uppercaseEnums ? name.toUpperCase() : name;
65
+ /**
66
+ * Returns given field's default value (or undefined). The `lang` ID is required
67
+ * to obtain the language specific value if the default is given as object.
68
+ *
69
+ * @param f
70
+ * @param lang
71
+ */
72
+ export const defaultValue = (f, lang) => f.default !== undefined
73
+ ? isPlainObject(f.default)
74
+ ? f.default[lang]
75
+ : f.default
76
+ : undefined;
64
77
  /**
65
78
  * Takes an array of strings or splits given string into lines, word wraps and
66
79
  * then prefixes each line with given `width` and `prefix`. Returns array of new
package/codegen/zig.js CHANGED
@@ -1,7 +1,7 @@
1
- import { isNumber } from "@thi.ng/checks/is-number";
1
+ import { isNumber } from "@thi.ng/checks";
2
2
  import { isString } from "@thi.ng/checks/is-string";
3
3
  import { unsupported } from "@thi.ng/errors/unsupported";
4
- import { ensureLines, enumName, isPadding, isStringSlice, isWasmString, prefixLines, withIndentation, } from "./utils.js";
4
+ import { defaultValue, ensureLines, isPadding, isStringSlice, isWasmString, prefixLines, withIndentation, } from "./utils.js";
5
5
  /**
6
6
  * Zig code generator. Call with options and then pass to {@link generateTypes}
7
7
  * (see its docs for further usage).
@@ -28,12 +28,12 @@ export const ZIG = (opts = {}) => {
28
28
  let line;
29
29
  if (!isString(v)) {
30
30
  v.doc && gen.doc(v.doc, lines, opts);
31
- line = enumName(opts, v.name);
31
+ line = v.name;
32
32
  if (v.value != null)
33
33
  line += ` = ${v.value}`;
34
34
  }
35
35
  else {
36
- line = enumName(opts, v);
36
+ line = v;
37
37
  }
38
38
  lines.push(line + ",");
39
39
  }
@@ -109,7 +109,7 @@ export const fieldType = (parent, f, opts) => {
109
109
  ? "[*:0]const u8"
110
110
  : "[*:0]u8"
111
111
  : f.type;
112
- let defaultVal = "";
112
+ let defaultVal = defaultValue(f, "zig");
113
113
  switch (f.tag) {
114
114
  case "array":
115
115
  type =
@@ -128,14 +128,16 @@ export const fieldType = (parent, f, opts) => {
128
128
  break;
129
129
  case "scalar":
130
130
  default:
131
- if (f.default != undefined) {
132
- if (!(isString(f.default) || isNumber(f.default))) {
133
- unsupported(`wrong default value for ${parent.name}.${f.name} (${f.default})`);
131
+ if (defaultVal != undefined) {
132
+ if (!(isString(defaultVal) || isNumber(defaultVal))) {
133
+ unsupported(`wrong default value for ${parent.name}.${f.name} (${defaultVal})`);
134
134
  }
135
- defaultVal = ` = ${JSON.stringify(f.default)}`;
136
135
  }
137
136
  }
138
- return { type, defaultVal };
137
+ return {
138
+ type,
139
+ defaultVal: defaultVal != undefined ? ` = ${defaultVal}` : "",
140
+ };
139
141
  };
140
142
  const __packedPadding = (id, n, res) => {
141
143
  let i = 0;
package/codegen.js CHANGED
@@ -113,13 +113,13 @@ const alignOf = defmulti((x) => x.type, {}, {
113
113
  });
114
114
  const prepareType = defmulti((x) => x.type, {}, {
115
115
  [DEFAULT]: (x, types, alignImpl, opts) => {
116
- if (x.__align && x.__size)
116
+ if (x.__align)
117
117
  return;
118
118
  alignOf(x, types, alignImpl, opts);
119
119
  sizeOf(x, types, alignImpl, opts);
120
120
  },
121
121
  struct: (x, types, align, opts) => {
122
- if (x.__align && x.__size)
122
+ if (x.__align)
123
123
  return;
124
124
  const struct = x;
125
125
  alignOf(struct, types, align, opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/wasm-api",
3
- "version": "0.16.0",
3
+ "version": "0.17.1",
4
4
  "description": "Generic, modular, extensible API bridge, polyglot glue code and bindings code generators for hybrid JS & WebAssembly projects",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -141,5 +141,5 @@
141
141
  "status": "alpha",
142
142
  "year": 2022
143
143
  },
144
- "gitHead": "41e59c7ad9bf24bb0230a5f60d05715e0fc1c1e6\n"
144
+ "gitHead": "78682fcd936495fefa1345db5a8dfd709eace2c8\n"
145
145
  }