2
2
3
3
const {
4
4
ArrayPrototypeForEach,
5
+ ObjectGetPrototypeOf,
6
+ SyntaxErrorPrototype,
5
7
} = primordials ;
6
8
7
9
const {
@@ -24,12 +26,36 @@ const {
24
26
ERR_INVALID_ARG_TYPE ,
25
27
} = require ( 'internal/errors' ) . codes ;
26
28
29
+ /**
30
+ * Checks if the given object is a context object.
31
+ * @param {object } object - The object to check.
32
+ * @returns {boolean } - Returns true if the object is a context object, else false.
33
+ */
27
34
function isContext ( object ) {
28
35
validateObject ( object , 'object' , kValidateObjectAllowArray ) ;
29
36
30
37
return _isContext ( object ) ;
31
38
}
32
39
40
+ /**
41
+ * Compiles a function from the given code string.
42
+ * @param {string } code - The code string to compile.
43
+ * @param {string[] } [params] - An optional array of parameter names for the compiled function.
44
+ * @param {object } [options] - An optional object containing compilation options.
45
+ * @param {string } [options.filename=''] - The filename to use for the compiled function.
46
+ * @param {number } [options.columnOffset=0] - The column offset to use for the compiled function.
47
+ * @param {number } [options.lineOffset=0] - The line offset to use for the compiled function.
48
+ * @param {Buffer } [options.cachedData=undefined] - The cached data to use for the compiled function.
49
+ * @param {boolean } [options.produceCachedData=false] - Whether to produce cached data for the compiled function.
50
+ * @param {ReturnType<import('vm').createContext } [options.parsingContext=undefined] - The parsing context to use for the compiled function.
51
+ * @param {object[] } [options.contextExtensions=[]] - An array of context extensions to use for the compiled function.
52
+ * @param {import('internal/modules/esm/utils').ImportModuleDynamicallyCallback } [options.importModuleDynamically] -
53
+ * A function to use for dynamically importing modules.
54
+ * @param {boolean } [options.shouldThrowOnError=true] - Whether to throw an error if the code contains syntax errors.
55
+ * @returns {Object } An object containing the compiled function and any associated data.
56
+ * @throws {TypeError } If any of the arguments are of the wrong type.
57
+ * @throws {ERR_INVALID_ARG_TYPE } If the parsing context is not a valid context object.
58
+ */
33
59
function internalCompileFunction ( code , params , options ) {
34
60
validateString ( code , 'code' ) ;
35
61
if ( params !== undefined ) {
@@ -45,6 +71,7 @@ function internalCompileFunction(code, params, options) {
45
71
parsingContext = undefined ,
46
72
contextExtensions = [ ] ,
47
73
importModuleDynamically,
74
+ shouldThrowOnError = true ,
48
75
} = options ;
49
76
50
77
validateString ( filename , 'options.filename' ) ;
@@ -71,6 +98,7 @@ function internalCompileFunction(code, params, options) {
71
98
const name = `options.contextExtensions[${ i } ]` ;
72
99
validateObject ( extension , name , kValidateObjectAllowNullable ) ;
73
100
} ) ;
101
+ validateBoolean ( shouldThrowOnError , 'options.shouldThrowOnError' ) ;
74
102
75
103
const result = compileFunction (
76
104
code ,
@@ -82,8 +110,14 @@ function internalCompileFunction(code, params, options) {
82
110
parsingContext ,
83
111
contextExtensions ,
84
112
params ,
113
+ shouldThrowOnError ,
85
114
) ;
86
115
116
+ // If we're not supposed to throw on errors, and compilation errored, then return the error.
117
+ if ( ! shouldThrowOnError && result != null && ObjectGetPrototypeOf ( result ) === SyntaxErrorPrototype ) {
118
+ return result ;
119
+ }
120
+
87
121
if ( produceCachedData ) {
88
122
result . function . cachedDataProduced = result . cachedDataProduced ;
89
123
}
0 commit comments