From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id A936D7EFCD for ; Sat, 1 Nov 2014 13:54:30 +0100 (CET) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of oleg@okmij.org) identity=pra; client-ip=66.39.3.115; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="oleg@okmij.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of oleg@okmij.org designates 66.39.3.115 as permitted sender) identity=mailfrom; client-ip=66.39.3.115; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="oleg@okmij.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@www1.g3.pair.com) identity=helo; client-ip=66.39.3.115; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="postmaster@www1.g3.pair.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvEFAHfXVFRCJwNzY2dsb2JhbABcgw5UWII2umyQE4dVgRgWAQEBAQEGGAcWQoQEBHoFFiETIR2IfAnJdZEQHYMXgR4FlmeHFQGBMT2DEJVjIS+CSwEBAQ X-IPAS-Result: AvEFAHfXVFRCJwNzY2dsb2JhbABcgw5UWII2umyQE4dVgRgWAQEBAQEGGAcWQoQEBHoFFiETIR2IfAnJdZEQHYMXgR4FlmeHFQGBMT2DEJVjIS+CSwEBAQ X-IronPort-AV: E=Sophos;i="5.07,295,1413237600"; d="scan'208";a="85706467" Received: from www1.g3.pair.com ([66.39.3.115]) by mail3-smtp-sop.national.inria.fr with ESMTP; 01 Nov 2014 13:54:29 +0100 Received: by www1.g3.pair.com (Postfix, from userid 9370) id 90219C38B8; Sat, 1 Nov 2014 08:54:25 -0400 (EDT) From: oleg@okmij.org To: christoph.hoeger@tu-berlin.de cc: caml-list@inria.fr Message-Id: <20141101125425.90219C38B8@www1.g3.pair.com> Date: Sat, 1 Nov 2014 08:54:25 -0400 (EDT) Subject: Re: [Caml-list] is it possible to embed an OCaml interpreter into an OCaml Module? Christoph Hoeger wrote: > 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 The MetaOCaml top-level does exactly what you have described. It is the standard OCaml toplevel with the ability to execute the generated code. Please look at the file metalib/runcode.ml in the MetaOCaml distribution. If you get it from the metaocaml.bundle, you get the current version. Otherwise, please to make sure the beginning of run_bytecode starts as follows: let run_bytecode' exp = if !initial_env = Env.empty then begin let old_time = Ident.current_time() in (* does Ident.reinit() and may corrupt the timestamp if we run in top-level. See Ident.reinit code *) initial_env := Compmisc.initial_env(); Ident.set_current_time old_time end; (* Ctype.init_def(Ident.current_time()); *) ....