Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Main.eval and Main.include exist in embedded julia #32062

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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\")");
c42f marked this conversation as resolved.
Show resolved Hide resolved
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