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

Enable Captures #8

Merged
merged 1 commit into from
Oct 20, 2024
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 Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MistyClosures"
uuid = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"
authors = ["Will Tebbutt", "Frames White", "Hong Ge"]
version = "1.0.2"
version = "1.0.3"

[compat]
julia = "1.10"
Expand Down
4 changes: 3 additions & 1 deletion src/MistyClosures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ struct MistyClosure{Toc<:OpaqueClosure}
ir::IRCode
end

MistyClosure(ir::IRCode; kwargs...) = MistyClosure(OpaqueClosure(ir; kwargs...), ir)
function MistyClosure(ir::IRCode, env...; kwargs...)
return MistyClosure(OpaqueClosure(ir, env...; kwargs...), ir)
end

(mc::MistyClosure)(x::Vararg{Any, N}) where {N} = mc.oc(x...)

Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ using MistyClosures, Test

using Core: OpaqueClosure

struct Foo
x::Float64
end

(f::Foo)(y) = f.x * y

@testset "MistyClosures.jl" begin
ir = Base.code_ircode_by_type(Tuple{typeof(sin), Float64})[1][1]

Expand All @@ -13,6 +19,15 @@ using Core: OpaqueClosure
mc_default = MistyClosure(OpaqueClosure(ir; do_compile=true), ir)
@test @inferred(mc_default(5.0) == sin(5.0))

# Recommended constructor with env.
ir_foo = Base.code_ircode_by_type(Tuple{Foo, Float64})[1][1]
mc_with_env = MistyClosure(ir_foo, 5.0; do_compile=true)
@test @inferred(mc_with_env(4.0)) == Foo(5.0)(4.0)

# Default constructor with env.
mc_env_default = MistyClosure(OpaqueClosure(ir_foo, 4.0; do_compile=true), ir_foo)
@test @inferred(mc_env_default(5.0) == Foo(5.0)(4.0))

# deepcopy
@test deepcopy(mc) isa typeof(mc)

Expand Down