-
Notifications
You must be signed in to change notification settings - Fork 28.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: move options checks from C++ to JS #19822
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
const { internalBinding } = require('internal/bootstrap/loaders'); | ||
const { emitExperimentalWarning } = require('internal/util'); | ||
const { URL } = require('internal/url'); | ||
const { kParsingContext, isContext } = process.binding('contextify'); | ||
const { isContext } = process.binding('contextify'); | ||
const { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_OUT_OF_RANGE, | ||
ERR_VM_MODULE_ALREADY_LINKED, | ||
ERR_VM_MODULE_DIFFERENT_CONTEXT, | ||
ERR_VM_MODULE_LINKING_ERRORED, | ||
|
@@ -55,23 +56,26 @@ class 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); | ||
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); | ||
|
||
let context; | ||
if (options.context !== undefined) { | ||
if (typeof options.context !== 'object' || options.context === null) { | ||
throw new ERR_INVALID_ARG_TYPE('options.context', 'object', | ||
options.context); | ||
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(options.context)) { | ||
context = options.context; | ||
} else { | ||
throw new ERR_INVALID_ARG_TYPE('options.context', | ||
'vm.Context', options.context); | ||
if (!isContext(context)) { | ||
throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context', | ||
context); | ||
} | ||
} | ||
|
||
let url = options.url; | ||
let { url } = options; | ||
if (url !== undefined) { | ||
if (typeof url !== 'string') { | ||
throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url); | ||
|
@@ -88,22 +92,19 @@ class Module { | |
perContextModuleId.set(context, 1); | ||
} | ||
|
||
if (options.initializeImportMeta !== undefined) { | ||
if (typeof options.initializeImportMeta === 'function') { | ||
initImportMetaMap.set(this, options.initializeImportMeta); | ||
validateInteger(lineOffset, 'options.lineOffset'); | ||
validateInteger(columnOffset, 'options.columnOffset'); | ||
|
||
if (initializeImportMeta !== undefined) { | ||
if (typeof initializeImportMeta === 'function') { | ||
initImportMetaMap.set(this, initializeImportMeta); | ||
} else { | ||
throw new ERR_INVALID_ARG_TYPE( | ||
'options.initializeImportMeta', 'function', | ||
options.initializeImportMeta); | ||
'options.initializeImportMeta', 'function', initializeImportMeta); | ||
} | ||
} | ||
|
||
const wrap = new ModuleWrap(src, url, { | ||
[kParsingContext]: context, | ||
lineOffset: options.lineOffset, | ||
columnOffset: options.columnOffset | ||
}); | ||
|
||
const wrap = new ModuleWrap(src, url, context, lineOffset, columnOffset); | ||
wrapMap.set(this, wrap); | ||
linkingStatusMap.set(this, 'unlinked'); | ||
wrapToModuleMap.set(wrap, this); | ||
|
@@ -194,7 +195,25 @@ class Module { | |
wrap.instantiate(); | ||
} | ||
|
||
async evaluate(options) { | ||
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 if (!Number.isInteger(timeout) || timeout <= 0) { | ||
throw new ERR_INVALID_ARG_TYPE('options.timeout', 'a positive integer', | ||
timeout); | ||
} | ||
|
||
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 && | ||
|
@@ -204,7 +223,7 @@ class Module { | |
'must be one of instantiated, evaluated, and errored' | ||
); | ||
} | ||
const result = wrap.evaluate(options); | ||
const result = wrap.evaluate(timeout, breakOnSigint); | ||
return { result, __proto__: null }; | ||
} | ||
|
||
|
@@ -224,6 +243,15 @@ class Module { | |
} | ||
} | ||
|
||
function validateInteger(prop, propName) { | ||
if (!Number.isInteger(prop)) { | ||
throw new ERR_INVALID_ARG_TYPE(propName, 'integer', prop); | ||
} | ||
if ((prop >> 0) !== prop) { | ||
throw new ERR_OUT_OF_RANGE(propName, '32-bit integer', prop); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this not belong somewhere a little more generic perhaps? Somewhat surprising there isn't already something for this too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, especially since I copied this function from another file. I'll look into bringing together type validation functions in another PR if that's fine for you. |
||
|
||
module.exports = { | ||
Module, | ||
initImportMetaMap, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should timeout also run through validateInteger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check is a bit different.
timeout
may not be negative, while validateInteger allows negative values.