Really fix StringStream with ARC at compile-time, improve streams test (#19739)

* Fix compile-time StringStream with ARC

* make readDataStr work with ARC, improve test
This commit is contained in:
Danil Yarantsev 2022-04-25 12:15:03 +03:00 • committed by GitHub
commit 2f32b450d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -243,6 +243,9 @@ proc readDataStr*(s: Stream, buffer: var string, slice: Slice[int]): int =
result = s.readDataStrImpl(s, buffer, slice) result = s.readDataStrImpl(s, buffer, slice)
else: else:
# fallback # fallback
when declared(prepareMutation):
# buffer might potentially be a CoW literal with ARC
prepareMutation(buffer)
result = s.readData(addr buffer[slice.a], slice.b + 1 - slice.a) result = s.readData(addr buffer[slice.a], slice.b + 1 - slice.a)
template jsOrVmBlock(caseJsOrVm, caseElse: untyped): untyped = template jsOrVmBlock(caseJsOrVm, caseElse: untyped): untyped =
@ -1274,8 +1277,11 @@ else: # after 1.3 or JS not defined
new(result) new(result)
result.data = s result.data = s
when declared(prepareMutation): when nimvm:
prepareMutation(result.data) # Allows us to mutate using `addr` logic like `copyMem`, otherwise it errors. discard
else:
when declared(prepareMutation):
prepareMutation(result.data) # Allows us to mutate using `addr` logic like `copyMem`, otherwise it errors.
result.pos = 0 result.pos = 0
result.closeImpl = ssClose result.closeImpl = ssClose
result.atEndImpl = ssAtEnd result.atEndImpl = ssAtEnd

View file

@ -1,4 +1,5 @@
discard """ discard """
matrix: "--gc:refc; --gc:arc"
input: "Arne" input: "Arne"
output: ''' output: '''
Hello! What is your name? Hello! What is your name?
@ -74,9 +75,13 @@ block:
doAssert(ss.peekLine(str)) doAssert(ss.peekLine(str))
doAssert(str == "uick brown fox jumped over the lazy dog.") doAssert(str == "uick brown fox jumped over the lazy dog.")
doAssert(ss.getPosition == 5) # haven't moved doAssert(ss.getPosition == 5) # haven't moved
ss.setPosition(0) # Ensure we dont error with writing over literals on arc/orc #19707 # bug #19707 - Ensure we dont error with writing over literals on arc/orc
ss.setPosition(0)
ss.write("hello") ss.write("hello")
ss.setPosition(0)
doAssert(ss.peekStr(5) == "hello")
# bug #19716
static: # Ensure streams it doesnt break with nimscript on arc/orc #19716 static: # Ensure streams it doesnt break with nimscript on arc/orc #19716
let s = newStringStream("a") let s = newStringStream("a")
discard s.data doAssert s.data == "a"