improve js<->cpp interface

This commit is contained in:
Andrew Morris
2024-10-14 07:51:10 +11:00
parent 2ecfd97d32
commit 639e4d551c
2 changed files with 19 additions and 19 deletions

View File

@@ -11,25 +11,25 @@ void run_impl(int party);
// Implement send_js function to send data from C++ to JavaScript
EM_JS(void, send_js, (const void* data, size_t len), {
if (!Module.send) {
if (!Module.emp.send) {
throw new Error("Module.send is not defined in JavaScript.");
}
// Copy data from WebAssembly memory to a JavaScript Uint8Array
const dataArray = HEAPU8.slice(data, data + len);
Module.send(dataArray);
Module.emp.send(dataArray);
});
// Implement recv_js function to receive data from JavaScript to C++
EM_ASYNC_JS(void, recv_js, (void* data, size_t len), {
if (!Module.recv) {
reject(new Error("Module.recv is not defined in JavaScript."));
if (!Module.emp.recv) {
reject(new Error("Module.emp.recv is not defined in JavaScript."));
return;
}
// Wait for data from JavaScript
const dataArray = await Module.recv(arguments[1]);
const dataArray = await Module.emp.recv(arguments[1]);
// Copy data from JavaScript Uint8Array to WebAssembly memory
HEAPU8.set(dataArray, data);
@@ -51,11 +51,11 @@ public:
};
EM_JS(char*, get_circuit_raw, (int* lengthPtr), {
if (!Module.getCircuit) {
throw new Error("Module.getCircuit is not defined in JavaScript.");
if (!Module.emp.circuit) {
throw new Error("Module.emp.circuit is not defined in JavaScript.");
}
const circuitString = Module.getCircuit(); // Get the string from JavaScript
const circuitString = Module.emp.circuit; // Get the string from JavaScript
const length = lengthBytesUTF8(circuitString) + 1; // Calculate length including the null terminator
// Allocate memory for the string
@@ -81,11 +81,11 @@ emp::BristolFormat get_circuit() {
}
EM_JS(uint8_t*, get_input_bits_raw, (int* lengthPtr), {
if (!Module.getInputBits) {
throw new Error("Module.getInputBits is not defined in JavaScript.");
if (!Module.emp.input) {
throw new Error("Module.emp.input is not defined in JavaScript.");
}
const inputBits = Module.getInputBits(); // Assume this returns a Uint8Array
const inputBits = Module.emp.input; // Assume this is a Uint8Array
// Allocate memory for the input array
const bytePtr = Module._js_malloc(inputBits.length);
@@ -114,15 +114,15 @@ std::vector<bool> get_input_bits() {
}
EM_JS(void, handle_output_bits_raw, (uint8_t* outputBits, int length), {
if (!Module.handleOutputBits) {
throw new Error("Module.handleOutputBits is not defined in JavaScript.");
if (!Module.emp.handleOutput) {
throw new Error("Module.emp.handleOutput is not defined in JavaScript.");
}
// Copy the output bits to a Uint8Array
const outputBitsArray = new Uint8Array(Module.HEAPU8.buffer, outputBits, length);
// Call the JavaScript function with the output bits
Module.handleOutputBits(outputBitsArray);
Module.emp.handleOutput(outputBitsArray);
});
void handle_output_bits(const std::vector<bool>& output_bits) {