use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotHostObjectPartialEvaluationTest method writeBufferByte.
@Test
public void writeBufferByte() {
for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
if (buffer.isReadOnly()) {
continue;
}
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 {
interop.writeBufferByte(bufferHostObject, 0, (byte) 42);
} catch (UnsupportedMessageException | InvalidBufferOffsetException e) {
throw new RuntimeException(e);
}
return null;
}
};
assertPartialEvalNoInvokes(node);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class BaseInterop method toDisplayString.
@ExportMessage
@TruffleBoundary
public static Object toDisplayString(StaticObject object, boolean allowSideEffects) {
if (object.isForeignObject()) {
InteropLibrary interopLibrary = InteropLibrary.getUncached();
try {
return "Foreign object: " + interopLibrary.asString(interopLibrary.toDisplayString(object.rawForeignObject(), allowSideEffects));
} catch (UnsupportedMessageException e) {
throw EspressoError.shouldNotReachHere("Interop library failed to convert display string to string");
}
}
if (StaticObject.isNull(object)) {
return "NULL";
}
Klass thisKlass = object.getKlass();
Meta meta = thisKlass.getMeta();
if (allowSideEffects) {
// Call guest toString.
int toStringIndex = meta.java_lang_Object_toString.getVTableIndex();
Method toString = thisKlass.vtableLookup(toStringIndex);
return meta.toHostString((StaticObject) toString.invokeDirect(object));
}
// Handle some special instances without side effects.
if (thisKlass == meta.java_lang_Class) {
return "class " + thisKlass.getTypeAsString();
}
if (thisKlass == meta.java_lang_String) {
return meta.toHostString(object);
}
return thisKlass.getTypeAsString() + "@" + Integer.toHexString(System.identityHashCode(object));
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class Target_java_lang_System method handleForeignArray.
@TruffleBoundary
private static void handleForeignArray(Object src, int srcPos, Object dest, int destPos, int length, Klass destType, Meta meta, SubstitutionProfiler profiler) {
InteropLibrary library = InteropLibrary.getUncached();
ToEspressoNode toEspressoNode = ToEspressoNodeGen.getUncached();
if (library.isNull(src) || library.isNull(dest)) {
throw throwNullPointerEx(meta, profiler);
}
if (!library.hasArrayElements(src) || !library.hasArrayElements(dest)) {
throw throwArrayStoreEx(meta, profiler);
}
try {
int srclen = (int) library.getArraySize(src);
int destlen = (int) library.getArraySize(dest);
boundsCheck(meta, srclen, srcPos, destlen, destPos, length, profiler);
for (int i = 0; i < length; i++) {
Object cpy = toEspressoNode.execute(library.readArrayElement(src, i + srcPos), destType);
library.writeArrayElement(dest, destPos + i, cpy);
}
} catch (UnsupportedMessageException | UnsupportedTypeException e) {
CompilerDirectives.transferToInterpreter();
throw EspressoError.shouldNotReachHere();
} catch (InvalidArrayIndexException e) {
throw throwArrayStoreEx(meta, profiler);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class GR31558 method expectArgs.
private void expectArgs(int argCount, Object result) {
assertEquals(Arrays.toString(actualArguments), argCount, actualArguments.length);
assertEquals("EXECUTE", result);
if (argCount == 0) {
return;
}
Object arg0 = actualArguments[0];
InteropLibrary interop = InteropLibrary.getUncached();
try {
assertFalse(interop.asString(interop.toDisplayString(arg0)), interop.hasArrayElements(arg0));
} catch (UnsupportedMessageException e) {
throw new AssertionError(e);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class GR31558 method before.
@Before
public void before() {
HostAccess.Builder hostAccessBuilder = //
HostAccess.newBuilder().allowAccessAnnotatedBy(//
HostAccess.Export.class).allowPublicAccess(//
false).allowArrayAccess(//
true).allowIterableAccess(//
true).allowIteratorAccess(//
true).allowImplementationsAnnotatedBy(FunctionalInterface.class);
Context.Builder contextBuilder = Context.newBuilder();
contextBuilder.allowHostAccess(hostAccessBuilder.build());
setupEnv(contextBuilder, new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
String src = request.getSource().getCharacters().toString();
RootCallTarget invokeTestApi;
if ("testFunction".equals(src)) {
invokeTestApi = new RootNode(ProxyLanguage.get(null)) {
@Override
public Object execute(VirtualFrame frame) {
try {
InteropLibrary interop = InteropLibrary.getUncached();
Object test = frame.getArguments()[0];
interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction());
if (interop.isMemberExisting(test, "testMap")) {
interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction(false, false, false, true));
}
if (interop.isMemberExisting(test, "testMapEntry")) {
interop.invokeMember(test, "testMapEntry", "hi", new ArgumentsCollectorFunction(false, false, true, false));
}
if (interop.isMemberExisting(test, "testList")) {
interop.invokeMember(test, "testList", "hi", new ArgumentsCollectorFunction(false, false, true, false));
}
if (interop.isMemberExisting(test, "testIterator")) {
interop.invokeMember(test, "testIterator", "hi", new ArgumentsCollectorFunction(false, true, false, false));
}
if (interop.isMemberExisting(test, "testIterable")) {
interop.invokeMember(test, "testIterable", "hi", new ArgumentsCollectorFunction(true, false, false, false));
}
return "success";
} catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e) {
CompilerDirectives.transferToInterpreter();
throw new AssertionError(e);
}
}
}.getCallTarget();
} else {
throw new IllegalArgumentException(src);
}
return RootNode.createConstantNode(new HostExceptionTest.CatcherObject(invokeTestApi)).getCallTarget();
}
});
}
Aggregations