v8: fix ERR_NOT_BUILDING_SNAPSHOT is not a constructor

PR-URL: https://github.com/nodejs/node/pull/47721
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Chengzhong Wu 2023-04-30 00:50:42 +08:00 committed by GitHub
parent 4c62042774
commit bddf341774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions

View File

@ -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 {

View File

@ -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',
});

View File

@ -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',
});