From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id D120DBC37 for ; Fri, 7 Aug 2009 01:38:31 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvUBAH0Ge0rZSMDdi2dsb2JhbACBUph3AQEBCgsKBxEFtmmEGAU X-IronPort-AV: E=Sophos;i="4.43,337,1246831200"; d="scan'208";a="32164537" Received: from fmmailgate01.web.de ([217.72.192.221]) by mail3-smtp-sop.national.inria.fr with ESMTP; 07 Aug 2009 01:38:31 +0200 Received: from smtp07.web.de (fmsmtp07.dlan.cinetic.de [172.20.5.215]) by fmmailgate01.web.de (Postfix) with ESMTP id EF3AE1105EEB4 for ; Fri, 7 Aug 2009 01:38:30 +0200 (CEST) Received: from [95.208.117.111] (helo=frosties.localdomain) by smtp07.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #314) id 1MZCXW-0005Vv-00 for caml-list@inria.fr; Fri, 07 Aug 2009 01:38:30 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.69) (envelope-from ) id 1MZCXW-0006dj-D6 for caml-list@inria.fr; Fri, 07 Aug 2009 01:38:30 +0200 From: Goswin von Brederlow To: caml-list@inria.fr Subject: Re: [Caml-list] C wrappers and callbacks from Ocaml to C and back to Ocaml References: <20090807071731.bfd0d34b.mle+ocaml@mega-nerd.com> Date: Fri, 07 Aug 2009 01:38:30 +0200 In-Reply-To: <20090807071731.bfd0d34b.mle+ocaml@mega-nerd.com> (Erik de Castro Lopo's message of "Fri, 7 Aug 2009 07:17:31 +1000") Message-ID: <87r5voa4y1.fsf@frosties.localdomain> User-Agent: Gnus/5.110006 (No Gnus v0.6) XEmacs/21.4.22 (linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX1/o/yT8l+tVxoY3B89JWSs1Xn1bBZ4ubHxLtLGm mcrx+NTY02GCjQWXkCyKP9dwcB7Ip4LzqUbyeLXQY4mm4edzEP B25MvmTMw= X-Spam: no; 0.00; wrappers:01 callbacks:01 ocaml:01 ocaml:01 struct:01 struct:01 cheers:01 interfacing:01 mfg:98 invoke:01 caml-list:01 writes:01 closure:01 define:02 root:04 Erik de Castro Lopo writes: > Hi all, > > I'm wrapping some C code that requires me to define a struct in Ocaml > like this: > > type xt = { f : xt -> a } > > and then pass the struct to C which stores it, and later the C needs > to find function f, call it from C, passing it the Ocaml xt struct. > > Anyone have any clues on how to do this? I have most of the rest of > the C wrapper sorted out, its just this thats causing me any difficulty. > > Cheers, > Erik Well, you just do. A structure is a value like anything else. What you have to wath out for is the GC. If you store the value somewhere then you have to tell the GC about it by registering it as a new root. And when you are done with it you need to unregister it. As for access the docs tell you about how to access fields of a record and also how to invoke a ocaml closure from C. Just read the relevant sections from the chapter about interfacing ocaml and C. MfG Goswin