Iroh icon indicating copy to clipboard operation
Iroh copied to clipboard

Doesn't throw in functions

Open Z3TA opened this issue 7 years ago • 5 comments

Example code: `

const Iroh = require("iroh");

let stage = new Iroh.Stage(function foo() { throw "banana"; } foo(););

eval(stage.script);

` The exception will be ignored!

Z3TA avatar Sep 10 '17 14:09 Z3TA

I've tracked it to function DEBUG_FUNCTION_CALL, where theres a try catch ... Maybe make it possible to listen for errors ... !? hmm

Z3TA avatar Sep 10 '17 15:09 Z3TA

I'm working on it

maierfelix avatar Sep 10 '17 15:09 maierfelix

Errors shouldn't be suppressed anymore with https://github.com/maierfelix/Iroh/commit/3edc548f433fdd32362cc9d0b1f54652eb7f874e

maierfelix avatar Sep 10 '17 17:09 maierfelix

I think "throw" outside a try should generate an Iroh.PROGRAM "exception" event

That way you can listen for it and get the source location and maybe even a call stack.

Z3TA avatar Sep 10 '17 18:09 Z3TA

This would require to patch a try {} around the submitted code, which would break global variable/function support. However I experimented a bit and came up with this:

let stage = new Iroh.Stage(code);
stage.addListener(Iroh.PROGRAM)
.on("error", (e) => {
  console.error(e.error);
});

try {
  eval(stage.script);
} catch (e) {
  let event = stage.createEvent(Iroh.PROGRAM_ERROR);
  event.indent = stage.indent;
  event.error = e;
  event.trigger("error");
}

So if you want to listen for uncatched errors in your program, you have to evaluate your program this way, which remains global support but requires some additional code lines.

maierfelix avatar Sep 10 '17 18:09 maierfelix