From 15ac71f2e1b724ae7e8cf79dc0785f69068fcce1 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Thu, 4 Jan 2024 18:37:29 +0100 Subject: [PATCH] simpler fake plutorunner --- .../PlutoReactiveCore/ExpressionExplorer.jl | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/analysis/PlutoReactiveCore/ExpressionExplorer.jl b/src/analysis/PlutoReactiveCore/ExpressionExplorer.jl index fad708f3e7..821bf0f2a1 100644 --- a/src/analysis/PlutoReactiveCore/ExpressionExplorer.jl +++ b/src/analysis/PlutoReactiveCore/ExpressionExplorer.jl @@ -7,6 +7,7 @@ import ..PlutoReactiveCore using ExpressionExplorer using ExpressionExplorer: ScopeState +module Fake # this one is fake module PlutoRunner @@ -14,32 +15,21 @@ module PlutoRunner using Markdown using InteractiveUtils -# this is fake -load_integrations_if_needed() = nothing -# dont look at this -const initial_value_getter_ref = Ref{Function}(element -> missing) -struct GiveMeCellID end - # this one is fake macro bind(def, element) - if def isa Symbol - quote - # fake fake super fake alert - $(load_integrations_if_needed)() - local el = $(esc(element)) - global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : $(initial_value_getter_ref)[](el) - PlutoRunner.create_bond(el, $(Meta.quot(def)), $(GiveMeCellID())) - end - else - # not real - :(throw(ArgumentError("""\nMacro example usage: \n\n\t@bind my_number html""\n\n"""))) - end + quote + global $(esc(def)) = element + end end # fake end import .PlutoRunner +end + +import .Fake + """ ExpressionExplorer does not explore inside macro calls, i.e. the arguments of a macrocall (like `a+b` in `@time a+b`) are ignored. @@ -121,6 +111,20 @@ end const can_macroexpand_no_bind = Set(Symbol.(["@md_str", "Markdown.@md_str", "@gensym", "Base.@gensym", "@enum", "Base.@enum", "@assert", "Base.@assert", "@cmd"])) const can_macroexpand = can_macroexpand_no_bind ∪ Set(Symbol.(["@bind", "PlutoRunner.@bind"])) +const found_plutorunner = Ref{Union{Nothing,Module}}(nothing) +function get_plutorunner() + fpr = found_plutorunner[] + if fpr === nothing + if isdefined(Main, :PlutoRunner) && Main.PlutoRunner isa Module + found_plutorunner[] = Main.PlutoRunner + else + Fake.PlutoRunner # (the fake one) + end + else + fpr + end +end + """ If the macro is **known to Pluto**, expand or 'mock expand' it, if not, return the expression. Macros from external packages are not expanded, this is done later in the pipeline. See https://github.com/fonsp/Pluto.jl/pull/1032 """ @@ -129,7 +133,7 @@ function maybe_macroexpand_pluto(ex::Expr; recursive::Bool=false, expand_bind::B funcname = ExpressionExplorer.split_funcname(ex.args[1]) if funcname.joined ∈ (expand_bind ? can_macroexpand : can_macroexpand_no_bind) - macroexpand(PlutoRunner, ex; recursive=false)::Expr + macroexpand(get_plutorunner(), ex; recursive=false)::Expr else ex end