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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @run main/othervm Upcall 27 */ 28 29 import java.lang.invoke.MethodHandles; 30 import java.nicl.Libraries; 31 import java.nicl.metadata.C; 32 import java.nicl.metadata.CallingConvention; 33 import java.nicl.metadata.NativeHeader; 34 import java.nicl.metadata.NativeType; 35 36 public class Upcall { 37 private static final boolean DEBUG = false; 38 39 private static final int MAGIC_INTEGER = 4711; 40 41 @NativeHeader 42 public static interface upcall { 43 @FunctionalInterface 44 static interface visitor { 45 @C(file="dummy", line=47, column=11, USR="c:@F@slowsort") 46 @NativeType(layout="(i)V", ctype="void (int)", size=4l) 47 @CallingConvention(value=1) 48 public void fn(int i); 49 } 50 51 @C(file="dummy", line=47, column=11, USR="c:@F@do_upcall") 52 @NativeType(layout="(p:(i)Vi)V", ctype="void (visitor, int)", name="do_upcall", size=1) 53 @CallingConvention(value=1) 54 public abstract void do_upcall(visitor v, int i); 55 } 56 57 public static class visitorImpl implements upcall.visitor { 58 boolean called = false; 59 60 @Override 61 public void fn(int i) { 62 called = true; 63 64 if (DEBUG) { 65 System.err.println("visit(" + i + ")"); 66 } 67 68 // value passed up from native code 69 assertEquals(i, MAGIC_INTEGER); 70 } 71 } 72 73 public void test() { 74 upcall i = Libraries.bind(upcall.class, Libraries.loadLibrary(MethodHandles.lookup(), "Upcall")); 75 visitorImpl v = new visitorImpl(); 76 77 i.do_upcall(v, MAGIC_INTEGER); 78 79 assertTrue(v.called); 80 81 if (DEBUG) { 82 System.err.println("back in test()\n"); 83 } 84 } 85 86 static void assertEquals(long expected, long actual) { 87 if (expected != actual) { 88 throw new RuntimeException("expected: " + expected + " does not match actual: " + actual); 89 } 90 } 91 92 static void assertTrue(boolean actual) { 93 if (!actual) { 94 throw new RuntimeException("expected: true does not match actual: " + actual); 95 } 96 } 97 98 public static void main(String[] args) { 99 new Upcall().test(); 100 } 101 } --- EOF ---