Skip to content

vm.Module.evaluate() morphs loader errors into module evaluation rejections #60242

@joyeecheung

Description

@joyeecheung

This came up in #60205 (comment) when I was trying to make vm.Module.evaluate() return the module evaluation promise as-is (so synchronously fulfilled for synthetic modules and source text modules without TLA). Currently the validation errors, ERR_VM_MODULE_STATUS, THROW_ERR_SCRIPT_EXECUTION_TIMEOUT, and THROW_ERR_SCRIPT_EXECUTION_INTERRUPTED would always be rejected, so for a synchronous loader, they would either have to expose these underlying rejections to users when the loading goes wrong, or be forced to become asynchronous as it cannot catch these errors synchronously and paint it over. i.e. they cannot do something like this:

function syncLoad() {
  try {
    mod.evaluate();
  } catch(e) {  // This allows the user to get the loader errors synchronously
    if (e.code === 'ERR_VM_MODULE_STATUS' ||
        e.code === 'ERR_SCRIPT_EXECUTION_INTERRUPTED' ||
        e.code === 'ERR_SCRIPT_EXECUTION_TIMEOUT') {
      // fix it up and try again, or error and explain to user what to do
      // but hide the errors that do not directly come from module code
      // as implementation detail
    }
  }
  if (mod.status === 'errored') {
    throw mod.error;  // It's an evaluation error from the module code
  }
  return mod.namespace;
}

Opening a separate issue to discuss if there's another way to make this possible other than just making it throw these loader errors synchronously.

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions