feat(tracker): map script types through runner_map

Types follow CMC_RunnerFactory when the snapshot has it, so the shared
variable runner no longer forms a VARIABLE pseudo-type. Base CMC_Runner
verbs are listed once under * and referenced via inherits; type 0 becomes
the @ scope, matched against CodeParser.dispatchSpecialCall.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Patryk Gensch
2026-10-24 18:37:17 +02:00
co-authored by Claude Opus 5.5
parent 5c597a3a52
commit 87fcefa877
3 changed files with 185 additions and 33 deletions
+3 -2
View File
@@ -1,4 +1,5 @@
'use strict';
const MAPPING={explicit:'Jawne (klasa C++ z fabryki)',runner_factory:'CMC_RunnerFactory (typ → runner)',inferred:'Wywnioskowane z nazwy'};
const $ = id => document.getElementById(id);
const labels = {missing:'Brak rejestracji',registered:'Zarejestrowana',stub_candidate:'Podejrzenie stubu',partial:'Częściowa',implemented:'Zaimplementowana',unverified:'Niezweryfikowana',asm_reviewed:'Przegląd ASM',runtime_tested:'Test zachowania',stale:'Dowód wymaga aktualizacji'};
let data, selected = null;
@@ -22,7 +23,7 @@ function render(){
$('stats').replaceChildren(stat(methods.length,'par typ / metoda (także dziedziczone)'),stat(methods.filter(registered).length,'zarejestrowanych w emulatorze'),stat(methods.filter(m=>m.status==='implemented').length,'oznaczonych jako zaimplementowane'),stat(methods.filter(reviewed).length,'z aktualnym dowodem ASM / testu'));
$('types').replaceChildren();
for(const t of groups){const b=el('button',undefined,'type');b.type='button';b.setAttribute('aria-pressed',String(selected===t.name));const n=t.methods.length,r=t.methods.filter(registered).length,v=t.methods.filter(reviewed).length;b.append(el('strong',typeName(t.name)),el('small',n ? `${n} metod · ${r} rejestracji · ${v} dowodów` : 'Metody nierozpoznane — typ wykryty'));for(const [amount,cls,title] of [[r,'','Rejestracja'],[v,'review','Weryfikacja']]){const bar=el('div',undefined,'bar '+cls);bar.title=`${title}: ${amount} / ${n}`;const fill=el('i');fill.style.width=`${n ? 100*amount/n : 0}%`;bar.append(fill);b.append(bar);}b.onclick=()=>{selected=selected===t.name?null:t.name;render();};$('types').append(b);}
const chosen=data.types.find(t=>t.name===selected);$('type-notes').hidden=!chosen?.mapping_notes?.length;$('type-notes').textContent=(chosen?.mapping_notes||[]).map(note=>typeof note==='string'?note:JSON.stringify(note)).join('\n');$('methods-heading').textContent=selected?typeName(selected):'Wszystkie metody';$('count').textContent=`${methods.length} pozycji`;$('rows').replaceChildren();
const chosen=data.types.find(t=>t.name===selected);const typeNotes=[...(chosen?.inherits||[]).map(base=>`Dziedziczy metody bazowe zakresu ${base}.`),...(chosen?.mapping_notes||[])];$('type-notes').hidden=!typeNotes.length;$('type-notes').textContent=typeNotes.map(note=>typeof note==='string'?note:JSON.stringify(note)).join('\n');$('methods-heading').textContent=selected?typeName(selected):'Wszystkie metody';$('count').textContent=`${methods.length} pozycji`;$('rows').replaceChildren();
for(const t of active)for(const m of t.methods){const tr=el('tr'),name=el('td');name.append(el('small',typeName(t.name)),el('strong',m.name));const s=el('td'),v=el('td');s.append(badge(m.status));v.append(badge(m.verification));const found=original(m);const availability=el('td',found.length?`Wykryta · ${new Set(found.map(o=>o.binary_id)).size} wariantów`:'Nieustalone');const action=el('td'),b=el('button','Dowody ↗','quiet');b.setAttribute('aria-label',`Dowody ${t.name}.${m.name}`);b.onclick=()=>detail(t,m);action.append(b);tr.append(name,s,v,availability,action);$('rows').append(tr);}
$('empty').hidden=methods.length>0;$('empty').textContent=chosen && !chosen.methods.length ? 'Typ został wykryty, ale metody nie zostały jeszcze rozpoznane. Brak wpisów nie oznacza braku API.' : 'Brak metod dla wybranych filtrów.';
}
@@ -30,7 +31,7 @@ function detail(t,m){$('detail-title').textContent=`${typeName(t.name)} · ${m.n
const section=(title,text)=>{body.append(el('h3',title));if(text !== undefined)body.append(el('pre',text));};
section('Implementacja emulatora',m.source?`${m.source.path}:${m.source.line}\nWłaściciel: ${m.source.owner || '—'}\nHash treści: ${m.source.body_sha256 || '—'}`:'Nie znaleziono rejestracji w emulatorze.');
section('Dowody z oryginału');const entries=original(m);if(!entries.length)body.append(el('p','Nieustalone — ekstrakcja nie dostarcza dopasowania w wybranym zakresie.'));
for(const o of entries){const b=data.binaries.find(b=>b.id===o.binary_id);const a=el('article');a.append(el('strong',b?binaryName(b):o.binary_id));const dl=el('dl');for(const [key,value] of [['SHA-256',b?.sha256],['Silnik',typeof b?.engine==='object'?JSON.stringify(b.engine):b?.engine],['Właściciel',o.owner],['Id metody',o.method_id],['Implementacja',o.impl_addr],['Gałąź dispatchera',o.case_addr],['Funkcja rejestrująca',o.registration_addr],['Wywołanie rejestracji',o.registration_call_addr],['Przekazanie ID',o.id_push_addr],['Napis',o.string_addr],['Id dowodów',(o.evidence_ids||[]).join(', ')||'—'],['Dopasowanie',o.mapping==='explicit'?'Jawne':'Wywnioskowane'],['Dziedziczenie',o.inherited?'Tak':'Nie']]){dl.append(el('dt',key),el('dd',value===null || value===undefined?'—':String(value)));}a.append(dl);if(o.registration_addr || o.evidence_ids?.length)a.append(el('p','Dowód rejestracji ASM potwierdza rozpoznanie API; nie stanowi weryfikacji zachowania implementacji.','muted'));if(b?.limitations?.length){a.append(el('strong','Ograniczenia ekstrakcji'));a.append(el('pre',b.limitations.map(item=>typeof item==='string'?item:JSON.stringify(item,null,2)).join('\n')));}if(b?.coverage !== undefined)a.append(el('pre','Zakres ekstrakcji: '+(typeof b.coverage==='string'?b.coverage:JSON.stringify(b.coverage,null,2))));body.append(a);}
for(const o of entries){const b=data.binaries.find(b=>b.id===o.binary_id);const a=el('article');a.append(el('strong',b?binaryName(b):o.binary_id));const dl=el('dl');for(const [key,value] of [['SHA-256',b?.sha256],['Silnik',typeof b?.engine==='object'?JSON.stringify(b.engine):b?.engine],['Właściciel',o.owner],['Id metody',o.method_id],['Implementacja',o.impl_addr],['Gałąź dispatchera',o.case_addr],['Funkcja rejestrująca',o.registration_addr],['Wywołanie rejestracji',o.registration_call_addr],['Przekazanie ID',o.id_push_addr],['Napis',o.string_addr],['Id dowodów',(o.evidence_ids||[]).join(', ')||'—'],['Dopasowanie',MAPPING[o.mapping]||o.mapping],['Dziedziczenie',o.inherited?'Tak':'Nie']]){dl.append(el('dt',key),el('dd',value===null || value===undefined?'—':String(value)));}a.append(dl);if(o.registration_addr || o.evidence_ids?.length)a.append(el('p','Dowód rejestracji ASM potwierdza rozpoznanie API; nie stanowi weryfikacji zachowania implementacji.','muted'));if(b?.limitations?.length){a.append(el('strong','Ograniczenia ekstrakcji'));a.append(el('pre',b.limitations.map(item=>typeof item==='string'?item:JSON.stringify(item,null,2)).join('\n')));}if(b?.coverage !== undefined)a.append(el('pre','Zakres ekstrakcji: '+(typeof b.coverage==='string'?b.coverage:JSON.stringify(b.coverage,null,2))));body.append(a);}
section('Przeglądy i testy');body.append(el('p','Poniżej wszystkie zapisane przeglądy metody. Zbiorczy status weryfikacji nie oznacza potwierdzenia każdego wariantu; zakres dowodu określają jego dane.'));if(!(m.reviews||[]).length)body.append(el('p','Brak zapisanych dowodów weryfikacji zachowania.'));for(const review of m.reviews||[])body.append(el('pre',typeof review==='string'?review:JSON.stringify(review,null,2)));
const notes=[...(t.mapping_notes||[]),...(m.notes||[])];if(notes.length)section('Uwagi',notes.map(n=>typeof n==='string'?n:JSON.stringify(n,null,2)).join('\n'));
$('details').showModal();
+108 -28
View File
@@ -46,16 +46,55 @@ def code_mask(text: str) -> str:
return JAVA_TOKENS.sub(lambda m: re.sub(r"[^\n]", " ", m.group()), text)
def closing(code: str, start: int) -> int:
def closing(code: str, start: int, pair: str = "()") -> int:
depth = 0
for i in range(start, len(code)):
if code[i] == "(":
if code[i] == pair[0]:
depth += 1
elif code[i] == ")":
elif code[i] == pair[1]:
depth -= 1
if depth == 0:
return i + 1
raise ValueError("Unbalanced MethodSpec registration")
raise ValueError("Unbalanced " + pair)
BUILTINS_SOURCE = "pl/genschu/bloomooemulator/interpreter/parser/CodeParser.java"
BUILTINS_DISPATCH = re.compile(r"\bdispatchSpecialCall\s*\([^)]*\)\s*\{")
CASE_ARM = re.compile(r"\bcase\b([^;{}]*?)->|\bdefault\b\s*->")
def scan_builtins(root: Path, source_root: Path) -> dict | None:
"""`@IF`, `@LOOP`, ... are parsed by CodeParser.dispatchSpecialCall, not registered as
MethodSpec: each `case "NAME", ... ->` arm of its switch is one built-in statement."""
path = source_root / BUILTINS_SOURCE
if not path.is_file():
return None
text = path.read_text(encoding="utf-8")
code = code_mask(text)
head = BUILTINS_DISPATCH.search(code)
if head is None:
return None
switch = code.index("switch", head.end())
body_start = code.index("{", switch)
body_end = closing(code, body_start, "{}")
arms = list(CASE_ARM.finditer(code, body_start, body_end))
methods = {}
for arm, following in zip(arms, arms[1:] + [None]):
if arm.group(1) is None:
continue # default arm: unknown statements are a parse error
end = following.start() if following else body_end
body = text[arm.start():end]
status = "registered"
notes = []
if re.search(r"not (?:yet )?implemented", body, re.I) or "UnsupportedOperationException" in body:
status = "stub_candidate"
notes.append("Heurystyka: wyjątek lub komunikat o braku implementacji; wymaga przeglądu.")
for name in re.findall(r'"([A-Za-z_]+)"', text[arm.start(1):arm.end(1)]):
methods[name.upper()] = {"status": status, "notes": list(notes), "source": {
"path": str(path.relative_to(root)), "line": text.count("\n", 0, arm.start()) + 1,
"owner": "@", "body_sha256": digest(body.encode()), "file_sha256": digest(text.encode())}}
return {"methods": methods, "mapping_notes": [
"Instrukcje @ są rozpoznawane w CodeParser.dispatchSpecialCall, nie przez MethodSpec."]}
def _git(root: Path, *args: str) -> str | None:
@@ -110,6 +149,9 @@ def scan_emulator(root: Path) -> dict:
"owner": name, "body_sha256": digest(body.encode()), "file_sha256": digest(text.encode()),
}}
types[name] = {"methods": methods, "mapping_notes": notes}
builtins = scan_builtins(root, source_root)
if builtins is not None:
types["@"] = builtins
return {"root": str(root), "commit": _git(root, "rev-parse", "HEAD"),
"dirty": bool(_git(root, "status", "--porcelain")),
"source_tree_sha256": tree.hexdigest(), "types": types, "warnings": warnings}
@@ -123,6 +165,44 @@ def owner_name(owner: str) -> str:
return canonical(owner.removeprefix("CMC_"))
BASE_SCOPE = "*"
# getRunner(0) is the runner behind `@IF`, `@LOOP`, ... statements, not an object type.
BUILTIN_SCOPE, BUILTIN_TYPE_ID = "@", 0
def script_owners(snap: Snapshot, owners) -> dict[str, tuple[str, str]]:
"""Script scope -> (method owner, provenance) for one binary.
`runner_map` (CMC_RunnerFactory, Piklib 7.1+/BlooMoo) is the engine's own dispatch table and
wins: several types can share a runner (INTEGER/STRING/... -> Variable). Without it (Piklib
6.1, plugins, older extractions) the C++ class or its name is the best available guess."""
runner_map = snap.raw.get("runner_map") or {}
by_type_id = {e["type_id"]: e["runner"].removesuffix("_Runner") for e in runner_map.get("by_type_id", [])}
mappings = {}
if BUILTIN_TYPE_ID in by_type_id:
mappings[BUILTIN_SCOPE] = (by_type_id[BUILTIN_TYPE_ID], "runner_factory")
for t in snap.types:
name = canonical(t["script_name"])
if t.get("type_id") in by_type_id:
mappings[name] = (by_type_id[t["type_id"]], "runner_factory")
continue
owner, mapping = t.get("cpp_class"), "explicit"
# Known extractor failure: unrelated factory entries all mapped to Behaviour.
if owner == "CMC_Behaviour" and name != "BEHAVIOUR":
owner = None
if not owner:
candidate = [o for o in owners if owner_name(o) == name]
owner, mapping = (candidate[0] if len(candidate) == 1 else None), "inferred"
if owner:
mappings[name] = (owner, mapping)
used = {owner for owner, _ in mappings.values()}
for owner in owners:
# Singletons (SYSTEM, SCENE, ...) and the base scope run without a factory type.
if owner not in used and owner_name(owner) not in mappings:
mappings[owner_name(owner)] = (owner, "inferred")
return mappings
def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict] | None = None) -> dict:
"""Join script APIs, keeping every binary and inherited declaration as evidence."""
warnings = list(emulator.get("warnings", []))
@@ -174,39 +254,30 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
parents = defaultdict(list)
for edge in snap.method_inheritance:
parents[edge["runner"].removesuffix("_Runner")].append(edge["base_runner"].removesuffix("_Runner"))
mappings = {}
used_owners = set()
mappings = script_owners(snap, owners)
for t in snap.types:
name = canonical(t["script_name"])
row = type_row(name)
owner = t.get("cpp_class")
mapping = "explicit"
row = type_row(canonical(t["script_name"]))
if t.get("cpp_class_provenance"):
row["mapping_notes"].append(t["cpp_class_provenance"])
# Known extractor failure: unrelated factory entries all mapped to Behaviour.
if owner == "CMC_Behaviour" and name != "BEHAVIOUR":
owner = None
if not owner:
candidate = [o for o in owners if owner_name(o) == name]
owner = candidate[0] if len(candidate) == 1 else None
mapping = "inferred"
if owner:
mappings[name] = (owner, mapping)
used_owners.add(owner)
else:
if canonical(t["script_name"]) not in mappings:
row["mapping_notes"].append(f"{snap.binary.get('name', '?')}: typ wykryty, klasa/metody nieustalone.")
for owner in owners:
if owner not in used_owners:
# Includes singleton classes and separate base scopes. Label inference explicitly.
name = owner_name(owner)
if name not in mappings:
mappings[name] = (owner, "inferred")
sharing = defaultdict(set)
for name, (owner, _) in mappings.items():
sharing[owner].add(name)
for name, (owner, mapping) in mappings.items():
if len(sharing[owner]) > 1:
type_row(name)["mapping_notes"].append(
f"Wspólny runner {owner}_Runner z: {', '.join(sorted(sharing[owner] - {name}))}. "
"Rejestracja nie mówi, które metody działają dla tego typu.")
visited = set()
def add(current, inherited=False):
if current in visited:
return
visited.add(current)
if inherited and owner_name(current) == BASE_SCOPE and name != BASE_SCOPE:
# Base verbs (SEND, CLONE, ...) belong to every runner; they are listed once.
type_row(name).setdefault("inherits", []).append(BASE_SCOPE)
return
for m in owners[current]:
row = method_row(name, m["name"].upper())
if len(name_ids[(current, m["name"].upper())]) > 1:
@@ -247,6 +318,11 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
if row["source"] is None and method in globals_:
row.update(globals_[method])
row["notes"] = row["notes"] + ["Obsługa przez globalny fallback Variable."]
base_verbs = {m for m, row in rows.get(BASE_SCOPE, {}).get("methods", {}).items() if row["original"]}
for name, t in rows.items():
for method, row in t["methods"].items():
if name != BASE_SCOPE and method in base_verbs and row["source"] and not row["original"]:
row["notes"].append("W oryginale metoda bazowa (*) każdego runnera; emulator rejestruje ją lokalnie.")
# Reviews are declarative, version-controlled assertions; strict anchors prevent
# accidentally making an entire family green based on one stale binary/address.
for review in reviews or []:
@@ -276,6 +352,8 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
row["verification"] = "stale"
t["methods"] = sorted(t["methods"].values(), key=lambda m: m["name"])
t["mapping_notes"] = sorted(set(t["mapping_notes"]))
if "inherits" in t:
t["inherits"] = sorted(set(t["inherits"]))
all_rows = [m for t in rows.values() for m in t["methods"]]
return {"schema": "tracker/1", "emulator": {k: v for k, v in emulator.items() if k not in ("types", "warnings")},
"binaries": sorted(binaries, key=lambda b: (b["name"].lower(), b["id"])),
@@ -284,7 +362,9 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
"verification": dict(Counter(m["verification"] for m in all_rows))},
"warnings": warnings + ["Brak obserwacji w wariancie oznacza nieustalone, nie brak metody.",
"Rejestracja Java nie dowodzi poprawności. Liczniki obejmują pary typ/metoda, także odziedziczone.",
"Dopasowania klas po nazwie są wnioskowane; zakres * to metody bazowe/globalne."]}
"Typ → runner pochodzi z CMC_RunnerFactory, gdy snapshot ma runner_map; inaczej z nazwy klasy (wnioskowane).",
"Zakres * to metody bazowe CMC_Runner, wspólne dla każdego typu (pole inherits); nie są powielane przy typach.",
"Zakres @ to wbudowane instrukcje @IF/@LOOP/... (runner typu 0)."]}
def validate_review(review: dict) -> None:
+74 -3
View File
@@ -72,9 +72,80 @@ def test_inheritance_aliases_and_globals_are_not_implementation_proof(tmp_path):
assert row(result)["verification"] == "unverified"
assert row(result)["original"][0]["owner"] == "CMC_Bool"
assert len(row(result)["original"]) == 1 # child wins over SET on base
assert row(result, method="CLONE")["source"]["owner"] == "*"
assert row(result, method="CLONE")["original"][0]["inherited"]
assert next(t for t in result["types"] if t["name"] == "UNKNOWN")["methods"] == []
assert row(result, "*", "CLONE")["source"]["owner"] == "*"
assert not row(result, "*", "CLONE")["original"][0]["inherited"]
assert typ(result, "BOOLEAN")["inherits"] == ["*"]
assert "CLONE" not in {m["name"] for m in typ(result, "BOOLEAN")["methods"]} # listed once, under *
assert typ(result, "UNKNOWN")["methods"] == []
def factory_snapshot():
"""Piklib 8 shape: five variable types share one runner, VECTOR's runner has no symbol."""
return Snapshot({"binary": {"name": "p8.dll", "sha256": SHA},
"types": [{"script_name": "INTEGER", "cpp_class": "CMC_Integer", "type_id": 1},
{"script_name": "STRING", "cpp_class": "CMC_String", "type_id": 2},
{"script_name": "VECTOR", "cpp_class": None, "type_id": 0x7e0}],
"methods": [{"owner": "CMC_Variable", "runner": "CMC_Variable_Runner", "name": "ADD", "id": 6},
{"owner": "CMC_Vector", "runner": "CMC_Vector_Runner", "name": "NORMALIZE", "id": 8},
{"owner": "CMC_Dummy", "runner": "CMC_Dummy_Runner", "name": "IF", "id": 1},
{"owner": "CMC_Dummy", "runner": "CMC_Dummy_Runner", "name": "MSGBOX", "id": 6},
{"owner": "CMC", "runner": "CMC_Runner", "name": "CLONE", "id": 100}],
"method_inheritance": [{"runner": "CMC_Variable_Runner", "base_runner": "CMC_Runner"},
{"runner": "CMC_Vector_Runner", "base_runner": "CMC_Runner"}],
"runner_map": {"by_type_id": [{"type_id": 0, "runner": "CMC_Dummy_Runner"},
{"type_id": 1, "runner": "CMC_Variable_Runner"},
{"type_id": 2, "runner": "CMC_Variable_Runner"},
{"type_id": 0x7e0, "runner": "CMC_Vector_Runner"}]}})
def typ(result, name):
return next(t for t in result["types"] if t["name"] == name)
def test_runner_factory_maps_shared_and_unnamed_runners(tmp_path):
result = build_tracker([factory_snapshot()], emulator(tmp_path))
names = {t["name"] for t in result["types"]}
assert {"INTEGER", "STRING", "VECTOR", "@", "*"} <= names
assert not {"VARIABLE", "VECTOR_RUNNER", "DUMMY"} & names # no pseudo-types from runner classes
for name in ("INTEGER", "STRING"):
add = row(result, name, "ADD")["original"][0]
assert (add["owner"], add["mapping"], add["inherited"]) == ("CMC_Variable", "runner_factory", False)
assert any("Wspólny runner CMC_Variable_Runner" in n for n in typ(result, name)["mapping_notes"])
assert row(result, "VECTOR", "NORMALIZE")["original"]
assert {m["name"] for m in typ(result, "@")["methods"]} == {"IF", "MSGBOX"}
assert "inherits" not in typ(result, "@") # built-ins do not chain to CMC_Runner
def test_builtin_statements_are_read_from_the_parser(tmp_path):
parser = tmp_path / "core/src/pl/genschu/bloomooemulator/interpreter/parser"
parser.mkdir(parents=True)
(parser / "CodeParser.java").write_text('''class CodeParser {
private static ASTNode dispatchSpecialCall(String name, SourceSpan argsText) {
return switch (name) {
case "INT", "BOOL" -> new VarDefNode(name);
case "IF" -> buildIf(args); // "case" in a comment: "FAKE" ->
case "FOR" -> throw new ParseException("@FOR not implemented yet", location);
default -> throw new ParseException("Unknown special call: @" + name, location);
};
}
}''')
emu = emulator(tmp_path)
builtins = emu["types"]["@"]["methods"]
assert set(builtins) == {"INT", "BOOL", "IF", "FOR"}
assert builtins["FOR"]["status"] == "stub_candidate"
assert builtins["IF"]["status"] == "registered"
result = build_tracker([factory_snapshot()], emu)
assert row(result, "@", "IF")["status"] == "registered"
assert row(result, "@", "MSGBOX")["status"] == "missing"
def test_local_registration_of_a_base_verb_is_flagged(tmp_path):
snap = factory_snapshot()
emu = emulator(tmp_path)
emu["types"]["STRING"] = {"mapping_notes": [], "methods": {
"CLONE": {"status": "registered", "notes": [], "source": {"owner": "STRING"}}}}
result = build_tracker([snap], emu)
assert any("metoda bazowa (*)" in n for n in row(result, "STRING", "CLONE")["notes"])
def test_unknown_hash_not_fabricated_and_method_ids_are_binary_local(tmp_path):