Skip to content

Commit

Permalink
match EE updates (which are not yet released so CI will fail)
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Nov 13, 2023
1 parent 4870b8b commit d908138
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/analysis/Errors.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Base: showerror
import .ExpressionExplorer: FunctionName, join_funcname_parts
import .ExpressionExplorer: FunctionName

abstract type ReactivityError <: Exception end

Expand Down
43 changes: 40 additions & 3 deletions src/analysis/ExpressionExplorer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function ExpressionExplorer.explore_macrocall!(ex::Expr, scopestate::ScopeState{
end

# Some macros can be expanded on the server process
if ExpressionExplorer.join_funcname_parts(macro_name) can_macroexpand
if macro_name.joined can_macroexpand
new_ex = maybe_macroexpand_pluto(ex)
union!(symstate, ExpressionExplorer.explore!(new_ex, scopestate))
end
Expand Down Expand Up @@ -78,9 +78,8 @@ If the macro is **known to Pluto**, expand or 'mock expand' it, if not, return t
function maybe_macroexpand_pluto(ex::Expr; recursive::Bool=false, expand_bind::Bool=true)
result::Expr = if ex.head === :macrocall
funcname = ExpressionExplorer.split_funcname(ex.args[1])
funcname_joined = ExpressionExplorer.join_funcname_parts(funcname)

if funcname_joined (expand_bind ? can_macroexpand : can_macroexpand_no_bind)
if funcname.joined (expand_bind ? can_macroexpand : can_macroexpand_no_bind)
macroexpand(PlutoRunner, ex; recursive=false)::Expr
else
ex
Expand Down Expand Up @@ -133,4 +132,42 @@ function transform_dot_notation(ex::Expr)
end



###############


"""
```julia
can_be_function_wrapped(ex)::Bool
```
Is this code simple enough that we can wrap it inside a function, and run the function in global scope instead of running the code directly? Look for `Pluto.PlutoRunner.Computer` to learn more.
"""
function can_be_function_wrapped(x::Expr)
if x.head === :global || # better safe than sorry
x.head === :using ||
x.head === :import ||
x.head === :export ||
x.head === :public || # Julia 1.11
x.head === :module ||
x.head === :incomplete ||
# Only bail on named functions, but anonymous functions (args[1].head == :tuple) are fine.
# TODO Named functions INSIDE other functions should be fine too
(x.head === :function && !Meta.isexpr(x.args[1], :tuple)) ||
x.head === :macro ||
# Cells containing macrocalls will actually be function wrapped using the expanded version of the expression
# See https://github.com/fonsp/Pluto.jl/pull/1597
x.head === :macrocall ||
x.head === :struct ||
x.head === :abstract ||
(x.head === :(=) && ExpressionExplorer.is_function_assignment(x)) || # f(x) = ...
(x.head === :call && (x.args[1] === :eval || x.args[1] === :include))
false
else
all(can_be_function_wrapped, x.args)
end
end

can_be_function_wrapped(x::Any) = true

end
2 changes: 1 addition & 1 deletion src/analysis/Topology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ExprAnalysisCache(notebook, cell::Cell) = let
code=cell.code,
parsedcode=parsedcode,
module_usings_imports=ExpressionExplorer.compute_usings_imports(parsedcode),
function_wrapped=ExpressionExplorer.can_be_function_wrapped(parsedcode),
function_wrapped=ExpressionExplorerExtras.can_be_function_wrapped(parsedcode),
)
end

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/TopologyUpdate.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import .ExpressionExplorer
import .ExpressionExplorerExtras
import .ExpressionExplorer: join_funcname_parts, SymbolsState, FunctionNameSignaturePair
import .ExpressionExplorer: SymbolsState, FunctionNameSignaturePair

"Return a copy of `old_topology`, but with recomputed results from `cells` taken into account."
function updated_topology(old_topology::NotebookTopology, notebook::Notebook, cells)
Expand Down
2 changes: 1 addition & 1 deletion src/evaluation/MacroAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function resolve_topology(
if result isa Success
(expr, computer_id) = result.result
expanded_node = ExpressionExplorer.compute_reactive_node(expr; configuration=ExpressionExplorerExtras.PlutoConfiguration())
function_wrapped = ExpressionExplorer.can_be_function_wrapped(expr)
function_wrapped = ExpressionExplorerExtras.can_be_function_wrapped(expr)
Success((expanded_node, function_wrapped, computer_id))
else
result
Expand Down
3 changes: 2 additions & 1 deletion src/evaluation/Run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ function run_reactive_core!(
)...)

if will_run_code(notebook)
deletion_hook((session, notebook), old_workspace_name, nothing, to_delete_vars, to_delete_funcs, to_reimport, cells_to_macro_invalidate; to_run) # `deletion_hook` defaults to `WorkspaceManager.move_vars`
to_delete_funcs_simple = Set{Tuple{Vararg{Symbol}}}((id, name.parts) for (id,name) in to_delete_funcs)
deletion_hook((session, notebook), old_workspace_name, nothing, to_delete_vars, to_delete_funcs_simple, to_reimport, cells_to_macro_invalidate; to_run) # `deletion_hook` defaults to `WorkspaceManager.move_vars`
end

foreach(v -> delete!(notebook.bonds, v), to_delete_vars)
Expand Down
2 changes: 1 addition & 1 deletion src/evaluation/RunBonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function set_bond_values_reactive(;
bond_value_pairs = zip(syms_to_set, new_values)

syms_to_set_set = Set{Symbol}(syms_to_set)
function custom_deletion_hook((session, notebook)::Tuple{ServerSession,Notebook}, old_workspace_name, new_workspace_name, to_delete_vars::Set{Symbol}, methods_to_delete::Set{Tuple{UUID,FunctionName}}, to_reimport::Set{Expr}, invalidated_cell_uuids::Set{UUID}; to_run::AbstractVector{Cell})
function custom_deletion_hook((session, notebook)::Tuple{ServerSession,Notebook}, old_workspace_name, new_workspace_name, to_delete_vars::Set{Symbol}, methods_to_delete, to_reimport, invalidated_cell_uuids; to_run)
to_delete_vars = union(to_delete_vars, syms_to_set_set) # also delete the bound symbols
WorkspaceManager.move_vars(
(session, notebook),
Expand Down
4 changes: 2 additions & 2 deletions src/evaluation/WorkspaceManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ function move_vars(
old_workspace_name::Symbol,
new_workspace_name::Union{Nothing,Symbol},
to_delete::Set{Symbol},
methods_to_delete::Set{Tuple{UUID,FunctionName}},
methods_to_delete::Set{Tuple{UUID,Vararg{Symbol}}},
module_imports_to_move::Set{Expr},
invalidated_cell_uuids::Set{UUID},
keep_registered::Set{Symbol}=Set{Symbol}();
Expand All @@ -574,7 +574,7 @@ function move_vars(
end)
end

function move_vars(session_notebook::Union{SN,Workspace}, to_delete::Set{Symbol}, methods_to_delete::Set{Tuple{UUID,FunctionName}}, module_imports_to_move::Set{Expr}, invalidated_cell_uuids::Set{UUID}; kwargs...)
function move_vars(session_notebook::Union{SN,Workspace}, to_delete::Set{Symbol}, methods_to_delete::Set{Tuple{UUID,Vararg{Symbol}}}, module_imports_to_move::Set{Expr}, invalidated_cell_uuids::Set{UUID}; kwargs...)
move_vars(session_notebook, bump_workspace_module(session_notebook)..., to_delete, methods_to_delete, module_imports_to_move, invalidated_cell_uuids; kwargs...)
end

Expand Down
2 changes: 1 addition & 1 deletion src/runner/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ function move_vars(
old_workspace_name::Symbol,
new_workspace_name::Symbol,
vars_to_delete::Set{Symbol},
methods_to_delete::Set{Tuple{UUID,Vector{Symbol}}},
methods_to_delete::Set{Tuple{UUID,Tuple{Vararg{Symbol}}}},
module_imports_to_move::Set{Expr},
invalidated_cell_uuids::Set{UUID},
keep_registered::Set{Symbol},
Expand Down
40 changes: 40 additions & 0 deletions test/MoreAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,44 @@ using Test

@test transform(connections) == transform(wanted_connections)
end



@testset "can_be_function_wrapped" begin

c = ExpressionExplorerExtras.can_be_function_wrapped


@test c(quote
a = b + C
if d
for i = 1:10
while Y
end
end
end
end)


@test c(quote
map(1:10) do i
i + 1
end
end)


@test !c(quote
function x(x)
X
end
end)

@test !c(quote
if false
using Asdf
end
end)


end
end

0 comments on commit d908138

Please sign in to comment.