-
Notifications
You must be signed in to change notification settings - Fork 456
/
compiler.ts
902 lines (826 loc) · 30.7 KB
/
compiler.ts
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
import assert from 'assert';
import tape from 'tape';
import * as semver from 'semver';
import * as tmp from 'tmp';
import solc from '../';
import linker from '../linker';
import { execSync } from 'child_process';
import wrapper from '../wrapper';
const noRemoteVersions = (process.argv.indexOf('--no-remote-versions') >= 0);
function runTests (solc, versionText) {
console.log(`Running tests with ${versionText} ${solc.version()}`);
function resplitFileNameOnFirstColon (fileName, contractName) {
assert(!contractName.includes(':'));
const contractNameComponents = fileName.split(':');
const truncatedFileName = contractNameComponents.shift();
contractNameComponents.push(contractName);
return [truncatedFileName, contractNameComponents.join(':')];
}
function getBytecode (output, fileName, contractName) {
try {
let outputContract;
if (semver.lt(solc.semver(), '0.4.9')) {
outputContract = output.contracts[contractName];
} else {
outputContract = output.contracts[fileName + ':' + contractName];
}
return outputContract.bytecode;
} catch (e) {
return '';
}
}
function getBytecodeStandard (output, fileName, contractName) {
try {
let outputFile;
if (semver.lt(solc.semver(), '0.4.9')) {
outputFile = output.contracts[''];
} else {
if (semver.gt(solc.semver(), '0.4.10') && semver.lt(solc.semver(), '0.4.20')) {
[fileName, contractName] = resplitFileNameOnFirstColon(fileName, contractName);
}
outputFile = output.contracts[fileName];
}
return outputFile[contractName].evm.bytecode.object;
} catch (e) {
return '';
}
}
function getGasEstimate (output, fileName, contractName) {
try {
let outputFile;
if (semver.lt(solc.semver(), '0.4.9')) {
outputFile = output.contracts[''];
} else {
if (semver.gt(solc.semver(), '0.4.10') && semver.gt(solc.semver(), '0.4.20')) {
[fileName, contractName] = resplitFileNameOnFirstColon(fileName, contractName);
}
outputFile = output.contracts[fileName];
}
return outputFile[contractName].evm.gasEstimates;
} catch (e) {
return '';
}
}
function expectError (output: any, errorType: any, message: any) {
if (output.errors) {
for (const errorIndex in output.errors) {
const error = output.errors[errorIndex];
if (error.type === errorType) {
if (message) {
if (error.message === message || error.message.match(message) !== null) {
return true;
}
} else {
return true;
}
}
}
}
return false;
}
function expectNoError (output: any) {
if (output.errors) {
for (const errorIndex in output.errors) {
const error = output.errors[errorIndex];
if (error.severity === 'error') {
return false;
}
}
}
return true;
}
tape(versionText, function (t) {
const tape = t.test;
tape('Version and license', function (t) {
t.test('check version', function (st) {
st.equal(typeof solc.version(), 'string');
st.end();
});
t.test('check semver', function (st) {
st.equal(typeof solc.semver(), 'string');
st.end();
});
t.test('check license', function (st) {
st.ok(typeof solc.license() === 'undefined' || typeof solc.license() === 'string');
st.end();
});
});
tape('Compilation', function (t) {
t.test('single files can be compiled (using lowlevel API)', function (st) {
if (typeof solc.lowlevel.compileSingle !== 'function') {
st.skip('Low-level compileSingle interface not implemented by this compiler version.');
st.end();
return;
}
const output = JSON.parse(solc.lowlevel.compileSingle('contract A { function g() public {} }'));
st.ok('contracts' in output);
const bytecode = getBytecode(output, '', 'A');
st.ok(typeof bytecode === 'string');
st.ok(bytecode.length > 0);
st.end();
});
t.test('invalid source code fails properly (using lowlevel API)', function (st) {
// TODO: try finding an example which doesn't crash it?
if (semver.eq(solc.semver(), '0.4.11')) {
st.skip('Skipping on broken compiler version');
st.end();
return;
}
if (typeof solc.lowlevel.compileSingle !== 'function') {
st.skip('Low-level compileSingle interface not implemented by this compiler version.');
st.end();
return;
}
const output = JSON.parse(solc.lowlevel.compileSingle('contract x { this is an invalid contract }'));
if (semver.lt(solc.semver(), '0.1.4')) {
st.ok(output.error.indexOf('Parser error: Expected identifier') !== -1);
st.end();
return;
}
st.plan(3);
st.ok('errors' in output);
// Check if the ParserError exists, but allow others too
st.ok(output.errors.length >= 1);
for (const error in output.errors) {
// Error should be something like:
// ParserError
// Error: Expected identifier
// Parser error: Expected identifier
if (
output.errors[error].indexOf('ParserError') !== -1 ||
output.errors[error].indexOf('Error: Expected identifier') !== -1 ||
output.errors[error].indexOf('Parser error: Expected identifier') !== -1 ||
output.errors[error].indexOf(': Expected identifier') !== -1 // 0.4.12
) {
st.ok(true);
}
}
st.end();
});
t.test('multiple files can be compiled (using lowlevel API)', function (st) {
// <0.1.6 doesn't have this
if (typeof solc.lowlevel.compileMulti !== 'function') {
st.skip('Low-level compileMulti interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
'a.sol': 'contract A { function f() public returns (uint) { return 7; } }',
'b.sol': 'import "a.sol"; contract B is A { function g() public { f(); } }'
};
const output = JSON.parse(solc.lowlevel.compileMulti(JSON.stringify({ sources: input })));
const B = getBytecode(output, 'b.sol', 'B');
st.ok(typeof B === 'string');
st.ok(B.length > 0);
const A = getBytecode(output, 'a.sol', 'A');
st.ok(typeof A === 'string');
st.ok(A.length > 0);
st.end();
});
t.test('lazy-loading callback works (using lowlevel API)', function (st) {
// <0.2.1 doesn't have this
if (typeof solc.lowlevel.compileCallback !== 'function') {
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
'b.sol': 'import "a.sol"; contract B is A { function g() public { f(); } }'
};
function findImports (path) {
if (path === 'a.sol') {
return { contents: 'contract A { function f() public returns (uint) { return 7; } }' };
} else {
return { error: 'File not found' };
}
}
const output = JSON.parse(solc.lowlevel.compileCallback(JSON.stringify({ sources: input }), 0, { import: findImports }));
const B = getBytecode(output, 'b.sol', 'B');
st.ok(typeof B === 'string');
st.ok(B.length > 0);
const A = getBytecode(output, 'a.sol', 'A');
st.ok(typeof A === 'string');
st.ok(A.length > 0);
st.end();
});
t.test('lazy-loading callback works (with file not found) (using lowlevel API)', function (st) {
// <0.2.1 doesn't have this
if (typeof solc.lowlevel.compileCallback !== 'function') {
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
'b.sol': 'import "a.sol"; contract B { function g() public { f(); } }'
};
function findImports (path) {
return { error: 'File not found' };
}
const output = JSON.parse(solc.lowlevel.compileCallback(JSON.stringify({ sources: input }), 0, { import: findImports }));
st.plan(3);
st.ok('errors' in output);
// Check if the ParserError exists, but allow others too
st.ok(output.errors.length >= 1);
for (const error in output.errors) {
// Error should be something like:
// cont.sol:1:1: ParserError: Source "lib.sol" not found: File not found
// cont.sol:1:1: Error: Source "lib.sol" not found: File not found
if (output.errors[error].indexOf('Error') !== -1 && output.errors[error].indexOf('File not found') !== -1) {
st.ok(true);
} else if (output.errors[error].indexOf('not found: File not found') !== -1) {
// 0.4.12 had its own weird way:
// b.sol:1:1: : Source "a.sol" not found: File not found
st.ok(true);
}
}
st.end();
});
t.test('lazy-loading callback works (with exception) (using lowlevel API)', function (st) {
// <0.2.1 doesn't have this
if (typeof solc.lowlevel.compileCallback !== 'function') {
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
'b.sol': 'import "a.sol"; contract B { function g() public { f(); } }'
};
function findImports (path) {
throw new Error('Could not implement this interface properly...');
}
st.throws(function () {
solc.lowlevel.compileCallback(JSON.stringify({ sources: input }), 0, { import: findImports });
}, /^Error: Could not implement this interface properly.../);
st.end();
});
t.test('lazy-loading callback fails properly (with invalid callback) (using lowlevel API)', function (st) {
// <0.2.1 doesn't have this
if (typeof solc.lowlevel.compileCallback !== 'function') {
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
'cont.sol': 'import "lib.sol"; contract x { function g() public { L.f(); } }'
};
st.throws(function () {
solc.lowlevel.compileCallback(JSON.stringify({ sources: input }), 0, 'this isn\'t a callback');
}, /Invalid callback object specified./);
st.end();
});
t.test('file import without lazy-loading callback fails properly (using lowlevel API)', function (st) {
// <0.2.1 doesn't have this
if (typeof solc.lowlevel.compileCallback !== 'function') {
st.skip('Low-level compileCallback interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
'b.sol': 'import "a.sol"; contract B is A { function g() public { f(); } }'
};
const output = JSON.parse(solc.lowlevel.compileCallback(JSON.stringify({ sources: input })));
st.plan(3);
st.ok('errors' in output);
// Check if the ParserError exists, but allow others too
st.ok(output.errors.length >= 1);
for (const error in output.errors) {
// Error should be something like:
// cont.sol:1:1: ParserError: Source "lib.sol" not found: File import callback not supported
// cont.sol:1:1: Error: Source "lib.sol" not found: File import callback not supported
if (output.errors[error].indexOf('Error') !== -1 && output.errors[error].indexOf('File import callback not supported') !== -1) {
st.ok(true);
} else if (output.errors[error].indexOf('not found: File import callback not supported') !== -1) {
// 0.4.12 had its own weird way:
// b.sol:1:1: : Source "a.sol" not found: File import callback not supported
st.ok(true);
}
}
st.end();
});
t.test('compiling standard JSON (using lowlevel API)', function (st) {
if (typeof solc.lowlevel.compileStandard !== 'function') {
st.skip('Low-level compileStandard interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'a.sol': {
content: 'contract A { function f() public returns (uint) { return 7; } }'
},
'b.sol': {
content: 'import "a.sol"; contract B is A { function g() public { f(); } }'
}
}
};
function bytecodeExists (output, fileName, contractName) {
try {
return output.contracts[fileName][contractName].evm.bytecode.object.length > 0;
} catch (e) {
return false;
}
}
const output = JSON.parse(solc.lowlevel.compileStandard(JSON.stringify(input)));
st.ok(bytecodeExists(output, 'a.sol', 'A'));
st.ok(bytecodeExists(output, 'b.sol', 'B'));
st.end();
});
t.test('invalid source code fails properly with standard JSON (using lowlevel API)', function (st) {
// TODO: try finding an example which doesn't crash it?
if (semver.eq(solc.semver(), '0.4.11')) {
st.skip('Skipping on broken compiler version');
st.end();
return;
}
if (typeof solc.lowlevel.compileStandard !== 'function') {
st.skip('Low-level compileStandard interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'x.sol': {
content: 'contract x { this is an invalid contract }'
}
}
};
const output = JSON.parse(solc.lowlevel.compileStandard(JSON.stringify(input)));
st.plan(3);
st.ok('errors' in output);
st.ok(output.errors.length >= 1);
// Check if the ParserError exists, but allow others too
for (const error in output.errors) {
if (output.errors[error].type === 'ParserError') {
st.ok(true);
}
}
st.end();
});
t.test('compiling standard JSON (with callback) (using lowlevel API)', function (st) {
if (typeof solc.lowlevel.compileStandard !== 'function') {
st.skip('Low-level compileStandard interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'b.sol': {
content: 'import "a.sol"; contract B is A { function g() public { f(); } }'
}
}
};
function findImports (path) {
if (path === 'a.sol') {
return { contents: 'contract A { function f() public returns (uint) { return 7; } }' };
} else {
return { error: 'File not found' };
}
}
function bytecodeExists (output, fileName, contractName) {
try {
return output.contracts[fileName][contractName].evm.bytecode.object.length > 0;
} catch (e) {
return false;
}
}
const output = JSON.parse(solc.lowlevel.compileStandard(JSON.stringify(input), { import: findImports }));
st.ok(bytecodeExists(output, 'a.sol', 'A'));
st.ok(bytecodeExists(output, 'b.sol', 'B'));
st.end();
});
t.test('compiling standard JSON (single file)', function (st) {
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode', 'evm.gasEstimates']
}
}
},
sources: {
'c.sol': {
content: 'contract C { function g() public { } function h() internal {} }'
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectNoError(output));
const C = getBytecodeStandard(output, 'c.sol', 'C');
st.ok(typeof C === 'string');
st.ok(C.length > 0);
const CGas = getGasEstimate(output, 'c.sol', 'C');
st.ok(typeof CGas === 'object');
st.ok(typeof CGas.creation === 'object');
st.ok(typeof CGas.creation.codeDepositCost === 'string');
st.ok(typeof CGas.external === 'object');
st.ok(typeof CGas.external['g()'] === 'string');
st.ok(typeof CGas.internal === 'object');
st.ok(typeof CGas.internal['h()'] === 'string');
st.end();
});
t.test('compiling standard JSON (multiple files)', function (st) {
// <0.1.6 doesn't have this
if (!solc.features.multipleInputs) {
st.skip('Not supported by solc');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode', 'evm.gasEstimates']
}
}
},
sources: {
'a.sol': {
content: 'contract A { function f() public returns (uint) { return 7; } }'
},
'b.sol': {
content: 'import "a.sol"; contract B is A { function g() public { f(); } function h() internal {} }'
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectNoError(output));
const B = getBytecodeStandard(output, 'b.sol', 'B');
st.ok(typeof B === 'string');
st.ok(B.length > 0);
st.ok(Object.keys(linker.findLinkReferences(B)).length === 0);
const BGas = getGasEstimate(output, 'b.sol', 'B');
st.ok(typeof BGas === 'object');
st.ok(typeof BGas.creation === 'object');
st.ok(typeof BGas.creation.codeDepositCost === 'string');
st.ok(typeof BGas.external === 'object');
st.ok(typeof BGas.external['g()'] === 'string');
st.ok(typeof BGas.internal === 'object');
st.ok(typeof BGas.internal['h()'] === 'string');
const A = getBytecodeStandard(output, 'a.sol', 'A');
st.ok(typeof A === 'string');
st.ok(A.length > 0);
st.end();
});
t.test('compiling standard JSON (abstract contract)', function (st) {
// <0.1.6 doesn't have this
if (!solc.features.multipleInputs) {
st.skip('Not supported by solc');
st.end();
return;
}
const isVersion6 = semver.gt(solc.semver(), '0.5.99');
let source;
if (isVersion6) {
source = 'abstract contract C { function f() public virtual; }';
} else {
source = 'contract C { function f() public; }';
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode', 'evm.gasEstimates']
}
}
},
sources: {
'c.sol': {
content: source
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectNoError(output));
const C = getBytecodeStandard(output, 'c.sol', 'C');
st.ok(typeof C === 'string');
st.ok(C.length === 0);
st.end();
});
t.test('compiling standard JSON (with imports)', function (st) {
// <0.2.1 doesn't have this
if (!solc.features.importCallback) {
st.skip('Not supported by solc');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'b.sol': {
content: 'import "a.sol"; contract B is A { function g() public { f(); } }'
}
}
};
function findImports (path) {
if (path === 'a.sol') {
return { contents: 'contract A { function f() public returns (uint) { return 7; } }' };
} else {
return { error: 'File not found' };
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }));
st.ok(expectNoError(output));
const A = getBytecodeStandard(output, 'a.sol', 'A');
st.ok(typeof A === 'string');
st.ok(A.length > 0);
const B = getBytecodeStandard(output, 'b.sol', 'B');
st.ok(typeof B === 'string');
st.ok(B.length > 0);
st.ok(Object.keys(linker.findLinkReferences(B)).length === 0);
st.end();
});
t.test('compiling standard JSON (using libraries)', function (st) {
// 0.4.0 has a bug with libraries
if (semver.eq(solc.semver(), '0.4.0')) {
st.skip('Skipping on broken compiler version');
st.end();
return;
}
// <0.1.6 doesn't have this
if (!solc.features.multipleInputs) {
st.skip('Not supported by solc');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
libraries: {
'lib.sol': {
L: '0x4200000000000000000000000000000000000001'
}
},
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'lib.sol': {
content: 'library L { function f() public returns (uint) { return 7; } }'
},
'a.sol': {
content: 'import "lib.sol"; contract A { function g() public { L.f(); } }'
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectNoError(output));
const A = getBytecodeStandard(output, 'a.sol', 'A');
st.ok(typeof A === 'string');
st.ok(A.length > 0);
st.ok(Object.keys(linker.findLinkReferences(A)).length === 0);
const L = getBytecodeStandard(output, 'lib.sol', 'L');
st.ok(typeof L === 'string');
st.ok(L.length > 0);
st.end();
});
t.test('compiling standard JSON (with warning >=0.4.0)', function (st) {
// In 0.4.0 "pragma solidity" was added. Not including it is a warning.
if (semver.lt(solc.semver(), '0.4.0')) {
st.skip('Not supported by solc');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'c.sol': {
content: 'contract C { function f() public { } }'
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectError(output, 'Warning', 'Source file does not specify required compiler version!'));
st.end();
});
t.test('compiling standard JSON (using libraries) (using lowlevel API)', function (st) {
// 0.4.0 has a bug with libraries
if (semver.eq(solc.semver(), '0.4.0')) {
st.skip('Skipping on broken compiler version');
st.end();
return;
}
if (typeof solc.lowlevel.compileStandard !== 'function') {
st.skip('Low-level compileStandard interface not implemented by this compiler version.');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
libraries: {
'lib.sol': {
L: '0x4200000000000000000000000000000000000001'
}
},
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'lib.sol': {
content: 'library L { function f() public returns (uint) { return 7; } }'
},
'a.sol': {
content: 'import "lib.sol"; contract A { function g() public { L.f(); } }'
}
}
};
const output = JSON.parse(solc.lowlevel.compileStandard(JSON.stringify(input)));
st.ok(expectNoError(output));
const A = getBytecodeStandard(output, 'a.sol', 'A');
st.ok(typeof A === 'string');
st.ok(A.length > 0);
st.ok(Object.keys(linker.findLinkReferences(A)).length === 0);
const L = getBytecodeStandard(output, 'lib.sol', 'L');
st.ok(typeof L === 'string');
st.ok(L.length > 0);
st.end();
});
t.test('compiling standard JSON (invalid JSON)', function (st) {
const output = JSON.parse(solc.compile('{invalid'));
// TODO: change wrapper to output matching error
st.ok(
expectError(output, 'JSONError', 'Line 1, Column 2\n Missing \'}\' or object member name') ||
expectError(output, 'JSONError', 'Invalid JSON supplied:') ||
expectError(output, 'JSONError', 'parse error at line 1, column 2: syntax error while parsing object key - invalid literal; last read: \'{i\'; expected string literal')
);
st.end();
});
t.test('compiling standard JSON (invalid language)', function (st) {
const output = JSON.parse(solc.compile('{"language":"InvalidSolidity","sources":{"cont.sol":{"content":""}}}'));
st.ok(expectError(output, 'JSONError', 'supported as a language.') && expectError(output, 'JSONError', '"Solidity"'));
st.end();
});
t.test('compiling standard JSON (no sources)', function (st) {
const output = JSON.parse(solc.compile('{"language":"Solidity"}'));
st.ok(expectError(output, 'JSONError', 'No input sources specified.'));
st.end();
});
t.test('compiling standard JSON (multiple sources on old compiler)', function (st) {
const output = JSON.parse(solc.compile('{"language":"Solidity","sources":{"cont.sol":{"content":"import \\"lib.sol\\";"},"lib.sol":{"content":""}}}'));
if (solc.features.multipleInputs) {
st.ok(expectNoError(output));
} else {
st.ok(expectError(output, 'JSONError', 'Multiple sources provided, but compiler only supports single input.') || expectError(output, 'Parser error', 'Parser error: Source not found.'));
}
st.end();
});
t.test('compiling standard JSON (file names containing symbols)', function (st) {
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'!@#$%^&*()_+-=[]{}\\|"\';:~`<>,.?/': {
content: 'contract C {}'
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectNoError(output));
const C = getBytecodeStandard(output, '!@#$%^&*()_+-=[]{}\\|"\';:~`<>,.?/', 'C');
st.ok(typeof C === 'string');
st.ok(C.length > 0);
st.end();
});
t.test('compiling standard JSON (file names containing multiple semicolons)', function (st) {
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'a:b:c:d:e:f:G.sol': {
content: 'contract G {}'
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
st.ok(expectNoError(output));
const G = getBytecodeStandard(output, 'a:b:c:d:e:f:G.sol', 'G');
st.ok(typeof G === 'string');
st.ok(G.length > 0);
st.end();
});
});
});
// Only run on the latest version.
if (versionText === 'latest' && !noRemoteVersions) {
tape('Loading Legacy Versions', function (t) {
t.test('loading remote version - development snapshot', function (st) {
// getting the development snapshot
st.plan(2);
solc.loadRemoteVersion('latest', function (err, solcSnapshot) {
if (err) {
st.plan(1);
st.skip('Network error - skipping remote loading test');
st.end();
return;
}
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.bytecode']
}
}
},
sources: {
'cont.sol': {
content: 'contract x { function g() public {} }'
}
}
};
const output = JSON.parse(solcSnapshot.compile(JSON.stringify(input)));
const x = getBytecodeStandard(output, 'cont.sol', 'x');
st.ok(typeof x === 'string');
st.ok(x.length > 0);
});
});
});
}
}
runTests(solc, 'latest');
if (!noRemoteVersions) {
// New compiler interface features 0.1.6, 0.2.1, 0.4.11, 0.5.0, etc.
// 0.4.0 added pragmas (used in tests above)
const versions = [
'v0.1.1+commit.6ff4cd6',
'v0.1.6+commit.d41f8b7',
'v0.2.0+commit.4dc2445',
'v0.2.1+commit.91a6b35',
'v0.3.6+commit.3fc68da',
'v0.4.0+commit.acd334c9',
'v0.4.9+commit.364da425',
'v0.4.10+commit.f0d539ae',
'v0.4.11+commit.68ef5810',
'v0.4.12+commit.194ff033',
'v0.4.19+commit.c4cbbb05',
'v0.4.20+commit.3155dd80',
'v0.4.26+commit.4563c3fc'
];
for (let version in versions) {
version = versions[version];
// NOTE: The temporary directory will be removed on process exit.
const tempDir = tmp.dirSync({ unsafeCleanup: true, prefix: 'solc-js-compiler-test-' }).name;
execSync(`curl -L -o ${tempDir}/${version}.js https://binaries.soliditylang.org/bin/soljson-${version}.js`);
const newSolc = wrapper(require(`${tempDir}/${version}.js`));
runTests(newSolc, version);
}
}