xref: /third_party/node/lib/internal/vm.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ReflectApply,
51cb0ef41Sopenharmony_ci  Symbol,
61cb0ef41Sopenharmony_ci} = primordials;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  ContextifyScript,
101cb0ef41Sopenharmony_ci  compileFunction,
111cb0ef41Sopenharmony_ci  isContext: _isContext,
121cb0ef41Sopenharmony_ci} = internalBinding('contextify');
131cb0ef41Sopenharmony_ciconst {
141cb0ef41Sopenharmony_ci  runInContext,
151cb0ef41Sopenharmony_ci} = ContextifyScript.prototype;
161cb0ef41Sopenharmony_ciconst {
171cb0ef41Sopenharmony_ci  default_host_defined_options,
181cb0ef41Sopenharmony_ci  vm_dynamic_import_missing_flag,
191cb0ef41Sopenharmony_ci} = internalBinding('symbols');
201cb0ef41Sopenharmony_ciconst {
211cb0ef41Sopenharmony_ci  validateFunction,
221cb0ef41Sopenharmony_ci  validateObject,
231cb0ef41Sopenharmony_ci} = require('internal/validators');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst {
261cb0ef41Sopenharmony_ci  getOptionValue,
271cb0ef41Sopenharmony_ci} = require('internal/options');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction isContext(object) {
311cb0ef41Sopenharmony_ci  validateObject(object, 'object', { __proto__: null, allowArray: true });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  return _isContext(object);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cifunction getHostDefinedOptionId(importModuleDynamically, filename) {
371cb0ef41Sopenharmony_ci  if (importModuleDynamically !== undefined) {
381cb0ef41Sopenharmony_ci    // Check that it's either undefined or a function before we pass
391cb0ef41Sopenharmony_ci    // it into the native constructor.
401cb0ef41Sopenharmony_ci    validateFunction(importModuleDynamically,
411cb0ef41Sopenharmony_ci                     'options.importModuleDynamically');
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  if (importModuleDynamically === undefined) {
441cb0ef41Sopenharmony_ci    // We need a default host defined options that are the same for all
451cb0ef41Sopenharmony_ci    // scripts not needing custom module callbacks so that the isolate
461cb0ef41Sopenharmony_ci    // compilation cache can be hit.
471cb0ef41Sopenharmony_ci    return default_host_defined_options;
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci  // We should've thrown here immediately when we introduced
501cb0ef41Sopenharmony_ci  // --experimental-vm-modules and importModuleDynamically, but since
511cb0ef41Sopenharmony_ci  // users are already using this callback to throw a similar error,
521cb0ef41Sopenharmony_ci  // we also defer the error to the time when an actual import() is called
531cb0ef41Sopenharmony_ci  // to avoid breaking them. To ensure that the isolate compilation
541cb0ef41Sopenharmony_ci  // cache can still be hit, use a constant sentinel symbol here.
551cb0ef41Sopenharmony_ci  if (!getOptionValue('--experimental-vm-modules')) {
561cb0ef41Sopenharmony_ci    return vm_dynamic_import_missing_flag;
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  return Symbol(filename);
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cifunction registerImportModuleDynamically(referrer, importModuleDynamically) {
631cb0ef41Sopenharmony_ci  const { importModuleDynamicallyWrap } = require('internal/vm/module');
641cb0ef41Sopenharmony_ci  const { registerModule } = require('internal/modules/esm/utils');
651cb0ef41Sopenharmony_ci  registerModule(referrer, {
661cb0ef41Sopenharmony_ci    __proto__: null,
671cb0ef41Sopenharmony_ci    importModuleDynamically:
681cb0ef41Sopenharmony_ci      importModuleDynamicallyWrap(importModuleDynamically),
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cifunction internalCompileFunction(
731cb0ef41Sopenharmony_ci  code, filename, lineOffset, columnOffset,
741cb0ef41Sopenharmony_ci  cachedData, produceCachedData, parsingContext, contextExtensions,
751cb0ef41Sopenharmony_ci  params, hostDefinedOptionId, importModuleDynamically) {
761cb0ef41Sopenharmony_ci  const result = compileFunction(
771cb0ef41Sopenharmony_ci    code,
781cb0ef41Sopenharmony_ci    filename,
791cb0ef41Sopenharmony_ci    lineOffset,
801cb0ef41Sopenharmony_ci    columnOffset,
811cb0ef41Sopenharmony_ci    cachedData,
821cb0ef41Sopenharmony_ci    produceCachedData,
831cb0ef41Sopenharmony_ci    parsingContext,
841cb0ef41Sopenharmony_ci    contextExtensions,
851cb0ef41Sopenharmony_ci    params,
861cb0ef41Sopenharmony_ci    hostDefinedOptionId,
871cb0ef41Sopenharmony_ci  );
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  if (produceCachedData) {
901cb0ef41Sopenharmony_ci    result.function.cachedDataProduced = result.cachedDataProduced;
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  if (result.cachedData) {
941cb0ef41Sopenharmony_ci    result.function.cachedData = result.cachedData;
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  if (typeof result.cachedDataRejected === 'boolean') {
981cb0ef41Sopenharmony_ci    result.function.cachedDataRejected = result.cachedDataRejected;
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  if (importModuleDynamically !== undefined) {
1021cb0ef41Sopenharmony_ci    registerImportModuleDynamically(result.function, importModuleDynamically);
1031cb0ef41Sopenharmony_ci  }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  return result;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_cifunction makeContextifyScript(code,
1091cb0ef41Sopenharmony_ci                              filename,
1101cb0ef41Sopenharmony_ci                              lineOffset,
1111cb0ef41Sopenharmony_ci                              columnOffset,
1121cb0ef41Sopenharmony_ci                              cachedData,
1131cb0ef41Sopenharmony_ci                              produceCachedData,
1141cb0ef41Sopenharmony_ci                              parsingContext,
1151cb0ef41Sopenharmony_ci                              hostDefinedOptionId,
1161cb0ef41Sopenharmony_ci                              importModuleDynamically) {
1171cb0ef41Sopenharmony_ci  let script;
1181cb0ef41Sopenharmony_ci  // Calling `ReThrow()` on a native TryCatch does not generate a new
1191cb0ef41Sopenharmony_ci  // abort-on-uncaught-exception check. A dummy try/catch in JS land
1201cb0ef41Sopenharmony_ci  // protects against that.
1211cb0ef41Sopenharmony_ci  try { // eslint-disable-line no-useless-catch
1221cb0ef41Sopenharmony_ci    script = new ContextifyScript(code,
1231cb0ef41Sopenharmony_ci                                  filename,
1241cb0ef41Sopenharmony_ci                                  lineOffset,
1251cb0ef41Sopenharmony_ci                                  columnOffset,
1261cb0ef41Sopenharmony_ci                                  cachedData,
1271cb0ef41Sopenharmony_ci                                  produceCachedData,
1281cb0ef41Sopenharmony_ci                                  parsingContext,
1291cb0ef41Sopenharmony_ci                                  hostDefinedOptionId);
1301cb0ef41Sopenharmony_ci  } catch (e) {
1311cb0ef41Sopenharmony_ci    throw e; /* node-do-not-add-exception-line */
1321cb0ef41Sopenharmony_ci  }
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  if (importModuleDynamically !== undefined) {
1351cb0ef41Sopenharmony_ci    registerImportModuleDynamically(script, importModuleDynamically);
1361cb0ef41Sopenharmony_ci  }
1371cb0ef41Sopenharmony_ci  return script;
1381cb0ef41Sopenharmony_ci}
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci// Internal version of vm.Script.prototype.runInThisContext() which skips
1411cb0ef41Sopenharmony_ci// argument validation.
1421cb0ef41Sopenharmony_cifunction runScriptInThisContext(script, displayErrors, breakOnFirstLine) {
1431cb0ef41Sopenharmony_ci  return ReflectApply(
1441cb0ef41Sopenharmony_ci    runInContext,
1451cb0ef41Sopenharmony_ci    script,
1461cb0ef41Sopenharmony_ci    [
1471cb0ef41Sopenharmony_ci      null,                // sandbox - use current context
1481cb0ef41Sopenharmony_ci      -1,                  // timeout
1491cb0ef41Sopenharmony_ci      displayErrors,       // displayErrors
1501cb0ef41Sopenharmony_ci      false,               // breakOnSigint
1511cb0ef41Sopenharmony_ci      breakOnFirstLine,    // breakOnFirstLine
1521cb0ef41Sopenharmony_ci    ],
1531cb0ef41Sopenharmony_ci  );
1541cb0ef41Sopenharmony_ci}
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_cimodule.exports = {
1571cb0ef41Sopenharmony_ci  getHostDefinedOptionId,
1581cb0ef41Sopenharmony_ci  internalCompileFunction,
1591cb0ef41Sopenharmony_ci  isContext,
1601cb0ef41Sopenharmony_ci  makeContextifyScript,
1611cb0ef41Sopenharmony_ci  registerImportModuleDynamically,
1621cb0ef41Sopenharmony_ci  runScriptInThisContext,
1631cb0ef41Sopenharmony_ci};
164