diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-01-31 20:15:47 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-01-31 20:15:47 -0700 |
| commit | 954afe5d9a5ae634f7db22641ebac6e755cdaba7 (patch) | |
| tree | 21183728874263dd440cb46dcb563b418f3cefbe /test | |
| parent | f20d0665bb9bfc3079028df59b754b028e8b83ec (diff) | |
| download | zig-954afe5d9a5ae634f7db22641ebac6e755cdaba7.tar.gz zig-954afe5d9a5ae634f7db22641ebac6e755cdaba7.zip | |
fix C interaction with maybe function pointers
See #88
Diffstat (limited to 'test')
| -rw-r--r-- | test/run_tests.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
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", ""); } |
