|
| 1 | +<!doctype html> |
| 2 | +<html lang="en-us"> |
| 3 | + <body> |
| 4 | + <h1>Conan C++ WebAssembly Example</h1> |
| 5 | + <br /> |
| 6 | + <div id="status">Downloading...</div> |
| 7 | + <div> |
| 8 | + <progress value="0" max="100" id="progress" hidden="1"></progress> |
| 9 | + </div> |
| 10 | + <textarea id="output" rows="8" style="width: 100%"></textarea> |
| 11 | + <hr /> |
| 12 | + |
| 13 | + <!-- Example of calling JS from C --> |
| 14 | + <button onclick="printMessage()">Print Message</button> |
| 15 | + <hr /> |
| 16 | + |
| 17 | + <!-- Example of a simple invocation to fibonachi --> |
| 18 | + <input type="number" id="fibInput" placeholder="e.g., 10" /> |
| 19 | + <button onclick="fibExample()">Compute Fibonacci</button> |
| 20 | + <p id="fibResult"></p> |
| 21 | + <hr /> |
| 22 | + |
| 23 | + <!-- Example of a function call using two buffers --> |
| 24 | + <button onclick="pressBtn()">Click me to increase counter!</button> |
| 25 | + <p id="counterResult"></p> |
| 26 | + <hr /> |
| 27 | + |
| 28 | + <!-- Example of a function call using a Float32Array and Eigen --> |
| 29 | + <input |
| 30 | + type="text" |
| 31 | + id="numbersInput" |
| 32 | + placeholder="e.g., 42.2, 2.1, 8" |
| 33 | + size="50" |
| 34 | + /> |
| 35 | + <button onclick="sumExample()">Compute Float32 Sum with Eigen</button> |
| 36 | + <p id="sumResult"></p> |
| 37 | + |
| 38 | + <script type="text/javascript"> |
| 39 | + var statusElement = document.getElementById("status"); |
| 40 | + var progressElement = document.getElementById("progress"); |
| 41 | + |
| 42 | + var Module = { |
| 43 | + print: (function () { |
| 44 | + var element = document.getElementById("output"); |
| 45 | + if (element) element.value = ""; // clear browser cache |
| 46 | + return (...args) => { |
| 47 | + var text = args.join(" "); |
| 48 | + console.log(text); |
| 49 | + if (element) { |
| 50 | + element.value += text + "\n"; |
| 51 | + element.scrollTop = element.scrollHeight; // focus on bottom |
| 52 | + } |
| 53 | + }; |
| 54 | + })(), |
| 55 | + setStatus: (text) => { |
| 56 | + Module.setStatus.last ??= { time: Date.now(), text: "" }; |
| 57 | + if (text === Module.setStatus.last.text) return; |
| 58 | + var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/); |
| 59 | + var now = Date.now(); |
| 60 | + if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon |
| 61 | + Module.setStatus.last.time = now; |
| 62 | + Module.setStatus.last.text = text; |
| 63 | + if (m) { |
| 64 | + text = m[1]; |
| 65 | + progressElement.value = parseInt(m[2]) * 100; |
| 66 | + progressElement.max = parseInt(m[4]) * 100; |
| 67 | + progressElement.hidden = false; |
| 68 | + } else { |
| 69 | + progressElement.value = null; |
| 70 | + progressElement.max = null; |
| 71 | + progressElement.hidden = true; |
| 72 | + } |
| 73 | + statusElement.innerHTML = text; |
| 74 | + }, |
| 75 | + totalDependencies: 0, |
| 76 | + monitorRunDependencies: (left) => { |
| 77 | + this.totalDependencies = Math.max(this.totalDependencies, left); |
| 78 | + Module.setStatus( |
| 79 | + left |
| 80 | + ? "Preparing... (" + |
| 81 | + (this.totalDependencies - left) + |
| 82 | + "/" + |
| 83 | + this.totalDependencies + |
| 84 | + ")" |
| 85 | + : "All downloads complete.", |
| 86 | + ); |
| 87 | + }, |
| 88 | + }; |
| 89 | + Module.setStatus("Downloading..."); |
| 90 | + window.onerror = () => { |
| 91 | + Module.setStatus("Exception thrown, see JavaScript console"); |
| 92 | + Module.setStatus = (text) => { |
| 93 | + if (text) console.error("[post-exception status] " + text); |
| 94 | + }; |
| 95 | + }; |
| 96 | + |
| 97 | + // Example of auto string handle by WASM on simple string parameter passing |
| 98 | + const printMessage = () => { |
| 99 | + var printer = Module.ccall( |
| 100 | + "printMessage", |
| 101 | + null, |
| 102 | + ["string"], |
| 103 | + ["Hello from C++ WebAssembly!"], |
| 104 | + ); |
| 105 | + }; |
| 106 | + |
| 107 | + // Example of a simple invocation to fibonachi |
| 108 | + const fibExample = () => { |
| 109 | + var fib = Module.cwrap("fib", "number", ["number"]); // returns a number |
| 110 | + var input = parseInt(document.getElementById("fibInput").value); |
| 111 | + if (isNaN(input)) { |
| 112 | + alert("Please enter a valid integer."); |
| 113 | + return; |
| 114 | + } |
| 115 | + var result = fib(input); |
| 116 | + document.getElementById("fibResult").textContent = |
| 117 | + "Fibonacci of " + input + " is: " + result; |
| 118 | + }; |
| 119 | + |
| 120 | + var value = 0; // (static) value to increment by one |
| 121 | + const pressBtn = () => { |
| 122 | + var addOne = Module.cwrap("addOne", null, ["number", "number"]); // void function |
| 123 | + // alloc 4 bytes of memory for the input and 4 for the output (32-bit integers) |
| 124 | + var input_ptr = Module._malloc(4); |
| 125 | + var output_ptr = Module._malloc(4); |
| 126 | + |
| 127 | + Module.setValue(input_ptr, value, "i32"); // set the value in WASM memory |
| 128 | + addOne(input_ptr, output_ptr); // call the WASM function |
| 129 | + var result = Module.getValue(output_ptr, "i32"); // extract the result from WASM memory |
| 130 | + value = result; |
| 131 | + document.getElementById("counterResult").textContent = "Sum: " + result; |
| 132 | + |
| 133 | + // dealloc memory to avoid memory leaks |
| 134 | + Module._free(input_ptr); |
| 135 | + Module._free(output_ptr); |
| 136 | + }; |
| 137 | + |
| 138 | + const sumExample = () => { |
| 139 | + var sumArray = Module.cwrap("sumArray", "number", ["number", "number"]); |
| 140 | + // Get the input string and split by commas |
| 141 | + const inputStr = document.getElementById("numbersInput").value; |
| 142 | + const numberStrings = inputStr.split(",").map((s) => s.trim()); |
| 143 | + |
| 144 | + // Convert to Float32Array |
| 145 | + const inputArray = new Float32Array(numberStrings.map(Number)); |
| 146 | + var len = inputArray.length; |
| 147 | + var bytesPerElement = inputArray.BYTES_PER_ELEMENT; |
| 148 | + var inputPtr = Module._malloc(len * bytesPerElement); |
| 149 | + Module.HEAPF32.set(inputArray, inputPtr / bytesPerElement); |
| 150 | + var result = sumArray(inputPtr, len); |
| 151 | + Module._free(inputPtr); |
| 152 | + document.getElementById("sumResult").textContent = "Sum: " + result; |
| 153 | + }; |
| 154 | + </script> |
| 155 | + {{{ SCRIPT }}} |
| 156 | + </body> |
| 157 | +</html> |
0 commit comments