-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm.internalCompileFunction can now optionally return errors instead o…
…f throwing them
- Loading branch information
1 parent
3a37bc3
commit 6c1c2f5
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,36 @@ const { | |
ERR_INVALID_ARG_TYPE, | ||
} = require('internal/errors').codes; | ||
|
||
/** | ||
* Checks if the given object is a context object. | ||
* @param {object} object - The object to check. | ||
* @returns {boolean} - Returns true if the object is a context object, else false. | ||
*/ | ||
function isContext(object) { | ||
validateObject(object, 'object', kValidateObjectAllowArray); | ||
|
||
return _isContext(object); | ||
} | ||
|
||
/** | ||
* Compiles a function from the given code string. | ||
* @param {string} code - The code string to compile. | ||
* @param {string[]} [params] - An optional array of parameter names for the compiled function. | ||
* @param {object} [options] - An optional object containing compilation options. | ||
* @param {string} [options.filename=''] - The filename to use for the compiled function. | ||
* @param {number} [options.columnOffset=0] - The column offset to use for the compiled function. | ||
* @param {number} [options.lineOffset=0] - The line offset to use for the compiled function. | ||
* @param {Buffer} [options.cachedData=undefined] - The cached data to use for the compiled function. | ||
* @param {boolean} [options.produceCachedData=false] - Whether to produce cached data for the compiled function. | ||
* @param {ReturnType<import('vm').createContext} [options.parsingContext=undefined] - The parsing context to use for the compiled function. | ||
* @param {object[]} [options.contextExtensions=[]] - An array of context extensions to use for the compiled function. | ||
* @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback} [options.importModuleDynamically] - | ||
* A function to use for dynamically importing modules. | ||
* @param {boolean} [options.shouldThrowOnError=true] - Whether to throw an error if the code contains syntax errors. | ||
* @returns {Object} An object containing the compiled function and any associated data. | ||
Check warning on line 53 in lib/internal/vm.js GitHub Actions / lint-js-and-md
|
||
* @throws {TypeError} If any of the arguments are of the wrong type. | ||
* @throws {ERR_INVALID_ARG_TYPE} If the parsing context is not a valid context object. | ||
*/ | ||
function internalCompileFunction(code, params, options) { | ||
validateString(code, 'code'); | ||
if (params !== undefined) { | ||
|
@@ -45,6 +69,7 @@ function internalCompileFunction(code, params, options) { | |
parsingContext = undefined, | ||
contextExtensions = [], | ||
importModuleDynamically, | ||
shouldThrowOnError = true, | ||
} = options; | ||
|
||
validateString(filename, 'options.filename'); | ||
|
@@ -71,6 +96,7 @@ function internalCompileFunction(code, params, options) { | |
const name = `options.contextExtensions[${i}]`; | ||
validateObject(extension, name, kValidateObjectAllowNullable); | ||
}); | ||
validateBoolean(shouldThrowOnError, 'options.shouldThrowOnError'); | ||
|
||
const result = compileFunction( | ||
code, | ||
|
@@ -82,8 +108,14 @@ function internalCompileFunction(code, params, options) { | |
parsingContext, | ||
contextExtensions, | ||
params, | ||
shouldThrowOnError, | ||
); | ||
|
||
// If we're not supposed to throw on errors, and compilation errored, then return the error. | ||
if (!shouldThrowOnError && result != null && result instanceof Error) { | ||
return result; | ||
} | ||
|
||
if (produceCachedData) { | ||
result.function.cachedDataProduced = result.cachedDataProduced; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters