diff --git a/Project.toml b/Project.toml index e37f192..455976d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "RuntimeGeneratedFunctions" uuid = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" authors = ["Chris Rackauckas and contributors"] -version = "0.4.1" +version = "0.4.0" [deps] ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" diff --git a/src/RuntimeGeneratedFunctions.jl b/src/RuntimeGeneratedFunctions.jl index ced0a6a..8f2d4de 100644 --- a/src/RuntimeGeneratedFunctions.jl +++ b/src/RuntimeGeneratedFunctions.jl @@ -10,26 +10,17 @@ export @RuntimeGeneratedFunction This type should be constructed via the macro @RuntimeGeneratedFunction. """ -struct RuntimeGeneratedFunction{moduletag,id,argnames} +struct RuntimeGeneratedFunction{argnames,moduletag,id} <: Function body::Expr function RuntimeGeneratedFunction(moduletag, ex) def = splitdef(ex) args, body = normalize_args(def[:args]), def[:body] - id = expr2bytes(body) + id = expr_to_id(body) cached_body = _cache_body(moduletag, id, body) - new{moduletag,id,Tuple(args)}(cached_body) + new{Tuple(args),moduletag,id}(cached_body) end end -function Base.show(io::IO, ::Type{<:RuntimeGeneratedFunction{mod,id,arg}}) where {mod,id,arg} - print(io, "RuntimeGeneratedFunction{$arg}") -end - -# don't override typeof -function Base.show(io::IO, ::MIME"text/plain", ::Type{<:RuntimeGeneratedFunction{mod,id,arg}}) where {mod,id,arg} - print(io, "RuntimeGeneratedFunction{$mod, $id, $arg}") -end - """ @RuntimeGeneratedFunction(function_expression) @@ -68,7 +59,7 @@ macro RuntimeGeneratedFunction(ex) end end -function Base.show(io::IO, f::RuntimeGeneratedFunction{moduletag, id, argnames}) where {moduletag,id,argnames} +function Base.show(io::IO, f::RuntimeGeneratedFunction{argnames, moduletag, id}) where {argnames,moduletag,id} mod = parentmodule(moduletag) func_expr = Expr(:->, Expr(:tuple, argnames...), f.body) print(io, "RuntimeGeneratedFunction(#=in $mod=#, ", repr(func_expr), ")") @@ -80,7 +71,7 @@ end # @RuntimeGeneratedFunction function generated_callfunc end -function generated_callfunc_body(moduletag, id, argnames, __args) +function generated_callfunc_body(argnames, moduletag, id, __args) setup = (:($(argnames[i]) = @inbounds __args[$i]) for i in 1:length(argnames)) body = _lookup_body(moduletag, id) @assert body !== nothing @@ -110,7 +101,7 @@ end # @generated function. _cache_lock = Threads.SpinLock() _cachename = Symbol("#_RuntimeGeneratedFunctions_cache") -_tagname = Symbol("#_RuntimeGeneratedFunctions_ModTag") +_tagname = Symbol("#_RGF_ModTag") function _cache_body(moduletag, id, body) lock(_cache_lock) do @@ -168,8 +159,8 @@ function init(mod) # or so. See: # https://github.com/JuliaLang/julia/pull/32902 # https://github.com/NHDaly/StagedFunctions.jl/blob/master/src/StagedFunctions.jl#L30 - @inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{$_tagname, id, argnames}, __args...) where {id,argnames} - $RuntimeGeneratedFunctions.generated_callfunc_body($_tagname, id, argnames, __args) + @inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{argnames, $_tagname, id}, __args...) where {argnames,id} + $RuntimeGeneratedFunctions.generated_callfunc_body(argnames, $_tagname, id, __args) end end) end @@ -186,10 +177,10 @@ function normalize_args(arg::Expr) arg.args[1] end -function expr2bytes(ex) +function expr_to_id(ex) io = IOBuffer() Serialization.serialize(io, ex) - return Tuple(sha512(take!(io))) + return Tuple(reinterpret(UInt32, sha1(take!(io)))) end end diff --git a/test/runtests.jl b/test/runtests.jl index 3e24f90..9f73caa 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,6 +31,8 @@ f1 = @RuntimeGeneratedFunction(ex1) f2 = @RuntimeGeneratedFunction(ex2) f3 = @RuntimeGeneratedFunction(ex3) +@test f1 isa Function + du = rand(2) u = rand(2) p = nothing