@@ -31,12 +31,30 @@ function setupMethods (soljson) {
31
31
} ;
32
32
}
33
33
34
+ var alloc ;
35
+ if ( '_solidity_alloc' in soljson ) {
36
+ alloc = soljson . cwrap ( 'solidity_alloc' , 'number' , [ 'number' ] ) ;
37
+ } else {
38
+ alloc = soljson . _malloc ;
39
+ assert ( alloc , 'Expected malloc to be present.' ) ;
40
+ }
41
+
42
+ var reset ;
43
+ if ( '_solidity_reset' in soljson ) {
44
+ reset = soljson . cwrap ( 'solidity_reset' , null , [ ] ) ;
45
+ }
46
+
34
47
var copyToCString = function ( str , ptr ) {
35
48
var length = soljson . lengthBytesUTF8 ( str ) ;
36
49
// This is allocating memory using solc's allocator.
37
- // Assuming copyToCString is only used in the context of wrapCallback, solc will free these pointers.
38
- // See https://github.com/ethereum/solidity/blob/v0.5.13/libsolc/libsolc.h#L37-L40
39
- var buffer = soljson . _malloc ( length + 1 ) ;
50
+ //
51
+ // Before 0.6.0:
52
+ // Assuming copyToCString is only used in the context of wrapCallback, solc will free these pointers.
53
+ // See https://github.com/ethereum/solidity/blob/v0.5.13/libsolc/libsolc.h#L37-L40
54
+ //
55
+ // After 0.6.0:
56
+ // The duty is on solc-js to free these pointers. We accomplish that by calling `reset` at the end.
57
+ var buffer = alloc ( length + 1 ) ;
40
58
soljson . stringToUTF8 ( str , buffer , length + 1 ) ;
41
59
soljson . setValue ( ptr , buffer , '*' ) ;
42
60
} ;
@@ -61,6 +79,8 @@ function setupMethods (soljson) {
61
79
var wrapCallbackWithKind = function ( callback ) {
62
80
assert ( typeof callback === 'function' , 'Invalid callback specified.' ) ;
63
81
return function ( context , kind , data , contents , error ) {
82
+ // Must be a null pointer.
83
+ assert ( context === 0 , 'Callback context must be null.' ) ;
64
84
var result = callback ( copyFromCString ( kind ) , copyFromCString ( data ) ) ;
65
85
if ( typeof result . contents === 'string' ) {
66
86
copyToCString ( result . contents , contents ) ;
@@ -134,6 +154,14 @@ function setupMethods (soljson) {
134
154
throw e ;
135
155
}
136
156
removeFunction ( cb ) ;
157
+ if ( reset ) {
158
+ // Explicitly free memory.
159
+ //
160
+ // NOTE: cwrap() of "compile" will copy the returned pointer into a
161
+ // Javascript string and it is not possible to call free() on it.
162
+ // reset() however will clear up all allocations.
163
+ reset ( ) ;
164
+ }
137
165
return output ;
138
166
} ;
139
167
0 commit comments