From a53ebcb8cf23c022ffb95a2bf752314a9a493454 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Fri, 3 May 2019 14:01:51 +0200 Subject: [PATCH] Add primitives to PolyglotPlugin for Java interop --- .../squeak/nodes/plugins/PolyglotPlugin.java | 91 ++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/src/de.hpi.swa.graal.squeak/src/de/hpi/swa/graal/squeak/nodes/plugins/PolyglotPlugin.java b/src/de.hpi.swa.graal.squeak/src/de/hpi/swa/graal/squeak/nodes/plugins/PolyglotPlugin.java index e147cc978..5abacb104 100644 --- a/src/de.hpi.swa.graal.squeak/src/de/hpi/swa/graal/squeak/nodes/plugins/PolyglotPlugin.java +++ b/src/de.hpi.swa.graal.squeak/src/de/hpi/swa/graal/squeak/nodes/plugins/PolyglotPlugin.java @@ -105,8 +105,8 @@ protected final Object doParseAndCall(@SuppressWarnings("unused") final Object r } } catch (final RuntimeException e) { if (e instanceof TruffleException) { - PrimGetLastErrorNode.setLastError(e); - throw new PrimitiveFailed(); + PrimGetLastErrorNode.setLastError(e); + throw new PrimitiveFailed(); } else { throw e; } @@ -799,6 +799,93 @@ protected static final Object doWrite(final Object receiver, final NativeObject } } + /* + * Java interop. + */ + + @GenerateNodeFactory + @SqueakPrimitive(names = "primitiveAddToHostClassPath") + protected abstract static class PrimAddToHostClassPathNode extends AbstractPrimitiveNode implements BinaryPrimitive { + protected PrimAddToHostClassPathNode(final CompiledMethodObject method) { + super(method); + } + + @Specialization(guards = {"value.isByteType()"}) + protected final boolean doAddToHostClassPath(@SuppressWarnings("unused") final Object receiver, final NativeObject value) { + final String path = value.asStringUnsafe(); + try { + method.image.env.addToHostClassPath(method.image.env.getTruffleFile(path)); + } catch (final SecurityException e) { + PrimGetLastErrorNode.setLastError(e); + throw new PrimitiveFailed(); + } + return method.image.sqTrue; + } + } + + @GenerateNodeFactory + @SqueakPrimitive(names = "primitiveLookupHostSymbol") + protected abstract static class PrimLookupHostSymbolNode extends AbstractPrimitiveNode implements BinaryPrimitive { + protected PrimLookupHostSymbolNode(final CompiledMethodObject method) { + super(method); + } + + @Specialization(guards = {"method.image.env.isHostLookupAllowed()", "value.isByteType()"}) + protected final Object doLookupHostSymbol(@SuppressWarnings("unused") final Object receiver, final NativeObject value) { + final String symbolName = value.asStringUnsafe(); + Object hostValue; + try { + hostValue = method.image.env.lookupHostSymbol(symbolName); + } catch (final RuntimeException e) { + hostValue = null; + } + if (hostValue == null) { + throw new PrimitiveFailed(); + } else { + return hostValue; + } + } + } + + @GenerateNodeFactory + @SqueakPrimitive(names = "primitiveIsHostFunction") + protected abstract static class PrimIsHostFunctionNode extends AbstractPrimitiveNode implements UnaryPrimitiveWithoutFallback { + protected PrimIsHostFunctionNode(final CompiledMethodObject method) { + super(method); + } + + @Specialization + protected final boolean doIsHostFunction(@SuppressWarnings("unused") final Object receiver) { + return method.image.asBoolean(method.image.env.isHostFunction(receiver)); + } + } + + @GenerateNodeFactory + @SqueakPrimitive(names = "primitiveIsHostObject") + protected abstract static class PrimIsHostObjectNode extends AbstractPrimitiveNode implements UnaryPrimitiveWithoutFallback { + protected PrimIsHostObjectNode(final CompiledMethodObject method) { + super(method); + } + + @Specialization + protected final boolean doIsHostObject(@SuppressWarnings("unused") final Object receiver) { + return method.image.asBoolean(method.image.env.isHostObject(receiver)); + } + } + + @GenerateNodeFactory + @SqueakPrimitive(names = "primitiveIsHostSymbol") + protected abstract static class PrimIsHostSymbolNode extends AbstractPrimitiveNode implements UnaryPrimitiveWithoutFallback { + protected PrimIsHostSymbolNode(final CompiledMethodObject method) { + super(method); + } + + @Specialization + protected final boolean doIsHostSymbol(@SuppressWarnings("unused") final Object receiver) { + return method.image.asBoolean(method.image.env.isHostSymbol(receiver)); + } + } + /* * Helper functions. */