Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

21
test/fixtures/snapshot/check-marked.js vendored Normal file
View File

@ -0,0 +1,21 @@
'use strict';
let marked;
if (process.env.NODE_TEST_USE_SNAPSHOT === 'true') {
console.error('NODE_TEST_USE_SNAPSHOT true');
marked = globalThis.marked;
} else {
console.error('NODE_TEST_USE_SNAPSHOT false');
marked = require('./marked');
}
const md = `
# heading
[link][1]
[1]: #heading "heading"
`;
const html = marked(md)
console.log(html);

View File

@ -0,0 +1,6 @@
'use strict';
const fs = require('fs');
const assert = require('assert');
assert.strictEqual(fs.foo, 'I am from the snapshot');

View File

@ -0,0 +1,26 @@
'use strict';
const {
setDeserializeMainFunction,
isBuildingSnapshot
} = require('v8').startupSnapshot;
function spawn() {
const { spawnSync, execFileSync, execSync } = require('child_process');
spawnSync(process.execPath, [ __filename, 'spawnSync' ], { stdio: 'inherit' });
if (!process.env.DIRNAME_CONTAINS_SHELL_UNSAFE_CHARS)
execSync(`"${process.execPath}" "${__filename}" "execSync"`, { stdio: 'inherit' });
execFileSync(process.execPath, [ __filename, 'execFileSync' ], { stdio: 'inherit' });
}
if (process.argv[2] !== undefined) {
console.log('From child process', process.argv[2]);
} else {
spawn();
}
if (isBuildingSnapshot()) {
setDeserializeMainFunction(() => {
spawn();
});
}

9
test/fixtures/snapshot/console.js vendored Normal file
View File

@ -0,0 +1,9 @@
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
console.log(JSON.stringify(Object.keys(console), null, 2));
setDeserializeMainFunction(() => {
console.log(JSON.stringify(Object.keys(console), null, 2));
});

View File

@ -0,0 +1,20 @@
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
const assert = require('assert');
setDeserializeMainFunction(() => {
const vm = require('vm');
const { Worker } = require('worker_threads');
assert.strictEqual(vm.runInNewContext('21+21'), 42);
const worker = new Worker(
'require("worker_threads").parentPort.postMessage({value: 21 + 21})',
{ eval: true });
const messages = [];
worker.on('message', message => messages.push(message));
process.on('beforeExit', () => {
assert.deepStrictEqual(messages, [{value:42}]);
})
});

16
test/fixtures/snapshot/cwd.js vendored Normal file
View File

