-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.js
259 lines (228 loc) · 8.39 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var assert = require('assert');
var translate = require('./translate.js');
var requireFromString = require('require-from-string');
var https = require('https');
var MemoryStream = require('memorystream');
function setupMethods (soljson) {
var copyString = function (str, ptr) {
var length = soljson.lengthBytesUTF8(str);
var buffer = soljson._malloc(length + 1);
soljson.stringToUTF8(str, buffer, length + 1);
soljson.setValue(ptr, buffer, '*');
};
var wrapCallback = function (callback) {
assert(typeof callback === 'function', 'Invalid callback specified.');
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'
};
};
}
// This is to support multiple versions of Emscripten.
var addFunction = soljson.addFunction || soljson.Runtime.addFunction;
var removeFunction = soljson.removeFunction || soljson.Runtime.removeFunction;
var cb = addFunction(wrapCallback(readCallback));
var output;
try {
args.push(cb);
output = compile.apply(undefined, args);
} catch (e) {
removeFunction(cb);
throw e;
}
removeFunction(cb);
return output;
};
var compileJSON = null;
if ('_compileJSON' in soljson) {
compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
}
var compileJSONMulti = null;
if ('_compileJSONMulti' in soljson) {
compileJSONMulti = soljson.cwrap('compileJSONMulti', 'string', ['string', 'number']);
}
var compileJSONCallback = null;
if ('_compileJSONCallback' in soljson) {
var compileInternal = soljson.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
compileJSONCallback = function (input, optimize, readCallback) {
return runWithReadCallback(readCallback, compileInternal, [ input, optimize ]);
};
}
var compileStandard = null;
if ('_compileStandard' in soljson) {
var compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
compileStandard = function (input, readCallback) {
return runWithReadCallback(readCallback, compileStandardInternal, [ input ]);
};
}
if ('_solidity_compile' in soljson) {
var solidityCompile = soljson.cwrap('solidity_compile', 'string', ['string', 'number']);
compileStandard = function (input, readCallback) {
return runWithReadCallback(readCallback, solidityCompile, [ input ]);
};
}
// 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': 'dsolcjs',
'severity': 'error',
'message': message,
'formattedMessage': 'Error: ' + message
}
]
});
}
if (readCallback !== undefined && typeof readCallback !== 'function') {
return formatFatalError('Invalid import callback supplied');
}
try {
input = JSON.parse(input);
} catch (e) {
return formatFatalError('Invalid JSON supplied: ' + e.message);
}
if (input['language'] !== 'Solidity') {
return formatFatalError('Only Solidity sources are supported');
}
// NOTE: this is deliberately `== null`
if (input['sources'] == null || input['sources'].length === 0) {
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 librariesSupplied (input) {
if (input['settings'] !== null) {
return input['settings']['libraries'];
}
}
function translateOutput (output, libraries) {
try {
output = JSON.parse(output);
} catch (e) {
return formatFatalError('Compiler returned invalid JSON: ' + e.message);
}
output = translate.translateJsonCompilerOutput(output, libraries);
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 linking if libraries were supplied
var libraries = librariesSupplied(input);
// Try to wrap around old versions
if (compileJSONCallback !== null) {
return translateOutput(compileJSONCallback(JSON.stringify({ 'sources': sources }), isOptimizerEnabled(input), readCallback), libraries);
}
if (compileJSONMulti !== null) {
return translateOutput(compileJSONMulti(JSON.stringify({ 'sources': sources }), isOptimizerEnabled(input)), libraries);
}
// Try our luck with an ancient compiler
if (compileJSON !== null) {
return translateOutput(compileJSON(sources[Object.keys(sources)[0]], isOptimizerEnabled(input)), libraries);
}
return formatFatalError('Compiler does not support any known interface.');
};
var version;
if ('_solidity_version' in soljson) {
version = soljson.cwrap('solidity_version', 'string', []);
} else {
version = soljson.cwrap('version', 'string', []);
}
var versionToSemver = function () {
return translate.versionToSemver(version());
};
var license;
if ('_solidity_license' in soljson) {
license = soljson.cwrap('solidity_license', 'string', []);
} else if ('_license' in soljson) {
license = soljson.cwrap('license', 'string', []);
} else {
// pre 0.4.14
license = function () {
// return undefined
};
}
return {
version: version,
semver: versionToSemver,
license: license,
lowlevel: {
compileSingle: compileJSON,
compileMulti: compileJSONMulti,
compileCallback: compileJSONCallback,
compileStandard: compileStandard
},
features: {
legacySingleInput: compileJSON !== null,
multipleInputs: compileJSONMulti !== null || compileStandard !== null,
importCallback: compileJSONCallback !== null || compileStandard !== null,
nativeStandardJSON: compileStandard !== null
},
compile: compileStandardWrapper,
// Temporary wrappers to minimise breaking with other projects.
// NOTE: to be removed in 0.5.2
compileStandard: compileStandardWrapper,
compileStandardWrapper: compileStandardWrapper,
// 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://dexon-foundation.github.io/dsolc-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;