Skip to content

Commit

Permalink
NFC: harden some internal ccalls (#595)
Browse files Browse the repository at this point in the history
* NFC: harden some internal ccalls

* jl_method_def lies about only accepting `CodeInfo`
  • Loading branch information
simeonschaub authored Nov 28, 2023
1 parent ccc1c95 commit 68fa8be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/interpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ function evaluate_methoddef(frame, node)
end
length(node.args) == 1 && return f
sig = @lookup(frame, node.args[2])::SimpleVector
body = @lookup(frame, node.args[3])
body = @lookup(frame, node.args[3])::Union{CodeInfo, Expr}
# branching on https://github.com/JuliaLang/julia/pull/41137
@static if isdefined(Core.Compiler, :OverlayMethodTable)
ccall(:jl_method_def, Cvoid, (Any, Ptr{Cvoid}, Any, Any), sig, C_NULL, body, moduleof(frame))
ccall(:jl_method_def, Cvoid, (Any, Ptr{Cvoid}, Any, Any), sig, C_NULL, body, moduleof(frame)::Module)
else
ccall(:jl_method_def, Cvoid, (Any, Any, Any), sig, body, moduleof(frame))
ccall(:jl_method_def, Cvoid, (Any, Any, Any), sig, body, moduleof(frame)::Module)
end
return f
end
Expand All @@ -318,8 +318,8 @@ function structname(frame, node)
end

function set_structtype_const(mod::Module, name::Symbol)
dt = Base.unwrap_unionall(getfield(mod, name))
ccall(:jl_set_const, Cvoid, (Any, Any, Any), mod, dt.name.name, dt.name.wrapper)
dt = Base.unwrap_unionall(getfield(mod, name))::DataType
ccall(:jl_set_const, Cvoid, (Any, Any, Any), mod, dt.name.name::Symbol, dt.name.wrapper)
end

function inplace_lookup!(ex, i, frame)
Expand Down Expand Up @@ -378,12 +378,14 @@ function eval_rhs(@nospecialize(recurse), frame, node::Expr)
args = let mod=mod
Any[@lookup(mod, frame, arg) for arg in node.args]
end
T = popfirst!(args)
T = popfirst!(args)::DataType
rhs = ccall(:jl_new_structv, Any, (Any, Ptr{Any}, UInt32), T, args, length(args))
return rhs
elseif head === :splatnew # Julia 1.2+
mod = moduleof(frame)
rhs = ccall(:jl_new_structt, Any, (Any, Any), @lookup(mod, frame, node.args[1]), @lookup(mod, frame, node.args[2]))
T = @lookup(mod, frame, node.args[1])::DataType
args = @lookup(mod, frame, node.args[2])::Tuple
rhs = ccall(:jl_new_structt, Any, (Any, Any), T, args)
return rhs
elseif head === :isdefined
return check_isdefined(frame, node.args[1])
Expand Down
4 changes: 2 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ function whichtt(@nospecialize(tt))
match === nothing && return nothing
return match.method
else
m = ccall(:jl_gf_invoke_lookup, Any, (Any, UInt), tt, get_world_counter())
m = ccall(:jl_gf_invoke_lookup, Any, (Any, Csize_t), tt, get_world_counter())
m === nothing && return nothing
isa(m, Method) && return m
return m.func::Method
end
end

instantiate_type_in_env(arg, spsig, spvals) =
instantiate_type_in_env(arg, spsig::UnionAll, spvals::Vector{Any}) =
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), arg, spsig, spvals)

function sparam_syms(meth::Method)
Expand Down

0 comments on commit 68fa8be

Please sign in to comment.