Skip to content

Commit

Permalink
Ensure Main.eval and Main.include exist in embedded julia
Browse files Browse the repository at this point in the history
These two functions need to be added to Main as part of `jl_init()`,
but only when Base is present to avoid bootstrap issues.
  • Loading branch information
c42f committed May 21, 2019
1 parent 48634f9 commit 2bf916b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ function run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_fil
nothing
end

# MainInclude exists to hide Main.include and eval from `names(Main)`.
baremodule MainInclude
include(fname::AbstractString) = Main.Base.include(Main, fname)
eval(x) = Core.eval(Main, x)
Expand Down Expand Up @@ -459,7 +460,6 @@ MainInclude.include
function _start()
empty!(ARGS)
append!(ARGS, Core.ARGS)
@eval Main import Base.MainInclude: eval, include
try
exec_options(JLOptions())
catch
Expand Down
5 changes: 5 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,11 @@ void _julia_init(JL_IMAGE_SEARCH rel)
// it does "using Base" if Base is available.
if (jl_base_module != NULL) {
jl_add_standard_imports(jl_main_module);
jl_value_t *maininclude = jl_get_global(jl_base_module, jl_symbol("MainInclude"));
if (maininclude && jl_is_module(maininclude)) {
jl_module_import(jl_main_module, (jl_module_t*)maininclude, jl_symbol("include"));
jl_module_import(jl_main_module, (jl_module_t*)maininclude, jl_symbol("eval"));
}
// Do initialization needed before starting child threads
jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("__preinit_threads__"));
if (f) {
Expand Down
4 changes: 3 additions & 1 deletion test/embedding/embedding-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ end
@testset "embedding example" begin
out = Pipe()
err = Pipe()
p = run(pipeline(Cmd(ARGS), stdin=devnull, stdout=out, stderr=err), wait=false)
p = cd(@__DIR__) do
run(pipeline(Cmd(ARGS), stdin=devnull, stdout=out, stderr=err), wait=false)
end
close(out.in)
close(err.in)
out_task = @async readlines(out)
Expand Down
6 changes: 6 additions & 0 deletions test/embedding/embedding.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ int main()
checked_eval_string("LocalModule.myapp()");
}

{
// Main.include and Main.eval exist (#28825)
checked_eval_string("include(\"include_and_eval.jl\")");
checked_eval_string("f28825()");
}

int ret = 0;
jl_atexit_hook(ret);
return ret;
Expand Down
3 changes: 3 additions & 0 deletions test/embedding/include_and_eval.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f28825()
eval(:(1+1))
end

0 comments on commit 2bf916b

Please sign in to comment.