Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 11 additions & 44 deletions BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,23 @@ Notes; finished-work narrative does not.
Each repro is a whole program after `package Test` and `native testSuccess()`. The full product
(`WURST_GENERIC_MATRIX=full`) adds no other class of failure.

- **Jass: `this.field instanceof Box<T>` in a generic class fails translation** ("Could not find field
array for this.G_held<integer>"), also for a local captured by a closure, which becomes a field. A
local copy (`let h = held`) compiles, so the matrix reads one:

```wurst
interface Marker
class Box<T:> implements Marker
T item
class G<T:>
Marker held = null
function r() returns boolean
return held instanceof Box<T>
init
let g = new G<int>()
g.held = new Box<int>()
if g.r()
testSuccess()
```

- **Executing tests left off Lua.** Master CI runs every executing test on Lua too (`-PallBackends`) on
its Linux and Windows jobs, whose Lua is 5.3; macOS, with Homebrew's unpinned Lua, does not. The 13
`MISSING_TEST_NATIVE` exemptions wait for natives in `wc3shim.lua`, and `executeTests()` (Wurst `@Test`
functions) has no Lua path in the harness at all. `-PallBackends=full`, which adds
`-inline -localOptimizations`, found nothing the plain run did not (October 2026), so CI runs only that.

- **`EliminateGenerics.fillMissingTypeArgsFromScope` matches type variables by name.** On the Jass path it
completed the owner type arguments of a static inner class's type, which the IM translation now gives
itself (`WurstTypeClassOrInterface.imTranslateType`). Made to throw whenever it fills one, it is reached by
none of the generics, closure, module, class and Lua audit test classes (October 2026). Check the whole
suite and remove it.

- **The `Reflection` natives answer otherwise on Jass for a number which is no type id, and in the
interpreter.** `EliminateClasses` searches the type ids without bounds, so on Jass
`typeIdToTypeName(0)` is the first class's name and a number above `maxTypeId()` answers for the last
class, where the search's own default (`"unknown"`, 0) and Lua answer the default. The interpreter
(`WurstflectionProvider`, `ILInterpreter.getInstanceCount`) counts the objects of the class alone, not
of its allocator group, gives as `maxInstanceCount` the number of objects ever made, and raises in
`typeIdToTypeName` for an unknown number. Compile-time code sees that, and the parity tests run the
interpreter only on the translated program (`LuaTranslationTests.reflectionNativesAnswerAsOnJass`).
Bound the Jass search and make the interpreter answer as the backends do (docs/WC3_RUNTIME.md
"Objects and memory").

- **`GetHandleId` of a unit or another agent is the compiler's own counter on Lua.** `LuaNativeLowering`
replaces `GetHandleId` of the `OPAQUE_RUNTIME_HANDLE_TYPES` (unit, item, timer, trigger, ...) with
`__wurst_GetHandleId`, which numbers handles from 1 in `__wurst_objectIndexMap` and keeps every handle it
numbered. The game's ids start at 0x100000 (measured on the 3.0.0 client), as the interpreter's do, so
`InterpreterTests.conversionNativesAsMeasured` is exempt from Lua, and a program which subtracts 0x100000
to index an array reads a negative index there. The tests check the remap's shape
(`LuaTranslationTests.getHandleIdIsRemappedToWurstPolyfillInLua`), not this difference. Call the game's
`GetHandleId`, or record why the remap is needed and document the difference (AGENTS.md §7).
- **`GetHandleId` of an agent on Lua is the compiler's own counter, for a desync nobody measured.**
`LuaNativeLowering` replaces `GetHandleId` of the `OPAQUE_RUNTIME_HANDLE_TYPES` (unit, item, timer, trigger,
...) with `__wurst_GetHandleId`, which numbers handles from 1 in `__wurst_objectIndexMap` and keeps every
handle it numbered reachable. The code gives no reason now; #1181 added it with the note that the game's ids
"can desync in Lua mode", and nothing in the repository measures that. The game's ids are no contract a
program can use either: #1323 saw agent ids from 1048676 up on the 3.0.0 client, the game recycles them, and
they run past a Jass array's bounds even less 0x100000. The interpreter numbers agents from 0x100000 as that
client did, so `InterpreterTests.conversionNativesAsMeasured` is exempt from Lua.
Measure in game whether one agent's `GetHandleId` differs between clients on Lua. If it does not, call the
native and drop the remap with the handles it keeps alive; if it does, record the measurement and document
the difference (AGENTS.md §7).

- **Represent raw Lua helper bindings structurally.** Lua name compression now runs after backend
lowering so it includes descriptors, field storage and resolved dispatch slots. `LuaLiteral` still
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public void run() {

execute(toExecute);
long tExecuted = System.nanoTime();
if (globalState.typeIdsRead()) {
translator.rememberTypeIdsBeforeGenerics(globalState.genericTypeIdReads());
}

if (functionFlag == FunctionFlagToRun.CompiletimeFunctions) {
emitCompiletimeState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package de.peeeq.wurstio.jassinterpreter.providers;

import de.peeeq.wurstio.jassinterpreter.InterpreterException;
import de.peeeq.wurstscript.intermediatelang.ILconstInt;
import de.peeeq.wurstscript.intermediatelang.ILconstString;
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;
import de.peeeq.wurstscript.jassIm.ImClass;
import de.peeeq.wurstscript.jassIm.ImProg;

import java.util.Map;
import java.util.Optional;

import static de.peeeq.wurstscript.translation.imtranslation.EliminateClasses.calculateClassName;
import static de.peeeq.wurstscript.translation.imtranslation.EliminateClasses.calculateMaxTypeId;
Expand All @@ -20,34 +18,31 @@ public WurstflectionProvider(AbstractInterpreter interpreter) {
}


/** The qualified name of the class with the type id, "unknown" for any other number, as on both targets. */
public ILconstString typeIdToTypeName(ILconstInt typeId) {
interpreter.noteTypeIdsRead();
ImProg prog = interpreter.getImProg();
int typeIdInt = typeId.getVal();
for (Map.Entry<ImClass, Integer> e : prog.attrTypeId().entrySet()) {
if (e.getValue() == typeIdInt) {
ILconstString iLconstString = ILconstString.fromText(calculateClassName(e.getKey()));
return Optional.of(iLconstString)
.orElseGet(() -> {
throw new InterpreterException("Could not determine type name for id " + typeId);
});
return ILconstString.fromText(calculateClassName(e.getKey()));
}
}
return Optional.<ILconstString>empty()
.orElseGet(() -> {
throw new InterpreterException("Could not determine type name for id " + typeId);
});

return ILconstString.fromText("unknown");
}

public ILconstInt maxTypeId() {
interpreter.noteTypeIdsRead();
return new ILconstInt(calculateMaxTypeId(interpreter.getImProg()));
}

public ILconstInt instanceCount(ILconstInt typeId) {
interpreter.noteTypeIdsRead();
return new ILconstInt(interpreter.getInstanceCount(typeId.getVal()));
}

public ILconstInt maxInstanceCount(ILconstInt typeId) {
interpreter.noteTypeIdsRead();
return new ILconstInt(interpreter.getMaxInstanceCount(typeId.getVal()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public interface AbstractInterpreter {
int getInstanceCount(int val);

int getMaxInstanceCount(int val);

/** Records that the program read a type id; see {@code ProgramState.noteTypeIdsRead}. */
default void noteTypeIdsRead() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public static ILconst eval(ImInstanceof e, ProgramState globalState,

public static ILconst eval(ImTypeIdOfClass e,
ProgramState globalState, LocalState localState) {
globalState.noteTypeIdRead(e.getClazz().getClassDef(), e.attrTrace());
return new ILconstInt(e.getClazz().getClassDef().attrTypeId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,28 +704,30 @@ public ImProg getImProg() {
return prog;
}

@Override
public void noteTypeIdsRead() {
globalState.noteTypeIdsRead();
}

@Override
public int getInstanceCount(int val) {
long count = 0L;
for (ILconstObject o : globalState.getAllObjects()) {
if (o.getType().getClassDef().attrTypeId() == val) {
if (!o.isDestroyed()) {
count++;
}
}
}
return (int) count;
ImClass c = classOfTypeId(val);
return c == null ? 0 : globalState.liveObjectsOfGroup(c);
}

@Override
public int getMaxInstanceCount(int val) {
long count = 0L;
for (ILconstObject o : globalState.getAllObjects()) {
if (o.getType().getClassDef().attrTypeId() == val) {
count++;
ImClass c = classOfTypeId(val);
return c == null ? 0 : globalState.mostLiveObjectsOfGroup(c);
}

private @Nullable ImClass classOfTypeId(int typeId) {
for (Map.Entry<ImClass, Integer> e : prog.attrTypeId().entrySet()) {
if (e.getValue() == typeId) {
return e.getKey();
}
}
return (int) count;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class ProgramState extends State implements AutoCloseable {
/** The key of the one id space all classes share; see {@link #useOneObjectIdSpace}. */
private static final Object SHARED_ID_SPACE = new Object();
private boolean oneObjectIdSpace;
/** Whether the program read a type id; see {@link #noteTypeIdsRead}. */
private boolean typeIdsRead;
/** Where the program first read the type id of each new-generic class; see {@link #noteTypeIdRead}. */
private final Map<ImClass, Element> genericTypeIdReads = new LinkedHashMap<>();
/** The live objects and the most alive at once of each allocation group, by its key; only looked up. */
private final Map<Object, int[]> groupCounts = new HashMap<>();
/** See {@link #isGenericHierarchy}; only looked up, never iterated. */
private final Map<ImClass, Boolean> genericHierarchy = new IdentityHashMap<>();
/** See {@link #dispatch}; only looked up, never iterated. */
Expand Down Expand Up @@ -222,6 +228,9 @@ public ILconstObject allocate(ImClassType clazz, Element trace) {
int objId = !idSpace.freeObjectIds.isEmpty() ? idSpace.freeObjectIds.pop() : ++idSpace.objectIdCounter;
ILconstObject res = new ILconstObject(clazz, objId, trace);
idSpace.indexToObject.put(objId, res);
int[] counts = groupCounts(clazz.getClassDef());
counts[0]++;
counts[1] = Math.max(counts[1], counts[0]);
WLogger.trace(() -> "alloc objId=" + objId + " type=" + clazz + " trace=" + trace);
return res;
}
Expand Down Expand Up @@ -249,6 +258,7 @@ public void useOneObjectIdSpace() {
public void deallocate(ILconstObject obj, ImClass clazz, Element trace) {
assertAllocated(obj, trace);
obj.destroy();
groupCounts(clazz)[0]--;
ObjectIdSpace idSpace = getIdSpace(classKey(clazz));
idSpace.freeObjectIds.push(obj.getObjectId());
}
Expand Down Expand Up @@ -405,9 +415,55 @@ public boolean isGenericHierarchy(ImClass c) {

public int getTypeId(ILconstObject obj, Element trace) {
assertAllocated(obj, trace);
noteTypeIdRead(obj.getImClass(), trace);
return obj.getImClass().attrTypeId();
}

/**
* Records the read of the type id of {@code clazz} at {@code trace}. The interpreter has one class for all the
* instantiations of a new-generic class, as Lua does; Jass makes a class of each after compile time.
*/
public void noteTypeIdRead(ImClass clazz, Element trace) {
noteTypeIdsRead();
if (!clazz.getTypeVariables().isEmpty()) {
genericTypeIdReads.putIfAbsent(clazz, trace);
}
}

public Map<ImClass, Element> genericTypeIdReads() {
return genericTypeIdReads;
}

/**
* Records that the program read a type id, or a number which stands for one: compile-time code may keep it in
* the generated script, so the classes keep their type ids when the generics add classes
* ({@code ImTranslator.rememberTypeIdsBeforeGenerics}).
*/
public void noteTypeIdsRead() {
typeIdsRead = true;
}

public boolean typeIdsRead() {
return typeIdsRead;
}

/**
* The counts of the allocation group of {@code clazz}, every class joined to it by inheritance, which one
* allocator serves on Jass: its live objects and the most it had alive at once (Reflection's instanceCount and
* maxInstanceCount, which Lua counts the same way).
*/
private int[] groupCounts(ImClass clazz) {
return groupCounts.computeIfAbsent(classKeyLookup.getOrDefault(clazz, clazz), k -> new int[2]);
}

public int liveObjectsOfGroup(ImClass clazz) {
return groupCounts(clazz)[0];
}

public int mostLiveObjectsOfGroup(ImClass clazz) {
return groupCounts(clazz)[1];
}

/** {@code entry} is where the frame is entered: the call, or the function when nothing called it. */
public void pushStackframeWithTypes(ImFunction f, @Nullable ILconstObject receiver,
ILconst[] args, de.peeeq.wurstscript.jassIm.Element entry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public EliminateClasses(ImTranslator tr, ImProg prog, boolean checkedDispatch) {
translator = tr;
this.prog = prog;
this.checkedDispatch = checkedDispatch;
this.classIds = TypeId.calculate(prog);
this.classIds = tr.typeIds();
}

public void eliminateClasses() {
Expand Down Expand Up @@ -169,7 +169,11 @@ private ImStmts generateBinarySearch(int lower, int upper, ImVar typeId, ImClass
if (c == null) {
return JassIm.ImStmts();
}
return JassIm.ImStmts(JassIm.ImReturn(prog.getTrace(), makeAccess.apply(c)));
// The search narrows to one id even for a number below the first or above the last, so check it.
return JassIm.ImStmts(JassIm.ImIf(prog.getTrace(),
JassIm.ImOperatorCall(WurstOperator.EQ, JassIm.ImExprs(JassIm.ImVarAccess(typeId), JassIm.ImIntVal(lower))),
JassIm.ImStmts(JassIm.ImReturn(prog.getTrace(), makeAccess.apply(c))),
JassIm.ImStmts()));
} else {
int mid = lower + (upper - lower) / 2;
return
Expand Down Expand Up @@ -241,7 +245,7 @@ private void eliminateClass(ImClass c) {
}

// create management functions
recycleCodeGen.createAllocFunc(translator, prog, c);
recycleCodeGen.createAllocFunc(translator, prog, c, typeIdOf(c));
recycleCodeGen.createDeallocFunc(translator, prog, c);
}

Expand Down
Loading
Loading