Skip to content

Commit

Permalink
Merge pull request #5821 from JuliaLang/jb/replace_main
Browse files Browse the repository at this point in the history
add an internal function to replace Main, use it in sysimg.jl
  • Loading branch information
JeffBezanson committed Feb 16, 2014
2 parents 54fc022 + cabdd23 commit 2fdefe2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
1 change: 0 additions & 1 deletion base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
# runnable::Bool
# end

import Main
import Core.Intrinsics.ccall

export
Expand Down
6 changes: 6 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Core.Intrinsics.ccall
ccall(:jl_new_main_module, Any, ())

baremodule Base

eval(x) = Core.eval(Base,x)
Expand Down Expand Up @@ -247,4 +250,7 @@ end

end # baremodule Base

using Base
importall Base.Operators

Base.isfile("userimg.jl") && Base.include("userimg.jl")
1 change: 1 addition & 0 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ void jl_restore_system_image(char *fname)
jl_array_type->env = jl_deserialize_value(&f);

jl_main_module = (jl_module_t*)jl_deserialize_value(&f);
jl_internal_main_module = jl_main_module;
jl_core_module = (jl_module_t*)jl_get_global(jl_main_module,
jl_symbol("Core"));
jl_base_module = (jl_module_t*)jl_get_global(jl_main_module,
Expand Down
1 change: 1 addition & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ static void gc_mark(void)

// modules
gc_push_root(jl_main_module, 0);
gc_push_root(jl_internal_main_module, 0);
gc_push_root(jl_current_module, 0);
if (jl_old_base_module) gc_push_root(jl_old_base_module, 0);

Expand Down
11 changes: 5 additions & 6 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,14 +690,13 @@ void julia_init(char *imageFile)
jl_init_serializer();

if (!imageFile) {
jl_main_module = jl_new_module(jl_symbol("Main"));
jl_main_module->parent = jl_main_module;
jl_core_module = jl_new_module(jl_symbol("Core"));
jl_core_module->parent = jl_main_module;
jl_set_const(jl_main_module, jl_symbol("Core"),
(jl_value_t*)jl_core_module);
jl_module_using(jl_main_module, jl_core_module);
jl_new_main_module();
jl_internal_main_module = jl_main_module;

jl_current_module = jl_core_module;
jl_root_task->current_module = jl_current_module;

jl_init_intrinsic_functions();
jl_init_primitives();
jl_load("boot.jl");
Expand Down
2 changes: 2 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ DLLEXPORT jl_value_t *jl_apply_array_type(jl_datatype_t *type, size_t dim);

// modules and global variables
extern DLLEXPORT jl_module_t *jl_main_module;
extern DLLEXPORT jl_module_t *jl_internal_main_module;
extern DLLEXPORT jl_module_t *jl_core_module;
extern DLLEXPORT jl_module_t *jl_base_module;
extern DLLEXPORT jl_module_t *jl_current_module;
Expand All @@ -789,6 +790,7 @@ DLLEXPORT void jl_module_use(jl_module_t *to, jl_module_t *from, jl_sym_t *s);
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);
STATIC_INLINE jl_function_t *jl_get_function(jl_module_t *m, const char *name)
{
Expand Down
27 changes: 26 additions & 1 deletion src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,43 @@
int jl_lineno = 0;

jl_module_t *jl_old_base_module = NULL;
// the Main we started with, in case it is switched
jl_module_t *jl_internal_main_module = NULL;

jl_value_t *jl_toplevel_eval_flex(jl_value_t *e, int fast);

void jl_add_standard_imports(jl_module_t *m)
{
assert(jl_base_module != NULL);
// using Base
jl_module_using(m, jl_base_module);
// importall Base.Operators
jl_module_importall(m, (jl_module_t*)jl_get_global(jl_base_module,
jl_symbol("Operators")));
}

jl_module_t *jl_new_main_module(void)
{
// switch to a new top-level module
if (jl_current_module != jl_main_module && jl_current_module != NULL)
jl_error("Main can only be replaced from the top level");

jl_module_t *old_main = jl_main_module;

jl_main_module = jl_new_module(jl_symbol("Main"));
jl_main_module->parent = jl_main_module;
jl_core_module->parent = jl_main_module;
jl_set_const(jl_main_module, jl_symbol("Core"),
(jl_value_t*)jl_core_module);
jl_set_global(jl_core_module, jl_symbol("Main"),
(jl_value_t*)jl_main_module);

jl_current_module = jl_main_module;
jl_current_task->current_module = jl_main_module;

return old_main;
}

extern void jl_get_system_hooks(void);
extern void jl_get_uv_hooks(int);
extern int base_module_conflict;
Expand Down Expand Up @@ -63,7 +88,7 @@ jl_value_t *jl_eval_module_expr(jl_expr_t *ex)
jl_old_base_module = jl_base_module;
// pick up Base module during bootstrap
jl_base_module = newm;
newbase = 1;
newbase = base_module_conflict;
}
// export all modules from Main
if (parent_module == jl_main_module)
Expand Down

0 comments on commit 2fdefe2

Please sign in to comment.