From c5f2954cb3482a5fe6613fe8645db555879442dc Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 26 Feb 2024 18:14:01 +0100 Subject: [PATCH] feat: add ability to specify node::SnapshotFlags values MONGOSH-1605 --- resources/main-template.cc | 16 +++++++++++++++- src/index.ts | 9 +++++++++ test/index.ts | 3 ++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/resources/main-template.cc b/resources/main-template.cc index b04c19b..a45b277 100644 --- a/resources/main-template.cc +++ b/resources/main-template.cc @@ -17,6 +17,7 @@ #include #include #endif +#include // injected code may refer to std::underlying_type using namespace node; using namespace v8; @@ -33,6 +34,11 @@ using namespace v8; #define NODE_VERSION_SUPPORTS_EMBEDDER_SNAPSHOT 1 #endif +// Snapshot config is supported since https://github.com/nodejs/node/pull/50453 +#if NODE_VERSION_AT_LEAST(21, 6, 0) && !defined(BOXEDNODE_SNAPSHOT_CONFIG_FLAGS) +#define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (SnapshotFlags::kWithoutCodeCache) +#endif + // 18.1.0 is the current minimum version that has https://github.com/nodejs/node/pull/42809, // which introduced crashes when using workers, and later 18.9.0 is the current // minimum version to contain https://github.com/nodejs/node/pull/44252, which @@ -167,7 +173,15 @@ static int RunNodeInstance(MultiIsolatePlatform* platform, int exit_code = 0; std::vector errors; std::unique_ptr setup = - CommonEnvironmentSetup::CreateForSnapshotting(platform, &errors, args, exec_args); + CommonEnvironmentSetup::CreateForSnapshotting( + platform, + &errors, + args, + exec_args +#ifdef BOXEDNODE_SNAPSHOT_CONFIG_FLAGS + , SnapshotConfig { BOXEDNODE_SNAPSHOT_CONFIG_FLAGS, std::nullopt } +#endif + ); Isolate* isolate = setup->isolate(); diff --git a/src/index.ts b/src/index.ts index b9b4d07..59f0ab6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -275,6 +275,7 @@ type CompilationOptions = { useLegacyDefaultUvLoop?: boolean; useCodeCache?: boolean, useNodeSnapshot?: boolean, + nodeSnapshotConfigFlags?: string[], // e.g. 'WithoutCodeCache' executableMetadata?: ExecutableMetadata, preCompileHook?: (nodeSourceTree: string, options: CompilationOptions) => void | Promise } @@ -421,6 +422,14 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L if (snapshotMode === 'consume') { mainSource = `#define BOXEDNODE_CONSUME_SNAPSHOT 1\n${mainSource}`; } + if (options.nodeSnapshotConfigFlags) { + const flags = [ + '0', + ...options.nodeSnapshotConfigFlags.map(flag => + `static_cast::type>(SnapshotFlags::k${flag})`) + ].join(' | '); + mainSource = `#define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (static_cast(${flags}))\n${mainSource}`; + } await fs.writeFile(path.join(nodeSourcePath, 'src', 'node_main.cc'), mainSource); logger.stepCompleted(); diff --git a/test/index.ts b/test/index.ts index 2849974..725af8c 100644 --- a/test/index.ts +++ b/test/index.ts @@ -217,10 +217,11 @@ describe('basic functionality', () => { it('works with snapshot support', async function () { this.timeout(2 * 60 * 60 * 1000); // 2 hours await compileJSFileAsBinary({ - nodeVersionRange: 'v21.0.0-nightly20230801d396a041f7', + nodeVersionRange: '^21.6.2', sourceFile: path.resolve(__dirname, 'resources/snapshot-echo-args.js'), targetFile: path.resolve(__dirname, `resources/snapshot-echo-args${exeSuffix}`), useNodeSnapshot: true, + nodeSnapshotConfigFlags: ['WithoutCodeCache'], // the nightly path name is too long for Windows... tmpdir: process.platform === 'win32' ? path.join(os.tmpdir(), 'bn') : undefined });