Created
July 4, 2024 21:57
-
-
Save palaniraja/191ed3dc2187955b7342e9cefc48fed5 to your computer and use it in GitHub Desktop.
emscripten adventures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir build && cd $_ | |
emcmake cmake .. | |
make | |
# now you should have fileProcessor.js and fileProcessor.wasm | |
# Then run ../index.html (in a local webserver) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.12) | |
project(fileProcessor) | |
set(CMAKE_CXX_STANDARD 11) | |
add_executable(fileProcessor fileProcessor.cpp) | |
# set_target_properties(fileProcessor PROPERTIES LINK_FLAGS "-s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'") | |
# set_target_properties(fileProcessor PROPERTIES LINK_FLAGS "-s WASM=1 -s EXPORTED_RUNTIME_METHODS='[\"cwrap\"]' -s EXPORTED_FUNCTIONS='[\"_malloc\", \"_free\"]'") | |
set_target_properties(fileProcessor PROPERTIES LINK_FLAGS "-s WASM=1 -s EXPORTED_RUNTIME_METHODS='[\"cwrap\", \"UTF8ToString\"]' -s EXPORTED_FUNCTIONS='[\"_malloc\", \"_free\"]'") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef __EMSCRIPTEN__ | |
#include <emscripten/emscripten.h> | |
#else | |
#define EMSCRIPTEN_KEEPALIVE | |
#endif | |
#include <string> | |
extern "C" { | |
EMSCRIPTEN_KEEPALIVE | |
char* processFile(char* fileData, size_t fileSize) { | |
// Process the file data here... | |
// This is a placeholder - you'd replace this with your actual code. | |
std::string result = "Processed file of size: " + std::to_string(fileSize); | |
// Allocate memory on the heap for the result string. | |
char* heapResult = (char*) malloc(result.size() + 1); // +1 for the null-terminator | |
// Copy the result string into the heap memory. | |
strcpy(heapResult, result.c_str()); | |
return heapResult; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>File Processing with WASM</title> | |
</head> | |
<body> | |
<input type="file" id="fileInput"> | |
<script type="text/javascript" src="build/fileProcessor.js"> </script> | |
<script> | |
Module['onRuntimeInitialized'] = function() { | |
console.log("onRuntimeInitialized - called") | |
document.getElementById('fileInput').addEventListener('change', function(event) { | |
var file = event.target.files[0]; | |
var reader = new FileReader(); | |
reader.onload = function() { | |
var arrayBuffer = this.result; | |
var fileData = new Uint8Array(arrayBuffer); | |
// Allocate memory in the WASM module's memory space. | |
var pointer = Module._malloc(fileData.length); | |
// Copy the file data into the WASM module's memory. | |
Module.HEAPU8.set(fileData, pointer); | |
// Call the WASM function. | |
var resultPointer = Module._processFile(pointer, fileData.length); | |
// Get the result string. | |
var result = Module.UTF8ToString(resultPointer); | |
// Log the result. | |
console.log(result); | |
// Free the memory. | |
Module._free(pointer); | |
Module._free(resultPointer); | |
}; | |
reader.readAsArrayBuffer(file); | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment