Use Caml macros for variables

This commit is contained in:
Fabien Freling 2019-06-06 14:39:11 +02:00
parent d197159f5a
commit f0cab3f1c6

View file

@ -3,6 +3,7 @@
#include <caml/mlvalues.h> #include <caml/mlvalues.h>
#include <caml/callback.h> #include <caml/callback.h>
#include <caml/alloc.h> #include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/misc.h> #include <caml/misc.h>
#include <caml/osdeps.h> #include <caml/osdeps.h>
@ -11,40 +12,57 @@ OBoy::OBoy(QObject *parent) : QObject(parent)
} }
// https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html
// http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.html
QString OBoy::name() const QString OBoy::name() const
{ {
CAMLparam0();
value * closure_f = caml_named_value("oboy_name"); value * closure_f = caml_named_value("oboy_name");
if (closure_f == nullptr) { if (closure_f == nullptr) {
CAMLdrop;
return QString("<Unreachable>"); return QString("<Unreachable>");
} }
const char *str = String_val(caml_callback(*closure_f, Val_unit)); const char *str = String_val(caml_callback(*closure_f, Val_unit));
CAMLdrop;
return QString(str); return QString(str);
} }
QString OBoy::version() const QString OBoy::version() const
{ {
CAMLparam0();
value * closure_f = caml_named_value("oboy_version"); value * closure_f = caml_named_value("oboy_version");
if (closure_f == nullptr) { if (closure_f == nullptr) {
CAMLdrop;
return QString("<Unreachable>"); return QString("<Unreachable>");
} }
const char *str = String_val(caml_callback(*closure_f, Val_unit)); const char *str = String_val(caml_callback(*closure_f, Val_unit));
CAMLdrop;
return QString(str); return QString(str);
} }
bool OBoy::load(const QString &path) bool OBoy::load(const QString &path)
{ {
CAMLparam0();
CAMLlocal1(ocaml_path) ;
value * closure_f = caml_named_value("oboy_load"); value * closure_f = caml_named_value("oboy_load");
if (closure_f == nullptr) { if (closure_f == nullptr) {
CAMLdrop;
return false; return false;
} }
QString truncated(path); QString truncated(path);
truncated.remove(0, 7); // remove file:// truncated.remove(0, 7); // remove file://
QByteArray ba = truncated.toLocal8Bit(); QByteArray ba = truncated.toLocal8Bit();
value ocaml_path = caml_copy_string_of_os(ba.data());
ocaml_path = caml_copy_string_of_os(ba.data());
const bool success = Bool_val(caml_callback(*closure_f, ocaml_path)); const bool success = Bool_val(caml_callback(*closure_f, ocaml_path));
CAMLdrop;
return success; return success;
} }