Stipe Varnica
5 months ago
8 changed files with 1626 additions and 482 deletions
-
51.vscode/settings.json
-
2C++/cmd.txt
-
26C++/test.cpp
-
1878C++/test.js
-
BINC++/test.wasm
-
11README.md
-
123index.html
-
17index.js
@ -0,0 +1,51 @@ |
|||||
|
{ |
||||
|
"liveServer.settings.port": 5501, |
||||
|
"files.associations": { |
||||
|
"cctype": "cpp", |
||||
|
"clocale": "cpp", |
||||
|
"cmath": "cpp", |
||||
|
"cstdarg": "cpp", |
||||
|
"cstddef": "cpp", |
||||
|
"cstdio": "cpp", |
||||
|
"cstdlib": "cpp", |
||||
|
"cstring": "cpp", |
||||
|
"cwchar": "cpp", |
||||
|
"cwctype": "cpp", |
||||
|
"array": "cpp", |
||||
|
"atomic": "cpp", |
||||
|
"bit": "cpp", |
||||
|
"*.tcc": "cpp", |
||||
|
"compare": "cpp", |
||||
|
"concepts": "cpp", |
||||
|
"cstdint": "cpp", |
||||
|
"deque": "cpp", |
||||
|
"string": "cpp", |
||||
|
"unordered_map": "cpp", |
||||
|
"vector": "cpp", |
||||
|
"exception": "cpp", |
||||
|
"algorithm": "cpp", |
||||
|
"functional": "cpp", |
||||
|
"iterator": "cpp", |
||||
|
"memory": "cpp", |
||||
|
"memory_resource": "cpp", |
||||
|
"numeric": "cpp", |
||||
|
"random": "cpp", |
||||
|
"string_view": "cpp", |
||||
|
"system_error": "cpp", |
||||
|
"tuple": "cpp", |
||||
|
"type_traits": "cpp", |
||||
|
"utility": "cpp", |
||||
|
"initializer_list": "cpp", |
||||
|
"iosfwd": "cpp", |
||||
|
"iostream": "cpp", |
||||
|
"istream": "cpp", |
||||
|
"limits": "cpp", |
||||
|
"new": "cpp", |
||||
|
"numbers": "cpp", |
||||
|
"ostream": "cpp", |
||||
|
"stdexcept": "cpp", |
||||
|
"streambuf": "cpp", |
||||
|
"cinttypes": "cpp", |
||||
|
"typeinfo": "cpp" |
||||
|
} |
||||
|
} |
@ -1 +1 @@ |
|||||
emcc -o test.js test.cpp -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" |
|
||||
|
emcc -lembind -o test.js test.cpp -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" |
@ -1,18 +1,20 @@ |
|||||
#include <iostream>
|
#include <iostream>
|
||||
#include <emscripten/emscripten.h>
|
|
||||
|
#include <emscripten/bind.h>
|
||||
|
#include <string>
|
||||
|
using namespace emscripten; |
||||
|
|
||||
extern "C" { |
|
||||
EMSCRIPTEN_KEEPALIVE |
|
||||
int myFunction(int number) { |
|
||||
return number * number; |
|
||||
} |
|
||||
|
std::string myFunction(std::string word) |
||||
|
{ |
||||
|
std::string test = " testing"; |
||||
|
return word + test; |
||||
|
}; |
||||
|
|
||||
EMSCRIPTEN_KEEPALIVE |
|
||||
int add(int a, int b) { |
|
||||
return a + b; |
|
||||
} |
|
||||
} |
|
||||
|
EMSCRIPTEN_BINDINGS(my_module) |
||||
|
{ |
||||
|
function("myFunction", &myFunction); |
||||
|
}; |
||||
|
|
||||
int main() { |
|
||||
|
int main() |
||||
|
{ |
||||
return 0; |
return 0; |
||||
} |
} |
1878
C++/test.js
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,11 @@ |
|||||
|
# Prototype |
||||
|
|
||||
|
Link for compiling code with Embind → https://emscripten.org/docs/porting connecting_cpp_and_javascript/embind.html |
||||
|
Command for compiling code with Embind: |
||||
|
``` |
||||
|
emcc -lembind -o test.js test.cpp -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" |
||||
|
``` |
||||
|
- EMSCRIPTEN_BINDINGS macro is used to bind the C++ function to make it callable from JavaScript |
||||
|
- Emscripten compiles the C++ code to WebAssembly, generating a .wasm file and JavaScript glue code that loads the WebAssembly module and sets up the bindings |
||||
|
- When the HTML page is loaded, test.js is executed, loading the WebAssembly module , Emscripten's Module object is created, exposing the C++ functions (like myFunction) to JavaScript |
||||
|
- Example of using Module: var result = Module.myFunction(input) |
@ -1,16 +1,9 @@ |
|||||
function calculateResult() { |
function calculateResult() { |
||||
var numberInput = parseInt(document.getElementById("numberInput").value); |
|
||||
var result = Module.ccall("myFunction", "number", ["number"], [numberInput]); |
|
||||
document.getElementById("output").innerText = "Result: " + result; |
|
||||
|
var input = document.getElementById("numberInput").value; |
||||
|
var result = Module.myFunction(input) |
||||
|
document.getElementById("output").innerText = result; |
||||
} |
} |
||||
|
|
||||
// document
|
|
||||
// .getElementById("calculateBtn")
|
|
||||
// .addEventListener("click", calculateResult);
|
|
||||
|
|
||||
function calculate() { |
|
||||
var a = parseInt(document.getElementById("input1").value); |
|
||||
var b = parseInt(document.getElementById("input2").value); |
|
||||
var result = Module.ccall("add", "number", ["number", "number"], [a, b]); |
|
||||
document.getElementById("result").innerText = "Result: " + result; |
|
||||
} |
|
||||
|
|
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue