R7b batch 4 #2 (docs/design/lua-luau-kernel-port-checklist.md is the authoritative quirk list). ONE walker for both dialects (ccpp precedent) — the differences are exactly four: luau's type_definition aliases, the `export `-slice isExported hook, the return-type signature suffix, and the grammar handle. Grammar prep is kernel-side only, no wasm change: lua is the SECOND vendored-grammar-C language (the vendored wasm is the v0.4.1 tag, a revision not on crates.io — tag artifacts compiled via build.rs, shas pinned); luau is a plain crate pin =1.2.0 whose tarball is sha-identical to the tag (the swift tag≠crate divergence does not recur). Grammar-parity rows replace the bump gate entirely. Preserved bug-for-bug (all probe-pinned): the require/visitNode-hook ASYMMETRIES (top-level requires — including inside top-level if/for/while — mint import nodes while the identical body-level statement emits `calls "require"`; top-level `local x = foo()` initializers are invisible while global `x = foo()` calls emit), the BFS string-win inside require args (`require(script:WaitForChild("Kid"))` → import Kid) and Roblox instance paths, receiver-QN methods (`M.sub.deep::chained`, `_G::installed`, stack-QN nested globals like `render::leakedGlobal`), the raw-text callee world (colon forms with `self` never stripped, bracket callees, newline-glued chains byte-verbatim, the `(handler)` paren-conversion), LUA_SPEC function-as-value capture with the `M.cb = cb` param-storage skip and first-occurrence dedupe, LuaDoc `---` keeping a leading `- ` plus `--!strict` joining docstring chains (block-comment docstrings keep interior CRLF bytes), variable nodes at the IDENTIFIER with positional value pairing, duplicate same-(kind,name,line) ids, and the lua↔luau isExported wire divergence (lua functions: flag absent; luau functions: present-false; methods: absent in both; variables: present-false in both; `export type`: true). Gates: parity sweeps first-run 0-diff on kong/lazy.nvim/lua-resty-core (lua) + lune/Fusion (luau) — 1,734 clean files byte-parity, deferrals 1/0/0/3/8 matching the survey's both-arm predictions exactly (kong's 1 = a deliberately invalid fixture; luau's = grammar-inherent generic type packs and default type params); full-init dumps byte-identical kernel-vs-wasm ×4 (kong 157,650 dump lines); kernel-lua-parity suite (both torture fixtures + in-memory CRLF variants + glue-chain, duplicate-id, and cross-dialect defer pins + kernel-arm wire-flag pins); full suite 2,647 green ×2 with CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += lua, luau (18 langs). Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
71 lines
3.1 KiB
TypeScript
71 lines
3.1 KiB
TypeScript
/**
|
|
* Grammar-source parity gate (R1, migration plan §3.5).
|
|
*
|
|
* The native kernel compiles grammars from crates.io / vendored sources; the
|
|
* wasm fallback loads grammars from tree-sitter-wasms / src/extraction/wasm.
|
|
* If the two are built from different grammar revisions, a language's graph
|
|
* would depend on WHICH path extracted it — per-language routing (and the
|
|
* kernel-absent fallback) must be graph-neutral.
|
|
*
|
|
* Rather than trusting version metadata, this asserts the grammars are
|
|
* behaviorally identical where extraction can observe them: ABI version and
|
|
* the full node-kind and field tables, compared id by id.
|
|
*
|
|
* Runs wherever a kernel binary is staged (scripts/build-kernel.sh); skips
|
|
* otherwise. CI that builds the kernel sets CODEGRAPH_KERNEL_EXPECT=1 so the
|
|
* skip can't mask a missing build (asserted in kernel-scaffold.test.ts).
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import type { Language as WasmLanguage } from 'web-tree-sitter';
|
|
import { getKernel, resetKernelForTests } from '../src/extraction/kernel';
|
|
import { initGrammars, loadGrammarsForLanguages, getParser } from '../src/extraction/grammars';
|
|
import type { Language } from '../src/types';
|
|
|
|
const KERNEL_PATH = path.join(
|
|
__dirname,
|
|
'..',
|
|
'codegraph-kernel',
|
|
'prebuilds',
|
|
`${process.platform}-${process.arch}`,
|
|
'codegraph-kernel.node'
|
|
);
|
|
const kernelBuilt = fs.existsSync(KERNEL_PATH);
|
|
|
|
// Every kernel-capable language. `jsx` shares the javascript grammar on BOTH
|
|
// paths (langs.rs mirrors WASM_GRAMMAR_FILES), so the distinct grammars are:
|
|
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust', 'csharp', 'ruby', 'php', 'swift', 'kotlin', 'r', 'lua', 'luau'];
|
|
|
|
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
|
|
beforeAll(async () => {
|
|
resetKernelForTests();
|
|
await initGrammars();
|
|
await loadGrammarsForLanguages(GRAMMAR_LANGUAGES);
|
|
});
|
|
|
|
it.each(GRAMMAR_LANGUAGES)('%s: node-kind and field tables are identical', (language) => {
|
|
const kernel = getKernel();
|
|
expect(kernel).not.toBeNull();
|
|
const native = kernel!.grammarInfo(language);
|
|
expect(native, `kernel has no grammar for ${language}`).not.toBeNull();
|
|
|
|
const wasmLang = getParser(language)?.language as WasmLanguage | null | undefined;
|
|
expect(wasmLang, `wasm grammar for ${language} not loaded`).toBeTruthy();
|
|
|
|
expect(native!.abiVersion, 'grammar ABI version').toBe(wasmLang!.abiVersion);
|
|
expect(native!.nodeKindCount, 'node-kind count').toBe(wasmLang!.nodeTypeCount);
|
|
expect(native!.fieldCount, 'field count').toBe(wasmLang!.fieldCount);
|
|
|
|
const wasmKinds: (string | null)[] = [];
|
|
for (let i = 0; i < wasmLang!.nodeTypeCount; i++) wasmKinds.push(wasmLang!.nodeTypeForId(i));
|
|
expect(native!.nodeKinds).toEqual(wasmKinds.map((k) => k ?? ''));
|
|
|
|
// Field ids are 1-based on both sides.
|
|
const wasmFields: (string | null)[] = [];
|
|
for (let i = 1; i <= wasmLang!.fieldCount; i++) wasmFields.push(wasmLang!.fieldNameForId(i));
|
|
expect(native!.fieldNames).toEqual(wasmFields.map((f) => f ?? ''));
|
|
});
|
|
});
|