1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// hover declarations and instructions in the source-only documents opened by f12.
// interpreted hosts (no jit) are far slower; ZC_TEST_TIMEOUT_SCALE stretches every deadline
const TIMEOUT_SCALE = Number(process.env.ZC_TEST_TIMEOUT_SCALE) || 1;
const assert = require('assert');
const fs = require('fs');
const cp = require('child_process');
const path = require('path');
const frame = m => { const b = Buffer.from(JSON.stringify(m)); return Buffer.concat([Buffer.from(`Content-Length: ${b.length}\r\n\r\n`), b]); };
const messages = [{ id: 1, method: 'initialize', params: {} }], checks = [];
let id = 2;
function doc(uri, text) {
messages.push({ method: 'textDocument/didOpen', params: { textDocument: { uri, version: 1, text } } });
return (needle, offset = 0) => {
const at = text.indexOf(needle) + offset; assert.ok(at >= 0, needle);
const before = text.slice(0, at), rid = id++;
messages.push({ id: rid, method: 'textDocument/hover', params: { textDocument: { uri }, position: { line: before.split('\n').length - 1, character: before.length - before.lastIndexOf('\n') - 1 } } });
return rid;
};
}
// the compiler is the files listed in src/compiler/parts, joined: the one document the server embeds
const compiler = fs.readFileSync('src/compiler/parts', 'utf8').split(/\r?\n/).filter(Boolean).map(n => fs.readFileSync('src/compiler/' + n, 'utf8')).join('');
const ask = doc('tinyvm:/compiler.zc', compiler);
checks.push([ask('ifrcap = NIF', 9), /const int NIF = 431/, /431/]);
checks.push([ask('capture_width = NCAP', 16), /const int NCAP = 32/, /initial capture capacity/, /32/]);
checks.push([ask('closure_cap = NCLO', 14), /const int NCLO = 8/, /initial closure nesting capacity/]);
checks.push([ask('clo_n = clo_n_initial', 8), /word\[NCLO\] clo_n_initial/]);
checks.push([ask('int nclo, closure_cap', 10), /\(int\) let closure_cap/, /value at this position:[\s\S]*\n8\n/]);
const rt = doc('tinyvm:/rt.zc', fs.readFileSync('src/runtime/rt.zc', 'utf8'));
checks.push([rt('SYS_CLOSE = 2'), /const int SYS_CLOSE = 2/]);
const fixture = doc('tinyvm:/fixture.zc', 'const int FIRST = 1, SECOND = 2;\nword[4] backing; word active = backing;\nint x = SECOND;\n');
checks.push([fixture('x = SECOND', 4), /const int SECOND = 2/]);
checks.push([fixture('active = backing', 9), /word\[4\] backing/]);
const mnemonic = 'q@!$_~cjzr,?.+-*&|^><%';
for (const op of mnemonic) {
const get = doc('tinyvm:/asm-' + op.charCodeAt(0) + '.zc', `fn f() { asm { ${op} } return 0; }`);
checks.push([get(`{ ${op}`, 2), /opcode \d+/, /(stack:|return stack|data stack)/]);
}
const special = doc('tinyvm:/special.zc', 'fn f() { asm { #1224 } }\ninclude std;\n');
checks.push([special('#1224'), /load byte/, /byte address/]);
checks.push([special('1224'), /load byte/]);
checks.push([special('std;'), /include std;/, /embedded standard library/, /nothing imports it automatically/]);
const normal = doc('file:///ordinary.zc', 'fn main() { int std = 7; int c = 2; return std + c; }');
checks.push([normal('std +'), /\(int\) let std/, /value at this position/]);
checks.push([normal('c;'), /\(int\) let c/]);
messages.push({ method: 'exit' });
let out;
if (process.argv[2] === '--wasm' || process.argv[2] === '--vm') {
const wasm = process.argv[2] === '--wasm', host = require(path.resolve('editors/vscode/client', wasm ? 'wasmproc.js' : 'vmproc.js'));
const p = (wasm ? host.wasmProcess : host.vmProcess)(path.resolve(process.argv[3]), { log: () => {} });
const chunks = []; p.stdout.on('data', d => chunks.push(d));
for (const m of messages) p.stdin.write(frame({ jsonrpc: '2.0', ...m }));
out = Buffer.concat(chunks);
} else {
const r = cp.spawnSync(process.argv[2], process.argv.slice(3), { input: Buffer.concat(messages.map(m => frame({ jsonrpc: '2.0', ...m }))), timeout: 60000 * TIMEOUT_SCALE, maxBuffer: 32 << 20 });
assert.strictEqual(r.status, 0, String(r.stderr)); out = r.stdout;
}
const replies = new Map();
while (out.length) {
const e = out.indexOf('\r\n\r\n'), n = Number(/Content-Length:\s*(\d+)/i.exec(out.subarray(0, e).toString())[1]);
const m = JSON.parse(out.subarray(e + 4, e + 4 + n)); replies.set(m.id, m); out = out.subarray(e + 4 + n);
}
for (const [rid, ...patterns] of checks) {
const value = replies.get(rid)?.result?.contents?.value || '';
for (const pattern of patterns) assert.match(value, pattern, `hover #${rid}: ${value}`);
}
assert.ok(!replies.get(checks[2][0]).result.contents.value.includes('closures being compiled'), 'the section overview is not NCLO documentation');
assert.ok(!replies.get(checks[checks.length - 1][0]).result.contents.value.includes('opcode'), 'ordinary c is a variable');
console.log('ok embedded declaration hovers, constant values, asm instructions and explicit std');
|