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
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Lambdas used as the Jass `code` type cannot accept parameters or capture locals.

Objects created with `new` generally need `destroy`; tuples are value types and must not be destroyed. Destructors (`ondestroy`) remain explicit for Lua output: Lua garbage collection does not replace Wurst ownership and cleanup.

A field without an initialiser has no defined value in a new object that reuses a destroyed one's id: Jass keeps what the old object left, while Lua and compile-time code start from the field's default (`docs/WC3_RUNTIME.md`, "Objects and memory"). Give such a field an initialiser, or assign it in the constructor, when its starting value matters.

```wurst
class Missile
function onCollide(unit target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4921,4 +4921,69 @@ public void isLuaMagicConstantIsTrueInLuaMode() {
assertTrue("lua-path branch must be present", compiled.contains("lua-path"));
}

/**
* An object which gets the id of a destroyed one. A field without an initialiser keeps on Jass what the destroyed
* object left in it, since the field is an array indexed by id and nothing resets it; Lua writes the default of
* every field at allocation, and the interpreter of compile-time code makes a fresh object (docs/WC3_RUNTIME.md,
* "Objects and memory"). A field with an initialiser starts from it everywhere.
*/
private static String[] reusedObjectFields(int plain, int arrayElement) {
return new String[]{
"package Test",
"native testSuccess()",
"class A",
" int plain",
" int initialised = 7",
" int array[2] arr",
"init",
" let a = new A()",
" a.plain = 5",
" a.initialised = 9",
" a.arr[1] = 6",
" let id = a castTo int",
" destroy a",
" let b = new A()",
" if b castTo int == id and b.plain == " + plain + " and b.initialised == 7 and b.arr[1] == "
+ arrayElement,
" testSuccess()"
};
}

@Test
public void reusedObjectKeepsAFieldWithoutInitialiserOnJass() {
test().executeProg(true).executeProgOnlyAfterTransforms()
.luaExempt(LuaExemption.OBJECT_BOOKKEEPING, "Succeed function not called")
.lines(reusedObjectFields(5, 6));
}

@Test
public void reusedObjectStartsFromTheFieldDefaultsOnLua() {
test().testLua(true).executeProg(true).lines(reusedObjectFields(0, 0));
}

@Test
public void reusedObjectStartsFromTheFieldDefaultsAtCompileTime() {
test().executeProg(true).runCompiletimeFunctions(true).lines(
"package Test",
"native testSuccess()",
"function compiletime(int i) returns int",
" return i",
"class A",
" int plain",
" int array[2] arr",
"function reuse() returns int",
" let a = new A()",
" a.plain = 5",
" a.arr[1] = 6",
" destroy a",
" let b = new A()",
" let r = b.plain * 10 + b.arr[1]",
" destroy b",
" return r",
"constant reused = compiletime(reuse())",
"init",
" if reused == 0",
" testSuccess()");
}

}
1 change: 1 addition & 0 deletions docs/WC3_RUNTIME.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A test that passes in the first two columns says nothing about the third where t
* **code** Wurst has no garbage collector. A class instance is an integer id on both targets. `new` takes an id, `destroy` runs `ondestroy` and gives the id back, and a second `destroy` is an error (`Double free: object of type X` on Jass, `Double free or invalid Wurst object.` on Lua). A closure is a class instance too and keeps its id until destroyed. (`RecycleCodeGeneratorQueue`, `LuaTranslator.createObjectManagement`.)
* **code** Both targets reuse the most recently freed id first (a stack, despite the name `RecycleCodeGeneratorQueue`), so `destroy` followed by `new` from the same allocator returns the same id and a stale reference then reads the new object. On Jass each group of classes joined by inheritance (a class with its `superClasses`, `ImTranslator.buildClassPartitions`) has one allocator: a subclass, its siblings and the base class share ids and the cap, so a stale id may come back as an object of a related class. The group runs out at `JASS_MAX_ARRAY_SIZE` live instances (default `Constants.MAX_ARRAY_SIZE`, 32768) with `Out of memory: Could not create X.`. On Lua there is one id space shared by every class and no cap, so a stale id may come back as an object of any class.
* **code** Lua keeps one static table per field, indexed by id, and a class descriptor per live id. `destroy` removes the descriptor and recycles the id; field storage is not cleared, as Jass arrays are not. Until the id is reused a stale reference fails virtual dispatch and `instanceof` is false. Lua's collector therefore frees no Wurst object, and a table stored in a field stays reachable after `destroy`.
* **code** An object that gets a reused id starts differently on each target, by design. On Jass a field without an initialiser keeps what the destroyed object left there, an array field's elements included: nothing resets the field's array at `new`, and doing it would add a store per field to every allocation. On Lua `new` writes the default of every field and gives each array field a fresh table (`LuaFieldDefaults`, `LuaTranslator.createClassInitFunction`), and in compile-time code the interpreter makes a fresh object. A field with an initialiser starts from it on every target. Resetting on Jass would change what existing maps read, so the difference stays; code which needs a known value writes it in the constructor or gives the field an initialiser. `LuaTranslationTests.reusedObject*` pins the three.
* **code** The natives of the standard library's `Reflection` read this bookkeeping. A type id numbers a class from 1 (`TypeId`: by package, name and source position, each class followed by its subclasses); when compile-time code read a type id, the classes keep the ids they had then and Jass numbers the classes the generics add after them (`TypeId.keeping`), so an id stored at compile time stays its class's; `maxTypeId` is the largest and `typeIdToTypeName` the class's qualified name. `instanceCount` is the number of live objects of the class's allocator group, not of the class alone, and `maxInstanceCount` the most the group had alive at once, which on Jass is the group's highest id (`EliminateClasses`). Lua has no group allocator, so it keeps a live count and a peak per group, updated by the allocations and destroys of the group's classes, only in a program which calls these natives and only for the groups it can ask about (`LuaReflection`). Lua numbers the classes the emitted program keeps, except the copies a targeted specialisation makes, which have the type id and the group of the class they copy (`LuaTranslator.getTypeId`); a program which can see the numbering (`maxTypeId`, or a type id which is not `C.typeId` or `o.typeId`) keeps every class, so its numbers are Jass's. A number which is no type id answers `"unknown"` and 0 on both targets and in the interpreter. Two differences remain, both pinned in `LuaTranslationTests`: the Jass replay of compile-time objects sets the group's highest id to the highest one used at compile time, so the ids of the objects destroyed or dropped then count as live there, while Lua counts only the objects it makes again (compile-time code numbers a new-generic class's objects as the erased class's, so each instantiation's group continues after the highest id of them all: `CompiletimeFunctionRunner.emitJassObjectAllocs`); and a new-generic class is a class per instantiation on Jass but one erased class on Lua (AGENTS.md §9), so with `Box<T:>` and `Sub<T:> extends Box<T>`, `Box<int>`, `Sub<int>` and `Box<string>` are type ids 1, 2 and 3 in two groups on Jass, while Lua has the type ids 1 and 2 in one group which counts all three objects. Matching that would take a class per instantiation on Lua, or the type arguments at every allocation, which erased generic code does not have. The interpreter counts each allocator group's live objects and their peak as the targets do.

* **measured, 3.0.0** Hashtables: a handle load is typed (a saved unit loads as itself through `LoadUnitHandle` and `LoadWidgetHandle`, as null through `LoadItemHandle` or `LoadTimerHandle`); saving a null handle stores nothing and keeps what was there; `FlushParentHashtable` destroys the hashtable, so nothing saved to it afterwards stays and every load answers its default; a null hashtable answers every load with its default and ignores every write. The Lua emulation (`LuaHashtable`) and the interpreter (`HashtableProvider`) answer the same. A handle saved through a supertype (`SaveAgentHandle`, `SaveWidgetHandle`) and loaded as a subtype is answered on Lua, which cannot tell its type.
Expand Down
Loading