Skip to content

Commit be3195f

Browse files
committed
Implement support for the new memory allocation API
1 parent 7373354 commit be3195f

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

wrapper.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,30 @@ function setupMethods (soljson) {
3131
};
3232
}
3333

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+
3447
var copyToCString = function (str, ptr) {
3548
var length = soljson.lengthBytesUTF8(str);
3649
// 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);
4058
soljson.stringToUTF8(str, buffer, length + 1);
4159
soljson.setValue(ptr, buffer, '*');
4260
};
@@ -136,6 +154,14 @@ function setupMethods (soljson) {
136154
throw e;
137155
}
138156
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+
}
139165
return output;
140166
};
141167

0 commit comments

Comments
 (0)