Skip to content

Commit

Permalink
Add primitives to PolyglotPlugin for Java interop
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed May 3, 2019
1 parent 48361de commit a53ebcb
Showing 1 changed file with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit a53ebcb

Please sign in to comment.