Skip to content

Commit

Permalink
Move more of _include implementation to julia
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Apr 15, 2020
1 parent 1e42060 commit f0f535f
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1081,10 +1081,37 @@ The optional first argument `mapexpr` can be used to transform the included code
it is evaluated: for each parsed expression `expr` in `code`, the `include_string` function
actually evaluates `mapexpr(expr)`. If it is omitted, `mapexpr` defaults to [`identity`](@ref).
"""
function include_string(mapexpr::Function, m::Module, txt_::AbstractString, fname::AbstractString="string")
txt = String(txt_)
ccall(:jl_parse_eval_all, Any, (Any, Any, Any, Any),
m, txt, String(fname), mapexpr === identity ? nothing : mapexpr)
function include_string(mapexpr::Function, mod::Module, code::AbstractString,
filename::AbstractString="string")
loc = LineNumberNode(1, Symbol(filename))
try
ast = Meta.parseall(code, filename=filename)
@assert Meta.isexpr(ast, :toplevel)
result = nothing
line_and_ex = Expr(:toplevel, loc, nothing)
for ex in ast.args
if ex isa LineNumberNode
loc = ex
line_and_ex.args[1] = ex
continue
end
ex = mapexpr(ex)
# Wrap things to be eval'd in a :toplevel expr to carry line
# information as part of the expr.
line_and_ex.args[2] = ex
# Macro expand, lower, and evaluate successive top-level statements
# in the latest world.
#
# TODO: Separate macro expansion and lowering steps from the final
# evaluation step, and make these throw their own error types which
# carry the full source location.
result = invokelatest(Core.eval, mod, line_and_ex)
end
return result
catch exc
# TODO: Remove LoadError!
throw(LoadError(String(loc.file), loc.line, exc))
end
end

include_string(m::Module, txt::AbstractString, fname::AbstractString="string") =
Expand Down Expand Up @@ -1121,26 +1148,23 @@ actually evaluates `mapexpr(expr)`. If it is omitted, `mapexpr` defaults to [`i
Base.include # defined in Base.jl

# Full include() implementation which is used after bootstrap
# Hidden for nicer backtraces in include()
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
code = read(path, String)
tls = task_local_storage()
tls[:SOURCE_PATH] = path
local result
try
result = ccall(:jl_load_rewrite, Any, (Any, Any, Any), mod, path,
mapexpr === identity ? nothing : mapexpr)
include_string(mapexpr, mod, code, path)
finally
if prev === nothing
delete!(tls, :SOURCE_PATH)
else
tls[:SOURCE_PATH] = prev
end
end
return result
end

"""
Expand Down

0 comments on commit f0f535f

Please sign in to comment.