summaryrefslogtreecommitdiff
path: root/lib/internal/vm/module.js
diff options
context:
space:
mode:
authorGus Caplan <me@gus.host>2018-07-27 21:21:30 -0500
committerGus Caplan <me@gus.host>2018-07-31 10:23:03 -0500
commit2bdcdfc3cbf735ede8130901f58efcd4c6fe9697 (patch)
tree5f040a87e2f7ef06b67a4ef17a7478a89b5a985a /lib/internal/vm/module.js
parent3b23b4d7c08e3e61f7e1ff3b91a6626b2bcfd5e3 (diff)
downloadandroid-node-v8-2bdcdfc3cbf735ede8130901f58efcd4c6fe9697.tar.gz
android-node-v8-2bdcdfc3cbf735ede8130901f58efcd4c6fe9697.tar.bz2
android-node-v8-2bdcdfc3cbf735ede8130901f58efcd4c6fe9697.zip
vm: rename vm.Module to vm.SourceTextModule
At the last TC39 meeting, a new type of Module Records backed by JavaScript source called Dynamic Module Records was discussed, and it is now at Stage 1. Regardless of whether that proposal makes it all the way into the spec, SourceTextModule is indeed a more descriptive and accurate name for what this class represents. PR-URL: https://github.com/nodejs/node/pull/22007 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Diffstat (limited to 'lib/internal/vm/module.js')
-rw-r--r--lib/internal/vm/module.js250
1 files changed, 0 insertions, 250 deletions
diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js
deleted file mode 100644
index a799cf2c34..0000000000
--- a/lib/internal/vm/module.js
+++ /dev/null
@@ -1,250 +0,0 @@
-'use strict';
-
-const { internalBinding } = require('internal/bootstrap/loaders');
-const { URL } = require('internal/url');
-const { isContext } = process.binding('contextify');
-const {
- ERR_INVALID_ARG_TYPE,
- ERR_VM_MODULE_ALREADY_LINKED,
- ERR_VM_MODULE_DIFFERENT_CONTEXT,
- ERR_VM_MODULE_LINKING_ERRORED,
- ERR_VM_MODULE_NOT_LINKED,
- ERR_VM_MODULE_NOT_MODULE,
- ERR_VM_MODULE_STATUS
-} = require('internal/errors').codes;
-const {
- getConstructorOf,
- customInspectSymbol,
- emitExperimentalWarning
-} = require('internal/util');
-const { SafePromise } = require('internal/safe_globals');
-const { validateInt32, validateUint32 } = require('internal/validators');
-
-const {
- ModuleWrap,
- kUninstantiated,
- kInstantiating,
- kInstantiated,
- kEvaluating,
- kEvaluated,
- kErrored,
-} = internalBinding('module_wrap');
-
-const STATUS_MAP = {
- [kUninstantiated]: 'uninstantiated',
- [kInstantiating]: 'instantiating',
- [kInstantiated]: 'instantiated',
- [kEvaluating]: 'evaluating',
- [kEvaluated]: 'evaluated',
- [kErrored]: 'errored',
-};
-
-let globalModuleId = 0;
-const perContextModuleId = new WeakMap();
-const wrapMap = new WeakMap();
-const dependencyCacheMap = new WeakMap();
-const linkingStatusMap = new WeakMap();
-// vm.Module -> function
-const initImportMetaMap = new WeakMap();
-// ModuleWrap -> vm.Module
-const wrapToModuleMap = new WeakMap();
-const defaultModuleName = 'vm:module';
-
-class Module {
- constructor(src, options = {}) {
- emitExperimentalWarning('vm.Module');
-
- if (typeof src !== 'string')
- throw new ERR_INVALID_ARG_TYPE('src', 'string', src);
- if (typeof options !== 'object' || options === null)
- throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
-
- const {
- context,
- lineOffset = 0,
- columnOffset = 0,
- initializeImportMeta
- } = options;
-
- if (context !== undefined) {
- if (typeof context !== 'object' || context === null) {
- throw new ERR_INVALID_ARG_TYPE('options.context', 'Object', context);
- }
- if (!isContext(context)) {
- throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context',
- context);
- }
- }
-
- let { url } = options;
- if (url !== undefined) {
- if (typeof url !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url);
- }
- url = new URL(url).href;
- } else if (context === undefined) {
- url = `${defaultModuleName}(${globalModuleId++})`;
- } else if (perContextModuleId.has(context)) {
- const curId = perContextModuleId.get(context);
- url = `${defaultModuleName}(${curId})`;
- perContextModuleId.set(context, curId + 1);
- } else {
- url = `${defaultModuleName}(0)`;
- perContextModuleId.set(context, 1);
- }
-
- validateInt32(lineOffset, 'options.lineOffset');
- validateInt32(columnOffset, 'options.columnOffset');
-
- if (initializeImportMeta !== undefined) {
- if (typeof initializeImportMeta === 'function') {
- initImportMetaMap.set(this, initializeImportMeta);
- } else {
- throw new ERR_INVALID_ARG_TYPE(
- 'options.initializeImportMeta', 'function', initializeImportMeta);
- }
- }
-
- const wrap = new ModuleWrap(src, url, context, lineOffset, columnOffset);
- wrapMap.set(this, wrap);
- linkingStatusMap.set(this, 'unlinked');
- wrapToModuleMap.set(wrap, this);
-
- Object.defineProperties(this, {
- url: { value: url, enumerable: true },
- context: { value: context, enumerable: true },
- });
- }
-
- get linkingStatus() {
- return linkingStatusMap.get(this);
- }
-
- get status() {
- return STATUS_MAP[wrapMap.get(this).getStatus()];
- }
-
- get namespace() {
- const wrap = wrapMap.get(this);
- if (wrap.getStatus() < kInstantiated)
- throw new ERR_VM_MODULE_STATUS(
- 'must not be uninstantiated or instantiating'
- );
- return wrap.namespace();
- }
-
- get dependencySpecifiers() {
- let deps = dependencyCacheMap.get(this);
- if (deps !== undefined)
- return deps;
-
- deps = wrapMap.get(this).getStaticDependencySpecifiers();
- Object.freeze(deps);
- dependencyCacheMap.set(this, deps);
- return deps;
- }
-
- get error() {
- const wrap = wrapMap.get(this);
- if (wrap.getStatus() !== kErrored)
- throw new ERR_VM_MODULE_STATUS('must be errored');
- return wrap.getError();
- }
-
- async link(linker) {
- if (typeof linker !== 'function')
- throw new ERR_INVALID_ARG_TYPE('linker', 'function', linker);
- if (linkingStatusMap.get(this) !== 'unlinked')
- throw new ERR_VM_MODULE_ALREADY_LINKED();
- const wrap = wrapMap.get(this);
- if (wrap.getStatus() !== kUninstantiated)
- throw new ERR_VM_MODULE_STATUS('must be uninstantiated');
-
- linkingStatusMap.set(this, 'linking');
-
- const promises = wrap.link(async (specifier) => {
- const m = await linker(specifier, this);
- if (!m || !wrapMap.has(m))
- throw new ERR_VM_MODULE_NOT_MODULE();
- if (m.context !== this.context)
- throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
- const childLinkingStatus = linkingStatusMap.get(m);
- if (childLinkingStatus === 'errored')
- throw new ERR_VM_MODULE_LINKING_ERRORED();
- if (childLinkingStatus === 'unlinked')
- await m.link(linker);
- return wrapMap.get(m);
- });
-
- try {
- if (promises !== undefined)
- await SafePromise.all(promises);
- linkingStatusMap.set(this, 'linked');
- } catch (err) {
- linkingStatusMap.set(this, 'errored');
- throw err;
- }
- }
-
- instantiate() {
- const wrap = wrapMap.get(this);
- const status = wrap.getStatus();
- if (status === kInstantiating || status === kEvaluating)
- throw new ERR_VM_MODULE_STATUS('must not be instantiating or evaluating');
- if (linkingStatusMap.get(this) !== 'linked')
- throw new ERR_VM_MODULE_NOT_LINKED();
- wrap.instantiate();
- }
-
- async evaluate(options = {}) {
- if (typeof options !== 'object' || options === null) {
- throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
- }
-
- let timeout = options.timeout;
- if (timeout === undefined) {
- timeout = -1;
- } else {
- validateUint32(timeout, 'options.timeout', true);
- }
-
- const { breakOnSigint = false } = options;
- if (typeof breakOnSigint !== 'boolean') {
- throw new ERR_INVALID_ARG_TYPE('options.breakOnSigint', 'boolean',
- breakOnSigint);
- }
-
- const wrap = wrapMap.get(this);
- const status = wrap.getStatus();
- if (status !== kInstantiated &&
- status !== kEvaluated &&
- status !== kErrored) {
- throw new ERR_VM_MODULE_STATUS(
- 'must be one of instantiated, evaluated, and errored'
- );
- }
- const result = wrap.evaluate(timeout, breakOnSigint);
- return { result, __proto__: null };
- }
-
- [customInspectSymbol](depth, options) {
- let ctor = getConstructorOf(this);
- ctor = ctor === null ? Module : ctor;
-
- if (typeof depth === 'number' && depth < 0)
- return options.stylize(`[${ctor.name}]`, 'special');
-
- const o = Object.create({ constructor: ctor });
- o.status = this.status;
- o.linkingStatus = this.linkingStatus;
- o.url = this.url;
- o.context = this.context;
- return require('util').inspect(o, options);
- }
-}
-
-module.exports = {
- Module,
- initImportMetaMap,
- wrapToModuleMap
-};