Skip to content

Commit

Permalink
Fix print_script for 1.10 (#36)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
j-fu authored Oct 2, 2023
1 parent 2bb4650 commit fdc6f52
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,28 @@ 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})
return :(show(io, MIME"text/javascript"(), value))
end
throw("$(value) is not showable as text/javascript")
end
end
print_script(io::IO, ::Nothing) =
print(io, "undefined")
print_script(io::IO, ::Missing) =
print(io, "null")
print_script(io::IO, value::Union{Bool, Symbol}) =
print(io, value)


function print_script(io::IO, value::Union{NamedTuple, AbstractDict})
print(io, '{')
first = true
Expand Down

1 comment on commit fdc6f52

@j-fu
Copy link
Contributor Author

@j-fu j-fu commented on fdc6f52 Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/make.jl seems to need a fix for Documenter 1.0. A quick fix would be to limit the documenter version, see #37

Please sign in to comment.