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
21 changes: 9 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ jobs:
working_directory: ~/repo/de.peeeq.wurstscript

environment:
# Customize the JVM maximum heap limit
JVM_OPTS: -Xmx3200m
GRADLE_OPTS: -Dorg.gradle.parallel=false -Dorg.gradle.workers.max=2
TERM: dumb

steps:
Expand All @@ -28,20 +27,18 @@ jobs:
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "build.gradle" }}
- v2-gradle-{{ checksum "build.gradle" }}-{{ checksum "gradle.properties" }}-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: ./gradlew dependencies
- v2-gradle-

- save_cache:
paths:
- ~/.gradle
key: v1-dependencies-{{ checksum "build.gradle" }}

# run tests
- run: ./gradlew test --info
key: v2-gradle-{{ checksum "build.gradle" }}-{{ checksum "gradle.properties" }}-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}

# report tests results
- run: ./gradlew jacocoTestReport coveralls
# run tests and coverage in one invocation to avoid duplicate config/startup cost
- run:
name: Run tests and coverage
command: ./gradlew --no-daemon --stacktrace test jacocoTestReport coveralls
no_output_timeout: 30m

3 changes: 2 additions & 1 deletion de.peeeq.wurstscript/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ org.gradle.configuration-cache=true
org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.java.installations.auto-download=true
org.gradle.java.installations.auto-detect=true
org.gradle.java.installations.auto-detect=true
org.gradle.jvmargs=-Xmx3g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8
3 changes: 2 additions & 1 deletion de.peeeq.wurstscript/parserspec/wurstscript.parseq
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ ControlflowStatement =
CompoundStatement
| StmtReturn(@ignoreForEquality de.peeeq.wurstscript.parser.WPos source, OptExpr returnedObj)
| StmtExitwhen(@ignoreForEquality de.peeeq.wurstscript.parser.WPos source, Expr cond)
| StmtContinue(de.peeeq.wurstscript.parser.WPos source)

CompoundStatement =
StmtIf(@ignoreForEquality de.peeeq.wurstscript.parser.WPos source, Expr cond, WStatements thenBlock, WStatements elseBlock, boolean hasElse)
Expand Down Expand Up @@ -1073,4 +1074,4 @@ Annotation.getAnnotationType()

Annotation.getAnnotationMessage()
returns String
implemented by de.peeeq.wurstscript.attributes.Annotations.annotationMessage
implemented by de.peeeq.wurstscript.attributes.Annotations.annotationMessage
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ statement:
| stmtSet (externalLambda|NL)
| stmtReturn (externalLambda|NL)
| stmtBreak NL
| stmtContinue NL
| stmtSkip NL
| expr (externalLambda|NL)
| stmtIf
Expand Down Expand Up @@ -435,6 +436,7 @@ forIteratorLoop:


stmtBreak:'break';
stmtContinue:'continue';
stmtSkip:'skip';


Expand All @@ -461,6 +463,7 @@ WHILE: 'while';
FOR: 'for';
IN: 'in';
BREAK: 'break';
CONTINUE: 'continue';
NEW: 'new';
NULL: 'null';
PACKAGE: 'package';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import de.peeeq.wurstscript.translation.imtojass.ImAttrType;
import de.peeeq.wurstscript.translation.imtojass.ImToJassTranslator;
import de.peeeq.wurstscript.translation.imtranslation.*;
import de.peeeq.wurstscript.translation.lua.translation.RemoveGarbage;
import de.peeeq.wurstscript.translation.lua.translation.LuaTranslator;
import de.peeeq.wurstscript.types.TypesHelper;
import de.peeeq.wurstscript.utils.LineOffsets;
Expand Down Expand Up @@ -937,7 +938,12 @@ public LuaCompilationUnit transformProgToLua() {
printDebugImProg("./test-output/lua/im " + stage++ + "_afteroptimize.im");
timeTaker.endPhase();
}
beginPhase(13, "translate to lua");
beginPhase(13, "lua remove garbage");
RemoveGarbage.removeGarbage(imProg);
imProg.flatten(imTranslator);
timeTaker.endPhase();

