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