Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for binding foreign arrays #470

Merged
merged 2 commits into from Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/cstubs/cstubs_generate_ml.ml
Expand Up @@ -453,6 +453,10 @@ let rec pattern_of_typ : type a. a typ -> ml_pat = function
| View { ty } ->
static_con "View"
[`Record [path_of_string "CI.ty", pattern_of_typ ty]]
| Array (_, _) ->
static_con "Array" [`Underscore; `Underscore]
| Bigarray _ ->
static_con "Bigarray" [`Underscore]
| OCaml String ->
Ctypes_static.unsupported
"cstubs does not support OCaml strings as global values"
Expand All @@ -466,14 +470,6 @@ let rec pattern_of_typ : type a. a typ -> ml_pat = function
internal_error
"Unexpected abstract type encountered during ML code generation: %s"
(Ctypes.string_of_typ ty)
| Array _ as ty ->
internal_error
"Unexpected array type encountered during ML code generation: %s"
(Ctypes.string_of_typ ty)
| Bigarray _ as ty ->
internal_error
"Unexpected bigarray type encountered during ML code generation: %s"
(Ctypes.string_of_typ ty)

type wrapper_state = {
pat: ml_pat;
Expand Down
3 changes: 3 additions & 0 deletions tests/clib/test_functions.c
Expand Up @@ -700,3 +700,6 @@ GEN_RETURN_F(int64_t)
GEN_RETURN_F(float)
GEN_RETURN_F(double)
GEN_RETURN_F(bool)

char *string_array[2] = { "Hello", "world" };
int32_t int_array[5] = { 0, 1, 2, 3, 4 };
3 changes: 3 additions & 0 deletions tests/clib/test_functions.h
Expand Up @@ -251,4 +251,7 @@ float callback_returns_float(float (*f)(void));
double callback_returns_double(double (*f)(void));
bool callback_returns_bool(bool (*f)(void));

extern char *string_array[2];
extern int32_t int_array[5];

#endif /* TEST_FUNCTIONS_H */
3 changes: 3 additions & 0 deletions tests/test-foreign_values/stubs/functions.ml
Expand Up @@ -25,6 +25,9 @@ struct

let sum = F.(foreign "sum_range_with_plus_callback"
(int @-> int @-> returning int))

let string_array = F.(foreign_value "string_array" (array 2 string))
let int_array = F.(foreign_value "int_array" (bigarray array1 5 Bigarray.int32))
end


Expand Down
24 changes: 24 additions & 0 deletions tests/test-foreign_values/test_foreign_values.ml
Expand Up @@ -43,6 +43,24 @@ struct

plus <-@ None;
end

(* Access an array exposed as a global value *)
let test_retrieving_array _ =
let sarr = !@string_array in
begin
assert_equal "Hello" (CArray.get sarr 0);
assert_equal "world" (CArray.get sarr 1);
end;

let iarr = !@int_array in
begin
let expected_ints = Bigarray.(Array1.create int32 c_layout 5) in
for i = 0 to 4 do
Bigarray.Array1.set expected_ints i (Int32.of_int i)
done;
assert_equal expected_ints iarr
end

end


Expand Down Expand Up @@ -91,9 +109,15 @@ let suite = "Foreign value tests" >:::
"global callback function (foreign)"
>:: Foreign_tests.test_global_callback;

"retrieving global array (foreign)"
>:: Foreign_tests.test_retrieving_array;

"retrieving global struct (stubs)"
>:: Stub_tests.test_retrieving_struct;

"retrieving global array (stubs)"
>:: Stub_tests.test_retrieving_array;

"global callback function (stubs)"
>:: Stub_tests.test_global_callback;

Expand Down