Open yushihang opened 1 month ago
Examples
Another Example similiar to calculateWTNSBin()
class WasmSingleton {
constructor() {
if (WasmSingleton.instance) {
return WasmSingleton.instance;
}
this.data = "";
WasmSingleton.instance = this;
}
getData() {
return this.data;
}
computeData() {
this.data = this.data.toLowerCase();
}
async setInputData(data) {
this.data = data;
}
}
const wasmInstance = new WasmSingleton();
async function test(index) {
/*await*/ wasmInstance.setInputData("INPUT" + index);
wasmInstance.computeData();
const outputData = wasmInstance.getData();
console.log("outputData for index" + index + ": " + outputData);
}
Promise.all([test(1), test(2), test(3)]);
The output is as expected
outputData for index1: input1
outputData for index2: input2
outputData for index3: input3
After uncommented "await" for /*await*/ wasmInstance.setInputData("INPUT" + index);
class WasmSingleton {
constructor() {
if (WasmSingleton.instance) {
return WasmSingleton.instance;
}
this.data = "";
WasmSingleton.instance = this;
}
getData() {
return this.data;
}
computeData() {
this.data = this.data.toLowerCase();
}
async setInputData(data) {
this.data = data;
}
}
const wasmInstance = new WasmSingleton();
async function test(index) {
await wasmInstance.setInputData("INPUT" + index); //`await` is not commented now
wasmInstance.computeData();
const outputData = wasmInstance.getData();
console.log("outputData for index" + index + ": " + outputData);
}
Promise.all([test(1), test(2), test(3)]);
Output is not as expected
outputData for index1: input3
outputData for index2: input3
outputData for index3: input3
Ensure that the generate witness operation within the same function does not retrieve incorrect data when running concurrent code with promises, because the WASM side uses the same instance and memory.
This is a minimal change that ensures the functions calculateWTNSBin(), calculateBinWitness(), and calculateWitness() maintain their original async interfaces.
close https://github.com/iden3/circom/issues/297