From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 70DDCBC57 for ; Tue, 6 Jul 2010 23:33:48 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsIEAJ8+M0xV2gB4cWdsb2JhbACDHZx7AQwIDQcRIq84kTKBKYMKcgQ X-IronPort-AV: E=Sophos;i="4.53,548,1272837600"; d="scan'208";a="65934570" Received: from emailfrontal1.citycable.ch ([85.218.0.120]) by mail4-smtp-sop.national.inria.fr with SMTP; 06 Jul 2010 23:33:48 +0200 Received: from [192.168.1.27] (LVelizy-156-45-9-48.w80-11.abo.wanadoo.fr [80.11.30.48]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal1.citycable.ch (Postfix) with ESMTPA id 5C9ED12C15D; Tue, 6 Jul 2010 23:33:45 +0200 (CEST) Message-ID: <4C339FE3.6000402@citycable.ch> Date: Tue, 06 Jul 2010 23:28:03 +0200 From: Guillaume Yziquel Reply-To: guillaume.yziquel@citycable.ch User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: Paolo Donadeo Cc: OCaml List Subject: Re: [Caml-list] Binding the Lua library [was: adding a scripting language to an ocaml program] References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; guillaume:01 guillaume:01 ocaml:01 mismatch:01 ocaml:01 node:01 recursive:01 subtyping:01 recursive:01 subtyping:01 compiler:01 inference:01 compiler:01 bindings:01 doable:01 Paolo Donadeo a =C3=A9crit : >=20 > 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=20 the solution is far from perfect, but works. I do not know about Lua,=20 though. > And again: how to translate (read: how to bind) in OCaml functions like= these: >=20 > void *lua_newuserdata (lua_State *L, size_t size); [4] > or > void *lua_touserdata (lua_State *L, int index); [5] >=20 > 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=20 bottom). http://ocaml.janestreet.com/?q=3Dnode/59 Unfortunately, using this recursive subtyping trick (which can be=20 extended to more sophisticated recursive subtyping between more than 2=20 type systems) raises issues in the presence of OCaml classes: The=20 compiler goes in an infinite loop when doing type inference. The issue=20 should be solved in 3.12. It's not yet used in the OCaml-R binding because of compiler infinite=20 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=20 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=20 Java) is that it's doable. The first shot is often imperfect and=20 inefficient becauses GCs are not designed in the spirit of being binded.=20 (R's API only provides a cumbersome stack-based mechanism to interact=20 with R's GC, which is wholly inadequate). I guess that with more available time, it is indeed possible to do clean=20 tricks in order to get the GCs to cooperate efficiently... (I have a few=20 tricks in mind for R that will have to wait.) All the best, --=20 Guillaume Yziquel http://yziquel.homelinux.org/