diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-28 00:10:00 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-28 00:10:00 +1000 |
commit | 7b86e3251a28406862fe16d49f8533beb8ca3150 (patch) | |
tree | a90166418ada7ad88500843ce8881a453c1d4721 /bindgen/ocaml/bindings/caml_bindings.c | |
parent | 6b004c5ac6a25f1020774276803b62e8619ea61e (diff) |
start on ocaml bindings
Diffstat (limited to 'bindgen/ocaml/bindings/caml_bindings.c')
-rw-r--r-- | bindgen/ocaml/bindings/caml_bindings.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/bindgen/ocaml/bindings/caml_bindings.c b/bindgen/ocaml/bindings/caml_bindings.c new file mode 100644 index 0000000..4a7dc70 --- /dev/null +++ b/bindgen/ocaml/bindings/caml_bindings.c @@ -0,0 +1,132 @@ +/* automatically generated by ocaml-bindgen 0.0.1 */ + +#include "celeritas.h" +#include <caml/alloc.h> +#include <caml/callback.h> +#include <caml/fail.h> +#include <caml/memory.h> +#include <caml/mlvalues.h> +#include <caml/unixsupport.h> +Core* caml_get_global_core() { + CAMLparam0(); + CAMLlocal1(result); + result = get_global_core(); + CAMLreturn(result); +} + +void caml_core_Bringup(value caml_optional_window) { + CAMLparam1(caml_optional_window); + void* optional_window = Nativeint_val(Field(caml_optional_window, 1)); + core_Bringup(optional_window); + CAMLreturn0; +} + +void caml_frame_Begin() { + CAMLparam0(); + frame_Begin(); + CAMLreturn0; +} + +void caml_frame_Draw() { + CAMLparam0(); + frame_Draw(); + CAMLreturn0; +} + +void caml_frame_End() { + CAMLparam0(); + frame_End(); + CAMLreturn0; +} + +Vec2* caml_Vec2_of_value(value caml_x) { + Vec2* x = malloc(sizeof(struct Vec2)); + x->x = Double_val(Field(caml_x, 0)); + x->y = Double_val(Field(caml_x, 1)); + return x; +} + +value caml_Vec2_to_value(struct Vec2* x) { + CAMLparam0(); + CAMLlocal1(caml_x); + caml_x = caml_alloc_tuple(2); + Store_field(caml_x, 0, caml_copy_double(x->x)); + Store_field(caml_x, 1, caml_copy_double(x->y)); + CAMLreturn(caml_x); +} + +Vec3* caml_Vec3_of_value(value caml_x) { + Vec3* x = malloc(sizeof(struct Vec3)); + x->x = Double_val(Field(caml_x, 0)); + x->y = Double_val(Field(caml_x, 1)); + x->z = Double_val(Field(caml_x, 2)); + return x; +} + +value caml_Vec3_to_value(struct Vec3* x) { + CAMLparam0(); + CAMLlocal1(caml_x); + caml_x = caml_alloc_tuple(3); + Store_field(caml_x, 0, caml_copy_double(x->x)); + Store_field(caml_x, 1, caml_copy_double(x->y)); + Store_field(caml_x, 2, caml_copy_double(x->z)); + CAMLreturn(caml_x); +} + +Vec4* caml_Vec4_of_value(value caml_x) { + Vec4* x = malloc(sizeof(struct Vec4)); + x->x = Double_val(Field(caml_x, 0)); + x->y = Double_val(Field(caml_x, 1)); + x->z = Double_val(Field(caml_x, 2)); + x->w = Double_val(Field(caml_x, 3)); + return x; +} + +value caml_Vec4_to_value(struct Vec4* x) { + CAMLparam0(); + CAMLlocal1(caml_x); + caml_x = caml_alloc_tuple(4); + Store_field(caml_x, 0, caml_copy_double(x->x)); + Store_field(caml_x, 1, caml_copy_double(x->y)); + Store_field(caml_x, 2, caml_copy_double(x->z)); + Store_field(caml_x, 3, caml_copy_double(x->w)); + CAMLreturn(caml_x); +} + +Vec3 caml_vec3_add(value caml_a, value caml_b) { + CAMLparam2(caml_a, caml_b); + CAMLlocal1(result); + Vec3 a = caml_Vec3_of_value(caml_a); + Vec3 b = caml_Vec3_of_value(caml_b); + result = vec3_add(a, b); + CAMLreturn(result); +} + + +#include <stdlib.h> +value bindgen_alloc(value caml_size) { + CAMLparam1(caml_size); + + // Convert OCaml integer to C size + size_t size = Int_val(caml_size); + printf("Allocated size %ld \n", size); + + void* ptr = malloc(sizeof(size)); + if (ptr == NULL) { + // TODO: handle allocation failure + CAMLreturn(Val_unit); + } + + // Wrap the pointer as an OCaml value + CAMLreturn(caml_copy_nativeint(ptr)); +} + +void bindgen_free(value caml_addr) { + free(Nativeint_val(caml_addr)); +} + +value bindgen_alloc_string(value caml_string) { + CAMLparam1(caml_string); + char* str = String_val(caml_string); + CAMLreturn(caml_copy_nativeint((intnat)str)); +} |