Skip to content

Commit de7cbc5

Browse files
authored
Use HandleAllocator for emval handles. NFC (#19054)
1 parent 376c6ff commit de7cbc5

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

src/embind/emval.js

+24-38
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,41 @@
1212
/*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. */
1313

1414
// -- 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*/
1616
/*global craftEmvalAllocator, emval_addMethodCaller, emval_methodCallers, LibraryManager, mergeInto, emval_allocateDestructors, global, emval_lookupTypes, makeLegalFunctionName*/
1717
/*global emval_get_global*/
1818

1919
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();",
2822
$emval_symbols: {}, // address -> string
2923

30-
$init_emval__deps: ['$count_emval_handles', '$get_first_emval'],
24+
$init_emval__deps: ['$count_emval_handles', '$emval_handles'],
3125
$init_emval__postset: 'init_emval();',
3226
$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
3336
Module['count_emval_handles'] = count_emval_handles;
34-
Module['get_first_emval'] = get_first_emval;
3537
},
3638

37-
$count_emval_handles__deps: ['$emval_handle_array'],
39+
$count_emval_handles__deps: ['$emval_handles'],
3840
$count_emval_handles: function() {
3941
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) {
4244
++count;
4345
}
4446
}
4547
return count;
4648
},
4749

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-
5850
_emval_register_symbol__deps: ['$emval_symbols', '$readLatin1String'],
5951
_emval_register_symbol: function(address) {
6052
emval_symbols[address] = readLatin1String(address);
@@ -69,13 +61,13 @@ var LibraryEmVal = {
6961
return symbol;
7062
},
7163

72-
$Emval__deps: ['$emval_handle_array', '$emval_free_list', '$throwBindingError', '$init_emval'],
64+
$Emval__deps: ['$emval_handles', '$throwBindingError', '$init_emval'],
7365
$Emval: {
7466
toValue: (handle) => {
7567
if (!handle) {
7668
throwBindingError('Cannot use deleted val. handle = ' + handle);
7769
}
78-
return emval_handle_array[handle].value;
70+
return emval_handles.get(handle).value;
7971
},
8072

8173
toHandle: (value) => {
@@ -85,31 +77,25 @@ var LibraryEmVal = {
8577
case true: return 3;
8678
case false: return 4;
8779
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});
9481
}
9582
}
9683
}
9784
},
9885

9986
_emval_incref__sig: 'vp',
100-
_emval_incref__deps: ['$emval_handle_array'],
87+
_emval_incref__deps: ['$emval_handles'],
10188
_emval_incref: function(handle) {
10289
if (handle > 4) {
103-
emval_handle_array[handle].refcount += 1;
90+
emval_handles.get(handle).refcount += 1;
10491
}
10592
},
10693

10794
_emval_decref__sig: 'vp',
108-
_emval_decref__deps: ['$emval_free_list', '$emval_handle_array'],
95+
_emval_decref__deps: ['$emval_handles'],
10996
_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);
11399
}
114100
},
115101

0 commit comments

Comments
 (0)