Skip to content

Instantly share code, notes, and snippets.

@melwyn95
Last active September 13, 2021 04:25
Show Gist options
  • Save melwyn95/bae19fb06352497399178d3bad9fdc68 to your computer and use it in GitHub Desktop.
Save melwyn95/bae19fb06352497399178d3bad9fdc68 to your computer and use it in GitHub Desktop.
OCaml C stubs
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
// Reference: http://decapode314.free.fr/ocaml/ocaml-wrapping-c.html
// Reference: https://ocaml.org/manual/intfc.html
// This stub return a (int,string) result
value esy_test_result(value unit) {
CAMLparam1( unit );
CAMLlocal1( result );
// Ok 42
result = caml_alloc(1, 0);
Store_field( result, 0, Val_int(42) );
// Error "the walking camel"
// result = caml_alloc(1, 1);
// char *s = "the walking camel";
// Store_field( result, 0, caml_copy_string(s) );
CAMLreturn(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment