changeset 22313:06c5171e44c8

More tests executed in synchronous and asynchronous mode
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Fri, 16 Oct 2015 11:22:48 +0200
parents 8322a904a472
children e56d1a618f64
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ArrayTruffleObject.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/EngineAsynchTest.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/EngineTest.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolTest.java
diffstat 4 files changed, 235 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ArrayTruffleObject.java	Fri Oct 16 11:22:48 2015 +0200
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2012, 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.
+ *
+ * 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.api.test.vm;
+
+import com.oracle.truffle.api.CallTarget;
+import com.oracle.truffle.api.Truffle;
+import com.oracle.truffle.api.TruffleLanguage;
+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;
+import java.util.List;
+
+final class ArrayTruffleObject implements TruffleObject, ForeignAccess.Factory10 {
+    private final ForeignAccess access;
+    private final Object[] values;
+
+    ArrayTruffleObject(Object[] values) {
+        this.access = ForeignAccess.create(getClass(), this);
+        this.values = values;
+    }
+
+    @Override
+    public ForeignAccess getForeignAccess() {
+        return access;
+    }
+
+    @Override
+    public CallTarget accessIsNull() {
+        return target(RootNode.createConstantNode(Boolean.FALSE));
+    }
+
+    @Override
+    public CallTarget accessIsExecutable() {
+        return target(RootNode.createConstantNode(Boolean.FALSE));
+    }
+
+    @Override
+    public CallTarget accessIsBoxed() {
+        return target(RootNode.createConstantNode(Boolean.FALSE));
+    }
+
+    @Override
+    public CallTarget accessHasSize() {
+        return target(RootNode.createConstantNode(Boolean.TRUE));
+    }
+
+    @Override
+    public CallTarget accessGetSize() {
+        return target(RootNode.createConstantNode(values.length));
+    }
+
+    @Override
+    public CallTarget accessUnbox() {
+        return null;
+    }
+
+    @Override
+    public CallTarget accessRead() {
+        return target(new IndexNode());
+    }
+
+    @Override
+    public CallTarget accessWrite() {
+        return null;
+    }
+
+    @Override
+    public CallTarget accessExecute(int argumentsLength) {
+        return null;
+    }
+
+    @Override
+    public CallTarget accessInvoke(int argumentsLength) {
+        if (argumentsLength == 2) {
+            return target(new InvokeNode());
+        }
+        return null;
+    }
+
+    @Override
+    public CallTarget accessNew(int argumentsLength) {
+        return null;
+    }
+
+    @Override
+    public CallTarget accessMessage(Message unknown) {
+        return null;
+    }
+
+    private static CallTarget target(RootNode node) {
+        return Truffle.getRuntime().createCallTarget(node);
+    }
+
+    private final class IndexNode extends RootNode {
+        public IndexNode() {
+            super(TruffleLanguage.class, null, null);
+        }
+
+        @Override
+        public Object execute(VirtualFrame frame) {
+            int index = ((Number) ForeignAccess.getArguments(frame).get(0)).intValue();
+            if (values[index] instanceof Object[]) {
+                return new ArrayTruffleObject((Object[]) values[index]);
+            } else {
+                return values[index];
+            }
+        }
+    }
+
+    private final class InvokeNode extends RootNode {
+        public InvokeNode() {
+            super(TruffleLanguage.class, null, null);
+        }
+
+        @Override
+        public Object execute(VirtualFrame frame) {
+            final List<Object> args = ForeignAccess.getArguments(frame);
+            if (!"get".equals(args.get(0))) {
+                return null;
+            }
+            int index = ((Number) args.get(1)).intValue();
+            if (values[index] instanceof Object[]) {
+                return new ArrayTruffleObject((Object[]) values[index]);
+            } else {
+                return values[index];
+            }
+        }
+    }
+}
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/EngineAsynchTest.java	Fri Oct 16 09:59:21 2015 +0200
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/EngineAsynchTest.java	Fri Oct 16 11:22:48 2015 +0200
@@ -22,29 +22,17 @@
  */
 package com.oracle.truffle.api.test.vm;
 
