use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class HostProxy method getMembers.
@ExportMessage
@TruffleBoundary
Object getMembers(@SuppressWarnings("unused") boolean includeInternal, @CachedLibrary("this") InteropLibrary library, @Shared("cache") @Cached(value = "this.context.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws UnsupportedMessageException {
if (proxy instanceof ProxyObject) {
Object result = guestToHostCall(library, cache.memberKeys, context, proxy);
if (result == null) {
result = EMPTY;
}
Object guestValue = context.toGuestValue(library, result);
InteropLibrary interop = InteropLibrary.getFactory().getUncached();
if (!interop.hasArrayElements(guestValue)) {
if (guestValue instanceof HostObject) {
HostObject hostObject = (HostObject) guestValue;
if (hostObject.obj.getClass().isArray() && !hostObject.getHostClassCache().isArrayAccess()) {
throw illegalProxy(context, "getMemberKeys() returned a Java array %s, but allowArrayAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
} else if (hostObject.obj instanceof List && !hostObject.getHostClassCache().isListAccess()) {
throw illegalProxy(context, "getMemberKeys() returned a Java List %s, but allowListAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
}
}
throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
}
// Todo: Use interop to determine an array element type when the GR-5737 is resolved.
for (int i = 0; i < interop.getArraySize(guestValue); i++) {
try {
Object element = interop.readArrayElement(guestValue, i);
if (!interop.isString(element)) {
throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
}
} catch (UnsupportedOperationException e) {
CompilerDirectives.shouldNotReachHere(e);
} catch (InvalidArrayIndexException e) {
continue;
}
}
return guestValue;
} else {
throw UnsupportedMessageException.create();
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class HostLanguage method getLanguageView.
@Override
@TruffleBoundary
protected Object getLanguageView(HostContext hostContext, Object value) {
Object wrapped;
if (value instanceof TruffleObject) {
InteropLibrary lib = InteropLibrary.getFactory().getUncached(value);
try {
assert !lib.hasLanguage(value) || lib.getLanguage(value) != HostLanguage.class;
} catch (UnsupportedMessageException e) {
throw shouldNotReachHere(e);
}
wrapped = HostToTypeNode.convertToObject(hostContext, value, lib);
} else {
wrapped = value;
}
return HostObject.forObject(wrapped, hostContext);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class SLException method typeError.
/**
* Provides a user-readable message for run-time type errors. SL is strongly typed, i.e., there
* are no automatic type conversions of values.
*/
@TruffleBoundary
public static SLException typeError(Node operation, Object... values) {
StringBuilder result = new StringBuilder();
result.append("Type error");
if (operation != null) {
SourceSection ss = operation.getEncapsulatingSourceSection();
if (ss != null && ss.isAvailable()) {
result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn());
}
}
result.append(": operation");
if (operation != null) {
NodeInfo nodeInfo = SLLanguage.lookupNodeInfo(operation.getClass());
if (nodeInfo != null) {
result.append(" \"").append(nodeInfo.shortName()).append("\"");
}
}
result.append(" not defined for");
String sep = " ";
for (int i = 0; i < values.length; i++) {
/*
* For primitive or foreign values we request a language view so the values are printed
* from the perspective of simple language and not another language. Since this is a
* rather rarely invoked exceptional method, we can just create the language view for
* primitive values and then conveniently request the meta-object and display strings.
* Using the language view for core builtins like the typeOf builtin might not be a good
* idea for performance reasons.
*/
Object value = SLLanguageView.forValue(values[i]);
result.append(sep);
sep = ", ";
if (value == null) {
result.append("ANY");
} else {
InteropLibrary valueLib = InteropLibrary.getFactory().getUncached(value);
if (valueLib.hasMetaObject(value) && !valueLib.isNull(value)) {
String qualifiedName;
try {
qualifiedName = UNCACHED_LIB.asString(UNCACHED_LIB.getMetaQualifiedName(valueLib.getMetaObject(value)));
} catch (UnsupportedMessageException e) {
throw shouldNotReachHere(e);
}
result.append(qualifiedName);
result.append(" ");
}
if (valueLib.isString(value)) {
result.append("\"");
}
result.append(valueLib.toDisplayString(value));
if (valueLib.isString(value)) {
result.append("\"");
}
}
}
return new SLException(result.toString(), operation);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class WasmJsApiSuite method checkCustomSection.
private static void checkCustomSection(byte[] expected, ByteArrayBuffer actual) throws InvalidArrayIndexException, UnsupportedMessageException {
InteropLibrary interop = InteropLibrary.getUncached(actual);
Assert.assertEquals("Custom section length", expected.length, (int) interop.getArraySize(actual));
for (int i = 0; i < expected.length; i++) {
Assert.assertEquals("Custom section data", expected[i], interop.readArrayElement(actual, i));
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class WasmJsApiSuite method testTableInstanceOutOfBoundsSet.
@Test
public void testTableInstanceOutOfBoundsSet() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
final InteropLibrary lib = InteropLibrary.getUncached();
WasmContext wasmContext = WasmContext.get(null);
final WasmFunctionInstance functionInstance = new WasmFunctionInstance(wasmContext, null, new RootNode(wasmContext.language()) {
@Override
public Object execute(VirtualFrame frame) {
return 42;
}
}.getCallTarget());
// We should be able to set element 1.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object writeTable = wasm.readMember("table_write");
lib.execute(writeTable, table, 0, functionInstance);
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
}
// But not element 2.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object writeTable = wasm.readMember("table_write");
lib.execute(writeTable, table, 1, functionInstance);
Assert.fail("Should have failed - export count exceeds the limit");
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
} catch (WasmJsApiException e) {
Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
}
});
}
Aggregations