From 606523d37ed6dad9621f80b1238f46f9f17e736e Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Sun, 30 Apr 2023 00:50:42 +0800 Subject: [PATCH] v8: fix ERR_NOT_BUILDING_SNAPSHOT is not a constructor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/47721 Reviewed-By: Joyee Cheung Reviewed-By: Tobias Nießen --- lib/internal/v8/startup_snapshot.js | 6 +++-- .../snapshot/v8-startup-snapshot-api.js | 5 ++++ test/parallel/test-v8-startup-snapshot-api.js | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-v8-startup-snapshot-api.js diff --git a/lib/internal/v8/startup_snapshot.js b/lib/internal/v8/startup_snapshot.js index 5d88748b1c4719..49623627706bc0 100644 --- a/lib/internal/v8/startup_snapshot.js +++ b/lib/internal/v8/startup_snapshot.js @@ -4,8 +4,10 @@ const { validateFunction, } = require('internal/validators'); const { - ERR_NOT_BUILDING_SNAPSHOT, - ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION, + codes: { + ERR_NOT_BUILDING_SNAPSHOT, + ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION, + }, } = require('internal/errors'); const { diff --git a/test/fixtures/snapshot/v8-startup-snapshot-api.js b/test/fixtures/snapshot/v8-startup-snapshot-api.js index f41f519755f45e..9eccc655990572 100644 --- a/test/fixtures/snapshot/v8-startup-snapshot-api.js +++ b/test/fixtures/snapshot/v8-startup-snapshot-api.js @@ -30,3 +30,8 @@ addDeserializeCallback(({ filePath }) => { setDeserializeMainFunction(({ filePath }) => { console.log(storage[filePath].toString()); }, { filePath }); +assert.throws(() => setDeserializeMainFunction(() => { + assert.fail('unreachable duplicated main function'); +}), { + code: 'ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION', +}); diff --git a/test/parallel/test-v8-startup-snapshot-api.js b/test/parallel/test-v8-startup-snapshot-api.js new file mode 100644 index 00000000000000..c3738911540fc1 --- /dev/null +++ b/test/parallel/test-v8-startup-snapshot-api.js @@ -0,0 +1,26 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const { + isBuildingSnapshot, + addSerializeCallback, + addDeserializeCallback, + setDeserializeMainFunction +} = require('v8').startupSnapshot; + +// This test verifies that the v8.startupSnapshot APIs are not available when +// it is not building snapshot. + +assert(!isBuildingSnapshot()); + +assert.throws(() => addSerializeCallback(() => {}), { + code: 'ERR_NOT_BUILDING_SNAPSHOT', +}); +assert.throws(() => addDeserializeCallback(() => {}), { + code: 'ERR_NOT_BUILDING_SNAPSHOT', +}); +assert.throws(() => setDeserializeMainFunction(() => {}), { + code: 'ERR_NOT_BUILDING_SNAPSHOT', +});