I'm a bit surprised to find that native dynlink doesn't work in the
same way as bytecode dynlink in respect to reloading the same module.
(See attached test program)

In bytecode dynlink, reloading (ie. Dynlink.loadfile) the same module
causes the new module code to override the old module code:

  $ ./dynlink_test
  testing bytecode ...
  a
  b

But in native dynlink, the new module is silently discarded and the
old code appears to run:

  $ ./dynlink_test
  testing native ...
  a
  a

I would classify this as a bug, but I'm not quite sure what is
expected to happen.  Is there some other way to override a module as
in bytecode?

  $ ocaml -version
  The Objective Caml toplevel, version 3.12.1

Rich.

-- 
Richard Jones
Red Hat

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

(* Save this file as 'dynlink_test.ml'
 * and compile it with:
 *   ocamlc dynlink.cma dynlink_value.ml dynlink_test.ml -o dynlink_test
 * or:
 *   ocamlopt dynlink.cmxa dynlink_value.ml dynlink_test.ml -o dynlink_test
 *)

open Printf

let native = Dynlink.is_native
let suffix = if native then "cmxs" else "cmo"

let write_test_module v =
  let chan = open_out "dynlink_test_module.ml" in
  fprintf chan "Dynlink_value.value := \"%s\"\n" v;
  close_out chan;

  let cmd =
    if native then
      "ocamlopt -c dynlink_test_module.ml &&\nocamlopt -shared 
dynlink_test_module.cmx -o dynlink_test_module.cmxs"
    else
      "ocamlc -c dynlink_test_module.ml" in
  assert (Sys.command cmd = 0)

let main () =
  printf "testing %s ...\n" (if native then "native" else "bytecode");

  write_test_module "a";
  Dynlink.loadfile (sprintf "dynlink_test_module.%s" suffix);
  print_endline !Dynlink_value.value;

  write_test_module "b";
  Dynlink.loadfile (sprintf "dynlink_test_module.%s" suffix);
  print_endline !Dynlink_value.value

let () =
  try main ()
  with
  | Dynlink.Error err ->
    eprintf "dynlink: %s\n" (Dynlink.error_message err)
(* Save this file as 'dynlink_value.ml' *)

let value = ref "none"

Reply via email to