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.NativeCallback;

  32 import java.nicl.metadata.NativeHeader;
  33 import java.nicl.metadata.NativeLocation;
  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         @NativeCallback("(i)V")
  44         @FunctionalInterface
  45         static interface visitor {
  46             @NativeLocation(file="dummy", line=47, column=11, USR="c:@F@slowsort")


  47             public void fn(int i);
  48         }
  49 
  50         @NativeLocation(file="dummy", line=47, column=11, USR="c:@F@do_upcall")
  51         @NativeType(layout="(p:(i)Vi)V", ctype="void (visitor, int)", name="do_upcall")

  52         public abstract void do_upcall(visitor v, int i);
  53     }
  54 
  55     public static class visitorImpl implements upcall.visitor {
  56         boolean called = false;
  57 
  58         @Override
  59         public void fn(int i) {
  60             called = true;
  61 
  62             if (DEBUG) {
  63                 System.err.println("visit(" + i + ")");
  64             }
  65 
  66             // value passed up from native code
  67             assertEquals(i, MAGIC_INTEGER);
  68         }
  69     }
  70 
  71     public void test() {
  72         upcall i = Libraries.bind(upcall.class, Libraries.loadLibrary(MethodHandles.lookup(), "Upcall"));
  73         visitorImpl v = new visitorImpl();
  74 
  75         i.do_upcall(v, MAGIC_INTEGER);
  76 
  77         assertTrue(v.called);
  78 
  79         if (DEBUG) {
  80             System.err.println("back in test()\n");
  81         }
  82     }
  83 
  84     static void assertEquals(long expected, long actual) {
  85         if (expected != actual) {
  86             throw new RuntimeException("expected: " + expected + " does not match actual: " + actual);
  87         }
  88     }
  89 
  90     static void assertTrue(boolean actual) {
  91         if (!actual) {
  92             throw new RuntimeException("expected: true does not match actual: " + actual);
  93         }
  94     }
  95 
  96     public static void main(String[] args) {
  97         new Upcall().test();
  98     }
  99 }
--- EOF ---