From 9306b5e917ad62ee1c68869447cc49d38dafb519 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 24 Oct 2024 02:52:30 +0800 Subject: [PATCH] `std/nre` now uses destructors instead of finializer (#24353) Similar to changes in https://github.com/nim-lang/Nim/commit/bafb4f119ca85b47502f23fbc3f3f569caa0a3c6 (cherry picked from commit 3aaaed1acff259f185f7f2c8b05884e450e8dd88) --- lib/impure/nre.nim | 47 +++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim index 2321dc987b82c..c5adf0e995830 100644 --- a/lib/impure/nre.nim +++ b/lib/impure/nre.nim @@ -74,7 +74,14 @@ when defined(nimPreviewSlimSystem): export options type - Regex* = ref object + RegexDesc* = object + pattern*: string + pcreObj: ptr pcre.Pcre ## not nil + pcreExtra: ptr pcre.ExtraData ## nil + + captureNameToId: Table[string, int] + + Regex* = ref RegexDesc ## Represents the pattern that things are matched against, constructed with ## `re(string)`. Examples: `re"foo"`, `re(r"(*ANYCRLF)(?x)foo # ## comment".` @@ -145,11 +152,6 @@ type ## `DOLLAR_ENDONLY`, `FIRSTLINE`, `NO_AUTO_CAPTURE`, ## `JAVASCRIPT_COMPAT`, `U`, `NO_STUDY`. In other PCRE wrappers, you ## will need to pass these as separate flags to PCRE. - pattern*: string - pcreObj: ptr pcre.Pcre ## not nil - pcreExtra: ptr pcre.ExtraData ## nil - - captureNameToId: Table[string, int] RegexMatch* = object ## Usually seen as Option[RegexMatch], it represents the result of an @@ -216,12 +218,28 @@ type ## for whatever reason. The message contains the error ## code. -proc destroyRegex(pattern: Regex) = - `=destroy`(pattern.pattern) - pcre.free_substring(cast[cstring](pattern.pcreObj)) - if pattern.pcreExtra != nil: - pcre.free_study(pattern.pcreExtra) - `=destroy`(pattern.captureNameToId) +when defined(gcDestructors): + when defined(nimAllowNonVarDestructor) and defined(nimPreviewNonVarDestructor): + proc `=destroy`(pattern: RegexDesc) = + `=destroy`(pattern.pattern) + pcre.free_substring(cast[cstring](pattern.pcreObj)) + if pattern.pcreExtra != nil: + pcre.free_study(pattern.pcreExtra) + `=destroy`(pattern.captureNameToId) + else: + proc `=destroy`(pattern: var RegexDesc) = + `=destroy`(pattern.pattern) + pcre.free_substring(cast[cstring](pattern.pcreObj)) + if pattern.pcreExtra != nil: + pcre.free_study(pattern.pcreExtra) + `=destroy`(pattern.captureNameToId) +else: + proc destroyRegex(pattern: Regex) = + `=destroy`(pattern.pattern) + pcre.free_substring(cast[cstring](pattern.pcreObj)) + if pattern.pcreExtra != nil: + pcre.free_study(pattern.pcreExtra) + `=destroy`(pattern.captureNameToId) proc getinfo[T](pattern: Regex, opt: cint): T = let retcode = pcre.fullinfo(pattern.pcreObj, pattern.pcreExtra, opt, addr result) @@ -251,7 +269,10 @@ proc getNameToNumberTable(pattern: Regex): Table[string, int] = result[name] = num proc initRegex(pattern: string, flags: int, study = true): Regex = - new(result, destroyRegex) + when defined(gcDestructors): + result = Regex() + else: + new(result, destroyRegex) result.pattern = pattern var errorMsg: cstring