Skip to content

Commit

Permalink
add clear() function that replaces Main
Browse files Browse the repository at this point in the history
a link to the previous Main called LastMain is provided.
`using LastMain.Package` works to restore access to loaded libraries.

fixes #1195, fixes #2385
  • Loading branch information
JeffBezanson committed Jul 1, 2014
1 parent da158df commit 14aba85
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ export
versioninfo,
which,
whos,
clear,

# loading source files
evalfile,
Expand Down
13 changes: 13 additions & 0 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,16 @@ function download(url::String)
filename = tempname()
download(url, filename)
end

function clear()
last = Core.Main
b = last.Base
ccall(:jl_new_main_module, Any, ())
m = Core.Main
ccall(:jl_add_standard_imports, Void, (Any,), m)
eval(m,
Expr(:toplevel,
:(const Base = $(Expr(:quote, b))),
:(const LastMain = $(Expr(:quote, last)))))
m
end
22 changes: 20 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@ module_name(m::Module) = ccall(:jl_module_name, Any, (Any,), m)::Symbol
module_parent(m::Module) = ccall(:jl_module_parent, Any, (Any,), m)::Module
current_module() = ccall(:jl_get_current_module, Any, ())::Module

fullname(m::Module) = m===Main ? () : tuple(fullname(module_parent(m))...,
module_name(m))
function fullname(m::Module)
if m === Main
()
elseif module_parent(m) === m
# not Main, but is its own parent, means a prior Main module
n = ()
this = Main
while this !== m
if isdefined(this, :LastMain)
n = tuple(n..., :LastMain)
this = this.LastMain
else
error("no reference to module ", module_name(m))
end
end
return n
else
tuple(fullname(module_parent(m))..., module_name(m))
end
end

names(m::Module, all::Bool, imported::Bool) = ccall(:jl_module_names, Array{Symbol,1}, (Any,Int32,Int32), m, all, imported)
names(m::Module, all::Bool) = names(m, all, false)
Expand Down
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ DLLEXPORT void jl_module_import(jl_module_t *to, jl_module_t *from, jl_sym_t *s)
DLLEXPORT void jl_module_importall(jl_module_t *to, jl_module_t *from);
DLLEXPORT void jl_module_export(jl_module_t *from, jl_sym_t *s);
DLLEXPORT jl_module_t *jl_new_main_module(void);
void jl_add_standard_imports(jl_module_t *m);
DLLEXPORT void jl_add_standard_imports(jl_module_t *m);
STATIC_INLINE jl_function_t *jl_get_function(jl_module_t *m, const char *name)
{
return (jl_function_t*) jl_get_global(m, jl_symbol(name));
Expand Down

0 comments on commit 14aba85

Please sign in to comment.