@ -0,0 +1,16 @@
const {
addSerializeCallback,
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
// To make sure the cwd is present in the cache
process.cwd();
// Also access it from a serialization callback once
addSerializeCallback(() => {
process.cwd();
});
setDeserializeMainFunction(() => {
console.log(process.cwd());
});

View File

@ -0,0 +1,20 @@
'use strict';
const zlib = require('zlib');
const fs = require('fs');
const assert = require('assert');
const fixture = process.env.NODE_TEST_FIXTURE;
const mode = process.env.NODE_TEST_MODE;
const file = fs.readFileSync(fixture);
const result = zlib.gunzipSync(file);
console.log(`Result length = ${result.byteLength}`);
console.log('NODE_TEST_MODE:', mode);
if (mode === 'snapshot') {
globalThis.NODE_TEST_DATA = result;
} else if (mode === 'verify') {
assert.deepStrictEqual(globalThis.NODE_TEST_DATA, result);
} else {
assert.fail('Unknown mode');
}

39
test/fixtures/snapshot/dns-lookup.js vendored Normal file
View File

@ -0,0 +1,39 @@
'use strict';
const dns = require('dns');
const assert = require('assert');
assert(process.env.NODE_TEST_HOST);
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
function onError(err) {
console.error('error:', err);
}
function onLookup(address, family) {
console.log(`address: ${JSON.stringify(address)}`);
console.log(`family: ${JSON.stringify(family)}`);
}
function query() {
const host = process.env.NODE_TEST_HOST;
if (process.env.NODE_TEST_PROMISE === 'true') {
dns.promises.lookup(host, { family: 4 }).then(
({address, family}) => onLookup(address, family),
onError);
} else {
dns.lookup(host, { family: 4 }, (err, address, family) => {
if (err) {
onError(err);
} else {
onLookup(address, family);
}
});
}
}
query();
setDeserializeMainFunction(query);

59
test/fixtures/snapshot/dns-resolve.js vendored Normal file
View File

@ -0,0 +1,59 @@
'use strict';
const dns = require('dns');
const assert = require('assert');
assert(process.env.NODE_TEST_HOST);
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
function onError(err) {
console.error('error:', err);
}
function onResolve(addresses) {
console.log(`addresses: ${JSON.stringify(addresses)}`);
}
function onReverse(hostnames) {
console.log(`hostnames: ${JSON.stringify(hostnames)}`);
}
function query() {
if (process.env.NODE_TEST_DNS) {
dns.setServers([process.env.NODE_TEST_DNS])
}
const host = process.env.NODE_TEST_HOST;
if (process.env.NODE_TEST_PROMISE === 'true') {
dns.promises.resolve4(host).then(onResolve, onError);
} else {
dns.resolve4(host, (err, addresses) => {
if (err) {
onError(err);
} else {
onResolve(addresses);
}
});
}
const ip = process.env.NODE_TEST_IP;
if (ip) {
if (process.env.NODE_TEST_PROMISE === 'true') {
dns.promises.reverse(ip).then(onReverse, onError);
} else {
dns.reverse(ip, (err, hostnames) => {
if (err) {
onError(err);
} else {
onReverse(hostnames);
}
});
}
}
}
query();
setDeserializeMainFunction(query);

12
test/fixtures/snapshot/echo-args.js vendored Normal file
View File

@ -0,0 +1,12 @@
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
const originalArgv = [...process.argv];
setDeserializeMainFunction(() => {
console.log(JSON.stringify({
currentArgv: process.argv,
originalArgv
}));
});

24
test/fixtures/snapshot/error-stack.js vendored Normal file
View File

@ -0,0 +1,24 @@
const {
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
console.log(`During snapshot building, Error.stackTraceLimit =`, Error.stackTraceLimit);
console.log(getError('During snapshot building', 30));
setDeserializeMainFunction(() => {
console.log(`After snapshot deserialization, Error.stackTraceLimit =`, Error.stackTraceLimit);
console.log(getError('After snapshot deserialization', 30));
});
function getError(message, depth) {
let counter = 1;
function recurse() {
if (counter++ < depth) {
return recurse();
}
const error = new Error(message);
return error.stack;
}
return recurse();
}

1
test/fixtures/snapshot/error.js vendored Normal file
View File

@ -0,0 +1 @@
throw new Error('test');

2970
test/fixtures/snapshot/marked.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,44 @@
const {
addSerializeCallback,
setDeserializeMainFunction,
} = require('v8').startupSnapshot;
const assert = require('assert');
if (process.env.TEST_IN_SERIALIZER) {
addSerializeCallback(checkMutate);
} else {
checkMutate();
}
function checkMutate() {
// Check that mutation to Error.stackTraceLimit is effective in the snapshot
// builder script.
assert.strictEqual(typeof Error.stackTraceLimit, 'number');
Error.stackTraceLimit = 0;
assert.strictEqual(getError('', 30), 'Error');
}
setDeserializeMainFunction(() => {
// Check that the mutation is preserved in the deserialized main function.
assert.strictEqual(Error.stackTraceLimit, 0);
assert.strictEqual(getError('', 30), 'Error');
// Check that it can still be mutated.
Error.stackTraceLimit = 10;
const error = getError('', 30);
const matches = [...error.matchAll(/at recurse/g)];
assert.strictEqual(matches.length, 10);
});
function getError(message, depth) {
let counter = 1;
function recurse() {
if (counter++ < depth) {
return recurse();
}
const error = new Error(message);
return error.stack;
}
return recurse();
}

5
test/fixtures/snapshot/mutate-fs.js vendored Normal file
View File

@ -0,0 +1,5 @@
'use strict';
const fs = require('fs');
fs.foo = 'I am from the snapshot';

60
test/fixtures/snapshot/server.js vendored Normal file
View File

@ -0,0 +1,60 @@
'use strict';
const net = require('net');
const {
setDeserializeMainFunction
} = require('v8').startupSnapshot;
const dc = require('diagnostics_channel');
const echoServer = net.Server(function(connection) {
connection.on('data', function(chunk) {
connection.write(chunk);
});
connection.on('end', function() {
connection.end();
});
});
const kNumChars = 256;
const buffer = new Uint8Array(kNumChars);
for (let i = 0; i < kNumChars; ++i) {
buffer[i] = i;
}
let recv = '';
echoServer.on('listening', function() {
const port = this.address().port;
console.log(`server port`, port);
const c = net.createConnection({ host: '127.0.0.1', port });
c.on('data', function(chunk) {
recv += chunk.toString('latin1');
if (recv.length === buffer.length) {
c.end();
}
});
c.on('connect', function() {
c.write(buffer);
});
c.on('close', function() {
console.log(`recv.length: ${recv.length}`);
echoServer.close();
});
});
dc.subscribe('net.server.socket', (({ socket }) => {
console.log(`From server diagnostics channel:`, socket.localPort);
}));
dc.subscribe('net.client.socket', (({ socket }) => {
console.log(`From client diagnostics channel`);
}));
setDeserializeMainFunction(() => {
echoServer.listen(0);
});

8
test/fixtures/snapshot/ts-example.js vendored Normal file
View File

@ -0,0 +1,8 @@
var VirtualPoint = /** @class */ (function () {
function VirtualPoint(x, y) {
this.x = x;
this.y = y;
}
return VirtualPoint;
}());
var newVPoint = new VirtualPoint(13, 56);

11
test/fixtures/snapshot/ts-example.ts vendored Normal file
View File

@ -0,0 +1,11 @@
class VirtualPoint {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const newVPoint = new VirtualPoint(13, 56);

View File

@ -0,0 +1,28 @@
// This file is to be concatenated with
// https://github.com/microsoft/TypeScript/blob/main/lib/typescript.js
// to produce a snapshot that reads a file path from the command
// line and compile it into JavaScript, then write the
// result into another file.
const fs = require('fs');
const v8 = require('v8');
const assert = require('assert');
v8.startupSnapshot.setDeserializeMainFunction(( { ts }) => {
const input = process.argv[1];
const output = process.argv[2];
console.error(`Compiling ${input} to ${output}`);
assert(input);
assert(output);
const source = fs.readFileSync(input, 'utf8');
let result = ts.transpileModule(
source,
{
compilerOptions: {
module: ts.ModuleKind.CommonJS
}
});
fs.writeFileSync(output, result.outputText, 'utf8');
}, { ts });

168725
test/fixtures/snapshot/typescript.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,61 @@
'use strict';
const fs = require('node:fs');
const zlib = require('node:zlib');
const path = require('node:path');
const assert = require('node:assert');
const v8 = require('node:v8');
class BookShelf {
storage = new Map();
// Reading a series of files from directory and store them into storage.
constructor(directory, books) {
for (const book of books) {
this.storage.set(book, fs.readFileSync(path.join(directory, book)));
};
}
static compressAll(shelf) {
for (const [ book, content ] of shelf.storage) {
shelf.storage.set(book, zlib.gzipSync(content));
}
}
static decompressAll(shelf) {
for (const [ book, content ] of shelf.storage) {
shelf.storage.set(book, zlib.gunzipSync(content));
}
}
}
// __dirname here is where the snapshot script is placed
// during snapshot building time.
const shelf = new BookShelf(__dirname, [
'book1.en_US.txt',
'book1.es_ES.txt',
'book2.zh_CN.txt',
]);
assert(v8.startupSnapshot.isBuildingSnapshot());
// On snapshot serialization, compress the books to reduce size.
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
// On snapshot deserialization, decompress the books.
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
// process.env and process.argv are refreshed during snapshot
// deserialization.
const lang = process.env.BOOK_LANG || 'en_US';
const book = process.argv[1];
const name = `${book}.${lang}.txt`;
console.error('Reading', name);
console.log(shelf.storage.get(name).toString());
}, shelf);
assert.throws(() => v8.startupSnapshot.setDeserializeMainFunction(() => {
assert.fail('unreachable duplicated main function');
}), {
code: 'ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION',
});

1
test/fixtures/snapshot/warning.js vendored Normal file
View File

@ -0,0 +1 @@
process.emitWarning('test warning');

View File

@ -0,0 +1,30 @@
'use strict';
const { WeakReference } = require('internal/util');
const {
setDeserializeMainFunction
} = require('v8').startupSnapshot
let obj = { hello: 'world' };
const ref = new WeakReference(obj);
let gcCount = 0;
let maxGC = 10;
function run() {
globalThis.gc();
setImmediate(() => {
gcCount++;
if (ref.get() === undefined) {
return;
} else if (gcCount < maxGC) {
run();
} else {
throw new Error(`Reference is still around after ${maxGC} GC`);
}
});
}
setDeserializeMainFunction(() => {
obj = null;
run();
});

View File

@ -0,0 +1,14 @@
'use strict';
const { WeakReference } = require('internal/util');
const {
setDeserializeMainFunction
} = require('v8').startupSnapshot
const assert = require('assert');
let obj = { hello: 'world' };
const ref = new WeakReference(obj);
setDeserializeMainFunction(() => {
assert.strictEqual(ref.get(), obj);
});

5
test/fixtures/snapshot/worker.js vendored Normal file
View File

@ -0,0 +1,5 @@
'use strict';
const { Worker } = require('worker_threads');
new Worker('1', { eval: true });