forked from ethereum/solc-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.js
200 lines (178 loc) · 6.7 KB
/
wrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var translate = require('./translate.js');
var linker = require('./linker.js');
var requireFromString = require('require-from-string');
var https = require('https');
var MemoryStream = require('memorystream');
function setupMethods (soljson) {
var compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
var compileJSONMulti = null;
if ('_compileJSONMulti' in soljson) {
compileJSONMulti = soljson.cwrap('compileJSONMulti', 'string', ['string', 'number']);
}
var compileJSONCallback = null;
var compileStandard = null;
if (('_compileJSONCallback' in soljson) || ('_compileStandard' in soljson)) {
var copyString = function (str, ptr) {
var buffer = soljson._malloc(str.length + 1);
soljson.writeStringToMemory(str, buffer);
soljson.setValue(ptr, buffer, '*');
};
var wrapCallback = function (callback) {
return function (path, contents, error) {
var result = callback(soljson.Pointer_stringify(path));
if (typeof result.contents === 'string') {
copyString(result.contents, contents);
}
if (typeof result.error === 'string') {
copyString(result.error, error);
}
};
};
// This calls compile() with args || cb
var runWithReadCallback = function (readCallback, compile, args) {
if (readCallback === undefined) {
readCallback = function (path) {
return {
error: 'File import callback not supported'
};
};
}
var cb = soljson.Runtime.addFunction(wrapCallback(readCallback));
var output;
try {
args.push(cb);
output = compile.apply(undefined, args);
} catch (e) {
soljson.Runtime.removeFunction(cb);
throw e;
}
soljson.Runtime.removeFunction(cb);
return output;
};
var compileInternal = soljson.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
compileJSONCallback = function (input, optimize, readCallback) {
return runWithReadCallback(readCallback, compileInternal, [ input, optimize ]);
};
if ('_compileStandard' in soljson) {
var compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
compileStandard = function (input, readCallback) {
return runWithReadCallback(readCallback, compileStandardInternal, [ input ]);
};
}
}
var compile = function (input, optimise, readCallback) {
var result = '';
if (readCallback !== undefined && compileJSONCallback !== null) {
result = compileJSONCallback(JSON.stringify(input), optimise, readCallback);
} else if (typeof input !== 'string' && compileJSONMulti !== null) {
result = compileJSONMulti(JSON.stringify(input), optimise);
} else {
result = compileJSON(input, optimise);
}
return JSON.parse(result);
};
// Expects a Standard JSON I/O but supports old compilers
var compileStandardWrapper = function (input, readCallback) {
if (compileStandard !== null) {
return compileStandard(input, readCallback);
}
function formatFatalError (message) {
return JSON.stringify({
errors: [
{
'type': 'SOLCError',
'component': 'solcjs',
'severity': 'error',
'message': message,
'formattedMessage': 'Error' + message
}
]
});
}
input = JSON.parse(input);
if (input['language'] !== 'Solidity') {
return formatFatalError('Only Solidity sources are supported');
}
if (input['sources'] == null) {
return formatFatalError('No input specified');
}
// Bail out early
if ((input['sources'].length > 1) && (compileJSONMulti === null)) {
return formatFatalError('Multiple sources provided, but compiler only supports single input');
}
function isOptimizerEnabled (input) {
return input['settings'] && input['settings']['optimizer'] && input['settings']['optimizer']['enabled'];
}
function translateSources (input) {
var sources = {};
for (var source in input['sources']) {
if (input['sources'][source]['content'] !== null) {
sources[source] = input['sources'][source]['content'];
} else {
// force failure
return null;
}
}
return sources;
}
function translateOutput (output) {
output = translate.translateJsonCompilerOutput(JSON.parse(output));
if (output == null) {
return formatFatalError('Failed to process output');
}
return JSON.stringify(output);
}
var sources = translateSources(input);
if (sources === null || Object.keys(sources).length === 0) {
return formatFatalError('Failed to process sources');
}
// Try to wrap around old versions
if (compileJSONCallback !== null) {
return translateOutput(compileJSONCallback(JSON.stringify({ 'sources': sources }), isOptimizerEnabled(input), readCallback));
}
if (compileJSONMulti !== null) {
return translateOutput(compileJSONMulti(JSON.stringify({ 'sources': sources }), isOptimizerEnabled(input)));
}
// Try our luck with an ancient compiler
return translateOutput(compileJSON(sources[Object.keys(sources)[0]], isOptimizerEnabled(input)));
};
var version = soljson.cwrap('version', 'string', []);
var license = function () {
// return undefined
};
if ('_license' in soljson) {
license = soljson.cwrap('license', 'string', []);
}
return {
version: version,
license: license,
compile: compile,
compileStandard: compileStandard,
compileStandardWrapper: compileStandardWrapper,
linkBytecode: linker.linkBytecode,
supportsMulti: compileJSONMulti !== null,
supportsImportCallback: compileJSONCallback !== null,
supportsStandard: compileStandard !== null,
// Loads the compiler of the given version from the github repository
// instead of from the local filesystem.
loadRemoteVersion: function (versionString, cb) {
var mem = new MemoryStream(null, {readable: false});
var url = 'https://ethereum.github.io/solc-bin/bin/soljson-' + versionString + '.js';
https.get(url, function (response) {
if (response.statusCode !== 200) {
cb(new Error('Error retrieving binary: ' + response.statusMessage));
} else {
response.pipe(mem);
response.on('end', function () {
cb(null, setupMethods(requireFromString(mem.toString(), 'soljson-' + versionString + '.js')));
});
}
}).on('error', function (error) {
cb(error);
});
},
// Use this if you want to add wrapper functions around the pure module.
setupMethods: setupMethods
};
}
module.exports = setupMethods;