Skip to content

Commit

Permalink
Extend interop with BigInteger, meta parents, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 22, 2024
1 parent 4bb8aa7 commit 4dc15d8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,30 +413,30 @@ protected boolean fitsInLong(@Shared("lib") @CachedLibrary(limit = "LIMIT") fina
}

@ExportMessage
protected boolean fitsInFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
protected boolean fitsInBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (wrappedObject instanceof BigInteger) {
return true;
}
if (isNumber()) {
return lib.fitsInFloat(wrappedObject);
return lib.fitsInBigInteger(wrappedObject);
} else {
return false;
}
}

@ExportMessage
protected boolean fitsInDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
protected boolean fitsInFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (isNumber()) {
return lib.fitsInDouble(wrappedObject);
return lib.fitsInFloat(wrappedObject);
} else {
return false;
}
}

@ExportMessage
protected boolean fitsInBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (wrappedObject instanceof BigInteger) {
return true;
}
protected boolean fitsInDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
if (isNumber()) {
return lib.fitsInBigInteger(wrappedObject);
return lib.fitsInDouble(wrappedObject);
} else {
return false;
}
Expand Down Expand Up @@ -479,29 +479,29 @@ protected long asLong(@Shared("lib") @CachedLibrary(limit = "LIMIT") final Inter
}

@ExportMessage
protected float asFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isNumber()) {
return lib.asFloat(wrappedObject);
protected BigInteger asBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (wrappedObject instanceof final BigInteger w) {
return w;
} else if (isNumber()) {
return lib.asBigInteger(wrappedObject);
} else {
throw UnsupportedMessageException.create();
}
}

@ExportMessage
protected double asDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
protected float asFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isNumber()) {
return lib.asDouble(wrappedObject);
return lib.asFloat(wrappedObject);
} else {
throw UnsupportedMessageException.create();
}
}

@ExportMessage
protected BigInteger asBigInteger(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (wrappedObject instanceof final BigInteger w) {
return w;
} else if (isNumber()) {
return lib.asBigInteger(wrappedObject);
protected double asDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isNumber()) {
return lib.asDouble(wrappedObject);
} else {
throw UnsupportedMessageException.create();
}
Expand Down Expand Up @@ -864,6 +864,20 @@ protected boolean isMetaInstance(final Object other) throws UnsupportedMessageEx
}
}

@ExportMessage
public boolean hasMetaParents() {
return isMetaObject();
}

@ExportMessage
public Object getMetaParents(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
if (isClass()) {
return wrap(new Object[]{asClass().getSuperclass()});
} else {
throw UnsupportedMessageException.create();
}
}

