Skip to content

Commit aed930d

Browse files
committed
Move runWithCallback outside of the nested in JSONCallback
1 parent e50605a commit aed930d

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

wrapper.js

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

88
function setupMethods (soljson) {
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+
};
15+
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);
22+
}
23+
if (typeof result.error === 'string') {
24+
copyString(result.error, error);
25+
}
26+
};
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+
951
var compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
1052
var compileJSONMulti = null;
1153
if ('_compileJSONMulti' in soljson) {
@@ -14,47 +56,6 @@ function setupMethods (soljson) {
1456
var compileJSONCallback = null;
1557
var compileStandard = null;
1658
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-
};
35-
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-
};
44-
}
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;
53-
}
54-
soljson.Runtime.removeFunction(cb);
55-
return output;
56-
};
57-
5859
var compileInternal = soljson.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
5960
compileJSONCallback = function (input, optimize, readCallback) {
6061
return runWithReadCallback(readCallback, compileInternal, [ input, optimize ]);

0 commit comments

Comments
 (0)