Skip to content
Closed
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 @@ -100,12 +100,17 @@ public CompilationProcess(WurstGui gui, RunArgs runArgs) {
}

gui.sendProgress("Printing Lua");
StringBuilder luaOutput = new StringBuilder();
timeTaker.measure("Print Lua", () -> luaCode.print(luaOutput, 0));
mapScript = luaOutput;
String compiledLua;
if (compiler.getCompiledLuaScript() != null) {
compiledLua = compiler.getCompiledLuaScript();
} else {
StringBuilder luaOutput = new StringBuilder();
timeTaker.measure("Print Lua", () -> luaCode.print(luaOutput, 0));
compiledLua = luaOutput.toString();
}
mapScript = compiledLua;
LuaTranslator.assertNoLeakedHashtableNativeCalls(mapScript.toString());
LuaTranslator.assertNoLeakedGetHandleIdCalls(mapScript.toString());
CharSequence compiledLua = mapScript;
outputMapscript = timeTaker.measure("Write Lua",
() -> writeMapscript(compiledLua));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import org.wurstscript.projectconfig.WurstProjectConfigData;
import de.peeeq.wurstio.languageserver.requests.RequestFailedException;
Expand All @@ -30,6 +31,8 @@
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.translation.lua.modular.PackageChunkEmitter;
import de.peeeq.wurstscript.translation.lua.modular.PackageChunkResult;
import de.peeeq.wurstscript.types.TypesHelper;
import de.peeeq.wurstscript.utils.LineOffsets;
import de.peeeq.wurstscript.utils.NotNullList;
Expand Down Expand Up @@ -71,6 +74,21 @@ public class WurstCompilerJassImpl implements WurstCompiler {
private final List<File> dependencies = Lists.newArrayList();
private final @Nullable MpqEditor mapFileMpq;
private final TimeTaker timeTaker;
private @Nullable String compiledLuaScript = null;

public @Nullable String getCompiledLuaScript() {
return compiledLuaScript;
}

private File resolveCacheDir() {
if (runArgs.getCachePath() != null) {
return new File(runArgs.getCachePath());
}
if (runArgs.getWorkspaceroot() != null) {
return new File(runArgs.getWorkspaceroot(), "_build/cache/lua");
}
return new File("./_build/cache/lua");
}

public WurstCompilerJassImpl(@Nullable File projectFolder, WurstGui gui, @Nullable MpqEditor mapFileMpq, RunArgs runArgs) {
this(new TimeTaker.Default(), projectFolder, gui, mapFileMpq, runArgs);
Expand All @@ -83,7 +101,8 @@ public WurstCompilerJassImpl(TimeTaker timeTaker, @Nullable File projectFolder,
this.runArgs = runArgs;
this.errorHandler = new ErrorHandler(gui);
this.parser = new WurstParser(errorHandler, gui);
this.checker = new WurstChecker(gui, errorHandler, runArgs.isLegacyJassTypeChecks());
File cacheDir = runArgs.getCachePath() != null ? new File(runArgs.getCachePath()) : null;
this.checker = new WurstChecker(gui, errorHandler, runArgs.isLegacyJassTypeChecks(), runArgs.isIncremental(), cacheDir);
this.mapFileMpq = mapFileMpq;
}

Expand Down Expand Up @@ -824,16 +843,30 @@ private CompilationUnit parseFile2(File file) {
}

public CompilationUnit parse(String fileName, Reader reader) {
if (fileName.endsWith(".j")) {
return parser.parseJass(reader, fileName, hasCommonJ);
}
if (fileName.endsWith(".jurst")) {
return parser.parseJurst(reader, fileName, hasCommonJ);
// Capture the exact source snapshot being compiled (file or unsaved buffer)
// so downstream caches hash what the compiler saw, not a possibly-stale disk file.
// The reader is consumed fully and the parser re-reads from the snapshot copy.
final String sourceContent;
final Reader parseReader;
try {
sourceContent = CharStreams.toString(reader);
parseReader = new StringReader(sourceContent);
} catch (IOException e) {
throw new Error("Could not read source for " + fileName, e);
}
if (runArgs.isPrettyPrint()) {
parser.setRemoveSugar(false);
CompilationUnit cu;
if (fileName.endsWith(".j")) {
cu = parser.parseJass(parseReader, fileName, hasCommonJ);
} else if (fileName.endsWith(".jurst")) {
cu = parser.parseJurst(parseReader, fileName, hasCommonJ);
} else {
if (runArgs.isPrettyPrint()) {
parser.setRemoveSugar(false);
}
cu = parser.parse(parseReader, fileName, hasCommonJ);
}
return parser.parse(reader, fileName, hasCommonJ);
cu.getCuInfo().setSourceContent(sourceContent);
return cu;
}

private CompilationUnit emptyCompilationUnit() {
Expand Down Expand Up @@ -996,9 +1029,11 @@ public LuaCompilationUnit transformProgToLua() {
imTranslator2.assertProperties(AssertProperty.NOTUPLES);
timeTaker.endPhase();

optimizer.removeGarbage();
imProg.flatten(imTranslator);
timeTaker.endPhase();
if (!runArgs.isIncremental()) {
optimizer.removeGarbage();
imProg.flatten(imTranslator);
timeTaker.endPhase();
}
stage = 10;
if (runArgs.isLocalOptimizations()) {
beginPhase(10, "local optimizations");
Expand All @@ -1017,13 +1052,15 @@ public LuaCompilationUnit transformProgToLua() {

printDebugImProg("./test-output/lua/im " + stage++ + "_afterlocalopts.im");

boolean garbageChanged = optimizer.removeGarbage();
imProg.flatten(imTranslator);

// Re-run to avoid #883
if (garbageChanged) {
optimizer.removeGarbage();
if (!runArgs.isIncremental()) {
boolean garbageChanged = optimizer.removeGarbage();
imProg.flatten(imTranslator);

// Re-run to avoid #883
if (garbageChanged) {
optimizer.removeGarbage();
imProg.flatten(imTranslator);
}
}

printDebugImProg("./test-output/lua/im " + stage++ + "_afterremoveGarbage1.im");
Expand All @@ -1033,15 +1070,19 @@ public LuaCompilationUnit transformProgToLua() {
beginPhase(12, "froptimize");
optimizer.optimize();

optimizer.removeGarbage();
imProg.flatten(imTranslator);
if (!runArgs.isIncremental()) {
optimizer.removeGarbage();
imProg.flatten(imTranslator);
}
printDebugImProg("./test-output/lua/im " + stage++ + "_afteroptimize.im");
timeTaker.endPhase();
}
beginPhase(13, "lua remove garbage");
RemoveGarbage.removeGarbage(imProg, imTranslator);
imProg.flatten(imTranslator);
timeTaker.endPhase();
if (!runArgs.isIncremental()) {
beginPhase(13, "lua remove garbage");
RemoveGarbage.removeGarbage(imProg, imTranslator);
imProg.flatten(imTranslator);
timeTaker.endPhase();
}

beginPhase(13, "prepare lua dispatch");
LuaDispatchPreparation.prepare(imProg, imTranslator);
Expand All @@ -1053,8 +1094,17 @@ public LuaCompilationUnit transformProgToLua() {
timeTaker.endPhase();

beginPhase(14, "translate to lua");
LuaTranslator luaTranslator = new LuaTranslator(imProg, imTranslator);
LuaCompilationUnit luaCode = luaTranslator.translate();
LuaCompilationUnit luaCode;
if (runArgs.isIncremental()) {
File cacheDir = resolveCacheDir();
PackageChunkResult chunkResult = PackageChunkEmitter.emitAndAssemble(imProg, imTranslator, cacheDir);
this.compiledLuaScript = chunkResult.getAssembledScript();
luaCode = chunkResult.getAssembledCu();
} else {
LuaTranslator luaTranslator = new LuaTranslator(imProg, imTranslator);
luaCode = luaTranslator.translate();
this.compiledLuaScript = null;
}
ImAttrType.setWurstClassType(TypesHelper.imInt());
timeTaker.endPhase();
return luaCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public interface ModelManager {

void buildProject();

default void buildProject(boolean doTypeCheck) {
buildProject();
}

/**
* refresh discovered dependency roots (e.g. _build/dependencies after grill install)
*/
Expand All @@ -42,6 +46,10 @@ public interface ModelManager {
Changes syncDependencyCompilationUnits();

Changes syncCompilationUnit(WFile changedFilePath);

default Changes syncProjectFiles() {
return Changes.empty();
}

Changes syncCompilationUnitContent(WFile filename, String contents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public void clean() {
*/
@Override
public void buildProject() {
buildProject(true);
}

@Override
public void buildProject(boolean doTypeCheck) {
try {
WurstGui gui = new WurstGuiLogger();
readDependencies();
Expand All @@ -162,7 +167,9 @@ public void buildProject() {

resolveImports(gui);

doTypeCheck(gui);
if (doTypeCheck) {
doTypeCheck(gui);
}
} catch (Exception e) {
WLogger.severe(e);
throw new ModelManagerException(e);
Expand Down Expand Up @@ -236,6 +243,49 @@ public synchronized Changes syncDependencyCompilationUnits() {
return changes;
}

@Override
public synchronized Changes syncProjectFiles() {
if (model == null) {
buildProject();
return Changes.empty();
}
Changes changes = Changes.empty();
File wurstFolder = new File(projectPath, "wurst");
Set<WFile> expectedProjectFiles = new HashSet<>();
if (wurstFolder.exists()) {
collectWurstFiles(wurstFolder, expectedProjectFiles);
}

WurstModel model2 = model;
if (model2 != null) {
List<WFile> loadedProjectFiles = model2.stream()
.map(this::wFile)
.filter(this::isUnderWurstFolder)
.collect(Collectors.toList());

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

for (WFile f : expectedProjectFiles) {
changes = changes.mergeWith(syncCompilationUnit(f));
}
return changes;
}

private void collectWurstFiles(File dir, Set<WFile> result) {
for (File f : getFiles(dir)) {
if (f.isDirectory()) {
collectWurstFiles(f, result);
} else if (f.getName().endsWith(".wurst") || f.getName().endsWith(".jurst") || f.getName().endsWith(".j")) {
result.add(WFile.create(f));
}
}
}

private String getCanonicalPath(File f) {
try {
return f.getCanonicalPath();
Expand Down Expand Up @@ -985,6 +1035,18 @@ private boolean isUnderDependenciesFolder(WFile file) {
}
}

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


public File getProjectPath() {
return projectPath;
Expand Down
Loading