@ExportMessage
protected boolean hasLanguage() {
return true;
Expand Down Expand Up @@ -915,6 +929,8 @@ private static Object toJavaArgument(final Object argument) {
return lib.asInt(argument);
} else if (lib.fitsInLong(argument)) {
return lib.asLong(argument);
} else if (lib.fitsInBigInteger(argument)) {
return lib.asBigInteger(argument);
} else if (lib.fitsInFloat(argument)) {
return lib.asFloat(argument);
} else if (lib.fitsInDouble(argument)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.nio.ByteOrder;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,6 +56,7 @@
import de.hpi.swa.trufflesqueak.model.ArrayObject;
import de.hpi.swa.trufflesqueak.model.BooleanObject;
import de.hpi.swa.trufflesqueak.model.ClassObject;
import de.hpi.swa.trufflesqueak.model.LargeIntegerObject;
import de.hpi.swa.trufflesqueak.model.NativeObject;
import de.hpi.swa.trufflesqueak.model.NilObject;
import de.hpi.swa.trufflesqueak.model.PointersObject;
Expand All @@ -65,6 +67,7 @@
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.BinaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.QuaternaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.QuinaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.SenaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.TernaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveFallbacks.UnaryPrimitiveFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.SqueakPrimitive;
Expand Down Expand Up @@ -539,6 +542,33 @@ protected static final double doAsDouble(@SuppressWarnings("unused") final Objec
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveFitsInBigInteger")
protected abstract static class PrimFitsInBigIntegerNode extends AbstractPrimitiveNode {

@Specialization
protected static final boolean doFitsInBigInteger(@SuppressWarnings("unused") final Object receiver, final Object object,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
return BooleanObject.wrap(lib.fitsInBigInteger(object));
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveAsBigInteger")
protected abstract static class PrimAsBigIntegerNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {

@TruffleBoundary
@Specialization(guards = {"lib.fitsInBigInteger(object)"}, limit = "2")
protected final Object doAsDouble(@SuppressWarnings("unused") final Object receiver, final Object object,
@CachedLibrary("object") final InteropLibrary lib) {
try {
return new LargeIntegerObject(getContext(), lib.asBigInteger(object));
} catch (final UnsupportedMessageException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveIsExecutable")
protected abstract static class PrimIsExecutableNode extends AbstractPrimitiveNode {
Expand Down Expand Up @@ -1213,6 +1243,21 @@ protected static final long doReadBufferByte(@SuppressWarnings("unused") final O
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveReadBuffer")
protected abstract static class PrimReadBufferNode extends AbstractPrimitiveNode implements SenaryPrimitiveFallback {
@Specialization(guards = {"lib.hasBufferElements(object)", "destination.isByteType()"})
protected static final Object doReadBuffer(final Object receiver, final Object object, final long byteOffset, final NativeObject destination, final long destinationOffset, final long length,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
try {
lib.readBuffer(object, byteOffset, destination.getByteStorage(), MiscUtils.toIntExact(destinationOffset), MiscUtils.toIntExact(length));
return receiver;
} catch (final UnsupportedMessageException | InvalidBufferOffsetException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveWriteBufferByte")
protected abstract static class PrimWriteBufferByteNode extends AbstractPrimitiveNode implements QuaternaryPrimitiveFallback {
Expand Down Expand Up @@ -1657,6 +1702,29 @@ protected static final Object getMetaSimpleName(@SuppressWarnings("unused") fina
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveHasMetaParents")
protected abstract static class PrimHasMetaParentsNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization(guards = "lib.isMetaObject(object)")
protected static final boolean hasMetaParents(@SuppressWarnings("unused") final Object receiver, final Object object, @CachedLibrary(limit = "2") final InteropLibrary lib) {
return lib.hasMetaParents(object);
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveGetMetaParents")
protected abstract static class PrimGetMetaParentsNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization(guards = "lib.isMetaObject(object)")
protected static final Object getMetaParents(@SuppressWarnings("unused") final Object receiver, final Object object,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
try {
return lib.getMetaParents(object);
} catch (final UnsupportedMessageException e) {
throw primitiveFailedInInterpreterCapturing(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveHasSourceLocation")
protected abstract static class PrimHasSourceLocationNode extends AbstractPrimitiveNode {
Expand Down Expand Up @@ -2172,6 +2240,15 @@ protected static final int toJavaInteger(@SuppressWarnings("unused") final Objec
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveToJavaBigInteger")
protected abstract static class PrimToJavaBigIntegerNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
@Specialization
protected static final BigInteger toJavaInteger(@SuppressWarnings("unused") final Object receiver, final LargeIntegerObject value) {
return value.getBigInteger();
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveToJavaString")
protected abstract static class PrimToJavaStringNode extends AbstractPrimitiveNode implements BinaryPrimitiveFallback {
Expand Down
2 changes: 1 addition & 1 deletion src/image
Submodule image updated 38 files
+3 −0 src/TruffleSqueak-Core.package/Behavior.extension/instance/interopGetMetaParents.st
+3 −0 src/TruffleSqueak-Core.package/Behavior.extension/instance/interopHasMetaParents.st
+2 −0 src/TruffleSqueak-Core.package/Behavior.extension/methodProperties.json
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/^equals.st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/adaptToNumber.andSend..st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/asNumber.st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/asSmalltalk.st
+1 −0 src/TruffleSqueak-Core.package/ForeignObject.class/instance/isZero.st
+5 −5 src/TruffleSqueak-Core.package/ForeignObject.class/methodProperties.json
+6 −0 src/TruffleSqueak-Core.package/Interop.class/class/asBigInteger..st
+4 −0 src/TruffleSqueak-Core.package/Interop.class/class/fitsInBigInteger..st
+6 −0 src/TruffleSqueak-Core.package/Interop.class/class/getMetaParents..st
+6 −0 src/TruffleSqueak-Core.package/Interop.class/class/hasMetaParents..st
+8 −0 ...uffleSqueak-Core.package/Interop.class/class/readBuffer.byteOffset.destination.destinationOffset.length..st
+5 −0 src/TruffleSqueak-Core.package/Interop.class/methodProperties.json
+4 −0 src/TruffleSqueak-Core.package/Java.class/class/asBigInteger..st
+1 −1 src/TruffleSqueak-Core.package/Java.class/class/asHostObject..st
+2 −1 src/TruffleSqueak-Core.package/Java.class/methodProperties.json
+3 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/instance/interopAsBigInteger.st
+3 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/instance/interopFitsInBigInteger.st
+6 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/methodProperties.json
+2 −0 src/TruffleSqueak-Core.package/LargePositiveInteger.extension/properties.json
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopAsBigInteger.st
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopFitsInBigInteger.st
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopGetMetaParents.st
+3 −0 src/TruffleSqueak-Core.package/Object.extension/instance/interopHasMetaParents.st
+3 −0 ...fleSqueak-Core.package/Object.extension/instance/interopReadBuffer.destination.destinationOffset.length..st
+5 −0 src/TruffleSqueak-Core.package/Object.extension/methodProperties.json
+1 −0 src/TruffleSqueak-Tests.package/ForeignObjectTest.class/instance/testJSMetadataAPIs.st
+2 −0 src/TruffleSqueak-Tests.package/ForeignObjectTest.class/instance/testJSObject.st
+2 −2 src/TruffleSqueak-Tests.package/ForeignObjectTest.class/methodProperties.json
+4 −0 src/TruffleSqueak-Tests.package/InteropTest.class/instance/testMetadataAPIs.st
+8 −1 src/TruffleSqueak-Tests.package/InteropTest.class/instance/testNumbers.st
+2 −2 src/TruffleSqueak-Tests.package/InteropTest.class/methodProperties.json
+5 −1 src/TruffleSqueak-Tests.package/JavaTest.class/instance/testJavaBuffers.st
+1 −1 src/TruffleSqueak-Tests.package/JavaTest.class/methodProperties.json
+6 −3 src/TruffleSqueak-Tests.package/TruffleSqueakTest.class/instance/testVMIntrospection.st
+1 −1 src/TruffleSqueak-Tests.package/TruffleSqueakTest.class/methodProperties.json

0 comments on commit 4dc15d8

Please sign in to comment.