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

fix @manipulate hygiene and add a test #185

Merged
merged 8 commits into from
Sep 26, 2017
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ os:
- osx
julia:
- 0.5
- 0.6
- nightly
notifications:
email: false
Expand Down
26 changes: 21 additions & 5 deletions src/manipulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,29 @@ end
function map_block(block, symbols)
lambda = Expr(:(->), Expr(:tuple, symbols...),
block)
:(preserve(map($lambda, $(map(s->:(signal($s)), symbols)...), typ=Any)))
:(preserve(map($(esc(lambda)), $(map(s->:(signal($s)), esc.(symbols))...), typ=Any)))
end

function symbols(bindings)
map(x->x.args[1], bindings)
end

"""
Work-around for the fact that the order of arguments in `let...end` parsing
was reversed from v0.6 to v0.7.
See: https://github.com/JuliaLang/julia/issues/21774
"""
@static if VERSION >= v"0.7.0-DEV.1671"
function make_let_block(declarations, statements)
Expr(:let, Expr(:block, declarations...), Expr(:block, statements...))
end
else
function make_let_block(declarations, statements)
Expr(:let, Expr(:block, statements...), declarations...)
end
end


macro manipulate(expr)
if expr.head != :for
error("@manipulate syntax is @manipulate for ",
Expand All @@ -35,8 +51,8 @@ macro manipulate(expr)
bindings = [expr.args[1]]
end
syms = symbols(bindings)
Expr(:let, Expr(:block,
display_widgets(syms)...,
esc(map_block(block, syms))),
map(make_widget, bindings)...)
declarations = map(make_widget, bindings)
statements = vcat(display_widgets(syms)...,
map_block(block, syms))
make_let_block(declarations, statements)
end
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,29 @@ using Base.Test

include("widgets.jl")
include("ijulia.jl")

module HygieneTest
# Test that the `@manipulate` macro does not rely on
# any symbols from other packages (like Reactive or Interact)
# being defined in the user's current namespace
using Interact: @manipulate

@manipulate for i in 1:10, j in ["x", "y", "z"]
i, j
end
end

module HygieneTest2
# Verify that `@manipulate` is really using Reactive.signal
# and Reactive.preserve, not whatever the user has defined

using Interact: @manipulate

# Dummy implementations that should never be called:
signal(x...) = error("I should not be called")
preserve(x...) = error("I should not be called")

@manipulate for i in 1:10, j in ["x", "y", "z"]
2 * i, j * " hello"
end
end