-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic WASM POC showing CDF variables count
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
- Loading branch information
Showing
6 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
fs = import('fs') | ||
|
||
|
||
flags = ['-sWASM=2', '-sALLOW_MEMORY_GROWTH', '-sFETCH=1', '-sASYNCIFY']+['-sWASM=2', '-sEXPORT_ALL=1', '-sMODULARIZE', '-sEXPORTED_RUNTIME_METHODS=ccall', '-lembind', '-sFETCH=1', '--bind', '-sALLOW_MEMORY_GROWTH'] | ||
|
||
lib_wacdfpp = library('wacdfpp', 'wacdfpp.cpp', | ||
dependencies:[cdfpp_dep], | ||
cpp_args : flags, | ||
install: false, | ||
link_args : flags, | ||
build_by_default: meson.get_compiler('cpp').get_id() == 'emscripten', | ||
extra_files : ['wasm.txt', 'wacdfpp.html'] | ||
) | ||
|
||
js_wrapper = custom_target('js_wrapper', | ||
input : lib_wacdfpp, | ||
output : 'wacdfpp.js', | ||
command : [ 'emcc', '-lembind', '-sFETCH=1', '-sWASM=2', '--bind', '-sALLOW_MEMORY_GROWTH', '-sASYNCIFY', '-o', '@OUTPUT@', '-Wl,--whole-archive','@INPUT@', '-Wl,--no-whole-archive'], | ||
build_by_default: true | ||
) | ||
|
||
fs.copyfile('wacdfpp.html', 'wacdfpp.html') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <cdfpp/cdf.hpp> | ||
#include <filesystem> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#if __has_include(<emscripten.h>) | ||
#include <emscripten.h> | ||
#include <emscripten/bind.h> | ||
#include <emscripten/fetch.h> | ||
#else | ||
#define EMSCRIPTEN_KEEPALIVE | ||
#define EMSCRIPTEN_EXPORT(x) x | ||
#endif | ||
|
||
|
||
EMSCRIPTEN_KEEPALIVE | ||
emscripten::val count_variables(std::string url) | ||
{ | ||
struct fetch_data | ||
{ | ||
bool done = false; | ||
std::vector<char> data; | ||
}; | ||
emscripten_fetch_attr_t attr; | ||
emscripten_fetch_attr_init(&attr); | ||
strcpy(attr.requestMethod, "GET"); | ||
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; | ||
attr.userData = new fetch_data; | ||
attr.onsuccess = [](emscripten_fetch_t* fetch) | ||
{ | ||
auto data = static_cast<fetch_data*>(fetch->userData); | ||
data->data = std::vector<char>(fetch->data, fetch->data + fetch->numBytes); | ||
data->done = true; | ||
emscripten_fetch_close(fetch); | ||
}; | ||
attr.onerror = [](emscripten_fetch_t* fetch) | ||
{ | ||
auto data = static_cast<fetch_data*>(fetch->userData); | ||
data->done = true; | ||
emscripten_fetch_close(fetch); | ||
}; | ||
emscripten_fetch(&attr, url.c_str()); | ||
while (!static_cast<fetch_data*>(attr.userData)->done) | ||
{ | ||
emscripten_sleep(10); | ||
} | ||
auto data = static_cast<fetch_data*>(attr.userData); | ||
if (auto cdf_file = cdf::io::load(data->data); cdf_file) | ||
{ | ||
std::cout << "Variables: " << std::size(cdf_file->variables) << "\n"; | ||
auto sz = std::size(cdf_file->variables); | ||
delete data; | ||
return emscripten::val(sz); | ||
} | ||
return emscripten::val(-1); | ||
} | ||
|
||
|
||
EMSCRIPTEN_KEEPALIVE | ||
int my_cdf_test() | ||
{ | ||
return 42; | ||
} | ||
|
||
|
||
#ifdef __EMSCRIPTEN__ | ||
EMSCRIPTEN_BINDINGS(cdfppjs) | ||
{ | ||
emscripten::function("my_cdf_test", &my_cdf_test); | ||
emscripten::function("count_variables", &count_variables); | ||
} | ||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<canvas id="canvas" width="400" height="400"></canvas> | ||
<div id="container"> | ||
<p> | ||
<input type="text" id="URL" name="URL" /> | ||
<input id="myButton" type=button value="Read CDF"> | ||
<output name="result" id="count"/> | ||
</p> | ||
</div> | ||
<script> | ||
|
||
console.log("loading wasm"); | ||
|
||
var Module = { | ||
canvas: (function() { | ||
var canvas = document.getElementById('canvas'); | ||
return canvas; | ||
})(), | ||
|
||
onRuntimeInitialized: function() { | ||
console.log("wasm loaded"); | ||
console.log("wasm initialized"); | ||
} | ||
}; | ||
</script> | ||
<script src="wacdfpp.js"></script> | ||
|
||
<script> | ||
function callback() { | ||
const URL = document.getElementById("URL").value; | ||
|
||
// Disable the button while the operation is in progress | ||
document.getElementById("myButton").disabled = true; | ||
|
||
// Fetch the data and process it asynchronously | ||
Module.count_variables(URL).then(function(count) { | ||
// Update the count and enable the button when processing is done | ||
document.getElementById("count").innerHTML = count; | ||
document.getElementById("myButton").disabled = false; | ||
}); | ||
} | ||
|
||
myButton.addEventListener('click', callback); | ||
</script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[binaries] | ||
c = 'emcc' | ||
cpp = 'em++' | ||
ar = 'emar' | ||
exe_wrapper = 'node' | ||
|
||
[built-in options] | ||
c_args = [] | ||
c_link_args = ['-sEXPORT_ALL=1', '-sWASM=2', '-sMODULARIZE', '-sEXPORTED_RUNTIME_METHODS=ccall'] | ||
cpp_args = [] | ||
cpp_link_args = [] | ||
|
||
|
||
[properties] | ||
# Emscripten always needs an exe wrapper. Again, | ||
# maybe Meson could just know this. | ||
needs_exe_wrapper = true | ||
|
||
[host_machine] | ||
system = 'wasm' | ||
cpu_family = 'wasm' | ||
cpu = 'wasm' | ||
endian = 'little' |