use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotHostObjectPartialEvaluationTest method isBufferWritable.
@Test
public void isBufferWritable() {
for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
getContext().initialize(ProxyLanguage.ID);
final Object bufferHostObject = LanguageContext.get(null).getEnv().asGuestValue(buffer);
final RootNode node = new RootNode(null) {
@Child
InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1);
@Override
public Object execute(VirtualFrame frame) {
try {
return interop.isBufferWritable(bufferHostObject);
} catch (UnsupportedMessageException e) {
throw new RuntimeException(e);
}
}
};
assertPartialEvalEquals(buffer.isReadOnly() ? PolyglotHostObjectPartialEvaluationTest::constantFalse : PolyglotHostObjectPartialEvaluationTest::constantTrue, node);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotHostObjectPartialEvaluationTest method readBufferByte.
@Test
public void readBufferByte() {
for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
getContext().initialize(ProxyLanguage.ID);
final Object bufferHostObject = LanguageContext.get(null).getEnv().asGuestValue(buffer);
final RootNode node = new RootNode(null) {
@Child
InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1);
@Override
public Object execute(VirtualFrame frame) {
try {
return interop.readBufferByte(bufferHostObject, 0);
} catch (UnsupportedMessageException | InvalidBufferOffsetException e) {
throw new RuntimeException(e);
}
}
};
assertPartialEvalNoInvokes(node);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotValueDispatch method getSourceLocation.
@Override
public SourceSection getSourceLocation(Object languageContext, Object receiver) {
if (languageContext == null) {
return null;
}
PolyglotLanguageContext context = (PolyglotLanguageContext) languageContext;
Object prev = hostEnter(context);
try {
InteropLibrary lib = InteropLibrary.getFactory().getUncached(receiver);
com.oracle.truffle.api.source.SourceSection result = null;
if (lib.hasSourceLocation(receiver)) {
try {
result = lib.getSourceLocation(receiver);
} catch (UnsupportedMessageException e) {
}
}
if (result == null) {
return null;
}
return PolyglotImpl.getPolyglotSourceSection(impl, result);
} catch (Throwable e) {
throw guestToHostException(context, e, true);
} finally {
hostLeave(context, prev);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotWrapper method equals.
@TruffleBoundary
static boolean equals(Object context, Object receiver, Object obj) {
if (obj == null) {
return false;
} else if (receiver == obj) {
return true;
}
PolyglotLanguageContext languageContext = (PolyglotLanguageContext) context;
if (languageContext != null) {
PolyglotContextImpl.State localContextState = languageContext.context.state;
if (localContextState.isInvalidOrClosed()) {
return false;
}
}
Object prev = null;
try {
prev = PolyglotValueDispatch.hostEnter(languageContext);
} catch (Throwable t) {
// Can no longer call interop.
return false;
}
try {
InteropLibrary receiverLib = InteropLibrary.getUncached(receiver);
InteropLibrary objLib = InteropLibrary.getUncached(obj);
return receiverLib.isIdentical(receiver, obj, objLib);
} catch (Throwable t) {
// propagate errors in languages they should be reported.
throw PolyglotImpl.guestToHostException(languageContext, t, true);
} finally {
try {
PolyglotValueDispatch.hostLeave(languageContext, prev);
} catch (Throwable t) {
// ignore errors leaving we cannot propagate them.
}
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotWrapper method hashCode.
@TruffleBoundary
static int hashCode(Object context, Object receiver) {
PolyglotLanguageContext languageContext = (PolyglotLanguageContext) context;
if (languageContext != null) {
PolyglotContextImpl.State localContextState = languageContext.context.state;
if (localContextState.isInvalidOrClosed()) {
return System.identityHashCode(receiver);
}
}
Object prev = null;
try {
prev = PolyglotValueDispatch.hostEnter(languageContext);
} catch (Throwable t) {
// Can no longer call interop.
return System.identityHashCode(receiver);
}
try {
InteropLibrary receiverLib = InteropLibrary.getUncached(receiver);
if (receiverLib.hasIdentity(receiver)) {
return receiverLib.identityHashCode(receiver);
} else {
return System.identityHashCode(receiver);
}
} catch (Throwable t) {
// propagate errors in languages they should be reported.
throw PolyglotImpl.guestToHostException(languageContext, t, true);
} finally {
try {
PolyglotValueDispatch.hostLeave(languageContext, prev);
} catch (Throwable t) {
// ignore errors leaving we cannot propagate them.
}
}
}
Aggregations