From fdc6f52b1057bd625068884699d0a54c1387696b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Mon, 2 Oct 2023 09:37:18 +0200 Subject: [PATCH] Fix print_script for 1.10 (#36) * Fix print_script for 1.10 Appearantly, at the time when @generated is evaluated, methods from other packages may not have been compiled yet, so `hasmethod(show, Tuple{IO, MIME{Symbol("text/javascript")}, value})` returns false, and the @generated function print_script throws an error. As the old version worked in 1.9.3, this appears to be the result of subtle changes in precompilation/initialization behavior somewhere between 1.9.3 and 1.10.0-beta2. * Add version discriminator for print_script(::IO, ::MIME,::Any) Keep the old behavior (via @generated) for versions <=1.9.x. --- src/script.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/script.jl b/src/script.jl index e9cd280..e47b75f 100644 --- a/src/script.jl +++ b/src/script.jl @@ -95,6 +95,12 @@ The fallback behavior of `print_script` is to show the object as `"text/javascript"`. The `Javascript` wrapper will take any string and let it be printed in this way. """ +@static if VERSION > v"1.9.99" + # With the new JuliaSyntax parser, `hasmethod` appears to be invoked before + # other packages are loaded. + print_script(io::IO, value::Any) = + show(io, MIME"text/javascript"(), value) +else @generated function print_script(io::IO, value) # workaround for Julia#18221 if hasmethod(show, Tuple{IO, MIME{Symbol("text/javascript")}, value}) @@ -102,6 +108,7 @@ and let it be printed in this way. end throw("$(value) is not showable as text/javascript") end +end print_script(io::IO, ::Nothing) = print(io, "undefined") print_script(io::IO, ::Missing) = @@ -109,6 +116,7 @@ print_script(io::IO, ::Missing) = print_script(io::IO, value::Union{Bool, Symbol}) = print(io, value) + function print_script(io::IO, value::Union{NamedTuple, AbstractDict}) print(io, '{') first = true