This is a tutorial for initializing an instance of the WebAssembly module and calling the functions within it using zkc-sdk.
- zkc-sdk, which can be installed by executing:
npm install
With the following command, you can compile and preview the project
npm run start
- Import zkc-sdk and
hello-world.wasm
inindex.js
import { ZKCWasmService } from 'zkc-sdk';
// Get the URL of the wasm file for initializing the WebAssembly instance.
const helloWorldURL = new URL('./wasmsrc/c/hello-world.wasm', import.meta.url);
- Load wasm module instance and call the add function export from wasm
const runWasmAdd = async () => {
// load wasm module instance
const { exports } = await ZKCWasmService.loadWasm(helloWorldURL);
// Call the Add function export from wasm, save the result
const addResult = exports.add(26, 26);
// Set the result onto the body
document.body.textContent = `Hello World! addResult: ${addResult}`;
};
runWasmAdd();
- Load
index.js
file inindex.html
:
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World</title>
<link rel="preload" href="./index.js" as="script" />
</head>
<body>
<script type="module" src="./index.js"></script>
</body>
</html>