Skip to content

Commit 3aaaed1

Browse files
authored
std/nre now uses destructors instead of finializer (#24353)
Similar to changes in bafb4f1
1 parent 40b37c9 commit 3aaaed1

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

lib/impure/nre.nim

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ when defined(nimPreviewSlimSystem):
7474
export options
7575

7676
type
77-
Regex* = ref object
77+
RegexDesc* = object
78+
pattern*: string
79+
pcreObj: ptr pcre.Pcre ## not nil
80+
pcreExtra: ptr pcre.ExtraData ## nil
81+
82+
captureNameToId: Table[string, int]
83+
84+
Regex* = ref RegexDesc
7885
## Represents the pattern that things are matched against, constructed with
7986
## `re(string)`. Examples: `re"foo"`, `re(r"(*ANYCRLF)(?x)foo #
8087
## comment".`
@@ -145,11 +152,6 @@ type
145152
## `DOLLAR_ENDONLY`, `FIRSTLINE`, `NO_AUTO_CAPTURE`,
146153
## `JAVASCRIPT_COMPAT`, `U`, `NO_STUDY`. In other PCRE wrappers, you
147154
## will need to pass these as separate flags to PCRE.
148-
pattern*: string
149-
pcreObj: ptr pcre.Pcre ## not nil
150-
pcreExtra: ptr pcre.ExtraData ## nil
151-
152-
captureNameToId: Table[string, int]
153155

154156
RegexMatch* = object
155157
## Usually seen as Option[RegexMatch], it represents the result of an
@@ -216,12 +218,28 @@ type
216218
## for whatever reason. The message contains the error
217219
## code.
218220

219-
proc destroyRegex(pattern: Regex) =
220-
`=destroy`(pattern.pattern)
221-
pcre.free_substring(cast[cstring](pattern.pcreObj))
222-
if pattern.pcreExtra != nil:
223-
pcre.free_study(pattern.pcreExtra)
224-
`=destroy`(pattern.captureNameToId)
221+
when defined(gcDestructors):
222+
when defined(nimAllowNonVarDestructor) and defined(nimPreviewNonVarDestructor):
223+
proc `=destroy`(pattern: RegexDesc) =
224+
`=destroy`(pattern.pattern)
225+
pcre.free_substring(cast[cstring](pattern.pcreObj))
226+
if pattern.pcreExtra != nil:
227+
pcre.free_study(pattern.pcreExtra)
228+
`=destroy`(pattern.captureNameToId)
229+
else:
230+
proc `=destroy`(pattern: var RegexDesc) =
231+
`=destroy`(pattern.pattern)
232+
pcre.free_substring(cast[cstring](pattern.pcreObj))
233+
if pattern.pcreExtra != nil:
234+
pcre.free_study(pattern.pcreExtra)
235+
`=destroy`(pattern.captureNameToId)
236+
else:
237+
proc destroyRegex(pattern: Regex) =
238+
`=destroy`(pattern.pattern)
239+
pcre.free_substring(cast[cstring](pattern.pcreObj))
240+
if pattern.pcreExtra != nil:
241+
pcre.free_study(pattern.pcreExtra)
242+
`=destroy`(pattern.captureNameToId)
225243

226244
proc getinfo[T](pattern: Regex, opt: cint): T =
227245
let retcode = pcre.fullinfo(pattern.pcreObj, pattern.pcreExtra, opt, addr result)
@@ -251,7 +269,10 @@ proc getNameToNumberTable(pattern: Regex): Table[string, int] =
251269
result[name] = num
252270

253271
proc initRegex(pattern: string, flags: int, study = true): Regex =
254-
new(result, destroyRegex)
272+
when defined(gcDestructors):
273+
result = Regex()
274+
else:
275+
new(result, destroyRegex)
255276
result.pattern = pattern
256277

257278
var errorMsg: cstring

0 commit comments

Comments
 (0)