Skip to content

Commit 4181acd

Browse files
committed
Tests + RuntimeGeneratedFunctions.init(user_mod)
The syntax RuntimeGeneratedFunctions.init(user_mod) is less cute but more general way to name the initialization function. (The need to initialize data structures in user modules isn't entirely unique to RuntimeGeneratedFunctions.)
1 parent 8a499fb commit 4181acd

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/RuntimeGeneratedFunctions.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ then calling the resulting function. The differences are:
3232
* The result is not a named generic function, and doesn't participate in
3333
generic function dispatch; it's more like a callable method.
3434
35-
You need to use the special form `@RuntimeGeneratedFunction __init__` a single time
36-
at the top level of your module before any other uses of the macro.
35+
You need to use `RuntimeGeneratedFunctions.init(your_module)` a single time at
36+
the top level of `your_module` before any other uses of the macro.
3737
3838
# Examples
3939
```
40-
@RuntimeGeneratedFunction __init__ # Required once per module
40+
RuntimeGeneratedFunctions.init(@__MODULE__) # Required at module top-level
4141
4242
function foo()
4343
expression = :((x,y)->x+y+1) # May be generated dynamically
@@ -47,19 +47,15 @@ end
4747
```
4848
"""
4949
macro RuntimeGeneratedFunction(ex)
50-
if ex === :__init__
51-
_init_cache!(__module__)
52-
else
53-
quote
54-
if !($(esc(:(@isdefined($_tagname)))))
55-
error("""You must use `@RuntimeGeneratedFunction __init__` at module
56-
top level before using runtime generated functions""")
57-
end
58-
RuntimeGeneratedFunction(
59-
$(esc(_tagname)),
60-
$(esc(ex))
61-
)
50+
quote
51+
if !($(esc(:(@isdefined($_tagname)))))
52+
error("""You must use `RuntimeGeneratedFunctions.init(@__MODULE__)` at module
53+
top level before using runtime generated functions""")
6254
end
55+
RuntimeGeneratedFunction(
56+
$(esc(_tagname)),
57+
$(esc(ex))
58+
)
6359
end
6460
end
6561

@@ -138,7 +134,13 @@ function _lookup_body(moduletag, id)
138134
end
139135
end
140136

141-
function _init_cache!(mod)
137+
"""
138+
RuntimeGeneratedFunctions.init(mod)
139+
140+
Use this at top level to set up your module `mod` before using
141+
`@RuntimeGeneratedFunction`.
142+
"""
143+
function init(mod)
142144
lock(_cache_lock) do
143145
if !isdefined(mod, _cachename)
144146
mod.eval(quote

test/precomp/RGFPrecompTest.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module RGFPrecompTest
22
using RuntimeGeneratedFunctions
3+
RuntimeGeneratedFunctions.init(@__MODULE__)
34

45
f = @RuntimeGeneratedFunction(:((x,y)->x+y))
56
end

test/runtests.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using RuntimeGeneratedFunctions, BenchmarkTools
22
using Test
33

4+
RuntimeGeneratedFunctions.init(@__MODULE__)
5+
46
function f(_du,_u,_p,_t)
57
@inbounds _du[1] = _u[1]
68
@inbounds _du[2] = _u[2]
@@ -111,12 +113,12 @@ end
111113
# Test that globals are resolved within the correct scope
112114

113115
module GlobalsTest
116+
using RuntimeGeneratedFunctions
117+
RuntimeGeneratedFunctions.init(@__MODULE__)
114118

115-
using RuntimeGeneratedFunctions
116-
117-
@RuntimeGeneratedFunction __init__
118-
y = 10
119-
f = @RuntimeGeneratedFunction(:(x->x+y))
120-
119+
y = 40
120+
f = @RuntimeGeneratedFunction(:(x->x+y))
121121
end
122122

123+
@test GlobalsTest.f(2) == 42
124+

0 commit comments

Comments
 (0)