From 954afe5d9a5ae634f7db22641ebac6e755cdaba7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 31 Jan 2016 20:15:47 -0700 Subject: fix C interaction with maybe function pointers See #88 --- test/run_tests.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/run_tests.cpp') diff --git a/test/run_tests.cpp b/test/run_tests.cpp index c873ae1b0d..7cc8b0c565 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1447,6 +1447,42 @@ pub fn main(args: [][]u8) -> %void { f(false); } )SOURCE", "a\nb\n"); + + + add_simple_case("expose function pointer to C land", R"SOURCE( +#link("c") +export executable "test"; + +c_import { + @c_include("stdlib.h"); +} + +export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int { + const a_int = (&i32)(a ?? unreachable{}); + const b_int = (&i32)(b ?? unreachable{}); + if (*a_int < *b_int) { + -1 + } else if (*a_int > *b_int) { + 1 + } else { + 0 + } +} + +export fn main(args: c_int, argv: &&u8) -> c_int { + var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 }; + + qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn); + + for (item, array, i) { + if (item != i) { + abort(); + } + } + + return 0; +} + )SOURCE", ""); } -- cgit v1.2.3