2022-06-12 22:30:14 +01:00
|
|
|
import base64
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-06-16 22:03:43 +01:00
|
|
|
print("Embedding WASM into udspl_front.js")
|
2022-06-12 22:30:14 +01:00
|
|
|
# assumption: current working directory (relative to this script) is ../
|
|
|
|
# assumption: release wasm binary at ./pkg/usdpl_bg.wasm
|
2022-06-16 22:03:43 +01:00
|
|
|
with open("./pkg/usdpl_front_bg.wasm", mode="rb") as infile:
|
|
|
|
with open("./pkg/usdpl_front.js", mode="ab") as outfile:
|
2022-06-12 22:30:14 +01:00
|
|
|
outfile.write("\n\n// USDPL customization\nconst encoded = \"".encode())
|
|
|
|
encoded = base64.b64encode(infile.read())
|
|
|
|
outfile.write(encoded)
|
|
|
|
outfile.write("\";\n\n".encode())
|
|
|
|
outfile.write(
|
|
|
|
"""function asciiToBinary(str) {
|
|
|
|
if (typeof atob === 'function') {
|
|
|
|
return atob(str)
|
|
|
|
} else {
|
|
|
|
return new Buffer(str, 'base64').toString('binary');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode() {
|
|
|
|
var binaryString = asciiToBinary(encoded);
|
|
|
|
var bytes = new Uint8Array(binaryString.length);
|
|
|
|
for (var i = 0; i < binaryString.length; i++) {
|
|
|
|
bytes[i] = binaryString.charCodeAt(i);
|
|
|
|
}
|
2023-04-01 14:01:39 +01:00
|
|
|
return (async function() {
|
|
|
|
return new Response(bytes.buffer, {
|
|
|
|
status: 200,
|
|
|
|
statusText: 'OK',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/wasm'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
2022-06-12 22:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function init_embedded() {
|
|
|
|
return init(decode())
|
|
|
|
}
|
|
|
|
""".encode())
|
2022-06-16 22:03:43 +01:00
|
|
|
with open("./pkg/usdpl_front.d.ts", "a") as outfile:
|
2022-06-12 22:30:14 +01:00
|
|
|
outfile.write("\n\n// USDPL customization\nexport function init_embedded();\n")
|
2022-06-16 22:03:43 +01:00
|
|
|
print("Done: Embedded WASM into udspl_front.js")
|