caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Dynlink plugin reevaluates modules of main program
@ 2018-11-08 10:05 Frédéric Fort
  2018-11-08 10:23 ` Nicolás Ojeda Bär
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Frédéric Fort @ 2018-11-08 10:05 UTC (permalink / raw)
  To: caml-list

Hello,

I have an existing program and would like to allow to extend it's functionalities with plugins.
If I simplify my code structure it looks as follows:
 - a.ml : "main module" of the program
 - b.ml : additional definitions used in a.ml
 - c.ml : interface for plugins (a collection of function refs)
 - d.ml : plugin I would like to load

Now, d.ml uses values defined in b.ml. Some of them are of type string ref
and it seems that the code of b.ml is reevaluated when I call
Dynlink.loadfile "/path/to/d.cmxs" which resets them to the empty string.

Is there a way to prevent this from happening ?
Using allow_only and prohibit is not an option, since multiple plugins would each reevaluate C
and undo each others modifications.

Yours sincerely,
Frédéric Fort

P.S.: Here follows a minimal working example.
I compiled it with
ocamlbuild -use-ocamlfind -lib dynlink a.native
ocamlbuild -use-ocamlfind d.cmxs

a.ml:
open Format

let _ =
  B.str := "abc";
  printf "%s\n" !B.str;
  begin
    try
      Dynlink.loadfile "./_build/d.cmxs"
    with Dynlink.Error err ->
      failwith (Dynlink.error_message err) end;
  printf "%s\n" !B.str;
  match !C.f with
  | Some(f) -> printf "%s\n" (f 0)
  | None -> ()

b.ml:
let str = ref ""

c.ml:
let f : (int -> string) option ref = ref None

d.ml:
let _ =
  C.f := Some((fun x -> !B.str^(string_of_int x)))

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list https://inbox.ocaml.org/caml-list
Forum: https://discuss.ocaml.org/
Bug reports: http://caml.inria.fr/bin/caml-bugs

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

end of thread, other threads:[~2018-11-15 16:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-08 10:05 [Caml-list] Dynlink plugin reevaluates modules of main program Frédéric Fort
2018-11-08 10:23 ` Nicolás Ojeda Bär
2018-11-08 10:32 ` Gabriel Scherer
2018-11-08 13:02 ` Ivan Gotovchits
2018-11-15 16:35   ` Nicolás Ojeda Bär

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).