Skip to content

Commit a629477

Browse files
authored
Merge pull request #255 from ethereum/refactor-api
Refactor wrapper (and make compileJSON optional)
2 parents e50605a + 4ac210f commit a629477

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

wrapper.js

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,78 @@ var https = require('https');
66
var MemoryStream = require('memorystream');
77

88
function setupMethods (soljson) {
9-
var compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
10-
var compileJSONMulti = null;
11-
if ('_compileJSONMulti' in soljson) {
12-
compileJSONMulti = soljson.cwrap('compileJSONMulti', 'string', ['string', 'number']);
13-
}
14-
var compileJSONCallback = null;
15-
var compileStandard = null;
16-
if (('_compileJSONCallback' in soljson) || ('_compileStandard' in soljson) || ('_solidity_compile' in soljson)) {
17-
var copyString = function (str, ptr) {
18-
var length = soljson.lengthBytesUTF8(str);
19-
var buffer = soljson._malloc(length + 1);
20-
soljson.stringToUTF8(str, buffer, length + 1);
21-
soljson.setValue(ptr, buffer, '*');
22-
};
23-
var wrapCallback = function (callback) {
24-
assert(typeof callback === 'function', 'Invalid callback specified.');
25-
return function (path, contents, error) {
26-
var result = callback(soljson.Pointer_stringify(path));
27-
if (typeof result.contents === 'string') {
28-
copyString(result.contents, contents);
29-
}
30-
if (typeof result.error === 'string') {
31-
copyString(result.error, error);
32-
}
33-
};
34-
};
9+
var copyString = function (str, ptr) {
10+
var length = soljson.lengthBytesUTF8(str);
11+
var buffer = soljson._malloc(length + 1);
12+
soljson.stringToUTF8(str, buffer, length + 1);
13+
soljson.setValue(ptr, buffer, '*');
14+
};
3515

36-
// This calls compile() with args || cb
37-
var runWithReadCallback = function (readCallback, compile, args) {
38-
if (readCallback === undefined) {
39-
readCallback = function (path) {
40-
return {
41-
error: 'File import callback not supported'
42-
};
43-
};
16+
var wrapCallback = function (callback) {
17+
assert(typeof callback === 'function', 'Invalid callback specified.');
18+
return function (path, contents, error) {
19+
var result = callback(soljson.Pointer_stringify(path));
20+
if (typeof result.contents === 'string') {
21+
copyString(result.contents, contents);
4422
}
45-
var cb = soljson.Runtime.addFunction(wrapCallback(readCallback));
46-
var output;
47-
try {
48-
args.push(cb);
49-
output = compile.apply(undefined, args);
50-
} catch (e) {
51-
soljson.Runtime.removeFunction(cb);
52-
throw e;
23+
if (typeof result.error === 'string') {
24+
copyString(result.error, error);
5325
}
54-
soljson.Runtime.removeFunction(cb);
55-
return output;
5626
};
27+
};
28+
29+
// This calls compile() with args || cb
30+
var runWithReadCallback = function (readCallback, compile, args) {
31+
if (readCallback === undefined) {
32+
readCallback = function (path) {
33+
return {
34+
error: 'File import callback not supported'
35+
};
36+
};
37+
}
38+
var cb = soljson.Runtime.addFunction(wrapCallback(readCallback));
39+
var output;
40+
try {
41+
args.push(cb);
42+
output = compile.apply(undefined, args);
43+
} catch (e) {
44+
soljson.Runtime.removeFunction(cb);
45+
throw e;
46+
}
47+
soljson.Runtime.removeFunction(cb);
48+
return output;
49+
};
50+
51+
var compileJSON = null;
52+
if ('_compileJSON' in soljson) {
53+
compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
54+
}
55+
56+
var compileJSONMulti = null;
57+
if ('_compileJSONMulti' in soljson) {
58+
compileJSONMulti = soljson.cwrap('compileJSONMulti', 'string', ['string', 'number']);
59+
}
5760

61+
var compileJSONCallback = null;
62+
if ('_compileJSONCallback' in soljson) {
5863
var compileInternal = soljson.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
5964
compileJSONCallback = function (input, optimize, readCallback) {
6065
return runWithReadCallback(readCallback, compileInternal, [ input, optimize ]);
6166
};
62-
if ('_compileStandard' in soljson) {
63-
var compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
64-
compileStandard = function (input, readCallback) {
65-
return runWithReadCallback(readCallback, compileStandardInternal, [ input ]);
66-
};
67-
}
68-
if ('_solidity_compile' in soljson) {
69-
var solidityCompile = soljson.cwrap('solidity_compile', 'string', ['string', 'number']);
70-
compileStandard = function (input, readCallback) {
71-
return runWithReadCallback(readCallback, solidityCompile, [ input ]);
72-
};
73-
}
67+
}
68+
69+
var compileStandard = null;
70+
if ('_compileStandard' in soljson) {
71+
var compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
72+
compileStandard = function (input, readCallback) {
73+
return runWithReadCallback(readCallback, compileStandardInternal, [ input ]);
74+
};
75+
}
76+
if ('_solidity_compile' in soljson) {
77+
var solidityCompile = soljson.cwrap('solidity_compile', 'string', ['string', 'number']);
78+
compileStandard = function (input, readCallback) {
79+
return runWithReadCallback(readCallback, solidityCompile, [ input ]);
80+
};
7481
}
7582

7683
var compile = function (input, optimise, readCallback) {

0 commit comments

Comments
 (0)