Passing `string option' values between OCaml and C

From: Christian Lindig (lindig@ips.cs.tu-bs.de)
Date: Thu Aug 27 1998 - 11:16:34 MET DST


Date: Thu, 27 Aug 1998 11:16:34 +0200
Message-Id: <199808270916.LAA06931@infbsstq.ips.cs.tu-bs.de>
From: Christian Lindig <lindig@ips.cs.tu-bs.de>
To: Caml Mailing List <caml-list@inria.fr>
Subject: Passing `string option' values between OCaml and C

After reading the manual, the mailing list archive, and some OCaml C
sources I'm still not sure how to read and build constructed values in
C. Two functions in the OCaml C source that receive optional values
act quite differently:

weak_set() in byterun/weak.c tests for value el != 1 to recognize the
Some case:

       #define None_val 1
       #define Some_tag 0
       
       /* type 'a option = None | Some of 'a
        * val set : 'a t -> int -> 'a option -> unit
        */
       
       value weak_set (value ar, value n, value el) /* ML */
       {
         ...
         if (el != None_val){
           Modify (&Field (ar, offset), Field (el, 0));
         }
         ...
       }

win_create_process() in otherlibs/win32unix/createprocess.c on the
other hand tests optional value env != Val_int(0) to do this:

      /*
       * win_create_process : string -> string -> string option ->
       * file_descr -> file_descr -> file_descr -> int
       */
      
      value win_create_process_native(value cmd, value cmdline, value env,
                                      value fd1, value fd2, value fd3)
      {
        ...
        if (env != Val_int(0)) {
          envp = String_val(Field(env, 0));
        } else {
          envp = NULL;
        }
        ...
      }

Could someone explain the general way how to read and build constructed
values in C using optional values as an example? Starting with some
#defines derived from the OCaml type definition to nail down the tags
seems a good starting point to me.

Christian

------------------------------------------------------------------------------
Christian Lindig http: www.cs.tu-bs.de/softech/people/lindig
                                  mail: lindig@ips.cs.tu-bs.de
                                  phone: +49 531 391 7465
                                  fax: +49 531 391 8140



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:15 MET