module.evaluateSync() inside isolate swallows error
Hello.
I am trying to implement something like importScripts api, but with modules. I realized, that calling module.evaluateSync() inside isolate swallows module-level error, but that error caught outside of isolate.
Self-contained example:
const ivm = require("isolated-vm")
const isolate = new ivm.Isolate();
const context = isolate.createContextSync();
try {
context.evalClosureSync(`
const isolate = $0;
const context = $1;
const log = (...args) => $2.applySync(null, args);
const module = isolate.compileModuleSync("throw new Error('Error from module')");
module.instantiateSync(context, () => {});
try {
module.evaluateSync();
} catch (e) {
log("Error caught inside", e)
throw e
}
log("This should not be invoked")
`, [
isolate,
context,
new ivm.Reference((...args) => console.log(...args))
])
} catch (e) {
console.log('Caught error outside:', e)
}
I expected output:
Error caught inside: Error from module
Caught error outside: Error from module
But received output:
This should not be invoked
Caught error outside: Error: Error from module
Could you please help me with this? Is it a bug, or I am doing something wrong?
Thanks a lot for this awesome library!
v8 unfortunately changed the way modules worked after I wrote all this. I can't recommend the *Sync class of functions when working with modules, since they return promises internally. Instead, structure your code as async/await with the async methods.
@laverdet thank you for quick response!
Actually, I am trying to implement lazy-loading of imported modules, something like this
import * as module1 from "./module1" // this one would resolve to Proxy
function fn() {
return module_1.member; // accessing to proxy should sync load (via applySyncPromise) "./module1" content, create & eval module, return exported members
}
Actually, everything working fine, except module-level errors.
Looks like that task is not possible at all, if you could not recommend *Sync methods?