12
12
/*jslint sub:true*/ /* The symbols 'fromWireType' and 'toWireType' must be accessed via array notation to be closure-safe since craftInvokerFunction crafts functions as strings that can't be closured. */
13
13
14
14
// -- jshint doesn't understand library syntax, so we need to mark the symbols exposed here
15
- /*global getStringOrSymbol, emval_handle_array , Emval, __emval_unregister, count_emval_handles, emval_symbols, emval_free_list, get_first_emval , __emval_decref, emval_newers*/
15
+ /*global getStringOrSymbol, emval_handles , Emval, __emval_unregister, count_emval_handles, emval_symbols, __emval_decref, emval_newers*/
16
16
/*global craftEmvalAllocator, emval_addMethodCaller, emval_methodCallers, LibraryManager, mergeInto, emval_allocateDestructors, global, emval_lookupTypes, makeLegalFunctionName*/
17
17
/*global emval_get_global*/
18
18
19
19
var LibraryEmVal = {
20
- $emval_handle_array : [
21
- { } ,
22
- { value : undefined } ,
23
- { value : null } ,
24
- { value : true } ,
25
- { value : false }
26
- ] , // reserve zero and special values
27
- $emval_free_list : [ ] ,
20
+ $emval_handles__deps : [ '$HandleAllocator' ] ,
21
+ $emval_handles : "new HandleAllocator();" ,
28
22
$emval_symbols : { } , // address -> string
29
23
30
- $init_emval__deps : [ '$count_emval_handles' , '$get_first_emval ' ] ,
24
+ $init_emval__deps : [ '$count_emval_handles' , '$emval_handles ' ] ,
31
25
$init_emval__postset : 'init_emval();' ,
32
26
$init_emval : function ( ) {
27
+ // reserve some special values. These never get de-allocated.
28
+ // The HandleAllocator takes care of reserving zero.
29
+ emval_handles . allocated . push (
30
+ { value : undefined } ,
31
+ { value : null } ,
32
+ { value : true } ,
33
+ { value : false } ,
34
+ ) ;
35
+ emval_handles . reserved = emval_handles . allocated . length
33
36
Module [ 'count_emval_handles' ] = count_emval_handles ;
34
- Module [ 'get_first_emval' ] = get_first_emval ;
35
37
} ,
36
38
37
- $count_emval_handles__deps : [ '$emval_handle_array ' ] ,
39
+ $count_emval_handles__deps : [ '$emval_handles ' ] ,
38
40
$count_emval_handles : function ( ) {
39
41
var count = 0 ;
40
- for ( var i = 5 ; i < emval_handle_array . length ; ++ i ) {
41
- if ( emval_handle_array [ i ] !== undefined ) {
42
+ for ( var i = emval_handles . reserved ; i < emval_handles . allocated . length ; ++ i ) {
43
+ if ( emval_handles . allocated [ i ] !== undefined ) {
42
44
++ count ;
43
45
}
44
46
}
45
47
return count ;
46
48
} ,
47
49
48
- $get_first_emval__deps : [ '$emval_handle_array' ] ,
49
- $get_first_emval : function ( ) {
50
- for ( var i = 5 ; i < emval_handle_array . length ; ++ i ) {
51
- if ( emval_handle_array [ i ] !== undefined ) {
52
- return emval_handle_array [ i ] ;
53
- }
54
- }
55
- return null ;
56
- } ,
57
-
58
50
_emval_register_symbol__deps : [ '$emval_symbols' , '$readLatin1String' ] ,
59
51
_emval_register_symbol : function ( address ) {
60
52
emval_symbols [ address ] = readLatin1String ( address ) ;
@@ -69,13 +61,13 @@ var LibraryEmVal = {
69
61
return symbol ;
70
62
} ,
71
63
72
- $Emval__deps : [ '$emval_handle_array' , '$emval_free_list ', '$throwBindingError' , '$init_emval' ] ,
64
+ $Emval__deps : [ '$emval_handles ' , '$throwBindingError' , '$init_emval' ] ,
73
65
$Emval : {
74
66
toValue : ( handle ) => {
75
67
if ( ! handle ) {
76
68
throwBindingError ( 'Cannot use deleted val. handle = ' + handle ) ;
77
69
}
78
- return emval_handle_array [ handle ] . value ;
70
+ return emval_handles . get ( handle ) . value ;
79
71
} ,
80
72
81
73
toHandle : ( value ) => {
@@ -85,31 +77,25 @@ var LibraryEmVal = {
85
77
case true : return 3 ;
86
78
case false : return 4 ;
87
79
default :{
88
- var handle = emval_free_list . length ?
89
- emval_free_list . pop ( ) :
90
- emval_handle_array . length ;
91
-
92
- emval_handle_array [ handle ] = { refcount : 1 , value : value } ;
93
- return handle ;
80
+ return emval_handles . allocate ( { refcount : 1 , value : value } ) ;
94
81
}
95
82
}
96
83
}
97
84
} ,
98
85
99
86
_emval_incref__sig : 'vp' ,
100
- _emval_incref__deps : [ '$emval_handle_array ' ] ,
87
+ _emval_incref__deps : [ '$emval_handles ' ] ,
101
88
_emval_incref : function ( handle ) {
102
89
if ( handle > 4 ) {
103
- emval_handle_array [ handle ] . refcount += 1 ;
90
+ emval_handles . get ( handle ) . refcount += 1 ;
104
91
}
105
92
} ,
106
93
107
94
_emval_decref__sig : 'vp' ,
108
- _emval_decref__deps : [ '$emval_free_list' , '$emval_handle_array '] ,
95
+ _emval_decref__deps : [ '$emval_handles ' ] ,
109
96
_emval_decref : function ( handle ) {
110
- if ( handle > 4 && 0 === -- emval_handle_array [ handle ] . refcount ) {
111
- emval_handle_array [ handle ] = undefined ;
112
- emval_free_list . push ( handle ) ;
97
+ if ( handle >= emval_handles . reserved && 0 === -- emval_handles . get ( handle ) . refcount ) {
98
+ emval_handles . free ( handle ) ;
113
99
}
114
100
} ,
115
101
0 commit comments