2022-09-26 16:55:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
|
|
|
ArrayPrototypeForEach,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
|
|
|
compileFunction,
|
|
|
|
isContext: _isContext,
|
|
|
|
} = internalBinding('contextify');
|
|
|
|
const {
|
|
|
|
validateArray,
|
|
|
|
validateBoolean,
|
|
|
|
validateBuffer,
|
|
|
|
validateFunction,
|
|
|
|
validateObject,
|
|
|
|
validateString,
|
2023-01-01 10:50:49 +00:00
|
|
|
validateStringArray,
|
2023-10-02 12:56:39 +00:00
|
|
|
kValidateObjectAllowArray,
|
|
|
|
kValidateObjectAllowNullable,
|
2023-10-06 14:33:46 +00:00
|
|
|
validateInt32,
|
2022-09-26 16:55:59 +00:00
|
|
|
} = require('internal/validators');
|
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
} = require('internal/errors').codes;
|
|
|
|
|
|
|
|
function isContext(object) {
|
2023-10-02 12:56:39 +00:00
|
|
|
validateObject(object, 'object', kValidateObjectAllowArray);
|
2022-09-26 16:55:59 +00:00
|
|
|
|
|
|
|
return _isContext(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
function internalCompileFunction(code, params, options) {
|
|
|
|
validateString(code, 'code');
|
|
|
|
if (params !== undefined) {
|
2023-01-01 10:50:49 +00:00
|
|
|
validateStringArray(params, 'params');
|
2022-09-26 16:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
filename = '',
|
|
|
|
columnOffset = 0,
|
|
|
|
lineOffset = 0,
|
|
|
|
cachedData = undefined,
|
|
|
|
produceCachedData = false,
|
|
|
|
parsingContext = undefined,
|
|
|
|
contextExtensions = [],
|
|
|
|
importModuleDynamically,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
validateString(filename, 'options.filename');
|
2023-10-06 14:33:46 +00:00
|
|
|
validateInt32(columnOffset, 'options.columnOffset');
|
|
|
|
validateInt32(lineOffset, 'options.lineOffset');
|
2022-09-26 16:55:59 +00:00
|
|
|
if (cachedData !== undefined)
|
|
|
|
validateBuffer(cachedData, 'options.cachedData');
|
|
|
|
validateBoolean(produceCachedData, 'options.produceCachedData');
|
|
|
|
if (parsingContext !== undefined) {
|
|
|
|
if (
|
|
|
|
typeof parsingContext !== 'object' ||
|
|
|
|
parsingContext === null ||
|
|
|
|
!isContext(parsingContext)
|
|
|
|
) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.parsingContext',
|
|
|
|
'Context',
|
2023-02-14 17:45:16 +00:00
|
|
|
parsingContext,
|
2022-09-26 16:55:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validateArray(contextExtensions, 'options.contextExtensions');
|
|
|
|
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
|
|
|
|
const name = `options.contextExtensions[${i}]`;
|
2023-10-02 12:56:39 +00:00
|
|
|
validateObject(extension, name, kValidateObjectAllowNullable);
|
2022-09-26 16:55:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const result = compileFunction(
|
|
|
|
code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
|
|
|
parsingContext,
|
|
|
|
contextExtensions,
|
2023-02-14 17:45:16 +00:00
|
|
|
params,
|
2022-09-26 16:55:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (produceCachedData) {
|
|
|
|
result.function.cachedDataProduced = result.cachedDataProduced;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.cachedData) {
|
|
|
|
result.function.cachedData = result.cachedData;
|
|
|
|
}
|
|
|
|
|
2023-01-25 20:06:55 +00:00
|
|
|
if (typeof result.cachedDataRejected === 'boolean') {
|
|
|
|
result.function.cachedDataRejected = result.cachedDataRejected;
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:55:59 +00:00
|
|
|
if (importModuleDynamically !== undefined) {
|
|
|
|
validateFunction(importModuleDynamically,
|
|
|
|
'options.importModuleDynamically');
|
2022-12-13 21:58:06 +00:00
|
|
|
const { importModuleDynamicallyWrap } = require('internal/vm/module');
|
2022-09-26 16:55:59 +00:00
|
|
|
const wrapped = importModuleDynamicallyWrap(importModuleDynamically);
|
|
|
|
const func = result.function;
|
2023-06-21 14:01:50 +00:00
|
|
|
const { registerModule } = require('internal/modules/esm/utils');
|
|
|
|
registerModule(func, {
|
|
|
|
__proto__: null,
|
|
|
|
importModuleDynamically: wrapped,
|
2022-09-26 16:55:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
internalCompileFunction,
|
|
|
|
isContext,
|
|
|
|
};
|