#include extern "C" { # include # include # include extern void failwith(char *s); } #include "libcell.h" typedef struct { final_fun f; Cell *c; } mlcell; static void free_cell(value mlc) { delete ((mlcell*)mlc)->c; } static mlcell mlglobal_cell = { free_cell, &global_cell }; extern "C" value caml_global_cell(value) { return (value)&mlglobal_cell; } extern "C" value caml_cell_create(value mlv) { value res = alloc_final(sizeof(mlcell)/sizeof(value), free_cell, 1, 1000); /* ? */ ((mlcell*)res)->c = new Cell(Int_val(mlv)); return res; } extern "C" value caml_cell_set(value mlc, value mlv) { try { ((mlcell*)mlc)->c->set(Int_val(mlv)); } catch (Exc e) { failwith((char*)e.msg); } return Val_unit; } extern "C" value caml_cell_get(value mlc) { int v = ((mlcell*)mlc)->c->get(); return Val_int(v); } extern "C" value caml_cell_throw(value) { throw Exc("caml_cell_throw"); } extern "C" value caml_cell_call(value mlname) { value f = *caml_named_value(String_val(mlname)); try { return callback(f, Val_unit); } catch (Exc e) { return copy_string((char*)e.msg); } }