caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Passing `string option' values between OCaml and C
@ 1998-08-27  9:16 Christian Lindig
  0 siblings, 0 replies; 2+ messages in thread
From: Christian Lindig @ 1998-08-27  9:16 UTC (permalink / raw)
  To: Caml Mailing List


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 






^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re:  Passing `string option' values between OCaml and C
@ 1998-09-01 13:43 Damien Doligez
  0 siblings, 0 replies; 2+ messages in thread
From: Damien Doligez @ 1998-09-01 13:43 UTC (permalink / raw)
  To: caml-list

>From: Christian Lindig <lindig@ips.cs.tu-bs.de>

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

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

Well, it is the case that Val_int(0) is equal to 1, but you should use
Val_int(0) for this purpose.

WARNING: the following information is subject to change and is not
guaranteed to work for any version of O'Caml different from 1.07
and 2.00.


This is how concrete datatypes are encoded in O'Caml.

The constant constructors have the same representation as O'Caml
integers: the first constant constructor is 0, the second one 1, etc.

The constructors with arguments are represented by memory blocks,
which include a tag in their header.  The first non-constant
constructor's tag is 0, the second one 1, etc.

For example, let's consider the following declaration.

type foo =
  | AAA
  | BBB of int
  | CCC
  | DDD of string * string
  | EEE of (int -> int)
  | FFF
;;

In this case, AAA, CCC, FFF are numbered from 0 to 2, and BBB, DDD,
EEE are again numbered from 0 to 2.

So if we have in Caml

x = AAA
y = BBB 345
z = CCC
t = DDD ("foo", "bar")
u = EEE (fun x -> x)
v = FFF

then in C,

x = Val_int(0)

Tag_val (y) = 0
Wosize_val (y) = 1
Field (y, 0) = Val_int (345)

z = Val_int(1)

Tag_val (t) = 1
Wosize_val (t) = 2
String_val (Field (t, 0)) is a zero-terminated string that contains "foo"
String_val (Field (t, 1)) is a zero-terminated string that contains "bar"

Tag_val (u) = 2
Wosize_val (u) = 1
Field (u, 0) is the closure of "fun x -> x", and could be passed as
first argument to one of the callback functions.

v = Val_int(2)


HTH,

-- Damien





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-09-01 17:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-08-27  9:16 Passing `string option' values between OCaml and C Christian Lindig
1998-09-01 13:43 Damien Doligez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).