caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolás Ojeda Bär" <nicolas.ojeda.bar@lexifi.com>
To: "frederic.fort@univ-lille.fr" <frederic.fort@univ-lille.fr>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Dynlink plugin reevaluates modules of main program
Date: Thu, 8 Nov 2018 10:23:40 +0000	[thread overview]
Message-ID: <CADK7aFMN_Ggz_fKPO05BfomEhUrisGp-WBoKQTCLhPKUmovyKA@mail.gmail.com> (raw)
In-Reply-To: <1238750250.1711857.1541671514743.JavaMail.zimbra@univ-lille.fr>

Dear Frédéric,

The reason for this behaviour is that `ocamlbuild` is linking b.cmx
(and c.cmx for that matter) into your plugin, when in fact you want it
to use the copy of B (and C) linked to your main program.
You can see the right behaviour if you build your plugin by hand, e.g. by doing

  ocamlopt -shared d.cmx d.cmxs

and run a.native.

If I remember correctly you need to use a .mldylib or .mllib file to
tell `ocamlbuild` explicitly which modules to link into your plugin
(in your case, just B). Someone more knowledgeable about `ocamlbuild`
may want to chime in.

Hope it helps!

Best wishes,
Nicolás



On Thu, Nov 8, 2018 at 11:05 AM Frédéric Fort
<frederic.fort@univ-lille.fr> wrote:
>
> 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

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

  reply	other threads:[~2018-11-08 10:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-08 10:05 Frédéric Fort
2018-11-08 10:23 ` Nicolás Ojeda Bär [this message]
2018-11-08 10:32 ` Gabriel Scherer
2018-11-08 13:02 ` Ivan Gotovchits
2018-11-15 16:35   ` Nicolás Ojeda Bär

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADK7aFMN_Ggz_fKPO05BfomEhUrisGp-WBoKQTCLhPKUmovyKA@mail.gmail.com \
    --to=nicolas.ojeda.bar@lexifi.com \
    --cc=caml-list@inria.fr \
    --cc=frederic.fort@univ-lille.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).