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,
|
2022-09-26 16:55:59 +00:00
|
|
|
validateUint32,
|
|
|
|
} = require('internal/validators');
|
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
} = require('internal/errors').codes;
|
|
|
|
|
|
|
|
function isContext(object) {
|
|
|
|
validateObject(object, 'object', { __proto__: null, allowArray: true });
|
|
|
|
|
|
|
|
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');
|
|
|
|
validateUint32(columnOffset, 'options.columnOffset');
|
|
|
|
validateUint32(lineOffset, 'options.lineOffset');
|
|
|
|
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',
|
|
|
|
parsingContext
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validateArray(contextExtensions, 'options.contextExtensions');
|
|
|
|
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
|
|
|
|
const name = `options.contextExtensions[${i}]`;
|
|
|
|
validateObject(extension, name, { __proto__: null, nullable: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = compileFunction(
|
|
|
|
code,
|
|
|
|
filename,
|
|
|
|
lineOffset,
|
|
|
|
columnOffset,
|
|
|
|
cachedData,
|
|
|
|
produceCachedData,
|
|
|
|
parsingContext,
|
|
|
|
contextExtensions,
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
2022-12-13 21:58:06 +00:00
|
|
|
const { setCallbackForWrap } = require('internal/modules/esm/utils');
|
|
|
|
setCallbackForWrap(result.cacheKey, {
|
2022-09-26 16:55:59 +00:00
|
|
|
importModuleDynamically: (s, _k, i) => wrapped(s, func, i),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
internalCompileFunction,
|
|
|
|
isContext,
|
|
|
|
};
|