caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oleg@okmij.org
To: evgenyr@cs.ubc.ca
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Callback.register equivalent before runtime is
Date: 22 Aug 2013 05:47:56 -0000	[thread overview]
Message-ID: <20130822054756.29798.qmail@www1.g3.pair.com> (raw)
In-Reply-To: <20130815070432.GA31041@jade.home.test>


> 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

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

You may want to look into how MetaOCaml does it. Briefly, you just
have to link in your own .cmo file right before user's files. Your
file will set things up. For example, metaocamlc is a shell script
that does 

     exec ocamlc -I +compiler-libs ocamlcommon.cma ocamlbytecomp.cma ocamltoplevel.cma metalib.cma berstart.cmo "$@"

The key is the file berstart.cmo placed before any user files to
compile and link. That file is trivial and enclosed for
convenience. The file mentions Toplevel because MetaOCaml relies on
top-level for executing code created at run-time. You can add your
registration code to that file.

(*
   To `run' the code we use toplevel facilities.
   If we invoke BER MetaOcaml top level, then Toplevel.topstart() will
   initalialize the top level.
   If we execute a byte-compiled executable, we link with
   the top-level library. But we need initialize it first.
   This is the job of the current file.

   This file must be linked in *before* the first user executable.

   The present code roughly do the same steps OCaml top level does
   when executing a script.
   See Toplevel.topmain
*)

let () =
  Toploop.set_paths ();
  Compile.init_path();
  Toploop.initialize_toplevel_env ()




  parent reply	other threads:[~2013-08-22  5:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-15  7:04 [Caml-list] Callback.register equivalent before runtime is initialized? Evgeny Roubinchtein
2013-08-15  7:33 ` David Allsopp
2013-08-22  5:47 ` oleg [this message]
2013-08-23 20:39   ` [Caml-list] Callback.register equivalent before runtime is 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=20130822054756.29798.qmail@www1.g3.pair.com \
    --to=oleg@okmij.org \
    --cc=caml-list@inria.fr \
    --cc=evgenyr@cs.ubc.ca \
    /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).