Skip to content

Commit

Permalink
Improve backend error handling
Browse files Browse the repository at this point in the history
- Fix debug logging for #11088--attempt to create an exception that is its own
  cause fails.
- In case the parser is used after closing, throw an `IllegalStateException`
  instead of UB.
  • Loading branch information
kazcw committed Sep 19, 2024
1 parent f05997d commit 4951a6f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ private static boolean searchFromDirToTop(Throwable chain, File root, String...
return false;
}

private long state;
private long stateUnlessClosed;

private Parser(long stateIn) {
state = stateIn;
stateUnlessClosed = stateIn;
}

private static native long allocState();
Expand Down Expand Up @@ -115,14 +115,24 @@ public long isIdentOrOperator(CharSequence input) {
return isIdentOrOperator(inputBuf);
}

private long getState() {
if (stateUnlessClosed != 0) {
return stateUnlessClosed;
} else {
throw new IllegalStateException("Parser used after close()");
}
}

public ByteBuffer parseInputLazy(CharSequence input) {
var state = getState();
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
return parseTreeLazy(state, inputBuf);
}

public Tree parse(CharSequence input) {
var state = getState();
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
Expand All @@ -140,7 +150,7 @@ public static String getWarningMessage(Warning warning) {

@Override
public void close() {
freeState(state);
state = 0;
freeState(stateUnlessClosed);
stateUnlessClosed = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Value getType(String moduleName, String typeName) {
var ex =
new NullPointerException(
"Cannot get type for " + moduleName + " type: " + typeName + " at " + module);
ex.initCause(ex);
ex.initCause(e);
throw ex;
}
}
Expand Down

0 comments on commit 4951a6f

Please sign in to comment.