-import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.vm.PolyglotEngine;
 import java.util.concurrent.Executors;
-import static org.junit.Assert.assertNotNull;
-import org.junit.Before;
 import org.junit.Test;
 
-public class EngineAsynchTest {
-    PolyglotEngine tvm;
-
-    @Before
-    public void initEngine() {
-        tvm = PolyglotEngine.buildNew().executor(Executors.newSingleThreadExecutor()).build();
+public class EngineAsynchTest extends EngineTest {
+    @Test
+    public void marker() {
     }
 
-    @Test
-    public void npeWhenCastingAs() throws Exception {
-        PolyglotEngine.Language language1 = tvm.getLanguages().get("application/x-test-import-export-1");
-        PolyglotEngine.Language language2 = tvm.getLanguages().get("application/x-test-import-export-2");
-        language2.eval(Source.fromText("explicit.value=42", "define 42"));
-
-        PolyglotEngine.Value value = language1.eval(Source.fromText("return=value", "42.value"));
-        String res = value.as(String.class);
-        assertNotNull(res);
+    @Override
+    protected PolyglotEngine.Builder createBuilder() {
+        return PolyglotEngine.buildNew().executor(Executors.newSingleThreadExecutor());
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/EngineTest.java	Fri Oct 16 11:22:48 2015 +0200
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2012, 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.
+ *
+ * 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.api.test.vm;
+
+import com.oracle.truffle.api.source.Source;
+import com.oracle.truffle.api.vm.PolyglotEngine;
+import java.util.List;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import org.junit.Test;
+
+public class EngineTest {
+    protected PolyglotEngine.Builder createBuilder() {
+        return PolyglotEngine.buildNew();
+    }
+
+    @Test
+    public void npeWhenCastingAs() throws Exception {
+        PolyglotEngine tvm = createBuilder().build();
+
+        PolyglotEngine.Language language1 = tvm.getLanguages().get("application/x-test-import-export-1");
+        PolyglotEngine.Language language2 = tvm.getLanguages().get("application/x-test-import-export-2");
+        language2.eval(Source.fromText("explicit.value=42", "define 42"));
+
+        PolyglotEngine.Value value = language1.eval(Source.fromText("return=value", "42.value"));
+        String res = value.as(String.class);
+        assertNotNull(res);
+    }
+
+    private interface AccessArray {
+        List<? extends Number> get(int index);
+    }
+
+    @Test
+    public void wrappedAsArray() throws Exception {
+        Object[][] matrix = {{1, 2, 3}};
+
+        PolyglotEngine tvm = createBuilder().globalSymbol("arr", new ArrayTruffleObject(matrix)).build();
+        PolyglotEngine.Language language1 = tvm.getLanguages().get("application/x-test-import-export-1");
+        AccessArray access = language1.eval(Source.fromText("return=arr", "get the array")).as(AccessArray.class);
+        assertNotNull("Array converted to list", access);
+        List<? extends Number> list = access.get(0);
+        assertEquals("Size 3", 3, list.size());
+        assertEquals(1, list.get(0));
+        assertEquals(2, list.get(1));
+        assertEquals(3, list.get(2));
+        Integer[] arr = list.toArray(new Integer[0]);
+        assertEquals("Three items in array", 3, arr.length);
+        assertEquals(1, arr[0].intValue());
+        assertEquals(2, arr[1].intValue());
+        assertEquals(3, arr[2].intValue());
+    }
+}
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolTest.java	Fri Oct 16 09:59:21 2015 +0200
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolTest.java	Fri Oct 16 11:22:48 2015 +0200
@@ -89,5 +89,10 @@
         assertEquals("one", args.get(0));
         assertEquals("two", args.get(1));
         assertEquals("three", args.get(2));
+        String[] arr = args.toArray(new String[0]);
+        assertEquals("Three items in array", 3, arr.length);
+        assertEquals("one", arr[0]);
+        assertEquals("two", arr[1]);
+        assertEquals("three", arr[2]);
     }
 }