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
Re: Passing `string option' values between OCaml and C
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1998-09-01 (17:12)
From: Damien Doligez <Damien.Doligez@i...>
Subject: Re: Passing `string option' values between OCaml and C
>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)


-- Damien