From 5ab47d1e9a3745fd73145cd59756b89589a7533c Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Wed, 29 Nov 2017 21:32:33 -0800 Subject: [PATCH] Add function-first finalizers (#416) --- README.md | 4 ++++ src/Compat.jl | 8 ++++++++ test/runtests.jl | 12 +++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bbaf586dc..ebec0a541 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,9 @@ Currently, the `@compat` macro supports the following syntaxes: * `Uninitialized` and `uninitialized` with corresponding `Array` constructors ([#24652]). +* `finalizer` accepts the finalizer to run as the first argument and the object to be finalized + as the second ([#24605]) + ## Renaming @@ -363,5 +366,6 @@ includes this fix. Find the minimum version from there. [#24282]: https://github.com/JuliaLang/julia/issues/24282 [#24372]: https://github.com/JuliaLang/julia/issues/24372 [#24459]: https://github.com/JuliaLang/julia/issues/24459 +[#24605]: https://github.com/JuliaLang/julia/issues/24605 [#24652]: https://github.com/JuliaLang/julia/issues/24652 [#24657]: https://github.com/JuliaLang/julia/issues/24657 diff --git a/src/Compat.jl b/src/Compat.jl index 477f635ac..84e58090f 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -818,6 +818,14 @@ end export Uninitialized, uninitialized end +if VERSION < v"0.7.0-DEV.2562" + import Base: finalizer + finalizer(f::Function, o) = finalizer(o, f) + finalizer(f::Ptr{Void}, o) = finalizer(o, f) + finalizer(f::Ptr{Void}, o::Ptr{Void}) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o) + finalizer(f::Ptr{Void}, o::Function) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o) +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index ff2b667a0..62ace1c97 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -900,7 +900,6 @@ let a = [1 0 0; 0 1 0; 0 0 1] @test SparseMatrixCSC{Complex128,Int8}(I, 3, 2)::SparseMatrixCSC{Complex128,Int8} == a[:,1:2] end - # 0.7.0-DEV.2581 @test isa(Vector(uninitialized, 2), Vector{Any}) @test isa(Vector{Float64}(uninitialized, 2), Vector{Float64}) @@ -909,6 +908,17 @@ end @test isa(Array{Float64}(uninitialized, 2, 2), Matrix{Float64}) @test isa(Array{Float64,3}(uninitialized, 2, 2, 2), Array{Float64,3}) +# 0.7 +let A = [1] + local x = 0 + finalizer(a->(x+=1), A) + finalize(A) + @test x == 1 + A = 0 + gc(); gc() + @test x == 1 +end + if VERSION < v"0.6.0" include("deprecated.jl") end