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 @@ -31,6 +31,9 @@ public class JassInterpreter implements AbstractInterpreter {
private final Map<String, JassInitializedVar> uninitializedBjGlobals = new HashMap<>();
private boolean trace = false;
private final Map<String, ExecutableJassFunction> functionCache = new HashMap<>();
/** One set of native mocks per run: a mock keeps state between its natives, such as the unit ForGroup is
* visiting, which GetEnumUnit answers. */
private @Nullable ReflectionNativeProvider nativeProvider;
private final TimerMockHandler timerMockHandler = new TimerMockHandler();
/** Names of the real globals, which convert an integer stored into them. */
private final Set<String> realGlobals = new HashSet<>();
Expand Down Expand Up @@ -490,7 +493,11 @@ private ExecutableJassFunction searchNativeJassFunction(String name) {
if (name.equals("ExecuteFunc")) {
return executeFuncNative();
}
ReflectionNativeProvider nf = new ReflectionNativeProvider(this);
ReflectionNativeProvider nf = nativeProvider;
if (nf == null) {
nf = new ReflectionNativeProvider(this);
nativeProvider = nf;
}
ExecutableJassFunction functionPair = nf.getFunctionPair(name);
return functionPair != null ? functionPair : new UnknownJassFunction(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,26 @@ public ILconst FirstOfGroup(IlConstHandle group) {
return groupList.iterator().next();
}

/** Measured on the 3.0.0.24268 client: ForGroup visits the units the group held when it was called (a unit
* removed during the enumeration is still visited, one added is not), and GetEnumUnit answers the unit being
* visited, also after a nested ForGroup returns. */
public void ForGroup(IlConstHandle group, ILconstFuncRef funcRef) {
LinkedHashSet<IlConstHandle> groupList = (LinkedHashSet<IlConstHandle>) group.getObj();
groupList.forEach((IlConstHandle u) -> {
new ArrayList<>(groupList).forEach((IlConstHandle u) -> {
enumUnitStack.push(u);
interpreter.runFuncRef(funcRef, null);
enumUnitStack.pop();
});
}

public IlConstHandle GetEnumUnit() {
if (enumUnitStack.size() == 0 || enumUnitStack.peek() == null) {
/** Null outside any ForGroup, as measured on the 3.0.0.24268 client. */
public ILconst GetEnumUnit() {
IlConstHandle unit = enumUnitStack.peek();
if (unit == null) {
WLogger.warning("Calling GetEnumUnit() outside of callback");
return ILconstNull.instance();
}
return enumUnitStack.peek();
return unit;
}

public void DestroyGroup(IlConstHandle group) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* <li><b>GetHandleId</b>: replaced 1:1 by {@code __wurst_GetHandleId}, whose Lua
* implementation uses a stable table counter for selected opaque runtime handle
* families only. Enum-like handle families keep native semantics in Lua.</li>
* <li><b>Hashtable natives</b> ({@code SaveInteger}, {@code LoadBoolean}, …) and
* <b>context-callback natives</b> ({@code ForForce}, {@code ForGroup}, …):
* <li><b>Hashtable natives</b> ({@code SaveInteger}, {@code LoadBoolean}, …):
* replaced 1:1 by their {@code __wurst_} prefixed equivalents, whose Lua
* implementations are provided by {@link de.peeeq.wurstscript.translation.lua.translation.LuaNatives}.
* The backend prints a hashtable operation where it is called instead, as the table accesses
* it stands for, and calls its helper only where it cannot ({@link LuaHashtable}).</li>
* it stands for, and calls its helper only where it cannot ({@link LuaHashtable}).
* The enumeration natives ({@code ForGroup}, {@code ForForce}, {@code EnumItemsInRect},
* {@code EnumDestructablesInRect}) and their {@code GetEnum} getters are the game's own: it
* keeps the enumerated object per enumeration, nested ones included, as on Jass.</li>
* <li><b>All other BJ calls with at least one handle-typed parameter</b>: wrapped
* by a generated IM function that first checks each required handle param for
* {@code null} and returns the type-appropriate default (0 / 0.0 / false / "" / nil),
Expand All @@ -43,14 +45,6 @@
*/
public final class LuaNativeLowering {

/** Context-callback natives that need to be remapped to {@code __wurst_} equivalents. */
private static final Set<String> CONTEXT_CALLBACK_NATIVE_NAMES = new HashSet<>(Arrays.asList(
"ForForce", "GetEnumPlayer",
"ForGroup", "GetEnumUnit",
"EnumItemsInRect", "GetEnumItem",
"EnumDestructablesInRect", "GetEnumDestructable"
));

/** True runtime-object handles that should use Lua-side object identity for GetHandleId. */
private static final Set<String> OPAQUE_RUNTIME_HANDLE_TYPES = new HashSet<>(Arrays.asList(
"unit", "item", "destructable", "effect", "lightning", "timer", "trigger",
Expand Down Expand Up @@ -275,8 +269,6 @@ private ImFunction computeReplacement(ImFunction bj) {
// Kept by the translator, so the optimizer and the backend match the stub by identity.
return translator.luaHashtableStubs.computeIfAbsent(LuaHashtable.stubName(name),
stubName -> createNativeStub(stubName, bj));
} else if (CONTEXT_CALLBACK_NATIVE_NAMES.contains(name)) {
return createNativeStub("__wurst_" + name, bj);
} else if (hasHandleParam(bj)) {
return createNilSafeWrapper(bj, translator, deferredAdditions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,118 +180,6 @@ public class LuaNatives {
f.getBody().add(LuaAst.LuaLiteral("return x .. y"));
});

addNative("__wurst_GetEnumPlayer", f -> {
// Prefer the native enum player when inside an active native ForForce callback.
// This preserves Jass semantics for nested enumerations.
f.getBody().add(LuaAst.LuaLiteral("if GetEnumPlayer ~= nil then"));
f.getBody().add(LuaAst.LuaLiteral(" local p = GetEnumPlayer()"));
f.getBody().add(LuaAst.LuaLiteral(" if p ~= nil then return p end"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("if __wurst_enumPlayer_override ~= nil then return __wurst_enumPlayer_override end"));
f.getBody().add(LuaAst.LuaLiteral("return nil"));
});

addNative("__wurst_ForForce", f -> {
f.getParams().add(LuaAst.LuaVariable("whichForce", LuaAst.LuaNoExpr()));
f.getParams().add(LuaAst.LuaVariable("callback", LuaAst.LuaNoExpr()));
f.getBody().add(LuaAst.LuaLiteral("if ForForce == nil then return end"));
f.getBody().add(LuaAst.LuaLiteral("local players = {}"));
f.getBody().add(LuaAst.LuaLiteral("local count = 0"));
f.getBody().add(LuaAst.LuaLiteral("local prev = __wurst_enumPlayer_override"));
f.getBody().add(LuaAst.LuaLiteral("ForForce(whichForce, function()"));
f.getBody().add(LuaAst.LuaLiteral(" count = count + 1"));
f.getBody().add(LuaAst.LuaLiteral(" players[count] = GetEnumPlayer()"));
f.getBody().add(LuaAst.LuaLiteral("end)"));
f.getBody().add(LuaAst.LuaLiteral("for i = 1, count do"));
f.getBody().add(LuaAst.LuaLiteral(" __wurst_enumPlayer_override = players[i]"));
f.getBody().add(LuaAst.LuaLiteral(" callback()"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("__wurst_enumPlayer_override = prev"));
});

addNative("__wurst_GetEnumUnit", f -> {
f.getBody().add(LuaAst.LuaLiteral("if GetEnumUnit ~= nil then"));
f.getBody().add(LuaAst.LuaLiteral(" local u = GetEnumUnit()"));
f.getBody().add(LuaAst.LuaLiteral(" if u ~= nil then return u end"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("if __wurst_enumUnit_override ~= nil then return __wurst_enumUnit_override end"));
f.getBody().add(LuaAst.LuaLiteral("return nil"));
});

addNative("__wurst_ForGroup", f -> {
f.getParams().add(LuaAst.LuaVariable("whichGroup", LuaAst.LuaNoExpr()));
f.getParams().add(LuaAst.LuaVariable("callback", LuaAst.LuaNoExpr()));
f.getBody().add(LuaAst.LuaLiteral("if ForGroup == nil then return end"));
f.getBody().add(LuaAst.LuaLiteral("local units = {}"));
f.getBody().add(LuaAst.LuaLiteral("local count = 0"));
f.getBody().add(LuaAst.LuaLiteral("local prev = __wurst_enumUnit_override"));
f.getBody().add(LuaAst.LuaLiteral("ForGroup(whichGroup, function()"));
f.getBody().add(LuaAst.LuaLiteral(" count = count + 1"));
f.getBody().add(LuaAst.LuaLiteral(" units[count] = GetEnumUnit()"));
f.getBody().add(LuaAst.LuaLiteral("end)"));
f.getBody().add(LuaAst.LuaLiteral("for i = 1, count do"));
f.getBody().add(LuaAst.LuaLiteral(" __wurst_enumUnit_override = units[i]"));
f.getBody().add(LuaAst.LuaLiteral(" callback()"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("__wurst_enumUnit_override = prev"));
});

addNative("__wurst_GetEnumItem", f -> {
f.getBody().add(LuaAst.LuaLiteral("if GetEnumItem ~= nil then"));
f.getBody().add(LuaAst.LuaLiteral(" local it = GetEnumItem()"));
f.getBody().add(LuaAst.LuaLiteral(" if it ~= nil then return it end"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("if __wurst_enumItem_override ~= nil then return __wurst_enumItem_override end"));
f.getBody().add(LuaAst.LuaLiteral("return nil"));
});

addNative("__wurst_EnumItemsInRect", f -> {
f.getParams().add(LuaAst.LuaVariable("r", LuaAst.LuaNoExpr()));
f.getParams().add(LuaAst.LuaVariable("filter", LuaAst.LuaNoExpr()));
f.getParams().add(LuaAst.LuaVariable("actionFunc", LuaAst.LuaNoExpr()));
f.getBody().add(LuaAst.LuaLiteral("if EnumItemsInRect == nil then return end"));
f.getBody().add(LuaAst.LuaLiteral("local items = {}"));
f.getBody().add(LuaAst.LuaLiteral("local count = 0"));
f.getBody().add(LuaAst.LuaLiteral("local prev = __wurst_enumItem_override"));
f.getBody().add(LuaAst.LuaLiteral("EnumItemsInRect(r, filter, function()"));
f.getBody().add(LuaAst.LuaLiteral(" count = count + 1"));
f.getBody().add(LuaAst.LuaLiteral(" items[count] = GetEnumItem()"));
f.getBody().add(LuaAst.LuaLiteral("end)"));
f.getBody().add(LuaAst.LuaLiteral("for i = 1, count do"));
f.getBody().add(LuaAst.LuaLiteral(" __wurst_enumItem_override = items[i]"));
f.getBody().add(LuaAst.LuaLiteral(" actionFunc()"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("__wurst_enumItem_override = prev"));
});

addNative("__wurst_GetEnumDestructable", f -> {
f.getBody().add(LuaAst.LuaLiteral("if GetEnumDestructable ~= nil then"));
f.getBody().add(LuaAst.LuaLiteral(" local d = GetEnumDestructable()"));
f.getBody().add(LuaAst.LuaLiteral(" if d ~= nil then return d end"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("if __wurst_enumDestructable_override ~= nil then return __wurst_enumDestructable_override end"));
f.getBody().add(LuaAst.LuaLiteral("return nil"));
});

addNative("__wurst_EnumDestructablesInRect", f -> {
f.getParams().add(LuaAst.LuaVariable("r", LuaAst.LuaNoExpr()));
f.getParams().add(LuaAst.LuaVariable("filter", LuaAst.LuaNoExpr()));
f.getParams().add(LuaAst.LuaVariable("actionFunc", LuaAst.LuaNoExpr()));
f.getBody().add(LuaAst.LuaLiteral("if EnumDestructablesInRect == nil then return end"));
f.getBody().add(LuaAst.LuaLiteral("local dests = {}"));
f.getBody().add(LuaAst.LuaLiteral("local count = 0"));
f.getBody().add(LuaAst.LuaLiteral("local prev = __wurst_enumDestructable_override"));
f.getBody().add(LuaAst.LuaLiteral("EnumDestructablesInRect(r, filter, function()"));
f.getBody().add(LuaAst.LuaLiteral(" count = count + 1"));
f.getBody().add(LuaAst.LuaLiteral(" dests[count] = GetEnumDestructable()"));
f.getBody().add(LuaAst.LuaLiteral("end)"));
f.getBody().add(LuaAst.LuaLiteral("for i = 1, count do"));
f.getBody().add(LuaAst.LuaLiteral(" __wurst_enumDestructable_override = dests[i]"));
f.getBody().add(LuaAst.LuaLiteral(" actionFunc()"));
f.getBody().add(LuaAst.LuaLiteral("end"));
f.getBody().add(LuaAst.LuaLiteral("__wurst_enumDestructable_override = prev"));
});

// KeyedTable: membership keyed directly by the element, one index per operation.
// Lowered to these stubs by LuaKeyedTable/LuaNativeLowering before the inliner runs, so
// every call site agrees on the representation. No iteration is offered: pairs() order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ public void heroLevelsAsMeasured() {
/** Every expected value here was measured on the 3.0.0 client. */
@Test
public void groupOrderAsMeasured() {
test().withStdLib().executeProg(true).luaExempt(LuaExemption.MISSING_TEST_NATIVE, "GroupAddUnit").lines(
test().withStdLib().executeProg(true).testLua(true).luaOnly(false).lines(
"package Test",
"function measured(string name, string actual, string expected)",
" if actual != expected",
Expand Down
Loading
Loading