Skip to content

Commit

Permalink
Rename errno-related methods in NFI for clarity
Browse files Browse the repository at this point in the history
* Also pass a Node to get the language instance for efficiency.
  • Loading branch information
eregon committed Oct 28, 2024
1 parent 22a12ae commit fa5bda9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ public class ErrorContext {
}

@SuppressWarnings("preview") private MemorySegment errnoLocation;
private Integer nativeErrno = null;
private Integer nfiErrno = null;
final PanamaNFIContext ctx;

public boolean nativeErrnoSet() {
return (nativeErrno != null);
public boolean isNFIErrnoSet() {
return (nfiErrno != null);
}

public int getNativeErrno() {
return nativeErrno;
public int getNFIErrno() {
return nfiErrno;
}

public void setNativeErrno(int nativeErrno) {
this.nativeErrno = nativeErrno;
public void setNFIErrno(int nativeErrno) {
this.nfiErrno = nativeErrno;
}

@SuppressWarnings({"preview", "restricted"})
Expand Down Expand Up @@ -118,12 +118,12 @@ private MemorySegment getErrnoLocation() {
}

@SuppressWarnings("preview")
int getErrno() {
int getNativeErrno() {
return getErrnoLocation().get(ValueLayout.JAVA_INT, 0);
}

@SuppressWarnings("preview")
void setErrno(int newErrno) {
void setNativeErrno(int newErrno) {
getErrnoLocation().set(ValueLayout.JAVA_INT, 0, newErrno);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Object doGeneric(VirtualFrame frame) {
throw silenceException(RuntimeException.class, ex);
}

return signatureInfo.execute(signature, args, address);
return signatureInfo.execute(signature, args, address, this);
}

@SuppressWarnings({"unchecked"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ Object doCall(VirtualFrame frame, Object receiver,
}

try {
ErrorContext ctx = PanamaNFILanguage.get(null).errorContext.get();
int errnoMirror = ctx.getErrno();
if (ctx.nativeErrnoSet()) {
ctx.setErrno(ctx.getNativeErrno());
ErrorContext ctx = PanamaNFILanguage.get(this).errorContext.get();
int savedNativeErrno = ctx.getNativeErrno();
if (ctx.isNFIErrnoSet()) {
ctx.setNativeErrno(ctx.getNFIErrno());
}
Object result = interop.execute(receiver, args);

ctx.setNativeErrno(ctx.getErrno());
ctx.setErrno(errnoMirror);
ctx.setNFIErrno(ctx.getNativeErrno());
ctx.setNativeErrno(savedNativeErrno);

return result;
} catch (InteropException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
import com.oracle.truffle.nfi.backend.panama.FunctionExecuteNode.SignatureExecuteNode;
import com.oracle.truffle.nfi.backend.panama.FunctionExecuteNodeGen.SignatureExecuteNodeGen;
import com.oracle.truffle.nfi.backend.spi.NFIBackendSignatureBuilderLibrary;
import com.oracle.truffle.nfi.backend.spi.NFIBackendSignatureLibrary;
Expand Down Expand Up @@ -373,20 +374,20 @@ PanamaType getRetType() {
return retType;
}

Object execute(PanamaSignature signature, Object[] args, @SuppressWarnings("preview") MemorySegment segment) {
Object execute(PanamaSignature signature, Object[] args, @SuppressWarnings("preview") MemorySegment segment, Node node) {
assert signature.signatureInfo == this;
CompilerAsserts.partialEvaluationConstant(retType);

ErrorContext ctx = PanamaNFILanguage.get(null).errorContext.get();
ErrorContext ctx = PanamaNFILanguage.get(node).errorContext.get();
try {
int errnoMirror = ctx.getErrno();
if (ctx.nativeErrnoSet()) {
ctx.setErrno(ctx.getNativeErrno());
int savedNativeErrno = ctx.getNativeErrno();
if (ctx.isNFIErrnoSet()) {
ctx.setNativeErrno(ctx.getNFIErrno());
}
Object result = (Object) downcallHandle.invokeExact(segment, args);

ctx.setNativeErrno(ctx.getErrno());
ctx.setErrno(errnoMirror);
ctx.setNFIErrno(ctx.getNativeErrno());
ctx.setNativeErrno(savedNativeErrno);

if (result == null) {
return NativePointer.NULL;
Expand Down

0 comments on commit fa5bda9

Please sign in to comment.