NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
1.96k stars 352 forks source link

Try catch syntax does not work correctly when using native syntax #263

Closed ayahito-saji closed 4 months ago

ayahito-saji commented 4 months ago

Hi, I am using JS-interpreter and have observed that the try-catch syntax does not work correctly and stops with an error in the following case.

try {
  new Date(NaN).toISOString();
} catch (e) {
  alert("caught");
}

I am guessing the cause is that this program is using the native toISOString() and the error occurs outside of the interpreter (native). These errors need to be propagated inside the interpreter, or to be raised internally.

ayahito-saji commented 4 months ago

https://github.com/NeilFraser/JS-Interpreter/blob/master/interpreter.js#L1926-L1954

NeilFraser commented 4 months ago

Thanks for this report. Fixed. I've tested the rest of the Date instance methods, this appears to be the only one that can throw, the rest return either NaN or "Invalid Date".

This class of bug is still lurking in other places. I'll fix them one by one as they are found.