Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Indeterminate Initialization: Is it a Bug?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Cole <rcole@i...>
Subject: [Caml-list] Indeterminate Initialization: Is it a Bug?
Hi,

There seems to be a difference in initialization behaviour using the 
native compiler depending on whether a module contains non "external" 
calls. Let me give an example:

#!/bin/bash
rm -f test_funky.ml funky.ml ml_funky.c *funky*.cm* *funky*.a *funky*.so

echo '
  Printf.fprintf stderr "Test.Init\n"; flush stderr ;;
  open Funky ;;
  funky 4 ;;
' > test_funky.ml

echo '
  #include <caml/mlvalues.h>
  #include <caml/memory.h>
  #include <stdio.h>

  value ml_funky(value v)
  {
    CAMLparam1(v);
    int i = Int_val(v) + 1;
    fprintf(stderr, "ML_FUNKY!\n"); fflush(stderr);
    CAMLreturn(Val_int(i));
  };
' > ml_funky.c

echo '
  external funky : int -> int = "ml_funky" ;;
  Printf.fprintf stderr "Funky.Init.\n"; flush stderr ;;
' > funky.ml

gcc -Wall -c ml_funky.c -I/usr/local/godi/lib/ocaml/std-lib/
ocamlopt -c funky.ml
ocamlopt -c test_funky.ml
ocamlmklib -o funky ml_funky.o funky.cmx
ocamlopt -o test_funky.exe funky.cmxa test_funky.cmx -I .
./test_funky.exe

The output doesn't contain "Funky.Init". The problem is that the 
initialization code in the funky module didn't get called. If I wrap the 
functions as in:

#!/bin/bash
echo '
  external ml_funky: int -> int = "ml_funky" ;;
  let funky x = ml_funky x ;;
  Printf.fprintf stderr "Init.\n"; flush stderr ;;
' > funky.ml

ocamlopt -c funky.ml
ocamlmklib -o funky ml_funky.o funky.cmx
ocamlopt -o test_funky.exe funky.cmxa test_funky.ml -I .
./test_funky.exe

Then the initialization code, namely the printf statement in flunky.ml 
is called.

So I have the following question: Is there some way to guarrentee that 
modules are initialized exactly once? Does the development team see the 
current situation, whereby intialization depends on wheher there is a 
non external method in the module that is called, as a problem? Can it 
be fixed?

I should mention that I'm using Godi with godi-ocaml version 3.07pl2 and 
fairly vanilla Redhat 9. Is this problem replicatable on other platforms?

Why do I want to have intialization code always called in my modules. 
Mostly because I want the following initialization code to work without 
having to add a layer of indirection to my wrapper modules.

exception NotFound ;;
let _ = Callback.register "Funky.NotFound" NotFound ;;

best regards,

Richard.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners