use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotValueDispatch method toStringImpl.
protected String toStringImpl(@SuppressWarnings("unused") Object languageContext, Object receiver) throws AssertionError {
InteropLibrary lib = InteropLibrary.getFactory().getUncached(receiver);
Object result = lib.toDisplayString(receiver);
InteropLibrary resultLib = InteropLibrary.getFactory().getUncached(result);
try {
return resultLib.asString(result);
} catch (UnsupportedMessageException e) {
throw shouldNotReachHere("toDisplayString must be coercible to java.lang.String, but is not.", e);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class WebAssembly method readModuleImports.
private static HashMap<String, ImportModule> readModuleImports(WasmModule module, Object importObject) {
CompilerAsserts.neverPartOfCompilation();
final Sequence<ModuleImportDescriptor> imports = moduleImports(module);
if (imports.getArraySize() != 0 && importObject == null) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Module requires imports, but import object is undefined.");
}
HashMap<String, ImportModule> importModules = new HashMap<>();
final InteropLibrary lib = InteropLibrary.getUncached();
try {
int i = 0;
while (i < imports.getArraySize()) {
final ModuleImportDescriptor d = (ModuleImportDescriptor) imports.readArrayElement(i);
final Object importedModule = getMember(importObject, d.module());
final Object member = getMember(importedModule, d.name());
switch(d.kind()) {
case function:
if (!lib.isExecutable(member)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not callable.");
}
WasmFunction f = module.importedFunction(d.name());
ensureImportModule(importModules, d.module()).addFunction(d.name(), Pair.create(f, member));
break;
case memory:
if (!(member instanceof WasmMemory)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid memory.");
}
ensureImportModule(importModules, d.module()).addMemory(d.name(), (WasmMemory) member);
break;
case table:
if (!(member instanceof WasmTable)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid table.");
}
ensureImportModule(importModules, d.module()).addTable(d.name(), (WasmTable) member);
break;
case global:
if (!(member instanceof WasmGlobal)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid global.");
}
ensureImportModule(importModules, d.module()).addGlobal(d.name(), (WasmGlobal) member);
break;
default:
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented case: " + d.kind());
}
i += 1;
}
} catch (InvalidArrayIndexException | UnknownIdentifierException | ClassCastException | UnsupportedMessageException e) {
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unexpected state.");
}
return importModules;
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class HighlightRequestHandler method findOtherReadOrWrites.
List<? extends DocumentHighlight> findOtherReadOrWrites(TextDocumentSurrogate surrogate, InstrumentableNode nodeAtCaret, int line, int character) {
InteropUtils.VariableInfo[] caretVariables = InteropUtils.getNodeObjectVariables(nodeAtCaret);
if (caretVariables.length > 0) {
Set<String> variableNames = new HashSet<>();
for (InteropUtils.VariableInfo varInfo : caretVariables) {
if (contains(varInfo.getSourceSection(), line, character)) {
variableNames.add(varInfo.getName());
}
}
Object scope = getScope(surrogate, nodeAtCaret);
List<DocumentHighlight> highlights = new ArrayList<>();
while (scope != null) {
InteropLibrary interop = InteropLibrary.getUncached(scope);
if (interop.hasSourceLocation(scope)) {
try {
SourceSection sourceLocation = interop.getSourceLocation(scope);
Node[] nodeLocation = new Node[] { null };
EventBinding<LoadSourceSectionListener> binding = env.getInstrumenter().attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().sourceSectionEquals(sourceLocation).build(), e -> {
Node node = e.getNode();
if (nodeLocation[0] == null || isParent(node, nodeLocation[0])) {
nodeLocation[0] = node;
}
}, true);
binding.dispose();
addHighlights(nodeLocation[0], variableNames, highlights);
} catch (UnsupportedMessageException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
}
if (interop.hasScopeParent(scope)) {
try {
scope = interop.getScopeParent(scope);
} catch (UnsupportedMessageException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
} else {
scope = null;
}
}
return highlights;
}
return Collections.emptyList();
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class HashTest method testInteropMessages.
private static void testInteropMessages(Object hash, KeyFactory<?> keyFactory) throws InteropException {
final int count = 100;
final int inc = 2;
InteropLibrary interop = InteropLibrary.getUncached();
assertTrue(interop.hasHashEntries(hash));
for (int i = 0; i < count; i += inc) {
Object key = keyFactory.create(i);
Object value = i;
assertNonExisting(hash, key, interop);
interop.writeHashEntry(hash, key, value);
}
assertEquals((count / inc), interop.getHashSize(hash));
for (int i = 0; i < count; i++) {
Object key = keyFactory.create(i);
if ((i & 1) == 0) {
Object expectedValue = i;
assertExisting(hash, key, interop);
assertEquals(expectedValue, interop.readHashValue(hash, key));
assertEquals(expectedValue, interop.readHashValueOrDefault(hash, key, "failure"));
} else {
assertNonExisting(hash, key, interop);
assertEquals("failure", interop.readHashValueOrDefault(hash, key, "failure"));
}
}
Map<Object, Integer> expected = new HashMap<>();
for (int i = 0; i < count; i += inc) {
Object key = keyFactory.create(i);
int value = -1 * i;
interop.writeHashEntry(hash, key, value);
assertExisting(hash, key, interop);
assertEquals(value, interop.readHashValue(hash, key));
expected.put(key, value);
}
Object iterator = interop.getHashEntriesIterator(hash);
assertTrue(interop.isIterator(iterator));
Map<Object, Integer> expected2 = new HashMap<>();
while (interop.hasIteratorNextElement(iterator)) {
Object entry = interop.getIteratorNextElement(iterator);
assertTrue(interop.hasArrayElements(entry));
Object key = interop.readArrayElement(entry, 0);
int value = (int) interop.readArrayElement(entry, 1);
int expectedValue = expected.remove(key);
assertEquals(expectedValue, value);
int newValue = -1 * value;
interop.writeArrayElement(entry, 1, newValue);
expected2.put(key, newValue);
}
assertTrue(expected.isEmpty());
Set<Object> expectedKeys = new HashSet<>(expected2.keySet());
Collection<Integer> expectedValues = new ArrayList<>(expected2.values());
iterator = interop.getHashEntriesIterator(hash);
while (interop.hasIteratorNextElement(iterator)) {
Object entry = interop.getIteratorNextElement(iterator);
Object key = interop.readArrayElement(entry, 0);
int value = (int) interop.readArrayElement(entry, 1);
int expectedValue = expected2.remove(key);
assertEquals(expectedValue, value);
}
assertTrue(expected2.isEmpty());
iterator = interop.getHashKeysIterator(hash);
while (interop.hasIteratorNextElement(iterator)) {
Object key = interop.getIteratorNextElement(iterator);
assertTrue(expectedKeys.remove(key));
}
assertTrue(expectedKeys.isEmpty());
iterator = interop.getHashValuesIterator(hash);
while (interop.hasIteratorNextElement(iterator)) {
Object value = interop.getIteratorNextElement(iterator);
assertTrue(expectedValues.remove(value));
}
assertTrue(expectedValues.isEmpty());
for (int i = 0; i < count; i += inc) {
Object key = keyFactory.create(i);
interop.removeHashEntry(hash, key);
assertNonExisting(hash, key, interop);
}
assertEquals(0, interop.getHashSize(hash));
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class GR31558 method expectArrayArg.
private void expectArrayArg(int elementCount, Object result) {
assertEquals(Arrays.toString(actualArguments), 1, actualArguments.length);
assertEquals("EXECUTE", result);
Object arg0 = actualArguments[0];
InteropLibrary interop = InteropLibrary.getUncached();
try {
assertTrue(interop.asString(interop.toDisplayString(arg0)), interop.hasArrayElements(arg0));
assertEquals(interop.asString(interop.toDisplayString(arg0)), elementCount, interop.getArraySize(arg0));
} catch (UnsupportedMessageException e) {
throw new AssertionError(e);
}
}
Aggregations