Skip to content
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
4 changes: 4 additions & 0 deletions src/hooks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ function core_parser_hook(code, filename::String, lineno::Int, offset::Int, opti
# The C entry points will pass us this form.
(ptr,len) = code
code = String(unsafe_wrap(Array, ptr, len))
elseif !(code isa String || code isa SubString || code isa Vector{UInt8})
# For non-Base string types, convert to UTF-8 encoding, using an
# invokelatest to avoid world age issues.
code = Base.invokelatest(String, code)
end
if !isnothing(_debug_log[])
print(_debug_log[], """
Expand Down
13 changes: 13 additions & 0 deletions test/hooks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@
@test_throws JuliaSyntax.ParseError Meta.parse("[x)")
end

# Check custom string types defined in a world age later than
# enable_in_core!() can be passed to Meta.parse()
mystr = @eval begin
struct MyString <: AbstractString
x::String
end
Base.String(s::MyString) = s.x
Base.ncodeunits(s::MyString) = ncodeunits(s.x)

MyString("hi")
end
@test Meta.parse(mystr) == :hi

JuliaSyntax.enable_in_core!(false)
end

Expand Down