Permalink
Browse files

add an internal function to replace Main, use it in sysimg.jl

make the Core.Main binding non-constant, so uses of "Main" in general
refer to the *current* Main. Doing "import Main" will give you a
constant reference to the Main at that time.

fixes #5802

related to #1195
  • Loading branch information...
1 parent 26928e8 commit cabdd238bef07742ba1d24862a127eb3c9c21488 @JeffBezanson JeffBezanson committed Feb 14, 2014
Showing with 41 additions and 8 deletions.
  1. +0 −1 base/boot.jl
  2. +6 −0 base/sysimg.jl
  3. +1 −0 src/dump.c
  4. +1 −0 src/gc.c
  5. +5 −6 src/init.c
  6. +2 −0 src/julia.h
  7. +26 −1 src/toplevel.c
View
@@ -112,7 +112,6 @@
# runnable::Bool
# end
-import Main
import Core.Intrinsics.ccall
export
View
@@ -1,3 +1,6 @@
+import Core.Intrinsics.ccall
+ccall(:jl_new_main_module, Any, ())
+
baremodule Base
eval(x) = Core.eval(Base,x)
@@ -247,4 +250,7 @@ end
end # baremodule Base
+using Base
+importall Base.Operators
+
Base.isfile("userimg.jl") && Base.include("userimg.jl")
View
@@ -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,
View
@@ -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);
View
@@ -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");
View
@@ -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;
@@ -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)
{
View
@@ -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;
@@ -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)

0 comments on commit cabdd23

Please sign in to comment.