@@ -28,22 +28,29 @@ function setupMethods (soljson) {
28
28
} ;
29
29
}
30
30
31
- var copyString = function ( str , ptr ) {
31
+ var copyToCString = function ( str , ptr ) {
32
32
var length = soljson . lengthBytesUTF8 ( str ) ;
33
+ // This is allocating memory using solc's allocator.
34
+ // Assuming copyToCString is only used in the context of wrapCallback, solc will free these pointers.
35
+ // See https://github.com/ethereum/solidity/blob/v0.5.13/libsolc/libsolc.h#L37-L40
33
36
var buffer = soljson . _malloc ( length + 1 ) ;
34
37
soljson . stringToUTF8 ( str , buffer , length + 1 ) ;
35
38
soljson . setValue ( ptr , buffer , '*' ) ;
36
39
} ;
37
40
41
+ // This is to support multiple versions of Emscripten.
42
+ // Take a single `ptr` and returns a `str`.
43
+ var copyFromCString = soljson . UTF8ToString || soljson . Pointer_stringify ;
44
+
38
45
var wrapCallback = function ( callback ) {
39
46
assert ( typeof callback === 'function' , 'Invalid callback specified.' ) ;
40
47
return function ( path , contents , error ) {
41
- var result = callback ( soljson . Pointer_stringify ( path ) ) ;
48
+ var result = callback ( copyFromCString ( path ) ) ;
42
49
if ( typeof result . contents === 'string' ) {
43
- copyString ( result . contents , contents ) ;
50
+ copyToCString ( result . contents , contents ) ;
44
51
}
45
52
if ( typeof result . error === 'string' ) {
46
- copyString ( result . error , error ) ;
53
+ copyToCString ( result . error , error ) ;
47
54
}
48
55
} ;
49
56
} ;
0 commit comments