@@ -462,24 +462,16 @@ open class XMLParser : NSObject {
462
462
463
463
open var allowedExternalEntityURLs : Set < URL > ?
464
464
465
- /// The current parser context for the current thread.
466
- private class _CurrentParserContext {
467
- var _stack : [ XMLParser ] = [ ]
468
- var _current : XMLParser ? {
469
- return _stack. last
470
- }
471
- }
472
-
473
465
#if os(WASI)
474
466
/// The current parser associated with the current thread. (assuming no multi-threading)
475
467
/// FIXME: Unify the implementation with the other platforms once we unlock `threadDictionary`
476
468
/// or migrate to `FoundationEssentials._ThreadLocal`.
477
- private static nonisolated ( unsafe) var _currentParserContext : _CurrentParserContext ?
469
+ private static nonisolated ( unsafe) var _currentParser : XMLParser ? = nil
478
470
#else
479
471
/// The current parser associated with the current thread.
480
- private static var _currentParserContext : _CurrentParserContext ? {
472
+ private static var _currentParser : XMLParser ? {
481
473
get {
482
- return Thread . current. threadDictionary [ " __CurrentNSXMLParser " ] as? _CurrentParserContext
474
+ return Thread . current. threadDictionary [ " __CurrentNSXMLParser " ] as? XMLParser
483
475
}
484
476
set {
485
477
Thread . current. threadDictionary [ " __CurrentNSXMLParser " ] = newValue
@@ -489,29 +481,14 @@ open class XMLParser : NSObject {
489
481
490
482
/// The current parser associated with the current thread.
491
483
internal static func currentParser( ) -> XMLParser ? {
492
- if let ctx = _currentParserContext {
493
- return ctx. _current
494
- }
495
- return nil
484
+ return _currentParser
496
485
}
497
486
498
487
/// Execute the given closure with the current parser set to the given parser.
499
488
internal static func withCurrentParser< R> ( _ parser: XMLParser , _ body: ( ) -> R ) -> R {
500
- var ctx : _CurrentParserContext
501
- if let current = _currentParserContext {
502
- // Use the existing context if it exists
503
- ctx = current
504
- } else {
505
- // Create a new context in TLS
506
- ctx = _CurrentParserContext ( )
507
- _currentParserContext = ctx
508
- }
509
- // Push the parser onto the stack
510
- ctx. _stack. append ( parser)
511
- defer {
512
- // Pop the parser off the stack
513
- ctx. _stack. removeLast ( )
514
- }
489
+ let oldParser = _currentParser
490
+ _currentParser = parser
491
+ defer { _currentParser = oldParser }
515
492
return body ( )
516
493
}
517
494
0 commit comments