Skip to content

vm: reject in importModuleDynamically without --experimental-vm-modules

vm: unify host-defined option generation in vm.compileFunction

Set a default host-defined option for vm.compileFunction so that it's consistent with vm.Script.

vm: use internal versions of compileFunction and Script

Instead of using the public versions of the vm APIs internally, use the internal versions so that we can skip unnecessary argument validation.

The public versions would need special care to the generation of host-defined options to hit the isolate compilation cache when imporModuleDynamically isn't used, while internally it's almost always used, so this allows us to handle the host-defined options separately.

vm: reject in importModuleDynamically without --experimental-vm-modules

Users cannot access any API that can be used to return a module or module namespace in this callback without --experimental-vm-modules anyway, so this would eventually lead to a rejection. This patch rejects in this case with our own error message and use a constant host-defined option for the rejection, so that scripts with the same source can still be compiled using the compilation cache if no import() is actually called in the script.

Refs: https://github.com/nodejs/node/issues/35375

with-dynamic-import-callback is Jest's current behavior which blocks users from upgrading from v16.x (see https://github.com/nodejs/node/pull/49950#issuecomment-1748178407).

                                                                                   confidence improvement accuracy (*)     (**)    (***)
vm/compile-script-in-isolate-cache.js n=100 type='with-dynamic-import-callback'           ***   9880.21 %     ±166.83% ±237.30% ±343.44%
vm/compile-script-in-isolate-cache.js n=100 type='without-dynamic-import-callback'                 1.87 %       ±4.21%   ±5.83%   ±8.06%

Merge request reports

Loading