_wasmInit

initializes WASM-compiled C++ code in your project

Requires

import {_wasmInit} from "./tt/ttWASM.js";

Definition

_wasmInit (ttWasmObject, config, onReadyCallback, 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.

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.

onReadyCallback: could either be an arrow-function or function reference to be called in case of the successfull initialization of your C++ - code. The function could have any name just as myCPPReady and be implemented as following:

var cppObj = null;
function myCPPReady(wasmObj)
{
	cppObj = wasmObj;
}

options: should contain null. It is not used yet, but might be in future implementations

Return Value

-

Remarks

-

Example 1: initialize Web-Assembled code and call C++ Function

main.js:

import {_wasmInit} from "./tt/ttWASM.js";
import {_newTag, _render} from "./tt/ttHTML.js";
import wasm from "./web.wasm";

function onWasmReady(wasmInstance)
{
	var cppResult;
	cppResult = wasmInstance.exports.addNumbers(19,23);
	_render(<h1>The Result is: {cppResult}</h1>);
}

_wasmInit(wasm, {}, onWasmReady, null);

cppExample.cpp:

#include "tiktag.h"

_export int addNumbers (int n1, int n2)
{
	return (n1+n2);
}

Output:

Example 2: initialize Web-Assembled code, call custom Javascript Function from C++

main.js:

import {_wasmInit} from "./tt/ttWASM.js";
import {_newTag, _render} from "./tt/ttHTML.js";
import wasm from "./web.wasm";

function jsRenderResult(num)
{
	_render(<h1>called from C++: the Result is: {num}</h1>);	
}

function onWasmReady(wasmInstance)
{
	wasmInstance.exports.addNumbersAndRenderResult(200,103);
}

_wasmInit(wasm, {jsRenderResult: jsRenderResult}, onWasmReady, null);

cppExample.cpp:

#include "tiktag.h"

_import void jsRenderResult(int num);

_export void addNumbersAndRenderResult (int n1, int n2)
{
	jsRenderResult(n1+n2);
}

Output:

See Also

_render_wasmInitA