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 @@ -40,7 +40,7 @@
import java.util.*;
import java.util.stream.Collectors;

public class CompiletimeFunctionRunner {
public class CompiletimeFunctionRunner implements AutoCloseable {

private final ImProg imProg;
private final ILInterpreter interpreter;
Expand Down Expand Up @@ -659,4 +659,17 @@ public void setOutputStream(PrintStream printStream) {
interpreter.getGlobalState().setOutStream(printStream);
}

/**
* Releases any resources held by the interpreter or global state (such as open SQLite connections and statements)
* created during compiletime function execution.
*/
@Override
public void close() {
if (interpreter != null) {
interpreter.close();
} else if (globalState != null) {
globalState.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ public void runCompiletime(WurstProjectConfigData projectConfigData, boolean isP
// compile & inject object-editor data
// TODO run optimizations later?
gui.sendProgress("Running compiletime functions");
CompiletimeFunctionRunner ctr = new CompiletimeFunctionRunner(imTranslator, getImProg(), getMapFile(), getMapfileMpqEditor(), gui,
CompiletimeFunctions, projectConfigData, isProd, cache);
ctr.setInjectObjects(runArgs.isInjectObjects());
ctr.setOutputStream(new PrintStream(System.err));
ctr.run();
// Use try-with-resources to release open native resources (e.g., SQLite DB handles) after compiletime execution finishes.
try (CompiletimeFunctionRunner ctr = new CompiletimeFunctionRunner(imTranslator, getImProg(), getMapFile(), getMapfileMpqEditor(), gui,
CompiletimeFunctions, projectConfigData, isProd, cache)) {
ctr.setInjectObjects(runArgs.isInjectObjects());
ctr.setOutputStream(new PrintStream(System.err));
ctr.run();
}
}

if (gui.getErrorCount() > 0) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.wurstscript.projectconfig.WurstProjectConfigData;
import de.peeeq.wurstio.CompiletimeFunctionRunner;
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
import de.peeeq.wurstio.jassinterpreter.ReflectionNativeProvider;
import de.peeeq.wurstio.languageserver.ModelManager;
import de.peeeq.wurstio.languageserver.WFile;
import de.peeeq.wurstscript.RunArgs;
Expand Down Expand Up @@ -162,17 +161,15 @@ public int getTotalTests() {
public TestResult runTests(ImTranslator translator, ImProg imProg, Optional<FuncDef> funcToTest, Optional<CompilationUnit> cu) {
WurstGui gui = new TestGui();

CompiletimeFunctionRunner cfr = new CompiletimeFunctionRunner(translator, imProg, Optional.empty(), null, gui,
CompiletimeFunctions, WurstProjectConfigData.empty(), false, false);
ILInterpreter interpreter = cfr.getInterpreter();
ProgramState globalState = cfr.getGlobalState();
if (globalState == null) {
globalState = new ProgramState(gui, imProg, true);
}
if (interpreter == null) {
interpreter = new ILInterpreter(imProg, gui, Optional.empty(), globalState);
interpreter.addNativeProvider(new ReflectionNativeProvider(interpreter));
}
// Use try-with-resources to ensure NativeProvider resources (e.g. SQLite database connections and file handles)
// created during test execution are automatically closed when the language server finishes processing the request.
try (CompiletimeFunctionRunner cfr = new CompiletimeFunctionRunner(translator, imProg, Optional.empty(), null, gui,
CompiletimeFunctions, WurstProjectConfigData.empty(), false, false)) {
// CompiletimeFunctionRunner always constructs both, and cfr.close() (the
// try-with-resources above) owns their lifecycle. Building local replacements
// here would be dead code that also escaped that cleanup, so use the runner's.
ILInterpreter interpreter = cfr.getInterpreter();
ProgramState globalState = cfr.getGlobalState();

redirectInterpreterOutput(globalState);

Expand Down Expand Up @@ -355,6 +352,7 @@ public TestResult runTests(ImTranslator translator, ImProg imProg, Optional<Func

WLogger.info("finished tests");
return new TestResult(successTests.size(), successTests.size() + failTests.size());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static de.peeeq.wurstscript.validation.GlobalCaches.LOCAL_STATE_CACHE;
import static de.peeeq.wurstscript.validation.GlobalCaches.LOCAL_STATE_NOARG_CACHE;

public class ILInterpreter implements AbstractInterpreter {
public class ILInterpreter implements AbstractInterpreter, AutoCloseable {
private ImProg prog;
private final ProgramState globalState;
private final TimerMockHandler timerMockHandler = new TimerMockHandler();
Expand Down Expand Up @@ -570,4 +570,14 @@ public int getMaxInstanceCount(int val) {
}
return (int) count;
}

/**
* Closes the interpreter by shutting down its global ProgramState and releasing registered native provider resources.
*/
@Override
public void close() {
if (globalState != null) {
globalState.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

import java.io.PrintStream;

public interface NativesProvider {
public interface NativesProvider extends AutoCloseable {

ILconst invoke(String funcname, ILconst[] args) throws NoSuchNativeException;

void setOutStream(PrintStream outStream);

/**
* Optional lifecycle hook to release any resources (e.g. SQLite database connections, JDBC statements)
* held by this provider when interpreter execution completes.
*/
@Override
default void close() {
// Default implementation does nothing
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.PrintStream;
import java.util.*;

public class ProgramState extends State {
public class ProgramState extends State implements AutoCloseable {

public static final int GENERATED_BY_WURST = 42;
private de.peeeq.wurstscript.jassIm.Element lastStatement;
Expand Down Expand Up @@ -185,6 +185,20 @@ public Iterable<NativesProvider> getNativeProviders() {
return nativeProviders;
}

/**
* Closes all registered NativesProvider instances to release external native resources (such as SQLite connections).
*/
@Override
public void close() {
for (NativesProvider provider : nativeProviders) {
try {
provider.close();
} catch (Exception e) {
WLogger.severe(e);
}
}
}

public @Nullable NativesProvider getCachedNativeProvider(String funcName) {
return nativeProviderByFunc.get(funcName);
}
Expand Down
Loading
Loading