Skip to content

Commit c0d34d5

Browse files
authored
Merge pull request #283 from ethereum/revert-new-breaking-api
Revert "Merge pull request #259 from ethereum/new-breaking-api"
2 parents 7d6f480 + dd0b4b3 commit c0d34d5

File tree

4 files changed

+146
-32
lines changed

4 files changed

+146
-32
lines changed

test/cli.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
const tape = require('tape');
22
const spawn = require('tape-spawn');
33
const pkg = require('../package.json');
4+
const semver = require('semver');
45

5-
var daodir = 'DAO';
6+
var daodir;
7+
if (semver.lt(pkg.version, '0.5.0')) {
8+
daodir = 'DAO040';
9+
} else {
10+
daodir = 'DAO';
11+
}
612

713
tape('CLI', function (t) {
814
t.test('--version', function (st) {

test/determinism.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
const tape = require('tape');
22
const fs = require('fs');
33
const solc = require('../index.js');
4+
const semver = require('semver');
45

56
tape('Deterministic Compilation', function (t) {
67
t.test('DAO', function (st) {
78
var input = {};
89
var prevBytecode = null;
9-
var testdir = 'test/DAO/';
10+
var testdir;
11+
if (semver.lt(solc.semver(), '0.5.0')) {
12+
testdir = 'test/DAO040/';
13+
} else {
14+
testdir = 'test/DAO/';
15+
}
1016
var files = ['DAO.sol', 'Token.sol', 'TokenCreation.sol', 'ManagedAccount.sol'];
1117
var i;
1218
for (i in files) {

test/package.js

Lines changed: 108 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ tape('Version and license', function (t) {
4747
});
4848

4949
tape('Compilation', function (t) {
50+
t.test('single files can be compiled', function (st) {
51+
if (!solc.supportsSingle) {
52+
st.skip('Not supported by solc');
53+
st.end();
54+
return;
55+
}
56+
var output = solc.compile('contract x { function g() public {} }');
57+
st.ok('contracts' in output);
58+
var bytecode = getBytecode(output, '', 'x');
59+
st.ok(bytecode);
60+
st.ok(bytecode.length > 0);
61+
st.end();
62+
});
63+
5064
t.test('single files can be compiled (using lowlevel API)', function (st) {
5165
if (typeof solc.lowlevel.compileSingle !== 'function') {
5266
st.skip('Low-level compileSingle interface not implemented by this compiler version.');
@@ -61,13 +75,13 @@ tape('Compilation', function (t) {
6175
st.end();
6276
});
6377

64-
t.test('invalid source code fails properly (using lowlevel API)', function (st) {
65-
if (typeof solc.lowlevel.compileSingle !== 'function') {
66-
st.skip('Low-level compileSingle interface not implemented by this compiler version.');
78+
t.test('invalid source code fails properly', function (st) {
79+
if (!solc.supportsSingle) {
80+
st.skip('Not supported by solc');
6781
st.end();
6882
return;
6983
}
70-
var output = JSON.parse(solc.lowlevel.compileSingle('contract x { this is an invalid contract }'));
84+
var output = solc.compile('contract x { this is an invalid contract }');
7185
if (semver.lt(solc.semver(), '0.1.4')) {
7286
st.ok(output.error.indexOf('Parser error: Expected identifier') !== -1);
7387
st.end();
@@ -93,8 +107,34 @@ tape('Compilation', function (t) {
93107
st.end();
94108
});
95109

110+
t.test('multiple files can be compiled', function (st) {
111+
if (semver.lt(solc.semver(), '0.1.6')) {
112+
st.skip('Not supported by solc <0.1.6');
113+
st.end();
114+
return;
115+
}
116+
117+
if (!solc.supportsMulti) {
118+
st.skip('Not supported by solc');
119+
st.end();
120+
return;
121+
}
122+
123+
var input = {
124+
'lib.sol': 'library L { function f() public returns (uint) { return 7; } }',
125+
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
126+
};
127+
var output = solc.compile({sources: input});
128+
var x = getBytecode(output, 'cont.sol', 'x');
129+
st.ok(x);
130+
st.ok(x.length > 0);
131+
var L = getBytecode(output, 'lib.sol', 'L');
132+
st.ok(L);
133+
st.ok(L.length > 0);
134+
st.end();
135+
});
136+
96137
t.test('multiple files can be compiled (using lowlevel API)', function (st) {
97-
// Introduced in 0.1.6
98138
if (typeof solc.lowlevel.compileMulti !== 'function') {
99139
st.skip('Low-level compileMulti interface not implemented by this compiler version.');
100140
st.end();
@@ -115,8 +155,40 @@ tape('Compilation', function (t) {
115155
st.end();
116156
});
117157

158+
t.test('lazy-loading callback works', function (st) {
159+
if (semver.lt(solc.semver(), '0.2.1')) {
160+
st.skip('Not supported by solc <0.2.1');
161+
st.end();
162+
return;
163+
}
164+
165+
if (!solc.supportsImportCallback) {
166+
st.skip('Not supported by solc');
167+
st.end();
168+
return;
169+
}
170+
171+
var input = {
172+
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
173+
};
174+
function findImports (path) {
175+
if (path === 'lib.sol') {
176+
return { contents: 'library L { function f() public returns (uint) { return 7; } }' };
177+
} else {
178+
return { error: 'File not found' };
179+
}
180+
}
181+
var output = solc.compile({sources: input}, 0, findImports);
182+
var x = getBytecode(output, 'cont.sol', 'x');
183+
var L = getBytecode(output, 'lib.sol', 'L');
184+
st.ok(x);
185+
st.ok(x.length > 0);
186+
st.ok(L);
187+
st.ok(L.length > 0);
188+
st.end();
189+
});
190+
118191
t.test('lazy-loading callback works (using lowlevel API)', function (st) {
119-
// Introduced in 0.2.1
120192
if (typeof solc.lowlevel.compileCallback !== 'function') {
121193
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
122194
st.end();
@@ -143,10 +215,15 @@ tape('Compilation', function (t) {
143215
st.end();
144216
});
145217

146-
t.test('lazy-loading callback works (with file not found) (using lowlevel API)', function (st) {
147-
// Introduced in 0.2.1
148-
if (typeof solc.lowlevel.compileCallback !== 'function') {
149-
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
218+
t.test('lazy-loading callback works (with file not found)', function (st) {
219+
if (semver.lt(solc.semver(), '0.2.1')) {
220+
st.skip('Not supported by solc <0.2.1');
221+
st.end();
222+
return;
223+
}
224+
225+
if (!solc.supportsImportCallback) {
226+
st.skip('Not supported by solc');
150227
st.end();
151228
return;
152229
}
@@ -157,7 +234,7 @@ tape('Compilation', function (t) {
157234
function findImports (path) {
158235
return { error: 'File not found' };
159236
}
160-
var output = JSON.parse(solc.lowlevel.compileCallback(JSON.stringify({sources: input}), 0, findImports));
237+
var output = solc.compile({sources: input}, 0, findImports);
161238
st.plan(3);
162239
st.ok('errors' in output);
163240
// Check if the ParserError exists, but allow others too
@@ -173,10 +250,15 @@ tape('Compilation', function (t) {
173250
st.end();
174251
});
175252

176-
t.test('lazy-loading callback works (with exception) (using lowlevel API)', function (st) {
177-
// Introduced in 0.2.1
178-
if (typeof solc.lowlevel.compileCallback !== 'function') {
179-
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
253+
t.test('lazy-loading callback works (with exception)', function (st) {
254+
if (semver.lt(solc.semver(), '0.2.1')) {
255+
st.skip('Not supported by solc <0.2.1');
256+
st.end();
257+
return;
258+
}
259+
260+
if (!solc.supportsImportCallback) {
261+
st.skip('Not supported by solc');
180262
st.end();
181263
return;
182264
}
@@ -188,15 +270,20 @@ tape('Compilation', function (t) {
188270
throw new Error('Could not implement this interface properly...');
189271
}
190272
st.throws(function () {
191-
solc.compileStandardWrapper(JSON.stringify({sources: input}), 0, findImports);
273+
solc.compile({sources: input}, 0, findImports);
192274
}, /^Error: Could not implement this interface properly.../);
193275
st.end();
194276
});
195277

196278
t.test('lazy-loading callback fails properly (with invalid callback)', function (st) {
197-
// Introduced in 0.2.1
198-
if (typeof solc.lowlevel.compileCallback !== 'function') {
199-
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
279+
if (semver.lt(solc.semver(), '0.2.1')) {
280+
st.skip('Not supported by solc <0.2.1');
281+
st.end();
282+
return;
283+
}
284+
285+
if (!solc.supportsImportCallback) {
286+
st.skip('Not supported by solc');
200287
st.end();
201288
return;
202289
}
@@ -205,7 +292,7 @@ tape('Compilation', function (t) {
205292
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
206293
};
207294
st.throws(function () {
208-
solc.compileStandardWrapper(JSON.stringify({sources: input}), 0, "this isn't a callback");
295+
solc.compile({sources: input}, 0, "this isn't a callback");
209296
}, /Invalid callback specified./);
210297
st.end();
211298
});
@@ -226,7 +313,7 @@ tape('Compilation', function (t) {
226313
var input = {
227314
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
228315
};
229-
var output = JSON.parse(solc.compileStandard({sources: input}, 0));
316+
var output = solc.compile({sources: input}, 0);
230317
st.plan(3);
231318
st.ok('errors' in output);
232319
// Check if the ParserError exists, but allow others too
@@ -499,7 +586,6 @@ tape('Loading Legacy Versions', function (t) {
499586
return;
500587
}
501588
if (!solcSnapshot.supportsSingle) {
502-
st.plan(1);
503589
st.skip('Not supported by solc');
504590
st.end();
505591
return;

wrapper.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ function setupMethods (soljson) {
4848
};
4949

5050
var compileJSON = null;
51-
if ('x_compileJSON' in soljson) {
51+
if ('_compileJSON' in soljson) {
5252
compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
5353
}
5454

5555
var compileJSONMulti = null;
56-
if ('x_compileJSONMulti' in soljson) {
56+
if ('_compileJSONMulti' in soljson) {
5757
compileJSONMulti = soljson.cwrap('compileJSONMulti', 'string', ['string', 'number']);
5858
}
5959

6060
var compileJSONCallback = null;
61-
if ('x_compileJSONCallback' in soljson) {
61+
if ('_compileJSONCallback' in soljson) {
6262
var compileInternal = soljson.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
6363
compileJSONCallback = function (input, optimize, readCallback) {
6464
return runWithReadCallback(readCallback, compileInternal, [ input, optimize ]);
6565
};
6666
}
6767

6868
var compileStandard = null;
69-
if ('x_compileStandard' in soljson) {
69+
if ('_compileStandard' in soljson) {
7070
var compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
7171
compileStandard = function (input, readCallback) {
7272
return runWithReadCallback(readCallback, compileStandardInternal, [ input ]);
@@ -79,6 +79,20 @@ function setupMethods (soljson) {
7979
};
8080
}
8181

82+
var compile = function (input, optimise, readCallback) {
83+
var result = '';
84+
if (readCallback !== undefined && compileJSONCallback !== null) {
85+
result = compileJSONCallback(JSON.stringify(input), optimise, readCallback);
86+
} else if (typeof input !== 'string' && compileJSONMulti !== null) {
87+
result = compileJSONMulti(JSON.stringify(input), optimise);
88+
} else if (compileJSON !== null) {
89+
result = compileJSON(input, optimise);
90+
} else {
91+
return { errors: 'No suitable compiler interface found.' };
92+
}
93+
return JSON.parse(result);
94+
};
95+
8296
// Expects a Standard JSON I/O but supports old compilers
8397
var compileStandardWrapper = function (input, readCallback) {
8498
if (compileStandard !== null) {
@@ -216,11 +230,13 @@ function setupMethods (soljson) {
216230
compileCallback: compileJSONCallback,
217231
compileStandard: compileStandard
218232
},
219-
compile: compileStandardWrapper,
220-
221-
compileStandard: compileStandardWrapper,
233+
compile: compile,
234+
compileStandard: compileStandard,
222235
compileStandardWrapper: compileStandardWrapper,
223-
236+
supportsSingle: compileJSON !== null,
237+
supportsMulti: compileJSONMulti !== null,
238+
supportsImportCallback: compileJSONCallback !== null,
239+
supportsStandard: compileStandard !== null,
224240
// Loads the compiler of the given version from the github repository
225241
// instead of from the local filesystem.
226242
loadRemoteVersion: function (versionString, cb) {

0 commit comments

Comments
 (0)