@@ -459,26 +459,25 @@ Domain.prototype.remove = function(ee) {
459459} ;
460460
461461
462- Domain . prototype . run = function ( fn ) {
462+ // Helper to run a function within a domain context.
463+ // Uses enterWith instead of ALS.run() so the context is NOT automatically
464+ // restored on exception - this matches original domain behavior where
465+ // exit() only runs on success.
466+ function runInDomain ( domain , thisArg , fn , args ) {
463467 const currentStore = getDomainStore ( ) || { domain : null , stack : [ ] } ;
464468 const newStack = ArrayPrototypeSlice ( currentStore . stack ) ;
465- ArrayPrototypePush ( newStack , this ) ;
466-
467- const args = ArrayPrototypeSlice ( arguments , 1 ) ;
469+ ArrayPrototypePush ( newStack , domain ) ;
468470 const previousDomain = currentDomain ;
469471 const previousStack = currentStack ;
470472
471- // Set currentDomain and currentStack before running so exception handler can see them
472- currentDomain = this ;
473+ currentDomain = domain ;
473474 currentStack = newStack ;
474475 updateExceptionCapture ( ) ;
475476
476- // Use enterWith instead of run() so the context is NOT automatically restored on exception.
477- // This matches the original domain.run() behavior where exit() only runs on success.
478- setDomainStore ( { domain : this , stack : newStack } ) ;
479- exports . active = this ;
477+ setDomainStore ( { domain, stack : newStack } ) ;
478+ exports . active = domain ;
480479
481- const result = ReflectApply ( fn , this , args ) ;
480+ const result = ReflectApply ( fn , thisArg , args ) ;
482481
483482 // On success, restore context (if exception thrown, context stays for catch blocks)
484483 setDomainStore ( currentStore ) ;
@@ -488,6 +487,10 @@ Domain.prototype.run = function(fn) {
488487 updateExceptionCapture ( ) ;
489488
490489 return result ;
490+ }
491+
492+ Domain . prototype . run = function ( fn ) {
493+ return runInDomain ( this , this , fn , ArrayPrototypeSlice ( arguments , 1 ) ) ;
491494} ;
492495
493496
@@ -507,31 +510,7 @@ function intercepted(_this, self, cb, fnargs) {
507510 return ;
508511 }
509512
510- const currentStore = getDomainStore ( ) || { domain : null , stack : [ ] } ;
511- const newStack = ArrayPrototypeSlice ( currentStore . stack ) ;
512- ArrayPrototypePush ( newStack , self ) ;
513- const args = ArrayPrototypeSlice ( fnargs , 1 ) ;
514- const previousDomain = currentDomain ;
515- const previousStack = currentStack ;
516-
517- currentDomain = self ;
518- currentStack = newStack ;
519- updateExceptionCapture ( ) ;
520-
521- // Use enterWith instead of run() so the context is NOT automatically restored on exception.
522- setDomainStore ( { domain : self , stack : newStack } ) ;
523- exports . active = self ;
524-
525- const result = ReflectApply ( cb , _this , args ) ;
526-
527- // On success, restore context
528- setDomainStore ( currentStore ) ;
529- exports . active = currentStore . domain ;
530- currentDomain = previousDomain ;
531- currentStack = previousStack ;
532- updateExceptionCapture ( ) ;
533-
534- return result ;
513+ return runInDomain ( self , _this , cb , ArrayPrototypeSlice ( fnargs , 1 ) ) ;
535514}
536515
537516
@@ -547,30 +526,7 @@ Domain.prototype.intercept = function(cb) {
547526
548527
549528function bound ( _this , self , cb , fnargs ) {
550- const currentStore = getDomainStore ( ) || { domain : null , stack : [ ] } ;
551- const newStack = ArrayPrototypeSlice ( currentStore . stack ) ;
552- ArrayPrototypePush ( newStack , self ) ;
553- const previousDomain = currentDomain ;
554- const previousStack = currentStack ;
555-
556- currentDomain = self ;
557- currentStack = newStack ;
558- updateExceptionCapture ( ) ;
559-
560- // Use enterWith instead of run() so the context is NOT automatically restored on exception.
561- setDomainStore ( { domain : self , stack : newStack } ) ;
562- exports . active = self ;
563-
564- const result = ReflectApply ( cb , _this , fnargs ) ;
565-
566- // On success, restore context
567- setDomainStore ( currentStore ) ;
568- exports . active = currentStore . domain ;
569- currentDomain = previousDomain ;
570- currentStack = previousStack ;
571- updateExceptionCapture ( ) ;
572-
573- return result ;
529+ return runInDomain ( self , _this , cb , fnargs ) ;
574530}
575531
576532
0 commit comments