Skip to content

Commit d95096a

Browse files
committed
more lua fixes
1 parent 039bae2 commit d95096a

7 files changed

Lines changed: 433 additions & 34 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/MapRequest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,12 @@ private String resolveCachedMapFileName() {
420420
if (!cachedMapFileName.isEmpty()) {
421421
return cachedMapFileName;
422422
}
423+
return resolveCachedMapFileName(runArgs.isLua());
424+
}
425+
426+
private String resolveCachedMapFileName(boolean luaMode) {
423427
if (!map.isPresent()) {
424-
return "cached_map.w3x";
428+
return luaMode ? "cached_map_lua.w3x" : "cached_map_jass.w3x";
425429
}
426430
File inputMap = map.get();
427431
String inputName = inputMap.getName();
@@ -430,7 +434,8 @@ private String resolveCachedMapFileName() {
430434
// Keep only filesystem-safe characters and avoid collisions for same basename from different folders.
431435
String safeBase = baseName.replaceAll("[^a-zA-Z0-9._-]", "_");
432436
String pathHash = Integer.toUnsignedString(inputMap.getAbsolutePath().hashCode(), 36);
433-
return safeBase + "_" + pathHash + "_cached.w3x";
437+
String modeSuffix = luaMode ? "lua" : "jass";
438+
return safeBase + "_" + pathHash + "_" + modeSuffix + "_cached.w3x";
434439
}
435440

436441
protected File ensureWritableTargetFile(File targetFile, String dialogTitle, String lockMessage,
@@ -500,6 +505,7 @@ private boolean isLocked(File targetMap) {
500505
*/
501506
protected File ensureCachedMap(WurstGui gui) throws IOException {
502507
File cachedMap = getCachedMapFile();
508+
cleanupOppositeModeCacheAndOutputs();
503509

504510
if (!map.isPresent()) {
505511
throw new RequestFailedException(MessageType.Error, "No source map provided");
@@ -517,6 +523,29 @@ protected File ensureCachedMap(WurstGui gui) throws IOException {
517523
return cachedMap;
518524
}
519525

526+
private void cleanupOppositeModeCacheAndOutputs() {
527+
if (cachedMapFileName.isEmpty()) {
528+
File cacheDir = new File(getBuildDir(), "cache");
529+
String oppositeModeCacheName = resolveCachedMapFileName(!runArgs.isLua());
530+
java.nio.file.Path oppositeModeCache = new File(cacheDir, oppositeModeCacheName).toPath();
531+
try {
532+
java.nio.file.Files.deleteIfExists(oppositeModeCache);
533+
} catch (IOException e) {
534+
WLogger.warning("Could not delete opposite-mode cached map: " + oppositeModeCache + " (" + e.getMessage() + ")");
535+
}
536+
}
537+
538+
File buildDir = getBuildDir();
539+
File oppositeCompiledOutput = runArgs.isLua()
540+
? new File(buildDir, BUILD_COMPILED_JASS_NAME)
541+
: new File(buildDir, BUILD_COMPILED_LUA_NAME);
542+
try {
543+
java.nio.file.Files.deleteIfExists(oppositeCompiledOutput.toPath());
544+
} catch (IOException e) {
545+
WLogger.warning("Could not delete opposite-mode compiled output: " + oppositeCompiledOutput + " (" + e.getMessage() + ")");
546+
}
547+
}
548+
520549
protected CompilationResult compileScript(ModelManager modelManager, WurstGui gui, Optional<File> testMap,
521550
WurstProjectConfigData projectConfigData, File buildDir,
522551
boolean isProd) throws Exception {

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/StmtTranslation.java

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public static ImStmt translate(StmtForFrom s, ImTranslator t, ImFunction f) {
101101
ImExpr nextCallWrapped = ExprTranslation.wrapTranslation(s, t, nextCall, nextReturn, loopVarType);
102102

103103
imBody.add(ImSet(s, ImVarAccess(t.getVarFor(s.getLoopVar())), nextCallWrapped));
104-
ImVar continueFlag = createContinueFlagVar(s, f);
105-
imBody.addAll(translateLoopBodyWithContinue(t, f, s.getBody(), continueFlag, s));
104+
imBody.addAll(translateLoopBody(t, f, s.getBody(), s));
106105

107106
result.add(ImLoop(s, imBody));
108107
}
@@ -207,8 +206,7 @@ public static ImStmt translate(StmtForIn forIn, ImTranslator t, ImFunction f) {
207206
imBody.add(ImSet(forIn, ImVarAccess(t.getVarFor(forIn.getLoopVar())), nextCallWrapped));
208207

209208
// loop body
210-
ImVar continueFlag = createContinueFlagVar(forIn, f);
211-
imBody.addAll(translateLoopBodyWithContinue(t, f, forIn.getBody(), continueFlag, forIn));
209+
imBody.addAll(translateLoopBody(t, f, forIn.getBody(), forIn));
212210

213211
// optional close()<S>()
214212
Optional<FuncLink> closeFunc = forIn.attrCloseFunc();
@@ -296,8 +294,7 @@ private static ImStmt case_StmtForRange(ImTranslator t, ImFunction f, LocalVarDe
296294
// exitwhen imLoopVar > toExpr
297295
imBody.add(ImExitwhen(trace, ImOperatorCall(opCompare, ImExprs(ImVarAccess(imLoopVar), toExpr))));
298296
// loop body:
299-
ImVar continueFlag = createContinueFlagVar(trace, f);
300-
imBody.addAll(translateLoopBodyWithContinue(t, f, body, continueFlag, trace));
297+
imBody.addAll(translateLoopBody(t, f, body, trace));
301298
// set imLoopVar = imLoopVar + stepExpr
302299
imBody.add(ImSet(trace, ImVarAccess(imLoopVar), ImOperatorCall(opStep, ImExprs(ImVarAccess(imLoopVar), stepExpr))));
303300
result.add(ImLoop(trace, imBody));
@@ -323,8 +320,7 @@ public static ImStmt translate(StmtIf s, ImTranslator t, ImFunction f) {
323320

324321

325322
public static ImStmt translate(StmtLoop s, ImTranslator t, ImFunction f) {
326-
ImVar continueFlag = createContinueFlagVar(s, f);
327-
return ImLoop(s, ImStmts(translateLoopBodyWithContinue(t, f, s.getBody(), continueFlag, s)));
323+
return ImLoop(s, ImStmts(translateLoopBody(t, f, s.getBody(), s)));
328324
}
329325

330326

@@ -344,8 +340,7 @@ public static ImStmt translate(StmtWhile s, ImTranslator t, ImFunction f) {
344340
List<ImStmt> body = Lists.newArrayList();
345341
// exitwhen not while_condition
346342
body.add(ImExitwhen(s.getCond(), ImOperatorCall(WurstOperator.NOT, ImExprs(s.getCond().imTranslateExpr(t, f)))));
347-
ImVar continueFlag = createContinueFlagVar(s, f);
348-
body.addAll(translateLoopBodyWithContinue(t, f, s.getBody(), continueFlag, s));
343+
body.addAll(translateLoopBody(t, f, s.getBody(), s));
349344
return ImLoop(s, ImStmts(body));
350345
}
351346

@@ -371,6 +366,55 @@ private static List<ImStmt> translateLoopBodyWithContinue(ImTranslator t, ImFunc
371366
return guardedBody;
372367
}
373368

369+
private static List<ImStmt> translateLoopBody(ImTranslator t, ImFunction f, List<WStatement> body, Element trace) {
370+
if (!hasContinueForCurrentLoop(body)) {
371+
return t.translateStatements(f, body);
372+
}
373+
ImVar continueFlag = createContinueFlagVar(trace, f);
374+
return translateLoopBodyWithContinue(t, f, body, continueFlag, trace);
375+
}
376+
377+
private static boolean hasContinueForCurrentLoop(List<WStatement> body) {
378+
for (WStatement statement : body) {
379+
if (statement instanceof StmtContinue) {
380+
return true;
381+
}
382+
if (statement instanceof StmtLoop
383+
|| statement instanceof StmtWhile
384+
|| statement instanceof StmtForIn
385+
|| statement instanceof StmtForFrom
386+
|| statement instanceof StmtForRangeUp
387+
|| statement instanceof StmtForRangeDown) {
388+
// Continue inside nested loops should not trigger guarding for the outer loop.
389+
continue;
390+
}
391+
if (statement instanceof WBlock && hasContinueForCurrentLoop(((WBlock) statement).getBody())) {
392+
return true;
393+
}
394+
if (statement instanceof StmtIf) {
395+
StmtIf stmtIf = (StmtIf) statement;
396+
if (hasContinueForCurrentLoop(stmtIf.getThenBlock()) || hasContinueForCurrentLoop(stmtIf.getElseBlock())) {
397+
return true;
398+
}
399+
}
400+
if (statement instanceof SwitchStmt) {
401+
SwitchStmt switchStmt = (SwitchStmt) statement;
402+
for (SwitchCase switchCase : switchStmt.getCases()) {
403+
if (hasContinueForCurrentLoop(switchCase.getStmts())) {
404+
return true;
405+
}
406+
}
407+
if (switchStmt.getSwitchDefault() instanceof SwitchDefaultCaseStatements) {
408+
SwitchDefaultCaseStatements defaultCase = (SwitchDefaultCaseStatements) switchStmt.getSwitchDefault();
409+
if (hasContinueForCurrentLoop(defaultCase.getStmts())) {
410+
return true;
411+
}
412+
}
413+
}
414+
}
415+
return false;
416+
}
417+
374418
public static ImStmt translate(StmtSkip s, ImTranslator translator, ImFunction f) {
375419
return ImHelper.nullExpr();
376420
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/ExprTranslation.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,40 @@ public static LuaExpr translate(ImDealloc e, LuaTranslator tr) {
5353
public static LuaExpr translate(ImFuncRef e, LuaTranslator tr) {
5454
// return LuaAst.LuaExprFuncRef(tr.luaFunc.getFor(e.getFunc()));
5555
// alternative: use xpcall to get stacktraces (did not work)
56+
boolean returnsValue = !(e.getFunc().getReturnType() instanceof ImVoid);
5657
LuaVariable dots = LuaAst.LuaVariable("...", LuaAst.LuaNoExpr());
57-
LuaVariable tempDots = LuaAst.LuaVariable("temp", LuaAst.LuaExprVarAccess(dots));
58-
LuaVariable tempRes = LuaAst.LuaVariable("tempRes", LuaAst.LuaExprNull());
59-
return LuaAst.LuaExprFunctionAbstraction(LuaAst.LuaParams(dots),
60-
LuaAst.LuaStatements(
61-
tempDots,
62-
tempRes,
63-
LuaAst.LuaExprFunctionCallByName("xpcall",
64-
LuaAst.LuaExprlist(
65-
LuaAst.LuaExprFunctionAbstraction(
66-
LuaAst.LuaParams(),
67-
LuaAst.LuaStatements(
68-
LuaAst.LuaAssignment(LuaAst.LuaExprVarAccess(tempRes),
69-
LuaAst.LuaExprFunctionCall(tr.luaFunc.getFor(e.getFunc()), LuaAst.LuaExprlist(LuaAst.LuaExprVarAccess(tempDots)))))
70-
),
71-
// LuaAst.LuaLiteral("function(err) " + errorFuncName(tr) + "(tostring(err)) end")
72-
LuaAst.LuaLiteral("function(err) if err == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end xpcall(function() " + callErrorFunc(tr, "tostring(err)") + " end, function(err2) if err2 == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end BJDebugMsg(\"error reporting error: \" .. tostring(err2)) BJDebugMsg(\"while reporting: \" .. tostring(err)) end) end")
73-
// unfortunately BJDebugMsg(debug.traceback()) is not working
74-
)
75-
),
76-
LuaAst.LuaReturn(LuaAst.LuaExprVarAccess(tempRes))
77-
)
78-
);
58+
LuaStatements callbackBody = LuaAst.LuaStatements();
59+
if (returnsValue) {
60+
LuaVariable tempRes = LuaAst.LuaVariable("tempRes", LuaAst.LuaExprNull());
61+
callbackBody.add(tempRes);
62+
callbackBody.add(LuaAst.LuaExprFunctionCallByName("xpcall",
63+
LuaAst.LuaExprlist(
64+
LuaAst.LuaExprFunctionAbstraction(
65+
LuaAst.LuaParams(dots.copy()),
66+
LuaAst.LuaStatements(
67+
LuaAst.LuaAssignment(LuaAst.LuaExprVarAccess(tempRes),
68+
LuaAst.LuaExprFunctionCall(tr.luaFunc.getFor(e.getFunc()), LuaAst.LuaExprlist(LuaAst.LuaExprVarAccess(dots.copy())))))
69+
),
70+
LuaAst.LuaLiteral("function(err) if err == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end BJDebugMsg(\"lua callback error: \" .. tostring(err)) xpcall(function() " + callErrorFunc(tr, "tostring(err)") + " end, function(err2) if err2 == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end BJDebugMsg(\"error reporting error: \" .. tostring(err2)) BJDebugMsg(\"while reporting: \" .. tostring(err)) end) end"),
71+
LuaAst.LuaExprVarAccess(dots.copy())
72+
)
73+
));
74+
callbackBody.add(LuaAst.LuaReturn(LuaAst.LuaExprVarAccess(tempRes)));
75+
} else {
76+
callbackBody.add(LuaAst.LuaExprFunctionCallByName("xpcall",
77+
LuaAst.LuaExprlist(
78+
LuaAst.LuaExprFunctionAbstraction(
79+
LuaAst.LuaParams(dots.copy()),
80+
LuaAst.LuaStatements(
81+
LuaAst.LuaExprFunctionCall(tr.luaFunc.getFor(e.getFunc()), LuaAst.LuaExprlist(LuaAst.LuaExprVarAccess(dots.copy())))
82+
)
83+
),
84+
LuaAst.LuaLiteral("function(err) if err == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end BJDebugMsg(\"lua callback error: \" .. tostring(err)) xpcall(function() " + callErrorFunc(tr, "tostring(err)") + " end, function(err2) if err2 == \"" + WURST_ABORT_THREAD_SENTINEL + "\" then return end BJDebugMsg(\"error reporting error: \" .. tostring(err2)) BJDebugMsg(\"while reporting: \" .. tostring(err)) end) end"),
85+
LuaAst.LuaExprVarAccess(dots.copy())
86+
)
87+
));
88+
}
89+
return LuaAst.LuaExprFunctionAbstraction(LuaAst.LuaParams(dots), callbackBody);
7990
}
8091

8192
private static String callErrorFunc(LuaTranslator tr, String msg) {

0 commit comments

Comments
 (0)