quickjs-emscripten-sync icon indicating copy to clipboard operation
quickjs-emscripten-sync copied to clipboard

Impossible to catch interruptions when returning promises

Open damianofalcioni opened this issue 1 year ago • 0 comments

Hi, I have the following code for quickjs-emscripten that handle correctly the interruption:

const QuickJS = await getQuickJS();
const runtime = QuickJS.newRuntime();
runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + 1000));
const vm = runtime.newContext();
const result = vm.unwrapResult(vm.evalCode("new Promise((out,err)=>{i = 0; while (1) { i++; } out(i);});"));
const promise = result.consume(result=>vm.resolvePromise(result));
vm.runtime.executePendingJobs();
const resolvedPromise = await promise;
if (resolvedPromise.error) {
  const errorJson = vm.dump(resolvedPromise.error);
  console.log(errorJson);
  resolvedPromise.error.dispose();
}

but when I try to recreate it with quickjs-emscripten-sync it do not catch and terminate Node:

const runtime = (await getQuickJS()).newRuntime();
runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + 1000));
const vm = runtime.newContext();
const arena = new Arena(vm, { isMarshalable: true });
try{
  const result = arena.evalCode("new Promise((out,err)=>{i = 0; while (1) { i++; } out(i);});");
  arena.executePendingJobs();
  console.log(await result);
}catch(e) {
  console.error(e.message);
}

error:

> node index

npm error Lifecycle script `run` failed with error:
npm error code 13

I did something wrong? Thanks

damianofalcioni avatar Oct 18 '24 13:10 damianofalcioni