Skip to content

Commit

Permalink
Ensure Main.eval and Main.include exist in embedded julia (#32062)
Browse files Browse the repository at this point in the history
Import these two functions into Main as part of `jl_init()`
so that they're present in embedded julia.
  • Loading branch information
c42f committed May 21, 2019
1 parent a43c46b commit 633ad82
Show file tree
Hide file tree
Showing 5 changed files with 20 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 @@ -794,6 +794,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
6 changes: 5 additions & 1 deletion test/embedding/embedding-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ if Sys.iswindows()
end

@test length(ARGS) == 1

@testset "embedding example" begin
out = Pipe()
err = Pipe()
p = run(pipeline(Cmd(ARGS), stdin=devnull, stdout=out, stderr=err), wait=false)
embedded_cmd_path = abspath(ARGS[1])
p = cd(@__DIR__) do
run(pipeline(Cmd([embedded_cmd_path]), 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 633ad82

Please sign in to comment.