caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: RE: [Caml-list] Callback.register equivalent before runtime is initialized?
Date: Thu, 15 Aug 2013 07:33:53 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9CCB111EF@Remus.metastack.local> (raw)
In-Reply-To: <20130815070432.GA31041@jade.home.test>

Evgeny Roubinchtein wrote:
> Hello, everyone,
> 
> I am learning about run-time compilation, and using OCaml as a base for my
> experiments.  So far, I have a really simple C primitive that just calls
> back into a simple OCaml wrapper around Toplevel.load_lambda (it passes a
> Lambda.lambda that has been marshalled to a string constant).
> Since the C primitive is in the run-time (libcamlrun) library, and the
> wrapper is in the Toplevel, I am using Callback.register to pass the
> address of the wrapper to the C primitive.  That set-up works pretty well
> in the toplevel because I can call Callback.register when the toplevel is
> being initialized, i.e., before any code is read from the user.
> 
> However, I am now trying to move to producing bytecode executables, so, if
> I stay with my current set-up I would need to do the equivalent of
> Callback.register (i.e., put the address of the wrapper into the hash
> table maintained by caml_callback_register) before any of the "user code"
> in the executable starts to run.  I have been looking at the
> (bytecode) linker and also the caml_startup function to understand how the
> running of bytecode "gets started", and I have also modified tools/objinfo
> to print the contens of the SYMB section in bytecode executables and the
> relocations table in .cmo's to see if inspiration strikes ;-) -- but so
> far I am not quite seeing "how things work."
> 
> I'll keep looking, but I though I'd ask for advice here in case someone
> has some advice to offer.

Exposing the primitives directly is in general a bad idea - and the "problem" you're looking at here is one reason why it's bad. Wrap the primitive in a module and instruct your users (even if that's just you!) to use the module - link order then takes care of your problem. So if your primitive is my_first_primitive then you could have:

MyPrimitives.mli:
external first_primitive : foo -> bar = "my_first_primitive"

MyPrimitives.ml:
external first_primitive : foo -> bar = "my_first_primitive"

let _ =
  Callback.register ...

Now when you compile a program using your primitive, you should link MyPrimitives.cmo (or MyPrimitives.cmx in native) and your initialisation code is guaranteed to be called before MyPrimitives.first_primitive is ever called by a user. This also ensures that your primitive is used with the correct OCaml type (although bear in mind that C primitives can be written to be legitimately used with multiple types - e.g. %identity).

Yes, your primitive could still be called directly - but that's now an "unsupported" route and a user doing that gets all that's coming to them! Incidentally, you don't have to use the "external" declaration in the .mli, but the manual chapter on C linking explains that there is a small benefit to doing that, if you don't mind revealing that the function is a C stub.

HTH,


David

  reply	other threads:[~2013-08-15  7:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-15  7:04 Evgeny Roubinchtein
2013-08-15  7:33 ` David Allsopp [this message]
2013-08-22  5:47 ` [Caml-list] Callback.register equivalent before runtime is oleg
2013-08-23 20:39   ` Evgeny Roubinchtein

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=E51C5B015DBD1348A1D85763337FB6D9CCB111EF@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.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).