# HG changeset patch # User Jaroslav Tulach # Date 1441819124 -7200 # Node ID 597953a8e6f01142b9ba4368622d076bb2dc0401 # Parent a583d7ffd285c4905fef3658432c8bd297edb84e Testing behavior of primitive types returned from an interop method. diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java --- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Sep 09 18:53:14 2015 +0200 +++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Sep 09 19:18:44 2015 +0200 @@ -152,4 +152,12 @@ @Override public void testPlusWithFloat() throws Exception { } + + @Override + public void testPrimitiveReturnTypeDouble() throws Exception { + } + + @Override + public void testPrimitiveReturnTypeFloat() throws Exception { + } } diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLInvokeNode.java --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLInvokeNode.java Wed Sep 09 18:53:14 2015 +0200 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLInvokeNode.java Wed Sep 09 19:18:44 2015 +0200 @@ -110,6 +110,7 @@ if (crossLanguageCall == null) { crossLanguageCall = insert(Message.createExecute(argumentValues.length).createNode()); } - return ForeignAccess.execute(crossLanguageCall, frame, function, argumentValues); + Object res = ForeignAccess.execute(crossLanguageCall, frame, function, argumentValues); + return SLContext.fromForeignValue(res); } } diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java Wed Sep 09 18:53:14 2015 +0200 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java Wed Sep 09 19:18:44 2015 +0200 @@ -53,6 +53,7 @@ import com.oracle.truffle.sl.nodes.*; import com.oracle.truffle.sl.nodes.local.*; import com.oracle.truffle.sl.parser.*; +import java.math.BigInteger; /** * The run-time state of SL during execution. One context is instantiated before any source code is @@ -198,4 +199,13 @@ public static DynamicObject castSLObject(Object value) { return LAYOUT.getType().cast(value); } + + public static Object fromForeignValue(Object a) { + if (a instanceof Long || a instanceof BigInteger) { + return a; + } else if (a instanceof Number) { + return ((Number) a).longValue(); + } + return a; + } } diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLFunctionForeignAccess.java --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLFunctionForeignAccess.java Wed Sep 09 18:53:14 2015 +0200 +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLFunctionForeignAccess.java Wed Sep 09 19:18:44 2015 +0200 @@ -49,7 +49,7 @@ import com.oracle.truffle.sl.SLLanguage; import com.oracle.truffle.sl.nodes.call.SLDispatchNode; import com.oracle.truffle.sl.nodes.call.SLDispatchNodeGen; -import java.math.BigInteger; +import static com.oracle.truffle.sl.runtime.SLContext.fromForeignValue; import java.util.List; /** @@ -102,20 +102,10 @@ arr = args.toArray(); } for (int i = 0; i < arr.length; i++) { - Object a = arr[i]; - if (a instanceof Long) { - continue; - } - if (a instanceof BigInteger) { - continue; - } - if (a instanceof Number) { - arr[i] = ((Number) a).longValue(); - } + arr[i] = fromForeignValue(arr[i]); } return dispatch.executeDispatch(frame, function, arr); } - } private static class SLForeignNullCheckNode extends RootNode { diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ConstantFunction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ConstantFunction.java Wed Sep 09 19:18:44 2015 +0200 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.tck; + +final class ConstantFunction implements ObjectBinaryOperation { + private final Object constant; + + public ConstantFunction(Object constant) { + this.constant = constant; + } + + @Override + public Object compute(Object ignore1, Object ignore2) { + return constant; + } +} diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ObjectBinaryOperation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ObjectBinaryOperation.java Wed Sep 09 19:18:44 2015 +0200 @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.tck; + +/** + * Binary operation on any objects. Mimics "functional interface" - e.g. has just a single method, + * so it should be easily usable with lamdas. + */ +public interface ObjectBinaryOperation { + Object compute(Object a, Object b); +} diff -r a583d7ffd285 -r 597953a8e6f0 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java --- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Sep 09 18:53:14 2015 +0200 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Wed Sep 09 19:18:44 2015 +0200 @@ -347,6 +347,72 @@ } @Test + public void testPrimitiveReturnTypeByte() throws Exception { + TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); + + byte value = (byte) RANDOM.nextInt(100); + + TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); + Number n = apply.invoke(null, fn).as(Number.class); + assertEquals("The same value returned", value + 10, n.byteValue()); + } + + @Test + public void testPrimitiveReturnTypeShort() throws Exception { + TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); + + short value = (short) RANDOM.nextInt(100); + + TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); + Number n = apply.invoke(null, fn).as(Number.class); + assertEquals("The same value returned", value + 10, n.shortValue()); + } + + @Test + public void testPrimitiveReturnTypeInt() throws Exception { + TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); + + int value = RANDOM.nextInt(100); + + TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); + Number n = apply.invoke(null, fn).as(Number.class); + assertEquals("The same value returned", value + 10, n.intValue()); + } + + @Test + public void testPrimitiveReturnTypeLong() throws Exception { + TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); + + long value = RANDOM.nextInt(1000); + + TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); + Number n = apply.invoke(null, fn).as(Number.class); + assertEquals("The same value returned", value + 10, n.longValue()); + } + + @Test + public void testPrimitiveReturnTypeFloat() throws Exception { + TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); + + float value = RANDOM.nextInt(1000) + RANDOM.nextFloat(); + + TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); + Number n = apply.invoke(null, fn).as(Number.class); + assertEquals("The same value returned", value + 10, n.floatValue(), 0.01); + } + + @Test + public void testPrimitiveReturnTypeDouble() throws Exception { + TruffleVM.Symbol apply = findGlobalSymbol(applyNumbers()); + + double value = RANDOM.nextInt(1000) + RANDOM.nextDouble(); + + TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value)); + Number n = apply.invoke(null, fn).as(Number.class); + assertEquals("The same value returned", value + 10, n.doubleValue(), 0.01); + } + + @Test public void testCoExistanceOfMultipleLanguageInstances() throws Exception { final String countMethod = countInvocations(); TruffleVM.Symbol count1 = findGlobalSymbol(countMethod);