Skip to content

Commit 108b048

Browse files
authored
Merge pull request #385 from ethereum/new-callback-backport
Backport new callback API to 0.5
2 parents 36dce8a + de0a3ce commit 108b048

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ It also accepts an optional callback function to resolve unmet dependencies. Thi
4646
It cannot be used together with callback-based, asynchronous, filesystem access. A workaround is to collect the names of dependencies, return an error, and keep re-running the compiler until all
4747
of them are resolved.
4848

49+
Starting 0.5.12 it also accepts an object in place of the callback to supply different kind of callbacks, however only file imports are supported.
50+
4951
*Note*: as an intermittent backwards compatibility feature, between versions 0.5.0 and 0.5.2, `compileStandard` and `compileStandardWrapper` also exists and behave like `compile` does.
5052

5153
#### Example usage without the import callback
@@ -106,8 +108,12 @@ function findImports (path) {
106108
return { error: 'File not found' }
107109
}
108110

111+
// Current syntax
109112
var output = JSON.parse(solc.compile(JSON.stringify(input), findImports))
110113

114+
// New syntax (supported from 0.5.12)
115+
var output = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }))
116+
111117
// `output` here contains the JSON output as specified in the documentation
112118
for (var contractName in output.contracts['test.sol']) {
113119
console.log(contractName + ': ' + output.contracts['test.sol'][contractName].evm.bytecode.object)

test/package.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,51 @@ tape('Compilation', function (t) {
396396
st.end();
397397
});
398398

399+
t.test('compiling standard JSON (with imports)', function (st) {
400+
// <0.1.6 doesn't have this
401+
if (!solc.features.multipleInputs) {
402+
st.skip('Not supported by solc');
403+
st.end();
404+
return;
405+
}
406+
407+
var input = {
408+
'language': 'Solidity',
409+
'settings': {
410+
'outputSelection': {
411+
'*': {
412+
'*': [ 'evm.bytecode' ]
413+
}
414+
}
415+
},
416+
'sources': {
417+
'cont.sol': {
418+
'content': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
419+
}
420+
}
421+
};
422+
423+
function findImports (path) {
424+
if (path === 'lib.sol') {
425+
return { contents: 'library L { function f() public returns (uint) { return 7; } }' };
426+
} else {
427+
return { error: 'File not found' };
428+
}
429+
}
430+
431+
var output = JSON.parse(solc.compile(JSON.stringify(input), findImports));
432+
var x = getBytecodeStandard(output, 'cont.sol', 'x');
433+
st.ok(x);
434+
st.ok(x.length > 0);
435+
var L = getBytecodeStandard(output, 'lib.sol', 'L');
436+
st.ok(L);
437+
st.ok(L.length > 0);
438+
439+
var outputNewApi = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }));
440+
st.deepEqual(output, outputNewApi);
441+
st.end();
442+
});
443+
399444
t.test('compiling standard JSON (using libraries)', function (st) {
400445
// <0.1.6 doesn't have this
401446
if (!solc.features.multipleInputs) {

wrapper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ function setupMethods (soljson) {
2727

2828
// This calls compile() with args || cb
2929
var runWithReadCallback = function (readCallback, compile, args) {
30+
// Forward compatibility with 0.6.x
31+
if (typeof readCallback === 'object') {
32+
readCallback = readCallback.import;
33+
}
34+
3035
if (readCallback === undefined) {
3136
readCallback = function (path) {
3237
return {

0 commit comments

Comments
 (0)