1
1
import { typeOf , proxyOf } from '../esm/index.js' ;
2
2
3
+ const assert = ( current , expected , message = `expected ${ expected } - got ${ current } ` ) => {
4
+ if ( ! Object . is ( current , expected ) )
5
+ throw new Error ( message ) ;
6
+ } ;
7
+
3
8
// 🦄 typeOf coverage related
4
9
let proxied = proxyOf ( {
5
10
// native cases
6
11
array : { } ,
7
12
function : { } ,
8
13
object : { } ,
9
14
10
- // extra primitives
15
+ // custom primitives
11
16
bigint : {
12
- get ( target , key ) {
13
- const value = target [ key ] ;
14
- return typeof value === 'function' ?
15
- value . bind ( target ) : value ;
17
+ get ( target , key , ... _ ) {
18
+ return key === Symbol . toPrimitive ?
19
+ ( ) => target . valueOf ( ) :
20
+ Reflect . get ( target , key , ... _ ) ;
16
21
} ,
17
- getPrototypeOf : ( ) => BigInt . prototype ,
18
22
} ,
19
23
boolean : { } ,
20
24
null : { } ,
@@ -28,31 +32,84 @@ let proxied = proxyOf({
28
32
} ) ;
29
33
30
34
// typeOf native cases
31
- console . assert ( typeOf ( [ ] ) === 'array' ) ;
32
- console . assert ( typeOf ( proxied . array ( 0 ) ) === 'array' ) ;
33
- console . assert ( typeOf ( ( ) => { } ) === 'function' ) ;
34
- console . assert ( typeOf ( proxied . function ( 0 ) ) === 'function' ) ;
35
- console . assert ( typeOf ( { } ) === 'object' ) ;
36
- console . assert ( typeOf ( proxied . object ( 0 ) ) === 'object' ) ;
35
+ assert ( typeOf ( [ ] ) , 'array' ) ;
36
+ assert ( typeOf ( proxied . array ( 0 ) ) , 'array' ) ;
37
+ assert ( typeOf ( ( ) => { } ) , 'function' ) ;
38
+ assert ( typeOf ( proxied . function ( 0 ) ) , 'function' ) ;
39
+ assert ( typeOf ( { } ) , 'object' ) ;
40
+ assert ( typeOf ( proxied . object ( 0 ) ) , 'object' ) ;
37
41
38
42
// typeOf extra primitives
39
- console . assert ( typeOf ( 1n ) === 'bigint' ) ;
40
- console . assert ( typeOf ( proxied . bigint ( 0 ) ) === 'bigint' ) ;
41
- console . assert ( typeOf ( false ) === 'boolean' ) ;
42
- console . assert ( typeOf ( proxied . boolean ( 0 ) ) === 'boolean' ) ;
43
- console . assert ( typeOf ( null ) === 'null' ) ;
44
- console . assert ( typeOf ( proxied . null ( 0 ) ) === 'null' ) ;
45
- console . assert ( typeOf ( 1 ) === 'number' ) ;
46
- console . assert ( typeOf ( proxied . number ( 0 ) ) === 'number' ) ;
47
- console . assert ( typeOf ( '' ) === 'string' ) ;
48
- console . assert ( typeOf ( proxied . string ( 0 ) ) === 'string' ) ;
49
- console . assert ( typeOf ( Symbol ( ) ) === 'symbol' ) ;
50
- console . assert ( typeOf ( proxied . symbol ( 0 ) ) === 'symbol' ) ;
51
- console . assert ( typeOf ( ) === 'undefined' ) ;
52
- console . assert ( typeOf ( proxied . undefined ( { } ) ) === 'undefined' ) ;
43
+ assert ( typeOf ( 1n ) , 'bigint' ) ;
44
+ assert ( typeOf ( proxied . bigint ( 0 ) ) , 'bigint' ) ;
45
+ assert ( typeOf ( false ) , 'boolean' ) ;
46
+ assert ( typeOf ( proxied . boolean ( 0 ) ) , 'boolean' ) ;
47
+ assert ( typeOf ( null ) , 'null' ) ;
48
+ assert ( typeOf ( proxied . null ( 0 ) ) , 'null' ) ;
49
+ assert ( typeOf ( 1 ) , 'number' ) ;
50
+ assert ( typeOf ( proxied . number ( 0 ) ) , 'number' ) ;
51
+ assert ( typeOf ( '' ) , 'string' ) ;
52
+ assert ( typeOf ( proxied . string ( 0 ) ) , 'string' ) ;
53
+ assert ( typeOf ( Symbol ( ) ) , 'symbol' ) ;
54
+ assert ( typeOf ( proxied . symbol ( 0 ) ) , 'symbol' ) ;
55
+ assert ( typeOf ( ) , 'undefined' ) ;
56
+ assert ( typeOf ( proxied . undefined ( 0 ) ) , 'undefined' ) ;
53
57
54
58
// typeOf custom direct/defined
55
- console . assert ( typeOf ( proxied . direct ( { } ) ) === 'direct' ) ;
59
+ assert ( typeOf ( proxied . direct ( { } ) ) , 'direct' ) ;
60
+
61
+ assert ( proxied . bigint ( 2n ) == 2n , true , 'bigint' ) ;
62
+ assert ( proxied . bigint ( 2n ) instanceof BigInt , true , 'bigint instanceof' ) ;
63
+
64
+ // 🦄 proxyOf coverage related
65
+ assert ( proxied . array ( [ 1 , 2 , 3 ] ) . length , 3 ) ;
66
+ assert ( proxied . direct ( [ 1 , 2 , 3 ] ) . length , 3 ) ;
67
+ assert ( proxied . object ( { a : 1 } ) . a , 1 ) ;
68
+ assert ( proxied . direct ( { a : 1 } ) . a , 1 ) ;
69
+ assert ( proxied . function ( ( ) => 1 ) ( ) , 1 ) ;
70
+ assert ( proxied . direct ( ( ) => 1 ) ( ) , 1 ) ;
71
+
72
+ let i = 0 ;
73
+
74
+ const gcdo = Object ( 1 ) ;
75
+ const gcdd = { b : 2 } ;
76
+
77
+ proxied = proxyOf ( {
78
+ object : {
79
+ destruct ( ref ) {
80
+ assert ( ref , gcdo . valueOf ( ) ) ;
81
+ i ++ ;
82
+ }
83
+ } ,
84
+ direct : {
85
+ destruct ( ref ) {
86
+ assert ( ref , gcdd ) ;
87
+ i ++ ;
88
+ }
89
+ } ,
90
+ } ) ;
91
+
92
+
93
+ let pgcdo = proxied . object ( gcdo . valueOf ( ) , gcdo ) ;
94
+ let pgcdd = proxied . direct ( gcdd ) ;
56
95
57
- console . assert ( proxied . bigint ( 2n ) == 2n ) ;
58
- console . assert ( proxied . bigint ( 2n ) instanceof BigInt ) ;
96
+ setTimeout ( ( ) => {
97
+ pgcdo = pgcdd = null ;
98
+ gc ( ) ;
99
+ setTimeout ( ( ) => {
100
+ gc ( ) ;
101
+ assert ( i , 2 ) ;
102
+ pgcdo = proxied . object ( gcdo . valueOf ( ) , gcdo ) ;
103
+ pgcdd = proxied . direct ( gcdd ) ;
104
+ setTimeout ( ( ) => {
105
+ proxied . free ( gcdo ) ;
106
+ proxied . free ( gcdd ) ;
107
+ gc ( ) ;
108
+ setTimeout ( ( ) => {
109
+ gc ( ) ;
110
+ assert ( i , 2 ) ;
111
+ console . log ( 'OK' ) ;
112
+ } , 100 ) ;
113
+ } , 100 ) ;
114
+ } ) ;
115
+ } , 100 ) ;
0 commit comments