-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathwrapper.ts
More file actions
executable file
·449 lines (394 loc) · 15.2 KB
/
wrapper.ts
File metadata and controls
executable file
·449 lines (394 loc) · 15.2 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import translate from './translate';
import { https } from 'follow-redirects';
import MemoryStream from 'memorystream';
import assert from 'assert';
import * as semver from 'semver';
import { Callbacks } from './common/interfaces';
const Module = module.constructor as any;
function setupMethods (soljson) {
let version;
if ('_solidity_version' in soljson) {
version = soljson.cwrap('solidity_version', 'string', []);
} else {
version = soljson.cwrap('version', 'string', []);
}
const versionToSemver = function () {
return translate.versionToSemver(version());
};
const isVersion6 = semver.gt(versionToSemver(), '0.5.99');
let 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
};
}
let alloc;
if ('_solidity_alloc' in soljson) {
alloc = soljson.cwrap('solidity_alloc', 'number', ['number']);
} else {
alloc = soljson._malloc;
assert(alloc, 'Expected malloc to be present.');
}
let reset;
if ('_solidity_reset' in soljson) {
reset = soljson.cwrap('solidity_reset', null, []);
}
// Copies the string at @p str to @p ptr.
const copyToCString = function (str, ptr) {
const length = soljson.lengthBytesUTF8(str);
// This is allocating memory using solc's allocator.
//
// Before 0.6.0:
// Assuming copyToCString is only used in the context of wrapCallback, solc will free these pointers.
// See https://github.com/ethereum/solidity/blob/v0.5.13/libsolc/libsolc.h#L37-L40
//
// After 0.6.0:
// The duty is on solc-js to free these pointers. We accomplish that by calling `reset` at the end.
const buffer = alloc(length + 1);
soljson.stringToUTF8(str, buffer, length + 1);
soljson.setValue(ptr, buffer, '*');
};
// Creates a wrapper around `int solidity_lsp_start(callbacks: Callbacks)`.
const createWrappedLspStart = function () {
if (!('_solidity_lsp_start' in soljson)) {
return () => {
throw new Error('lsp is not supported on this version.');
};
}
const wrappedLspStart = soljson.cwrap('solidity_lsp_start', 'number', ['number']);
return function (callbacks: Callbacks) {
const readCallback = callbacks.import;
assert(typeof readCallback === 'function', 'Invalid callback specified.');
const copyFromCString = soljson.UTF8ToString || soljson.Pointer_stringify;
const wrappedReadCallback = function (path: string, contents: string, error: string) {
console.log("wrappedReadCallback: \"" + path + "\"");
// Calls the user-supplied file read callback and passes the return values
// accordingly to either @p contents or into @p error on failure.
const result = readCallback(copyFromCString(path));
if (typeof result.contents === 'string') {
copyToCString(result.contents, contents);
}
if (typeof result.error === 'string') {
copyToCString(result.error, error);
}
};
const addFunction = soljson.addFunction || soljson.Runtime.addFunction;
const removeFunction = soljson.removeFunction || soljson.Runtime.removeFunction;
const wrappedReadCallbackId = addFunction(wrappedReadCallback, 'ii');
try {
// call solidity_lsp_start(callbacks)
const output = wrappedLspStart(wrappedReadCallbackId);
removeFunction(wrappedReadCallbackId);
return output;
} catch (e) {
removeFunction(wrappedReadCallbackId);
throw e;
}
// NOTE: We MUST NOT reset the compiler here.
// We instead could try to make sure to only release memory that is safe
// to be released. Probably by clearly defining semantics and memory
// lifetimes of output strings.
};
};
// C signature : int solidity_lsp_send(char const* jsonRpcInputObject);
// TS signature : int send(object jsonRpcInputObject);
const createWrappedLspSend = function () {
if (!('_solidity_lsp_send' in soljson)) {
return () => {
throw new Error('lsp is not supported on this version.');
};
}
const wrappedLspSend = soljson.cwrap('solidity_lsp_send', 'number', ['string']);
return (input: string) => wrappedLspSend(JSON.stringify(input));
};
// C signature : char* solidity_lsp_send_receive(char const* jsonRpcInputObject);
// TS signature : object sendReceive(object jsonRpcInputObject);
//
// sendReceive send one message to the LSP server (notification or method call).
// The method call may reply with zero or one message that is going to be returned.
const createWrappedLspSendReceive = function () {
if (!('_solidity_lsp_send_receive' in soljson)) {
return () => {
throw new Error('lsp is not supported on this version.');
};
}
const wrappedLspSendReceive = soljson.cwrap('solidity_lsp_send_receive', 'string', ['string']);
return (input: string) => JSON.parse(wrappedLspSendReceive(JSON.stringify(input)));
};
// This is to support multiple versions of Emscripten.
// Take a single `ptr` and returns a `str`.
const copyFromCString = soljson.UTF8ToString || soljson.Pointer_stringify;
const wrapCallback = function (callback) {
assert(typeof callback === 'function', 'Invalid callback specified.');
return function (data, contents, error) {
const result = callback(copyFromCString(data));
if (typeof result.contents === 'string') {
copyToCString(result.contents, contents);
}
if (typeof result.error === 'string') {
copyToCString(result.error, error);
}
};
};
const wrapCallbackWithKind = function (callback) {
assert(typeof callback === 'function', 'Invalid callback specified.');
return function (context, kind, data, contents, error) {
// Must be a null pointer.
assert(context === 0, 'Callback context must be null.');
const result = callback(copyFromCString(kind), copyFromCString(data));
if (typeof result.contents === 'string') {
copyToCString(result.contents, contents);
}
if (typeof result.error === 'string') {
copyToCString(result.error, error);
}
};
};
// This calls compile() with args || cb
const runWithCallbacks = function (callbacks, compile, args) {
if (callbacks) {
assert(typeof callbacks === 'object', 'Invalid callback object specified.');
} else {
callbacks = {};
}
let readCallback = callbacks.import;
if (readCallback === undefined) {
readCallback = function (data) {
return {
error: 'File import callback not supported'
};
};
}
let singleCallback;
if (isVersion6) {
// After 0.6.x multiple kind of callbacks are supported.
let smtSolverCallback = callbacks.smtSolver;
if (smtSolverCallback === undefined) {
smtSolverCallback = function (data) {
return {
error: 'SMT solver callback not supported'
};
};
}
singleCallback = function (kind, data) {
if (kind === 'source') {
return readCallback(data);
} else if (kind === 'smt-query') {
return smtSolverCallback(data);
} else {
assert(false, 'Invalid callback kind specified.');
}
};
singleCallback = wrapCallbackWithKind(singleCallback);
} else {
// Old Solidity version only supported imports.
singleCallback = wrapCallback(readCallback);
}
// This is to support multiple versions of Emscripten.
const addFunction = soljson.addFunction || soljson.Runtime.addFunction;
const removeFunction = soljson.removeFunction || soljson.Runtime.removeFunction;
const cb = addFunction(singleCallback, 'viiiii');
let output;
try {
args.push(cb);
if (isVersion6) {
// Callback context.
args.push(null);
}
output = compile.apply(undefined, args);
} catch (e) {
removeFunction(cb);
throw e;
}
removeFunction(cb);
if (reset) {
// Explicitly free memory.
//
// NOTE: cwrap() of "compile" will copy the returned pointer into a
// Javascript string and it is not possible to call free() on it.
// reset() however will clear up all allocations.
reset();
}
return output;
};
let compileJSON = null;
if ('_compileJSON' in soljson) {
// input (text), optimize (bool) -> output (jsontext)
compileJSON = soljson.cwrap('compileJSON', 'string', ['string', 'number']);
}
let compileJSONMulti = null;
if ('_compileJSONMulti' in soljson) {
// input (jsontext), optimize (bool) -> output (jsontext)
compileJSONMulti = soljson.cwrap('compileJSONMulti', 'string', ['string', 'number']);
}
let compileJSONCallback = null;
if ('_compileJSONCallback' in soljson) {
// input (jsontext), optimize (bool), callback (ptr) -> output (jsontext)
const compileInternal = soljson.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
compileJSONCallback = function (input, optimize, readCallback) {
return runWithCallbacks(readCallback, compileInternal, [input, optimize]);
};
}
let compileStandard = null;
if ('_compileStandard' in soljson) {
// input (jsontext), callback (ptr) -> output (jsontext)
const compileStandardInternal = soljson.cwrap('compileStandard', 'string', ['string', 'number']);
compileStandard = function (input, readCallback) {
return runWithCallbacks(readCallback, compileStandardInternal, [input]);
};
}
if ('_solidity_compile' in soljson) {
let solidityCompile;
if (isVersion6) {
// input (jsontext), callback (ptr), callback_context (ptr) -> output (jsontext)
solidityCompile = soljson.cwrap('solidity_compile', 'string', ['string', 'number', 'number']);
} else {
// input (jsontext), callback (ptr) -> output (jsontext)
solidityCompile = soljson.cwrap('solidity_compile', 'string', ['string', 'number']);
}
compileStandard = function (input, callbacks) {
return runWithCallbacks(callbacks, solidityCompile, [input]);
};
}
// Expects a Standard JSON I/O but supports old compilers
const compileStandardWrapper = function (input, readCallback?: any) {
if (compileStandard !== null) {
return compileStandard(input, readCallback);
}
function formatFatalError (message) {
return JSON.stringify({
errors: [
{
type: 'JSONError',
component: 'solcjs',
severity: 'error',
message: message,
formattedMessage: 'Error: ' + message
}
]
});
}
try {
input = JSON.parse(input);
} catch (e) {
return formatFatalError('Invalid JSON supplied: ' + e.message);
}
if (input.language !== 'Solidity') {
return formatFatalError('Only "Solidity" is supported as a language.');
}
// NOTE: this is deliberately `== null`
if (input.sources == null || input.sources.length === 0) {
return formatFatalError('No input sources specified.');
}
function isOptimizerEnabled (input) {
return input.settings && input.settings.optimizer && input.settings.optimizer.enabled;
}
function translateSources (input) {
const sources = {};
for (const 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) {
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);
}
const sources = translateSources(input);
if (sources === null || Object.keys(sources).length === 0) {
return formatFatalError('Failed to process sources.');
}
// Try linking if libraries were supplied
const 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) {
if (Object.keys(sources).length !== 1) {
return formatFatalError('Multiple sources provided, but compiler only supports single input.');
}
return translateOutput(compileJSON(sources[Object.keys(sources)[0]], isOptimizerEnabled(input)), libraries);
}
return formatFatalError('Compiler does not support any known interface.');
};
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
},
lsp: {
start: createWrappedLspStart(),
send: createWrappedLspSend(),
sendReceive: createWrappedLspSendReceive()
},
compile: compileStandardWrapper,
// Loads the compiler of the given version from the github repository
// instead of from the local filesystem.
loadRemoteVersion: function (versionString, cb) {
const mem = new MemoryStream(null, { readable: false });
const url = 'https://binaries.soliditylang.org/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 () {
// Based on the require-from-string package.
const soljson = new Module();
soljson._compile(mem.toString(), 'soljson-' + versionString + '.js');
if (module.parent && module.parent.children) {
// Make sure the module is plugged into the hierarchy correctly to have parent
// properly garbage collected.
module.parent.children.splice(module.parent.children.indexOf(soljson), 1);
}
cb(null, setupMethods(soljson.exports));
});
}
}).on('error', function (error) {
cb(error);
});
},
// Use this if you want to add wrapper functions around the pure module.
setupMethods: setupMethods
};
}
export = setupMethods;