Skip to content

Commit

Permalink
feat(wasm-api-dom): update generated types & string handling
Browse files Browse the repository at this point in the history
- switch to extern structs for generated types
- switch to zero-terminated pointers for string values
- update/rename/simplify API methods, remove obsolete
- fix string attrib handling/alloc sizes
  • Loading branch information
postspectacular committed Nov 7, 2022
1 parent f79f92d commit 686f867
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 210 deletions.
2 changes: 1 addition & 1 deletion packages/wasm-api-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"license": "Apache-2.0",
"scripts": {
"build": "yarn clean && tsc --declaration",
"build:types": "npx wasm-api -a analytics.json --lang ts -o src/generated/api.ts --lang zig -o zig/api.zig src/typedefs.json",
"build:types": "npx wasm-api -a analytics.json --string ptr --lang ts -o src/generated/api.ts --lang zig -o zig/api.zig src/typedefs.json",
"clean": "rimraf '*.js' '*.d.ts' '*.map' doc",
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
Expand Down
55 changes: 32 additions & 23 deletions packages/wasm-api-dom/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface DOMImports extends WebAssembly.ModuleImports {
*
* @param nameAddr
*/
_getElementByID(nameAddr: number): number;
getElementByID(nameAddr: number): number;

/**
* Takes a {@link CreateElementOpts} pointer and creates a new DOM element
Expand Down Expand Up @@ -86,26 +86,38 @@ export interface DOMImports extends WebAssembly.ModuleImports {
dpr: number
): void;

/**
* Sets `.innerHTML` property of a DOM element to provided `body` string.
*
* @param elementID
* @param body
*/
setInnerHtml(elementID: number, body: number): void;

/**
* Sets `.innerText` property of a DOM element to provided `body` string.
*
* @param elementID
* @param body
*/
setInnerText(elementID: number, body: number): void;

/**
* Sets attribute for given element to new string value. Both `nameAddr` and
* `valAddr` are pointers to zero-terminated u8 arrays (or standard Zig
* `[]u8` slices).
* string literals).
*
* @param elementID
* @param nameAddr
* @param valAddr
*/
_setStringAttrib(
elementID: number,
nameAddr: number,
valAddr: number
): void;
setStringAttrib(elementID: number, nameAddr: number, valAddr: number): void;

/**
* Reads a string attribute value from DOM element, encodes it as UTF-8 and
* writes zero-terminated bytes to char pointer `valAddr`. Only `maxBytes`
* are written. Returns actual number of bytes written (excluding the
* sentinel).
* are written (incl. any sentinel). Returns actual number of bytes written
* (excluding the sentinel).
*
* @param elementID
* @param nameAddr
Expand Down Expand Up @@ -143,27 +155,27 @@ export interface DOMImports extends WebAssembly.ModuleImports {
* @param nameAddr
* @param val
*/
_setNumericAttrib(elementID: number, nameAddr: number, val: number): void;
setNumericAttrib(elementID: number, nameAddr: number, val: number): void;

/**
* Sets (or removes) boolean attribute for given element. `nameAddr` is a
* pointer to a zero-terminated u8 array (or standard Zig `[]u8` slice). If
* `state` is non-zero, the attrib will be created/ensured, if zero it will
* be removed.
* Reads a numeric attribute value from DOM element and returns it as f64.
*
* @param elementID
* @param nameAddr
* @param state
*/
_setBooleanAttrib(elementID: number, nameAddr: number, state: number): void;
getNumericAttrib(elementID: number, nameAddr: number): number;

/**
* Reads a numeric attribute value from DOM element and returns it as f64.
* Sets (or removes) boolean attribute for given element. `nameAddr` is a
* pointer to a zero-terminated u8 array (or standard Zig `[]u8` slice). If
* `state` is non-zero, the attrib will be created/ensured, if zero it will
* be removed.
*
* @param elementID
* @param nameAddr
* @param state
*/
_getNumericAttrib(elementID: number, nameAddr: number): number;
_setBooleanAttrib(elementID: number, nameAddr: number, state: number): void;

/**
* Check if the DOM element has given attribute and returns 1 if so, else 0.
Expand All @@ -173,9 +185,9 @@ export interface DOMImports extends WebAssembly.ModuleImports {
*/
_getBooleanAttrib(elementID: number, nameAddr: number): number;

_addClass(elementID: number, nameAddr: number): void;
addClass(elementID: number, nameAddr: number): void;

_removeClass(elementID: number, nameAddr: number): void;
removeClass(elementID: number, nameAddr: number): void;

/**
* Attaches a new DOM event listener for given event name to element (or
Expand All @@ -200,9 +212,6 @@ export interface DOMImports extends WebAssembly.ModuleImports {
*/
_removeListener(listenerID: number): void;

_setInnerHtml(elementID: number, body: number): void;
_setInnerText(elementID: number, body: number): void;

_requestAnimationFrame(rafID: number): void;

_requestFullscreen(elementID: number): Promise<void>;
Expand Down
18 changes: 9 additions & 9 deletions packages/wasm-api-dom/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class WasmDom implements IWasmAPI<DOMExports> {
: 0);
},

_getElementByID: (nameAddr: number) => {
getElementByID: (nameAddr: number) => {
const name = this.parent.getString(nameAddr);
let id = this.elements.find((el) => el.id === name);
if (id === undefined) {
Expand Down Expand Up @@ -203,10 +203,10 @@ export class WasmDom implements IWasmAPI<DOMExports> {
dpr
),

_setStringAttrib: (elementID: number, name: number, val: number) =>
setStringAttrib: (elementID: number, name: number, val: number) =>
this.setAttrib(elementID, name, this.parent.getString(val)),

_setNumericAttrib: (elementID: number, name: number, val: number) =>
setNumericAttrib: (elementID: number, name: number, val: number) =>
this.setAttrib(elementID, name, val),

_setBooleanAttrib: (
Expand Down Expand Up @@ -244,21 +244,21 @@ export class WasmDom implements IWasmAPI<DOMExports> {
) =>
new WasmStringSlice(this.parent, slice).setAlloc(
String(this.getAttrib(elementID, nameAddr) || ""),
false
true
),

_getNumericAttrib: (elementID: number, nameAddr: number) =>
getNumericAttrib: (elementID: number, nameAddr: number) =>
Number(this.getAttrib(elementID, nameAddr) || ""),

_getBooleanAttrib: (elementID: number, nameAddr: number) =>
~~(this.getAttrib(elementID, nameAddr) != null),

_addClass: (elementID: number, name: number) =>
addClass: (elementID: number, name: number) =>
this.elements
.get(elementID)
.classList.add(this.parent.getString(name)),

_removeClass: (elementID: number, name: number) =>
removeClass: (elementID: number, name: number) =>
this.elements
.get(elementID)
.classList.remove(this.parent.getString(name)),
Expand Down Expand Up @@ -352,12 +352,12 @@ export class WasmDom implements IWasmAPI<DOMExports> {
}
},

_setInnerHtml: (elementID: number, body: number) => {
setInnerHtml: (elementID: number, body: number) => {
this.elements.get(elementID).innerHTML =
this.parent.getString(body);
},

_setInnerText: (elementID: number, body: number) => {
setInnerText: (elementID: number, body: number) => {
(<HTMLElement>this.elements.get(elementID)).innerText =
this.parent.getString(body);
},
Expand Down
Loading

0 comments on commit 686f867

Please sign in to comment.