Skip to content

Commit

Permalink
Handle :cfunction exprs. Fixes #105
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Mar 6, 2019
1 parent 22c954c commit fd01f86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/interpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ Evaluate a `:foreigncall` (from a `ccall`) statement `callexpr` in the context o
`stack` and `pc` are unused, but supplied for consistency with [`evaluate_call!`](@ref).
"""
function evaluate_foreigncall!(stack, frame::JuliaStackFrame, call_expr::Expr, pc)
args = collect_args(frame, call_expr; isfc=true)
head = call_expr.head
args = collect_args(frame, call_expr; isfc = head==:foreigncall)
for i = 2:length(args)
arg = args[i]
args[i] = isa(arg, Symbol) ? QuoteNode(arg) : arg
end
head == :cfunction && (args[2] = QuoteNode(args[2]))
scope = frame.code.scope
if !isempty(frame.sparams) && scope isa Method
sig = scope.sig
Expand All @@ -158,7 +160,7 @@ function evaluate_foreigncall!(stack, frame::JuliaStackFrame, call_expr::Expr, p
instantiate_type_in_env(arg, sig, frame.sparams)
end...)
end
return Core.eval(moduleof(frame), Expr(:foreigncall, args...))
return Core.eval(moduleof(frame), Expr(head, args...))
end

function evaluate_call!(::Compiled, frame::JuliaStackFrame, call_expr::Expr, pc; #=unused=# exec!::Function=finish_and_return!)
Expand Down Expand Up @@ -318,7 +320,7 @@ function eval_rhs(stack, frame, node::Expr, pc)
return check_isdefined(frame, node.args[1])
elseif head == :call
return evaluate_call!(stack, frame, node, pc)
elseif head == :foreigncall
elseif head == :foreigncall || head == :cfunction
return evaluate_foreigncall!(stack, frame, node, pc)
elseif head == :copyast
val = (node.args[1]::QuoteNode).value
Expand Down
5 changes: 5 additions & 0 deletions test/interpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ ex = quote
end
frame = JuliaInterpreter.prepare_thunk(Main, ex)
@test JuliaInterpreter.finish_and_return!(JuliaStackFrame[], frame, true) == 1
function cfcfun()
cf = @cfunction(fcfun, Int, (Int, Int))
ccall(cf, Int, (Int, Int), 1, 2)
end
@test @interpret(cfcfun()) == 1

# From Julia's test/ambiguous.jl. This tests whether we renumber :enter statements correctly.
ambig(x, y) = 1
Expand Down

0 comments on commit fd01f86

Please sign in to comment.