comparison truffle/com.oracle.truffle.api.interop.java.test/src/com/oracle/truffle/api/interop/java/test/MethodMessageTest.java @ 22135:e70b20f4bb00

Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 07 Sep 2015 17:07:20 +0200
parents
children dc83cc1f94f2
comparison
equal deleted inserted replaced
22134:025869c88840 22135:e70b20f4bb00
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.interop.java.test;
26
27 import com.oracle.truffle.api.interop.TruffleObject;
28 import com.oracle.truffle.api.interop.java.JavaInterop;
29 import com.oracle.truffle.api.interop.java.MethodMessage;
30 import static org.junit.Assert.*;
31 import org.junit.Test;
32
33 public class MethodMessageTest {
34 interface MathWrap {
35 @MethodMessage(message = "READ")
36 MaxFunction max();
37 }
38
39 interface MaxFunction {
40 @MethodMessage(message = "IS_NULL")
41 boolean isNull();
42
43 @MethodMessage(message = "HAS_SIZE")
44 boolean isArray();
45
46 @MethodMessage(message = "GET_SIZE")
47 int size();
48
49 @MethodMessage(message = "IS_EXECUTABLE")
50 boolean canExecute();
51
52 @MethodMessage(message = "EXECUTE")
53 Number compute(int a, int b);
54 }
55
56 @Test
57 public void functionTest() throws Exception {
58 TruffleObject truffleMath = JavaInterop.asTruffleObject(Math.class);
59 MathWrap wrap = JavaInterop.asJavaObject(MathWrap.class, truffleMath);
60 MaxFunction functionArityTwo = wrap.max();
61 assertTrue("function can be executed", functionArityTwo.canExecute());
62 assertFalse("function isn't null", functionArityTwo.isNull());
63 assertFalse("function isn't array", functionArityTwo.isArray());
64 int res = functionArityTwo.compute(10, 5).intValue();
65 assertEquals(10, res);
66 }
67
68 @Test
69 public void workWithAnArray() throws Exception {
70 TruffleObject arr = JavaInterop.asTruffleObject(new Object[]{1, 2, 3});
71
72 MaxFunction wrap = JavaInterop.asJavaObject(MaxFunction.class, arr);
73
74 assertTrue("It is an array", wrap.isArray());
75 assertFalse("No function", wrap.canExecute());
76 assertFalse("Not null", wrap.isNull());
77
78 assertEquals("Size is 3", 3, wrap.size());
79 }
80 }