WASM hello-world build (Emscripten)
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <emscripten/emscripten.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
// Exported function: add two numbers
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// Exported function: print hello message
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
void hello() {
|
||||
std::cout << "Hello from C++ WASM!" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>WASM Hello World</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<h1>WASM Hello World</h1>
|
||||
|
||||
<p>Open browser console (F12) to see output.</p>
|
||||
|
||||
<!-- Load generated Emscripten JS -->
|
||||
<script src="wasm_hello.js"></script>
|
||||
|
||||
<script>
|
||||
|
||||
// Wait until WASM runtime is ready
|
||||
Module.onRuntimeInitialized = () => {
|
||||
|
||||
console.log("WASM Runtime Initialized");
|
||||
|
||||
// Call C++ hello() function
|
||||
Module.ccall(
|
||||
'hello', // C++ function name
|
||||
null, // return type
|
||||
[], // argument types
|
||||
[] // arguments
|
||||
);
|
||||
|
||||
// Call C++ add() function
|
||||
const result = Module.ccall(
|
||||
'add', // function name
|
||||
'number', // return type
|
||||
['number', 'number'], // argument types
|
||||
[5, 7] // arguments
|
||||
);
|
||||
|
||||
console.log("Result =", result);
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+4583
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user