tests: adds an FFI test with different callbacks
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 9 Jun 2013 12:31:31 +0000 (08:31 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 5 Aug 2013 17:06:28 +0000 (13:06 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

tests/sav/test_ffi_c_more_callbacks.sav [new file with mode: 0644]
tests/test_ffi_c_more_callbacks.nit [new file with mode: 0644]

diff --git a/tests/sav/test_ffi_c_more_callbacks.sav b/tests/sav/test_ffi_c_more_callbacks.sav
new file mode 100644 (file)
index 0000000..1cc5e51
--- /dev/null
@@ -0,0 +1,6 @@
+From C, beginning out: 12345
+Back in Nit: in1 12345
+From C, a=1234
+From C, b=10
+From C, end of out: 1.234450
+567
diff --git a/tests/test_ffi_c_more_callbacks.nit b/tests/test_ffi_c_more_callbacks.nit
new file mode 100644 (file)
index 0000000..9a25f36
--- /dev/null
@@ -0,0 +1,45 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# FFI test with simple callbacks to Nit (within module)
+module test_ffi_c_more_callbacks
+
+`{
+#include <stdio.h>
+`}
+
+class A
+       var a: Int
+       init do a = 1234
+       init alt(i: Int) do a = i
+       fun to_i: Int do return a
+end
+
+fun in1(i: Int) do print "Back in Nit: in1 {i}"
+fun in2(i: Float) do print "Back in Nit: in2"
+
+fun out(i: Int, f: Float): Int import in1, in2, A, A::alt, A::to_i `{
+       printf("From C, beginning out: %ld\n", i);
+       Object_in1(recv, i);
+       A a = new_A();
+       A b = new_A_alt(10);
+       printf("From C, a=%ld\n", A_to_i(a));
+       printf("From C, b=%ld\n", A_to_i(b));
+       printf("From C, end of out: %f\n", f);
+       return 567;
+`}
+
+print out(12345, 1.23445)