1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2014-2021, VU University Amsterdam 7 SWI-Prolog Solutions b.v. 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(modules, 37 [ in_temporary_module/3, % ?Module, :Setup, :Goal 38 current_temporary_module/1 % ?Module 39 ]).
45:- meta_predicate
46 in_temporary_module( , , ).
The logical result of this predicate is the same as
`(Setup@Module -> Goal@Module)`, i.e., both Setup and Goal are
resolved relative to the current module, but executed in the
context of Module. If Goal must be called in Module, use
call(Goal)
.
The module and all its predicates are destroyed after Goal terminates, as defined by setup_call_cleanup/3.
Discussion This predicate is intended to load programs in an isolated environment and reclaim all resources. This unfortunately is incomplete:
88in_temporary_module(Module, Setup, Goal) :- 89 setup_call_cleanup( 90 prepare_temporary_module(Module), 91 ( @(Setup, Module) 92 -> @(Goal, Module) 93 ), 94 destroy_module(Module)). 95 96prepare_temporary_module(Module) :- 97 var(Module), 98 !, 99 ( thread_id(Tid), 100 repeat, 101 I is random(1<<62), 102 atomic_list_concat([tmp, Tid, I], -, Module), 103 catch(set_module(Module:class(temporary)), 104 error(permission_error(_,_,_),_), fail) 105 -> b_setval('$tmp_module', Module) 106 ). 107prepare_temporary_module(Module) :- 108 set_module(Module:class(temporary)), 109 b_setval('$tmp_module', Module). 110 111:- if(current_prolog_flag(threads, true)). 112thread_id(Id) :- 113 thread_self(Self), 114 thread_property(Self, id(Id)). 115:- else. 116thread_id(main). 117:- endif. 118 119destroy_module(Module) :- 120 retractall(system:'$load_context_module'(_File, Module, _Options)), 121 '$destroy_module'(Module).
127current_temporary_module(Module) :-
128 nb_current('$tmp_module', Module)
Module utility predicates
*/