Skip to content

Commit 7d6f480

Browse files
authored
Merge pull request #259 from ethereum/new-breaking-api
New breaking api, compile() is an alias to compileStandardWrapper()
2 parents 80a50f9 + a22870b commit 7d6f480

File tree

4 files changed

+32
-146
lines changed

4 files changed

+32
-146
lines changed

test/cli.js

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

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

137
tape('CLI', function (t) {
148
t.test('--version', function (st) {

test/determinism.js

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

65
tape('Deterministic Compilation', function (t) {
76
t.test('DAO', function (st) {
87
var input = {};
98
var prevBytecode = null;
10-
var testdir;
11-
if (semver.lt(solc.semver(), '0.5.0')) {
12-
testdir = 'test/DAO040/';
13-
} else {
14-
testdir = 'test/DAO/';
15-
}
9+
var testdir = 'test/DAO/';
1610
var files = ['DAO.sol', 'Token.sol', 'TokenCreation.sol', 'ManagedAccount.sol'];
1711
var i;
1812
for (i in files) {

test/package.js

Lines changed: 22 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,6 @@ 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-
6450
t.test('single files can be compiled (using lowlevel API)', function (st) {
6551
if (typeof solc.lowlevel.compileSingle !== 'function') {
6652
st.skip('Low-level compileSingle interface not implemented by this compiler version.');
@@ -75,13 +61,13 @@ tape('Compilation', function (t) {
7561
st.end();
7662
});
7763

78-
t.test('invalid source code fails properly', function (st) {
79-
if (!solc.supportsSingle) {
80-
st.skip('Not supported by solc');
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.');
8167
st.end();
8268
return;
8369
}
84-
var output = solc.compile('contract x { this is an invalid contract }');
70+
var output = JSON.parse(solc.lowlevel.compileSingle('contract x { this is an invalid contract }'));
8571
if (semver.lt(solc.semver(), '0.1.4')) {
8672
st.ok(output.error.indexOf('Parser error: Expected identifier') !== -1);
8773
st.end();
@@ -107,34 +93,8 @@ tape('Compilation', function (t) {
10793
st.end();
10894
});
10995

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-
13796
t.test('multiple files can be compiled (using lowlevel API)', function (st) {
97+
// Introduced in 0.1.6
13898
if (typeof solc.lowlevel.compileMulti !== 'function') {
13999
st.skip('Low-level compileMulti interface not implemented by this compiler version.');
140100
st.end();
@@ -155,40 +115,8 @@ tape('Compilation', function (t) {
155115
st.end();
156116
});
157117

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-
191118
t.test('lazy-loading callback works (using lowlevel API)', function (st) {
119+
// Introduced in 0.2.1
192120
if (typeof solc.lowlevel.compileCallback !== 'function') {
193121
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
194122
st.end();
@@ -215,15 +143,10 @@ tape('Compilation', function (t) {
215143
st.end();
216144
});
217145

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');
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.');
227150
st.end();
228151
return;
229152
}
@@ -234,7 +157,7 @@ tape('Compilation', function (t) {
234157
function findImports (path) {
235158
return { error: 'File not found' };
236159
}
237-
var output = solc.compile({sources: input}, 0, findImports);
160+
var output = JSON.parse(solc.lowlevel.compileCallback(JSON.stringify({sources: input}), 0, findImports));
238161
st.plan(3);
239162
st.ok('errors' in output);
240163
// Check if the ParserError exists, but allow others too
@@ -250,15 +173,10 @@ tape('Compilation', function (t) {
250173
st.end();
251174
});
252175

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');
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.');
262180
st.end();
263181
return;
264182
}
@@ -270,20 +188,15 @@ tape('Compilation', function (t) {
270188
throw new Error('Could not implement this interface properly...');
271189
}
272190
st.throws(function () {
273-
solc.compile({sources: input}, 0, findImports);
191+
solc.compileStandardWrapper(JSON.stringify({sources: input}), 0, findImports);
274192
}, /^Error: Could not implement this interface properly.../);
275193
st.end();
276194
});
277195

278196
t.test('lazy-loading callback fails properly (with invalid callback)', function (st) {
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');
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.');
287200
st.end();
288201
return;
289202
}
@@ -292,7 +205,7 @@ tape('Compilation', function (t) {
292205
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
293206
};
294207
st.throws(function () {
295-
solc.compile({sources: input}, 0, "this isn't a callback");
208+
solc.compileStandardWrapper(JSON.stringify({sources: input}), 0, "this isn't a callback");
296209
}, /Invalid callback specified./);
297210
st.end();
298211
});
@@ -313,7 +226,7 @@ tape('Compilation', function (t) {
313226
var input = {
314227
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
315228
};
316-
var output = solc.compile({sources: input}, 0);
229+
var output = JSON.parse(solc.compileStandard({sources: input}, 0));
317230
st.plan(3);
318231
st.ok('errors' in output);
319232
// Check if the ParserError exists, but allow others too
@@ -586,6 +499,7 @@ tape('Loading Legacy Versions', function (t) {
586499
return;
587500
}
588501
if (!solcSnapshot.supportsSingle) {
502+
st.plan(1);
589503
st.skip('Not supported by solc');
590504
st.end();
591505
return;

wrapper.js

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

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

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

6060
var compileJSONCallback = null;
61-
if ('_compileJSONCallback' in soljson) {
61+
if ('x_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 ('_compileStandard' in soljson) {
69+
if ('x_compileStandard' in soljson) {
7070
var compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
7171
compileStandard = function (input, readCallback) {
7272
return runWithReadCallback(readCallback, compileStandardInternal, [ input ]);
@@ -79,20 +79,6 @@ 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-
9682
// Expects a Standard JSON I/O but supports old compilers
9783
var compileStandardWrapper = function (input, readCallback) {
9884
if (compileStandard !== null) {
@@ -230,13 +216,11 @@ function setupMethods (soljson) {
230216
compileCallback: compileJSONCallback,
231217
compileStandard: compileStandard
232218
},
233-
compile: compile,
234-
compileStandard: compileStandard,
219+
compile: compileStandardWrapper,
220+
221+
compileStandard: compileStandardWrapper,
235222
compileStandardWrapper: compileStandardWrapper,
236-
supportsSingle: compileJSON !== null,
237-
supportsMulti: compileJSONMulti !== null,
238-
supportsImportCallback: compileJSONCallback !== null,
239-
supportsStandard: compileStandard !== null,
223+
240224
// Loads the compiler of the given version from the github repository
241225
// instead of from the local filesystem.
242226
loadRemoteVersion: function (versionString, cb) {

0 commit comments

Comments
 (0)