@@ -4,19 +4,22 @@ namespace Jint.Constraints;
44
55public  sealed  class  MemoryLimitConstraint  :  Constraint 
66{ 
7-     private  static   readonly  Func < long > ?  GetAllocatedBytesForCurrentThread ; 
87    private  readonly  long  _memoryLimit ; 
98    private  long  _initialMemoryUsage ; 
109
10+ #if ! NET8_0_OR_GREATER 
11+     private  static   readonly  Func < long > ?  _getAllocatedBytesForCurrentThread ; 
12+ 
1113    static   MemoryLimitConstraint ( ) 
1214    { 
1315        var  methodInfo  =  typeof ( GC ) . GetMethod ( "GetAllocatedBytesForCurrentThread" ) ; 
1416
1517        if  ( methodInfo  !=  null ) 
1618        { 
17-             GetAllocatedBytesForCurrentThread  =  ( Func < long > ) Delegate . CreateDelegate ( typeof ( Func < long > ) ,  null ,  methodInfo ) ; 
19+             _getAllocatedBytesForCurrentThread  =  ( Func < long > )   Delegate . CreateDelegate ( typeof ( Func < long > ) ,  null ,  methodInfo ) ; 
1820        } 
1921    } 
22+ #endif
2023
2124    internal  MemoryLimitConstraint ( long  memoryLimit ) 
2225    { 
@@ -25,28 +28,33 @@ internal MemoryLimitConstraint(long memoryLimit)
2528
2629    public  override  void  Check ( ) 
2730    { 
28-         if  ( _memoryLimit  >  0 ) 
31+         if  ( _memoryLimit  <=  0 ) 
2932        { 
30-             if  ( GetAllocatedBytesForCurrentThread  !=  null ) 
31-             { 
32-                 var  memoryUsage  =  GetAllocatedBytesForCurrentThread ( )  -  _initialMemoryUsage ; 
33-                 if  ( memoryUsage  >  _memoryLimit ) 
34-                 { 
35-                     ExceptionHelper . ThrowMemoryLimitExceededException ( $ "Script has allocated { memoryUsage }  but is limited to { _memoryLimit } ") ; 
36-                 } 
37-             } 
38-             else 
39-             { 
40-                 ExceptionHelper . ThrowPlatformNotSupportedException ( "The current platform doesn't support MemoryLimit." ) ; 
41-             } 
33+             return ; 
34+         } 
35+ 
36+ #if NET8_0_OR_GREATER 
37+         var  usage  =  GC . GetAllocatedBytesForCurrentThread ( ) ; 
38+ #else
39+         if  ( _getAllocatedBytesForCurrentThread  ==  null ) 
40+         { 
41+             ExceptionHelper . ThrowPlatformNotSupportedException ( "The current platform doesn't support MemoryLimit." ) ; 
42+         } 
43+ 
44+         var  usage  =  _getAllocatedBytesForCurrentThread ( ) ; 
45+ #endif
46+         if  ( usage  -  _initialMemoryUsage  >  _memoryLimit ) 
47+         { 
48+             ExceptionHelper . ThrowMemoryLimitExceededException ( $ "Script has allocated { usage  -  _initialMemoryUsage }  but is limited to { _memoryLimit } ") ; 
4249        } 
4350    } 
4451
4552    public  override  void  Reset ( ) 
4653    { 
47-         if  ( GetAllocatedBytesForCurrentThread  !=  null ) 
48-         { 
49-             _initialMemoryUsage  =  GetAllocatedBytesForCurrentThread ( ) ; 
50-         } 
54+ #if NET8_0_OR_GREATER 
55+         _initialMemoryUsage  =  GC . GetAllocatedBytesForCurrentThread ( ) ; 
56+ #else
57+         _initialMemoryUsage  =  _getAllocatedBytesForCurrentThread ? . Invoke ( )  ??  0 ; 
58+ #endif
5159    } 
5260} 
0 commit comments