@@ -24,12 +24,36 @@ const {
24
24
ERR_INVALID_ARG_TYPE ,
25
25
} = require ( 'internal/errors' ) . codes ;
26
26
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
+ */
27
32
function isContext ( object ) {
28
33
validateObject ( object , 'object' , kValidateObjectAllowArray ) ;
29
34
30
35
return _isContext ( object ) ;
31
36
}
32
37
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
+ */
33
57
function internalCompileFunction ( code , params , options ) {
34
58
validateString ( code , 'code' ) ;
35
59
if ( params !== undefined ) {
@@ -45,6 +69,7 @@ function internalCompileFunction(code, params, options) {
45
69
parsingContext = undefined ,
46
70
contextExtensions = [ ] ,
47
71
importModuleDynamically,
72
+ shouldThrowOnError = true ,
48
73
} = options ;
49
74
50
75
validateString ( filename , 'options.filename' ) ;
@@ -71,6 +96,7 @@ function internalCompileFunction(code, params, options) {
71
96
const name = `options.contextExtensions[${ i } ]` ;
72
97
validateObject ( extension , name , kValidateObjectAllowNullable ) ;
73
98
} ) ;
99
+ validateBoolean ( shouldThrowOnError , 'options.shouldThrowOnError' ) ;
74
100
75
101
const result = compileFunction (
76
102
code ,
@@ -82,8 +108,14 @@ function internalCompileFunction(code, params, options) {
82
108
parsingContext ,
83
109
contextExtensions ,
84
110
params ,
111
+ shouldThrowOnError ,
85
112
) ;
86
113
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
+
87
119
if ( produceCachedData ) {
88
120
result . function . cachedDataProduced = result . cachedDataProduced ;
89
121
}
0 commit comments