caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Peter Zotov <whitequark@whitequark.org>
To: "Christoph Höger" <christoph.hoeger@tu-berlin.de>
Cc: caml users <caml-list@inria.fr>, caml-list-request@inria.fr
Subject: Re: [Caml-list] is it possible to embed an OCaml interpreter into an OCaml Module?
Date: Fri, 31 Oct 2014 18:03:52 +0300	[thread overview]
Message-ID: <ac203386300caa1bc9701bb91418f685@whitequark.org> (raw)
In-Reply-To: <54539FD9.1050709@tu-berlin.de>

On 2014-10-31 17:42, Christoph Höger wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Dear all,
> 
> I already asked this on stackoverflow and was pointed to
> compiler-libs.toplevel - indeed this API seems to be sufficient to run
> an OCaml interpreter from inside an OCaml program (as that seems to be
> what utop does).
> 
> But ist it also possible in some way to embed that interpreter safely
> in an OCaml Module (so I can reuse it e.g. from within utop)?
> Currently, it seems that there is exactly one dedicated toplevel for
> every running bytecode interpreter and when running utop, it is
> already in use.
> 
> So what I would need would be the ability to execute a phrase from
> within a call of execute_phrase. I already clonded the toploop module
> and for tehe time being I am fine with that. What I need is a way to
> 
> a) safe the already set ('outer') toplevel value bindings
> b) restore the nested value bindings
> c) execute the compiled bytecode
> d) restore the 'outer' value bindings
> 
> is there someone who can point me to a solution?

There is nothing special about the toplevel or the OCaml interpreter.
Indeed, you can run a chunk of OCaml code at any time using
the caml_callback C call--this is exactly what the runtime would
invoke at startup, or what the toplevel would do to run a freshly
compiled phrase.

However, the OCaml runtime has quite some global data:
a cursory look brings up GC, backtrace machinery, polymorphic
comparison (!), debugger, dynlink (and its symbol tables),
exceptions, finalizers, and a few more things. Depending on your
desired level of isolation, it might be very hard to provide
a clean environment.

Incidentally, can someone explain how the runtime manages to be
multithreaded while it has so much global data? Please don't
tell me the answer is "every thread copies it back and forth
on context switch".

-- 
Peter Zotov

  reply	other threads:[~2014-10-31 15:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-31 14:42 Christoph Höger
2014-10-31 15:03 ` Peter Zotov [this message]
2014-11-01 13:10   ` Stephen Dolan
2014-11-01 12:54 oleg
2014-11-03 14:13 ` Christoph Höger

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=ac203386300caa1bc9701bb91418f685@whitequark.org \
    --to=whitequark@whitequark.org \
    --cc=caml-list-request@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=christoph.hoeger@tu-berlin.de \
    /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).