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

Reunify code for Base.include and MainInclude.include #35348

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 2 additions & 25 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,32 +363,9 @@ end
for m in methods(include)
delete_method(m)
end
# These functions are duplicated in client.jl/include(::String) for
# nicer stacktraces. Modifications here have to be backported there
include(mod::Module, _path::AbstractString) = include(identity, mod, _path)
function include(mapexpr::Function, mod::Module, _path::AbstractString)
path, prev = _include_dependency(mod, _path)
for callback in include_callbacks # to preserve order, must come before Core.include
invokelatest(callback, mod, path)
end
tls = task_local_storage()
tls[:SOURCE_PATH] = path
local result
try
# result = Core.include(mod, path)
if mapexpr === identity
result = ccall(:jl_load, Any, (Any, Cstring), mod, path)
else
result = ccall(:jl_load_rewrite, Any, (Any, Cstring, Any), mod, path, mapexpr)
end
finally
if prev === nothing
delete!(tls, :SOURCE_PATH)
else
tls[:SOURCE_PATH] = prev
end
end
return result
function include(mapexpr::Function, mod::Module, path::AbstractString)
@_code_for_include(mapexpr, mod, path)
end

end_base_include = time_ns()
Expand Down
26 changes: 5 additions & 21 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,30 +424,14 @@ end
baremodule MainInclude
using ..Base
include(mapexpr::Function, fname::AbstractString) = Base.include(mapexpr, Main, fname)
# We inline the definition of include from loading.jl/include_relative to get one-frame stacktraces
# for the common case of include(fname). Otherwise we would use:
# include(fname::AbstractString) = Base.include(Main, fname)

# We redefine include here rather than calling Base.include to get one-frame
# stacktraces for the common case of include(fname).
function include(fname::AbstractString)
mod = Main
isa(fname, String) || (fname = Base.convert(String, fname)::String)
path, prev = Base._include_dependency(mod, fname)
for callback in Base.include_callbacks # to preserve order, must come before Core.include
Base.invokelatest(callback, mod, path)
end
tls = Base.task_local_storage()
tls[:SOURCE_PATH] = path
local result
try
result = ccall(:jl_load, Any, (Any, Cstring), mod, path)
finally
if prev === nothing
Base.delete!(tls, :SOURCE_PATH)
else
tls[:SOURCE_PATH] = prev
end
end
return result
Base.@_code_for_include(identity, Main, fname)
end

eval(x) = Core.eval(Main, x)
end

Expand Down
36 changes: 36 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,42 @@ actually evaluates `mapexpr(expr)`. If it is omitted, `mapexpr` defaults to [`i
"""
Base.include # defined in sysimg.jl

# The include() definition needs to happen at a particular part of bootstrap
# (see Base.jl) so the code is only defined as a macro here.
#
# Also, the definition of include() is duplicated in Main for nicer stacktraces
# (see client.jl) and this macro ensures both definitions are the same.
macro _code_for_include(mapexpr, mod, _path)
quote
mapexpr = $(esc(mapexpr))
mod = $(esc(mod))
_path = $(esc(_path))

path, prev = _include_dependency(mod, _path)
for callback in include_callbacks # to preserve order, must come before Core.include
invokelatest(callback, mod, path)
end
tls = task_local_storage()
tls[:SOURCE_PATH] = path
local result
try
# result = Core.include(mod, path)
if mapexpr === identity
result = ccall(:jl_load, Any, (Any, Cstring), mod, path)
else
result = ccall(:jl_load_rewrite, Any, (Any, Cstring, Any), mod, path, mapexpr)
end
finally
if prev === nothing
delete!(tls, :SOURCE_PATH)
else
tls[:SOURCE_PATH] = prev
end
end
return result
end
end

"""
evalfile(path::AbstractString, args::Vector{String}=String[])

Expand Down