_wasmInitA
asynchronous initialization of WASM-compiled C++ code
Requires
import {_wasmInitA} from "./tt/ttWASM.js";
Definition
_wasmInitA = async (ttWasmObject, config, options)
Description
You will require to call _wasmInit or _wasmInitA once in your tiktag javascript to initialize the compiled C++ code.
This step is required to utilize and call C++ functions from Javascript and vice versa.
Whilst _wasmInit requires a callback-function, _wasmInitA can be called asynchronous, therfore you can use await in your code.
Parameters
ttWasmObject: must be a valid tiktag WASM Object typically imported using
import wasm from "./web.wasm";In this case the ttWasmObject would be wasm. Please remark that filename "./web.wasm" is the standard wasm-build from tiktag, so the name is fixed for now.
config: must be valid JSON-Object. If you do not wish to provide any config-reference you can use {} to declare this parameter is empty. This parameter is used to a set of Javascript-Functions in your current scope to export to your C++ instance. This enables you to call any Javascript-functions from C++.
{jsQueryIDB: this.queryIDB, jsGetItemLocalStorage: this.getItemLocalStorage}
In this example you could use the function-Names jsQueryIDB and jsGetItemLocalStorage directly in your C++ code. It then would trigger the according this.queryIDB / this.getItemLocalStorage functions from your js-implementation.
options: should contain null. It is not used yet, but might be in future implementations
Return Value
new Promise()... resolve(webAssemblyInstance);
Remarks
-
Example: await initialization of web-assembled C++ instance
main.js:
import {_wasmInitA} from "./tt/ttWASM.js";
import {_newTag, _render} from "./tt/ttHTML.js";
import wasm from "./web.wasm";
var wasmInstance = null;
function jsAlert()
{
alert ("This Alert was called from C++");
}
async function main()
{
wasmInstance = await _wasmInitA (wasm, {jsAlert: jsAlert}, null);
console.dir (wasmInstance);
_render (<button onclick={wasmInstance.exports.onClick}>Call C++ Function</button>);
}
main();
cppExample.cpp:
#include "tiktag.h"
_import void jsAlert();
_export void onClick()
{
jsAlert();
}
Output: