mirror of
https://github.com/privacy-scaling-explorations/emp-wasm.git
synced 2026-01-09 10:07:54 -05:00
improve js<->cpp interface
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -112,7 +112,7 @@ class BufferQueue {
|
||||
}
|
||||
}
|
||||
|
||||
Module.getCircuit = () => `375 439
|
||||
Module.emp.circuit = `375 439
|
||||
32 32 33
|
||||
|
||||
2 1 0 32 406 XOR
|
||||
@@ -598,12 +598,12 @@ class BufferQueue {
|
||||
bq.push(data);
|
||||
}
|
||||
|
||||
Module.getInputBits = () => Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
Module.emp.input = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
|
||||
Module.send = send;
|
||||
Module.recv = len => bq.pop(len);
|
||||
Module.emp.send = send;
|
||||
Module.emp.recv = len => bq.pop(len);
|
||||
|
||||
Module.handleOutputBits = bits => {
|
||||
Module.emp.handleOutput = bits => {
|
||||
console.log('output', bits);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user