English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Binding the Lua library [was: adding a scripting language to an ocaml program]
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-07-06 (21:33)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: [Caml-list] Binding the Lua library [was: adding a scripting language to an ocaml program]
Paolo Donadeo a écrit :
> So I wrote the binding to some basic functions and a stress test
> designed to reveal memory leaks of the binding. The test passed and so
> I consider the code quite stable, but I had to stop the development
> due to the chronic lack of time and due to a specific problem: I don't
> know how to handle the impedance mismatch between the two garbage
> collectors.

Yes. This kind of issue is tough. I also had it in the OCaml-R code. And 
the solution is far from perfect, but works. I do not know about Lua, 

> And again: how to translate (read: how to bind) in OCaml functions like these:
> void *lua_newuserdata (lua_State *L, size_t size); [4]
> or
> void *lua_touserdata (lua_State *L, int index); [5]
> Is Obj.magic the only possible way? It's ugly and, of course,
> "Obj.magic is not part of the OCaml language", but in this case you
> use a cast in C, so why not cast a spell in OCaml?

You might want to check out the code sample I published here (at the 


Unfortunately, using this recursive subtyping trick (which can be 
extended to more sophisticated recursive subtyping between more than 2 
type systems) raises issues in the presence of OCaml classes: The 
compiler goes in an infinite loop when doing type inference. The issue 
should be solved in 3.12.

It's not yet used in the OCaml-R binding because of compiler infinite 
loop problem. But it is used in my (unpublished) Python binding.

> If anyone is interested in my prototype I could clean up the source,
> remove comments in Italian and publish it on GitHub or OCamlCore.

I'm always interested in language bindings... And I think a few other 
people are interested in an Lua binding.

> And, of course, any ideas or help on the garbage collector(s) issue are welcome.

I do not know much about Lua's GC. My experience (for R, Python and 
Java) is that it's doable. The first shot is often imperfect and 
inefficient becauses GCs are not designed in the spirit of being binded. 
(R's API only provides a cumbersome stack-based mechanism to interact 
with R's GC, which is wholly inadequate).

I guess that with more available time, it is indeed possible to do clean 
tricks in order to get the GCs to cooperate efficiently... (I have a few 
tricks in mind for R that will have to wait.)

All the best,

      Guillaume Yziquel