@@ -24,12 +24,36 @@ const {
2424 ERR_INVALID_ARG_TYPE ,
2525} = require ( 'internal/errors' ) . codes ;
2626
27+ /**
28+ * Checks if the given object is a context object.
29+ * @param {object } object - The object to check.
30+ * @returns {boolean } - Returns true if the object is a context object, else false.
31+ */
2732function isContext ( object ) {
2833 validateObject ( object , 'object' , kValidateObjectAllowArray ) ;
2934
3035 return _isContext ( object ) ;
3136}
3237
38+ /**
39+ * Compiles a function from the given code string.
40+ * @param {string } code - The code string to compile.
41+ * @param {string[] } [params] - An optional array of parameter names for the compiled function.
42+ * @param {object } [options] - An optional object containing compilation options.
43+ * @param {string } [options.filename=''] - The filename to use for the compiled function.
44+ * @param {number } [options.columnOffset=0] - The column offset to use for the compiled function.
45+ * @param {number } [options.lineOffset=0] - The line offset to use for the compiled function.
46+ * @param {Buffer } [options.cachedData=undefined] - The cached data to use for the compiled function.
47+ * @param {boolean } [options.produceCachedData=false] - Whether to produce cached data for the compiled function.
48+ * @param {ReturnType<import('vm').createContext } [options.parsingContext=undefined] - The parsing context to use for the compiled function.
49+ * @param {object[] } [options.contextExtensions=[]] - An array of context extensions to use for the compiled function.
50+ * @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback } [options.importModuleDynamically] -
51+ * A function to use for dynamically importing modules.
52+ * @param {boolean } [options.shouldThrowOnError=true] - Whether to throw an error if the code contains syntax errors.
53+ * @returns {Object } An object containing the compiled function and any associated data.
54+ * @throws {TypeError } If any of the arguments are of the wrong type.
55+ * @throws {ERR_INVALID_ARG_TYPE } If the parsing context is not a valid context object.
56+ */
3357function internalCompileFunction ( code , params , options ) {
3458 validateString ( code , 'code' ) ;
3559 if ( params !== undefined ) {
@@ -45,6 +69,7 @@ function internalCompileFunction(code, params, options) {
4569 parsingContext = undefined ,
4670 contextExtensions = [ ] ,
4771 importModuleDynamically,
72+ shouldThrowOnError = true ,
4873 } = options ;
4974
5075 validateString ( filename , 'options.filename' ) ;
@@ -71,6 +96,7 @@ function internalCompileFunction(code, params, options) {
7196 const name = `options.contextExtensions[${ i } ]` ;
7297 validateObject ( extension , name , kValidateObjectAllowNullable ) ;
7398 } ) ;
99+ validateBoolean ( shouldThrowOnError , 'options.shouldThrowOnError' ) ;
74100
75101 const result = compileFunction (
76102 code ,
@@ -82,8 +108,14 @@ function internalCompileFunction(code, params, options) {
82108 parsingContext ,
83109 contextExtensions ,
84110 params ,
111+ shouldThrowOnError ,
85112 ) ;
86113
114+ // If we're not supposed to throw on errors, and compilation errored, then return the error.
115+ if ( ! shouldThrowOnError && result != null && result instanceof Error ) {
116+ return result ;
117+ }
118+
87119 if ( produceCachedData ) {
88120 result . function . cachedDataProduced = result . cachedDataProduced ;
89121 }
0 commit comments