From 97c28d6287b086c5ce0b386a7859c81641ede9a1 Mon Sep 17 00:00:00 2001 From: willtebbutt Date: Sun, 20 Oct 2024 10:32:03 +0100 Subject: [PATCH] Enable env vars --- Project.toml | 2 +- src/MistyClosures.jl | 4 +++- test/runtests.jl | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 84a1093..f7f96cc 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/MistyClosures.jl b/src/MistyClosures.jl index 3b15191..162e5eb 100644 --- a/src/MistyClosures.jl +++ b/src/MistyClosures.jl @@ -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...) diff --git a/test/runtests.jl b/test/runtests.jl index 3fc5c0f..304e0e1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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] @@ -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)