1 /*
   2  * Copyright (c) 2015, 2016, 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 DoubleUpcall
  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 DoubleUpcall {
  37     private static final boolean DEBUG = false;
  38 
  39     @NativeHeader
  40     public static interface upcall {

  41         @FunctionalInterface
  42         static interface cb {
  43             @C(file="dummy", line=47, column=11, USR="c:@F@slowsort")
  44             @NativeType(layout="(dd)d", ctype="double (double,double)", size=4l)
  45             @CallingConvention(value=1)
  46             public double fn(double d1, double d2);
  47         }
  48 
  49         @C(file="dummy", line=47, column=11, USR="c:@F@double_upcall")
  50         @NativeType(layout="(p:(DD)DDD)D", ctype="double (double_upcall_cb, double, double)", name="double_upcall", size=1)
  51         @CallingConvention(value=1)
  52         public abstract double double_upcall(cb cb, double d1, double d2);
  53     }
  54 
  55     public static class cbImpl implements upcall.cb {
  56         @Override
  57         public double fn(double d1, double d2) {
  58             if (DEBUG) {
  59                 System.err.println("fn(" + d1 + ", " + d2 + ")");
  60             }
  61 
  62             assertEquals(1.23, d1, 0.1);
  63             assertEquals(1.11, d2, 0.1);
  64 
  65             return d1 + d2;
  66         }
  67     }
  68 
  69     public void test() {
  70         upcall i = Libraries.bind(upcall.class, Libraries.loadLibrary(MethodHandles.lookup(), "Upcall"));
  71         upcall.cb v = new cbImpl();
  72 
  73         double d = i.double_upcall(v, 1.23, 1.11);
  74 
  75         if (DEBUG) {
  76             System.err.println("back in test()");
  77             System.err.println("call returned: " + d);
  78         }
  79 
  80         assertEquals(2.34, d, 0.1);
  81     }
  82 
  83     private static void assertEquals(double expected, double actual, double maxDiff) {
  84         if (Math.abs(expected - actual) > maxDiff) {
  85             throw new RuntimeException("actual: " + actual + " does not match expected: " + expected);
  86         }
  87     }
  88 
  89     public static void main(String[] args) {
  90         new DoubleUpcall().test();
  91     }
  92 }
--- EOF ---