beginPhase(14, "translate to lua");
LuaTranslator luaTranslator = new LuaTranslator(imProg, imTranslator);
LuaCompilationUnit luaCode = luaTranslator.translate();
ImAttrType.setWurstClassType(TypesHelper.imInt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,8 @@ private Workitem getNextWorkItem() {
FileReconcile fr = (FileReconcile) change;
affected = modelManager.syncCompilationUnitContent(fr.getFilename(), fr.getContents());
} else if (change instanceof FileSystemUpdated || change instanceof FileDeleted) {
// Dependency roots may have changed (e.g. grill install), refresh and sync incrementally.
modelManager.refreshDependencies();
if (change instanceof FileDeleted) {
affected = modelManager.removeCompilationUnit(change.getFilename());
} else {
affected = modelManager.syncCompilationUnit(change.getFilename());
}
// Dependency roots may have changed (e.g. grill install), sync full dependency state.
affected = modelManager.syncDependencyCompilationUnits();
} else {
// Editor-triggered updates (save/close) use the normal incremental path.
affected = modelManager.syncCompilationUnit(change.getFilename());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public interface ModelManager {
*/
void refreshDependencies();

/**
* refresh and synchronize all dependency compilation units.
* This handles dependency delete/replace/move/rename scenarios robustly.
*/
Changes syncDependencyCompilationUnits();

Changes syncCompilationUnit(WFile changedFilePath);

Changes syncCompilationUnitContent(WFile filename, String contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.function.Consumer;
Expand Down Expand Up @@ -184,6 +185,39 @@ public void refreshDependencies() {
readDependencies();
}

@Override
public synchronized Changes syncDependencyCompilationUnits() {
readDependencies();

WurstModel model2 = model;
if (model2 == null) {
return Changes.empty();
}

Set<WFile> expectedDependencyFiles = getDependencyWurstFiles().stream()
.map(WFile::create)
.collect(Collectors.toSet());

List<WFile> loadedDependencyFiles = model2.stream()
.map(this::wFile)
.filter(this::isUnderDependenciesFolder)
.collect(Collectors.toList());

Changes changes = Changes.empty();

for (WFile loadedFile : loadedDependencyFiles) {
if (!expectedDependencyFiles.contains(loadedFile)) {
changes = changes.mergeWith(removeCompilationUnit(loadedFile));
}
}

for (WFile dependencyFile : expectedDependencyFiles) {
changes = changes.mergeWith(syncCompilationUnit(dependencyFile));
}

return changes;
}

private String getCanonicalPath(File f) {
try {
return f.getCanonicalPath();
Expand Down Expand Up @@ -861,6 +895,18 @@ private boolean isAlreadyLoaded(WFile file) {
return false;
}

private boolean isUnderDependenciesFolder(WFile file) {
try {
Path filePath = file.getPath().toAbsolutePath().normalize();
Path dependencyRoot = Paths.get(projectPath.getAbsolutePath(), "_build", "dependencies")
.toAbsolutePath()
.normalize();
return filePath.startsWith(dependencyRoot) && Utils.isWurstFile(filePath.toString());
} catch (FileNotFoundException e) {
return false;
}
}


public File getProjectPath() {
return projectPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ public List<Either<String, MarkedString>> case_StmtExitwhen(StmtExitwhen stmtExi
return string("exitwhen: exits the current loop when the condition is true.");
}

@Override
public List<Either<String, MarkedString>> case_StmtContinue(StmtContinue stmtContinue) {
return string("continue: skips the rest of the current loop iteration.");
}

@Override
public List<Either<String, MarkedString>> case_ConstructorDef(ConstructorDef constr) {
return description(constr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import de.peeeq.wurstscript.jassprinter.JassPrinter;
import de.peeeq.wurstscript.luaAst.LuaCompilationUnit;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.translation.lua.translation.LuaTranslator;
import de.peeeq.wurstscript.utils.LineOffsets;
import de.peeeq.wurstscript.utils.Utils;
import net.moonlightflower.wc3libs.bin.app.W3I;
Expand Down Expand Up @@ -167,6 +168,7 @@ protected File compileMap(File projectFolder, WurstGui gui, Optional<File> mapCo
luaCode.get().print(sb, 0);

String compiledMapScript = sb.toString();
LuaTranslator.assertNoLeakedHashtableNativeCalls(compiledMapScript);
File buildDir = getBuildDir();
File outFile = new File(buildDir, BUILD_COMPILED_LUA_NAME);
Files.write(compiledMapScript.getBytes(Charsets.UTF_8), outFile);
Expand Down Expand Up @@ -418,8 +420,12 @@ private String resolveCachedMapFileName() {
if (!cachedMapFileName.isEmpty()) {
return cachedMapFileName;
}
return resolveCachedMapFileName(runArgs.isLua());
}

private String resolveCachedMapFileName(boolean luaMode) {
if (!map.isPresent()) {
return "cached_map.w3x";
return luaMode ? "cached_map_lua.w3x" : "cached_map_jass.w3x";
}
File inputMap = map.get();
String inputName = inputMap.getName();
Expand All @@ -428,7 +434,8 @@ private String resolveCachedMapFileName() {
// Keep only filesystem-safe characters and avoid collisions for same basename from different folders.
String safeBase = baseName.replaceAll("[^a-zA-Z0-9._-]", "_");
String pathHash = Integer.toUnsignedString(inputMap.getAbsolutePath().hashCode(), 36);
return safeBase + "_" + pathHash + "_cached.w3x";
String modeSuffix = luaMode ? "lua" : "jass";
return safeBase + "_" + pathHash + "_" + modeSuffix + "_cached.w3x";
}

protected File ensureWritableTargetFile(File targetFile, String dialogTitle, String lockMessage,
Expand Down Expand Up @@ -498,6 +505,7 @@ private boolean isLocked(File targetMap) {
*/
protected File ensureCachedMap(WurstGui gui) throws IOException {
File cachedMap = getCachedMapFile();
cleanupOppositeModeCacheAndOutputs();

if (!map.isPresent()) {
throw new RequestFailedException(MessageType.Error, "No source map provided");
Expand All @@ -515,6 +523,29 @@ protected File ensureCachedMap(WurstGui gui) throws IOException {
return cachedMap;
}

private void cleanupOppositeModeCacheAndOutputs() {
if (cachedMapFileName.isEmpty()) {
File cacheDir = new File(getBuildDir(), "cache");
String oppositeModeCacheName = resolveCachedMapFileName(!runArgs.isLua());
java.nio.file.Path oppositeModeCache = new File(cacheDir, oppositeModeCacheName).toPath();
try {
java.nio.file.Files.deleteIfExists(oppositeModeCache);
} catch (IOException e) {
WLogger.warning("Could not delete opposite-mode cached map: " + oppositeModeCache + " (" + e.getMessage() + ")");
}
}

File buildDir = getBuildDir();
File oppositeCompiledOutput = runArgs.isLua()
? new File(buildDir, BUILD_COMPILED_JASS_NAME)
: new File(buildDir, BUILD_COMPILED_LUA_NAME);
try {
java.nio.file.Files.deleteIfExists(oppositeCompiledOutput.toPath());
} catch (IOException e) {
WLogger.warning("Could not delete opposite-mode compiled output: " + oppositeCompiledOutput + " (" + e.getMessage() + ")");
}
}

protected CompilationResult compileScript(ModelManager modelManager, WurstGui gui, Optional<File> testMap,
WurstProjectConfigData projectConfigData, File buildDir,
boolean isProd) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.peeeq.wurstscript;

public class WurstKeywords {
public static final String[] KEYWORDS = new String[]{"class", "return", "if", "else", "while", "for", "in", "break", "new", "null",
public static final String[] KEYWORDS = new String[]{"class", "return", "if", "else", "while", "for", "in", "break", "continue", "new", "null",
"package", "endpackage", "function", "returns", "public", "private", "protected", "import", "initlater", "native", "nativetype", "extends",
"interface", "implements", "module", "use", "abstract", "static", "thistype", "override", "immutable", "it", "array", "and",
"or", "not", "this", "construct", "ondestroy", "destroy", "type", "constant", "endfunction", "nothing", "init", "castTo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private static void collect(Builder<Element, VarDef> result, ExprClosure closure

private static boolean isLocalVariable(NameDef def) {
return def instanceof LocalVarDef
|| def instanceof WParameter && !(def.getParent().getParent() instanceof TupleDef);
|| def instanceof WParameter && !(def.getParent().getParent() instanceof TupleDef)
|| def instanceof WShortParameter;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.peeeq.wurstscript.types.FunctionSignature;
import de.peeeq.wurstscript.types.VariableBinding;
import de.peeeq.wurstscript.types.WurstType;
import de.peeeq.wurstscript.types.WurstTypeCode;
import de.peeeq.wurstscript.types.WurstTypeUnknown;
import de.peeeq.wurstscript.utils.Utils;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -57,9 +58,41 @@ public static FunctionSignature calculate(StmtCall fc) {
fc.addError("Cannot infer type for type parameter " + mapping.printUnboundTypeVars());
}

checkCodeClosureCaptures(fc, sig);

return sig;
}

private static void checkCodeClosureCaptures(StmtCall fc, FunctionSignature sig) {
if (!sig.isValidParameterNumber(fc.getArgs().size())) {
return;
}
for (int i = 0; i < fc.getArgs().size(); i++) {
Expr arg = fc.getArgs().get(i);
if (!(arg instanceof ExprClosure)) {
continue;
}
if (!(sig.getParamType(i) instanceof WurstTypeCode)) {
continue;
}
ExprClosure closure = (ExprClosure) arg;
if (!closure.attrCapturedVariables().isEmpty()) {
String codeLambdaContext = codeLambdaContext(fc);
closure.attrCapturedVariables().entries().forEach(entry ->
entry.getKey().addError("Cannot capture local variable '" + entry.getValue().getName()
+ "' in anonymous function" + codeLambdaContext + ". This is only possible with closures."));
}
}
}

private static String codeLambdaContext(StmtCall stmtCall) {
String funcName = stmtCall instanceof FunctionCall fc ? fc.getFuncName() : "<unknown>";
if (stmtCall instanceof ExprMemberMethod) {
return " passed as code to ." + funcName + "() ->";
}
return " passed as code to " + funcName + "() ->";
}

private static FunctionSignature filterSigs(
Collection<FunctionSignature> sigs,
List<WurstType> argTypes, StmtCall location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ private static ImmutableCollection<FunctionSignature> findBestSignature(StmtCall
fc.getErrorHandler().sendError(c);
}
}

return ImmutableList.copyOf(inferred);
ImmutableList.Builder<FunctionSignature> result = ImmutableList.builder();
for (int i = 0; i < n; i++) {
var r = sigs.get(i).tryMatchAgainstArgs(argTypes, argsNode, fc);
if (r.getBadness() == bestBad) {
result.add(inferred[i]);
}
}
return result.build();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ public static String description(StmtSkip stmtSkip) {
return "The skip statement does nothing. Just skip this line.";
}

public static String description(StmtContinue stmtContinue) {
return "continue: Skips the rest of the current loop iteration.";
}

public static String description(StmtWhile stmtWhile) {
return "While Statement: Repeat while the condition is true.";
}
Expand Down
Loading
Loading