File tree 6 files changed +39
-9
lines changed
6 files changed +39
-9
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ const {
28
28
29
29
const { triggerUncaughtException } = internalBinding ( 'errors' ) ;
30
30
31
- const { WeakReference } = internalBinding ( ' util') ;
31
+ const { WeakReference } = require ( 'internal/ util') ;
32
32
33
33
// Can't delete when weakref count reaches 0 as it could increment again.
34
34
// Only GC can be used as a valid time to clean up the channels map.
Original file line number Diff line number Diff line change @@ -52,9 +52,8 @@ const {
52
52
const { createHook } = require ( 'async_hooks' ) ;
53
53
const { useDomainTrampoline } = require ( 'internal/async_hooks' ) ;
54
54
55
- // TODO(addaleax): Use a non-internal solution for this.
56
55
const kWeak = Symbol ( 'kWeak' ) ;
57
- const { WeakReference } = internalBinding ( ' util') ;
56
+ const { WeakReference } = require ( 'internal/ util') ;
58
57
59
58
// Overwrite process.domain with a getter/setter that will allow for more
60
59
// effective optimizations
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ const {
33
33
SafeMap,
34
34
SafeSet,
35
35
SafeWeakMap,
36
+ SafeWeakRef,
36
37
StringPrototypeReplace,
37
38
StringPrototypeToLowerCase,
38
39
StringPrototypeToUpperCase,
@@ -797,6 +798,38 @@ function guessHandleType(fd) {
797
798
return handleTypes [ type ] ;
798
799
}
799
800
801
+ class WeakReference {
802
+ #weak = null ;
803
+ #strong = null ;
804
+ #refCount = 0 ;
805
+ constructor ( object ) {
806
+ this . #weak = new SafeWeakRef ( object ) ;
807
+ }
808
+
809
+ incRef ( ) {
810
+ this . #refCount++ ;
811
+ if ( this . #refCount === 1 ) {
812
+ const derefed = this . #weak. deref ( ) ;
813
+ if ( derefed !== undefined ) {
814
+ this . #strong = derefed ;
815
+ }
816
+ }
817
+ return this . #refCount;
818
+ }
819
+
820
+ decRef ( ) {
821
+ this . #refCount-- ;
822
+ if ( this . #refCount === 0 ) {
823
+ this . #strong = null ;
824
+ }
825
+ return this . #refCount;
826
+ }
827
+
828
+ get ( ) {
829
+ return this . #weak. deref ( ) ;
830
+ }
831
+ }
832
+
800
833
module . exports = {
801
834
getLazy,
802
835
assertCrypto,
@@ -855,4 +888,5 @@ module.exports = {
855
888
kEnumerableProperty,
856
889
setOwnProperty,
857
890
pendingDeprecate,
891
+ WeakReference,
858
892
} ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const { internalBinding } = require ( 'internal/test/binding' ) ;
4
- const { WeakReference } = internalBinding ( 'util' ) ;
3
+ const { WeakReference } = require ( 'internal/util' ) ;
5
4
const {
6
5
setDeserializeMainFunction
7
6
} = require ( 'v8' ) . startupSnapshot
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const { internalBinding } = require ( 'internal/test/binding' ) ;
4
- const { WeakReference } = internalBinding ( 'util' ) ;
3
+ const { WeakReference } = require ( 'internal/util' ) ;
5
4
const {
6
5
setDeserializeMainFunction
7
6
} = require ( 'v8' ) . startupSnapshot
Original file line number Diff line number Diff line change 2
2
'use strict' ;
3
3
const common = require ( '../common' ) ;
4
4
const assert = require ( 'assert' ) ;
5
- const { internalBinding } = require ( 'internal/test/binding' ) ;
6
- const { WeakReference } = internalBinding ( 'util' ) ;
5
+ const { WeakReference } = require ( 'internal/util' ) ;
7
6
8
7
let obj = { hello : 'world' } ;
9
8
const ref = new WeakReference ( obj ) ;
You can’t perform that action at this time.
0 commit comments