node/lib/internal/v8_prof_processor.js
Joyee Cheung e1fa85133f
src: implement natives binding without special casing
This patch removes special case in the internal binding loader
for natives, and implements it using the builtins internal
binding. Internally we do not actually need the natives binding,
so implement it as a legacy wrapper instead.

PR-URL: https://github.com/nodejs/node/pull/48186
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-06-09 15:32:55 +02:00

55 lines
1.6 KiB
JavaScript

'use strict';
const {
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
StringPrototypeSlice,
} = primordials;
const Buffer = require('buffer').Buffer;
const console = require('internal/console/global');
const vm = require('vm');
const { SourceTextModule } = require('internal/vm/module');
const { natives } = internalBinding('builtins');
async function linker(specifier, referencingModule) {
// Transform "./file.mjs" to "file"
const file = StringPrototypeSlice(specifier, 2, -4);
const code = natives[`internal/deps/v8/tools/${file}`];
return new SourceTextModule(code, { context: referencingModule.context });
}
(async () => {
const tickArguments = [];
if (process.platform === 'darwin') {
ArrayPrototypePush(tickArguments, '--mac');
} else if (process.platform === 'win32') {
ArrayPrototypePush(tickArguments, '--windows');
}
ArrayPrototypePushApply(tickArguments,
ArrayPrototypeSlice(process.argv, 1));
const context = vm.createContext({
arguments: tickArguments,
write(s) { process.stdout.write(s); },
printErr(err) { console.error(err); },
console,
process,
Buffer,
});
const polyfill = natives['internal/v8_prof_polyfill'];
const script = `(function(module, require) {
${polyfill}
})`;
vm.runInContext(script, context)(module, require);
const tickProcessor = natives['internal/deps/v8/tools/tickprocessor-driver'];
const tickprocessorDriver = new SourceTextModule(tickProcessor, { context });
await tickprocessorDriver.link(linker);
await tickprocessorDriver.evaluate();
})();