Skip to content

Commit 9eb961f

Browse files
committed
Try gc preserve for ptrstring
1 parent cba1b4a commit 9eb961f

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

src/lazy.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,19 @@ function applyobject(keyvalfunc, x::LazyValues)
270270
b == UInt8('}') && return pos + 1
271271
while true
272272
# parsestring returns key as a PtrString
273-
key, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
274-
@nextbyte
275-
if b != UInt8(':')
276-
error = ExpectedColon
277-
@goto invalid
273+
GC.@preserve buf begin
274+
key, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
275+
@nextbyte
276+
if b != UInt8(':')
277+
error = ExpectedColon
278+
@goto invalid
279+
end
280+
pos += 1
281+
@nextbyte
282+
# we're now positioned at the start of the value
283+
val = _lazy(buf, pos, len, b, opts)
284+
ret = keyvalfunc(key, val)
278285
end
279-
pos += 1
280-
@nextbyte
281-
# we're now positioned at the start of the value
282-
val = _lazy(buf, pos, len, b, opts)
283-
ret = keyvalfunc(key, val)
284286
# if ret is an EarlyReturn, then we're short-circuiting
285287
# parsing via e.g. selection syntax, so return immediately
286288
ret isa StructUtils.EarlyReturn && return ret
@@ -732,8 +734,11 @@ function Base.show(io::IO, x::LazyValue)
732734
show(io, MIME"text/plain"(), la)
733735
end
734736
elseif T == JSONTypes.STRING
735-
str, _ = parsestring(x)
736-
Base.print(io, "JSON.LazyValue(", repr(convert(String, str)), ")")
737+
buf = getbuf(x)
738+
GC.@preserve buf begin
739+
str, _ = parsestring(x)
740+
Base.print(io, "JSON.LazyValue(", repr(convert(String, str)), ")")
741+
end
737742
elseif T == JSONTypes.NULL
738743
Base.print(io, "JSON.LazyValue(nothing)")
739744
else # bool/number

src/parse.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ function applyvalue(f, x::LazyValues, null)
277277
f(arr)
278278
return pos
279279
elseif type == JSONTypes.STRING
280-
str, pos = parsestring(x)
281-
f(convert(String, str))
280+
buf = getbuf(x)
281+
GC.@preserve buf begin
282+
str, pos = parsestring(x)
283+
f(convert(String, str))
284+
end
282285
return pos
283286
elseif type == JSONTypes.NUMBER
284287
num, pos = parsenumber(x)
@@ -344,8 +347,10 @@ end
344347
function StructUtils.lift(style::StructStyle, ::Type{T}, x::LazyValues, tags=(;)) where {T}
345348
type = gettype(x)
346349
if type == JSONTypes.STRING
347-
ptrstr, pos = parsestring(x)
348-
str, _ = StructUtils.lift(style, T, ptrstr, tags)
350+
GC.@preserve buf begin
351+
ptrstr, pos = parsestring(x)
352+
str, _ = StructUtils.lift(style, T, ptrstr, tags)
353+
end
349354
return str, pos
350355
elseif type == JSONTypes.NUMBER
351356
num, pos = parsenumber(x)
@@ -448,7 +453,9 @@ end
448453
Base.@nexprs $N i -> begin
449454
if typ == JSONTypes.OBJECT
450455
# consume key
451-
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
456+
GC.@preserve buf begin
457+
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
458+
end
452459
@nextbyte
453460
if b != UInt8(':')
454461
error = ExpectedColon
@@ -483,7 +490,9 @@ end
483490
while true
484491
if typ == JSONTypes.OBJECT
485492
# consume key
486-
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
493+
GC.@preserve buf begin
494+
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
495+
end
487496
@nextbyte
488497
if b != UInt8(':')
489498
error = ExpectedColon

0 commit comments

Comments
 (0)