merge wrapper js and emscripten module at compile

This commit is contained in:
ading2210 2024-01-07 20:22:37 -05:00
parent 57d1245c40
commit 29407ddd65
3 changed files with 36 additions and 11 deletions

View file

@ -6,22 +6,31 @@ INCLUDE_DIR="build/curl-wasm/include/"
LIB_DIR="build/curl-wasm/lib/"
CACERT_FILE="cacert.pem"
OUT_FILE="out/libcurl.js"
MODULE_FILE="out/libcurl_module.js"
WRAPPER_SOURCE="main.js"
EXPORTED_FUNCS="_load_certs,_perform_request"
RUNTIME_METHODS="addFunction,removeFunction,allocate,ALLOC_NORMAL"
COMPILER_OPTIONS="-o $OUT_FILE -lcurl -lssl -lcrypto -lcjson -I $INCLUDE_DIR -L $LIB_DIR"
COMPILER_OPTIONS="-o $MODULE_FILE -lcurl -lssl -lcrypto -lcjson -I $INCLUDE_DIR -L $LIB_DIR"
EMSCRIPTEN_OPTIONS="-lwebsocket.js -sSINGLE_FILE -sASYNCIFY -sALLOW_TABLE_GROWTH -sEXPORTED_FUNCTIONS=$EXPORTED_FUNCS -sEXPORTED_RUNTIME_METHODS=$RUNTIME_METHODS"
#ensure deps are compiled
tools/all_deps.sh
tools/generate_cert.sh
#clean output dir
rm -rf out
mkdir -p out
#compile the main c file - but only if the source has been modified
COMPILE_CMD="emcc main.c $COMPILER_OPTIONS $EMSCRIPTEN_OPTIONS"
echo $COMPILE_CMD
$COMPILE_CMD
#patch the output to work around some emscripten bugs
sed -i 's/err("__syscall_getsockname " \?+ \?fd);//' $OUT_FILE
sed -i 's/function _emscripten_console_error(str) {/& if(UTF8ToString(str).endsWith("__syscall_setsockopt\\n")) return;/' $OUT_FILE
sed -i 's/err("__syscall_getsockname " \?+ \?fd);//' $MODULE_FILE
sed -i 's/function _emscripten_console_error(str) {/& if(UTF8ToString(str).endsWith("__syscall_setsockopt\\n")) return;/' $MODULE_FILE
#merge compiled emscripten module and wrapper code
cp $WRAPPER_SOURCE $OUT_FILE
sed -i "/__emscripten_output__/r $MODULE_FILE" $OUT_FILE

View file

@ -1,9 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<script src="./out/libcurl.js"></script>
<script src="./main.js"></script>
<link rel="icon" href="data:;base64,=">
<script defer src="./out/libcurl.js"></script>
<script>
document.addEventListener("libcurl_load", ()=>{
console.log("libcurl.js ready!");
})
</script>
</head>
<body>
<p>emscripten tests</p>

View file

@ -1,3 +1,9 @@
//everything is wrapped in a function to prevent emscripten from polluting the global scope
window.libcurl = (function() {
//emscripten compiled code is inserted here
/* __emscripten_output__ */
const websocket_url = `wss://${location.hostname}/ws`;
const status_messages = {
@ -221,14 +227,19 @@ function set_websocket_url(url) {
Module.websocket.url = url;
}
async function main() {
function main() {
console.log("emscripten module loaded");
_load_certs();
set_websocket_url(websocket_url);
console.log(await libcurl_fetch("https://httpbin.org/anything"));
let load_event = new Event("libcurl_load");
document.dispatchEvent(load_event);
}
window.onload = () => {
console.log("page loaded, waiting for emscripten module load");
Module.onRuntimeInitialized = main;
};
Module.onRuntimeInitialized = main;
return {
fetch: libcurl_fetch,
set_websocket: set_websocket_url,
}
})()