# HG changeset patch # User Jaroslav Tulach # Date 1448964791 -3600 # Node ID 70a10a9f28ad0d9082761904cde9c65e5e30463d # Parent 30644770899d73539d691851ca5a29f8e7b84c88 Verify that primitive data (numbers as well as String) can be passed into a language as boxed TruffleObject instances diff -r 30644770899d -r 70a10a9f28ad truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/BoxedValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/BoxedValue.java Tue Dec 01 11:13:11 2015 +0100 @@ -0,0 +1,113 @@ +/* + * 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; + +import com.oracle.truffle.api.CallTarget; +import com.oracle.truffle.api.Truffle; +import com.oracle.truffle.api.frame.VirtualFrame; +import com.oracle.truffle.api.interop.ForeignAccess; +import com.oracle.truffle.api.interop.Message; +import com.oracle.truffle.api.interop.TruffleObject; +import com.oracle.truffle.api.nodes.RootNode; + +final class BoxedValue implements TruffleObject, ForeignAccess.Factory10 { + private final Object value; + + BoxedValue(Object v) { + v.getClass(); + this.value = v; + } + + @Override + public ForeignAccess getForeignAccess() { + return ForeignAccess.create(BoxedValue.class, this); + } + + @Override + public CallTarget accessIsNull() { + return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(Boolean.FALSE)); + } + + @Override + public CallTarget accessIsExecutable() { + return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(Boolean.FALSE)); + } + + @Override + public CallTarget accessIsBoxed() { + return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(Boolean.TRUE)); + } + + @Override + public CallTarget accessHasSize() { + return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(Boolean.FALSE)); + } + + @Override + public CallTarget accessGetSize() { + return null; + } + + @Override + public CallTarget accessUnbox() { + return Truffle.getRuntime().createCallTarget(new RootNode(TckLanguage.class, null, null) { + @Override + public Object execute(VirtualFrame frame) { + BoxedValue boxed = (BoxedValue) ForeignAccess.getReceiver(frame); + return boxed.value; + } + }); + } + + @Override + public CallTarget accessRead() { + return null; + } + + @Override + public CallTarget accessWrite() { + return null; + } + + @Override + public CallTarget accessExecute(int argumentsLength) { + return null; + } + + @Override + public CallTarget accessInvoke(int argumentsLength) { + return null; + } + + @Override + public CallTarget accessNew(int argumentsLength) { + return null; + } + + @Override + public CallTarget accessMessage(Message unknown) { + return null; + } +} diff -r 30644770899d -r 70a10a9f28ad 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 Mon Nov 30 15:13:33 2015 +0100 +++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java Tue Dec 01 11:13:11 2015 +0100 @@ -493,6 +493,21 @@ } @Test + public void testPrimitiveidentityBoxedByte() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + byte value = (byte) RANDOM.nextInt(100); + BoxedValue boxed = new BoxedValue(value); + + Number n = (Number) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, n.byteValue()); + } + + @Test public void testPrimitiveidentityShort() throws Exception { String id = identity(); if (id == null) { @@ -506,6 +521,21 @@ } @Test + public void testPrimitiveidentityBoxedShort() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + short value = (short) RANDOM.nextInt(100); + BoxedValue boxed = new BoxedValue(value); + + Number n = (Number) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, n.shortValue()); + } + + @Test public void testPrimitiveidentityInt() throws Exception { String id = identity(); if (id == null) { @@ -520,6 +550,21 @@ } @Test + public void testPrimitiveidentityBoxedInt() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + int value = RANDOM.nextInt(100); + BoxedValue boxed = new BoxedValue(value); + + Number n = (Number) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, n.intValue()); + } + + @Test public void testPrimitiveidentityLong() throws Exception { String id = identity(); if (id == null) { @@ -534,6 +579,21 @@ } @Test + public void testPrimitiveidentityBoxedLong() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + long value = RANDOM.nextInt(1000); + BoxedValue boxed = new BoxedValue(value); + + Number n = (Number) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, n.longValue()); + } + + @Test public void testPrimitiveidentityFloat() throws Exception { String id = identity(); if (id == null) { @@ -548,6 +608,21 @@ } @Test + public void testPrimitiveidentityBoxedFloat() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + float value = RANDOM.nextInt(1000) + RANDOM.nextFloat(); + BoxedValue boxed = new BoxedValue(value); + + Number n = (Number) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, n.floatValue(), 0.01); + } + + @Test public void testPrimitiveidentityDouble() throws Exception { String id = identity(); if (id == null) { @@ -562,6 +637,21 @@ } @Test + public void testPrimitiveidentityBoxedDouble() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + double value = RANDOM.nextInt(1000) + RANDOM.nextDouble(); + BoxedValue boxed = new BoxedValue(value); + + Number n = (Number) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, n.doubleValue(), 0.01); + } + + @Test public void testPrimitiveidentityString() throws Exception { String id = identity(); if (id == null) { @@ -576,6 +666,21 @@ } @Test + public void testPrimitiveidentityBoxedString() throws Exception { + String id = identity(); + if (id == null) { + return; + } + PolyglotEngine.Value apply = findGlobalSymbol(id); + + String value = "Value" + RANDOM.nextInt(1000) + RANDOM.nextDouble(); + BoxedValue boxed = new BoxedValue(value); + + String ret = (String) apply.invoke(null, boxed).get(); + assertEquals("The same value returned", value, ret); + } + + @Test public void testPrimitiveIdentityForeignObject() throws Exception { String id = identity(); if (id == null) {