forked from ethereum/solc-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.js
195 lines (174 loc) · 5.88 KB
/
translate.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
var linker = require('./linker.js');
/// Translate old style version numbers to semver.
/// Old style: 0.3.6-3fc68da5/Release-Emscripten/clang
/// 0.3.5-371690f0/Release-Emscripten/clang/Interpreter
/// 0.2.0-e7098958/.-Emscripten/clang/int linked to libethereum-1.1.1-bbb80ab0/.-Emscripten/clang/int
/// 0.1.3-0/.-/clang/int linked to libethereum-0.9.92-0/.-/clang/int
/// 0.1.2-5c3bfd4b*/.-/clang/int
/// 0.1.1-6ff4cd6b/RelWithDebInfo-Emscripten/clang/int
/// New style: 0.4.5+commit.b318366e.Emscripten.clang
function versionToSemver (version) {
// FIXME: parse more detail, but this is a good start
var parsed = version.match(/^([0-9]+\.[0-9]+\.[0-9]+)-([0-9a-f]{8})[/*].*$/);
if (parsed) {
return parsed[1] + '+commit.' + parsed[2];
}
if (version.indexOf('0.1.3-0') !== -1) {
return '0.1.3';
}
// assume it is already semver compatible
return version;
}
function translateErrors (ret, errors) {
for (var error in errors) {
var type = 'error';
var extractType = /^(.*):(\d+):(\d+):(.*):/;
extractType = extractType.exec(errors[error]);
if (extractType) {
type = extractType[4].trim();
} else if (errors[error].indexOf(': Warning:')) {
type = 'Warning';
} else if (errors[error].indexOf(': Error:')) {
type = 'Error';
}
ret.push({
type: type,
component: 'general',
severity: (type === 'Warning') ? 'warning' : 'error',
message: errors[error],
formattedMessage: errors[error]
});
}
}
function translateGasEstimates (gasEstimates) {
if (gasEstimates === null) {
return 'infinite';
}
if (typeof gasEstimates === 'number') {
return gasEstimates.toString();
}
var gasEstimatesTranslated = {};
for (var func in gasEstimates) {
gasEstimatesTranslated[func] = translateGasEstimates(gasEstimates[func]);
}
return gasEstimatesTranslated;
}
function translateJsonCompilerOutput (output, libraries) {
var ret = {};
ret['errors'] = [];
var errors;
if (output['error']) {
errors = [ output['error'] ];
} else {
errors = output['errors'];
}
translateErrors(ret['errors'], errors);
ret['contracts'] = {};
for (var contract in output['contracts']) {
// Split name first, can be `contract`, `:contract` or `filename:contract`
var tmp = contract.match(/^(([^:]*):)?([^:]+)$/);
if (tmp.length !== 4) {
// Force abort
return null;
}
var fileName = tmp[2];
if (fileName === undefined) {
// this is the case of `contract`
fileName = '';
}
var contractName = tmp[3];
var contractInput = output['contracts'][contract];
var gasEstimates = contractInput['gasEstimates'];
var translatedGasEstimates = {};
if (gasEstimates['creation']) {
translatedGasEstimates['creation'] = {
'codeDepositCost': translateGasEstimates(gasEstimates['creation'][1]),
'executionCost': translateGasEstimates(gasEstimates['creation'][0])
};
}
if (gasEstimates['internal']) {
translatedGasEstimates['internal'] = translateGasEstimates(gasEstimates['internal']);
}
if (gasEstimates['external']) {
translatedGasEstimates['external'] = translateGasEstimates(gasEstimates['external']);
}
var contractOutput = {
'abi': JSON.parse(contractInput['interface']),
'metadata': contractInput['metadata'],
'evm': {
'legacyAssembly': contractInput['assembly'],
'bytecode': {
'object': linker.linkBytecode(contractInput['bytecode'], libraries),
'opcodes': contractInput['opcodes'],
'sourceMap': contractInput['srcmap'],
'linkReferences': linker.findLinkReferences(contractInput['bytecode'])
},
'deployedBytecode': {
'object': linker.linkBytecode(contractInput['runtimeBytecode'], libraries),
'sourceMap': contractInput['srcmapRuntime'],
'linkReferences': linker.findLinkReferences(contractInput['runtimeBytecode'])
},
'methodIdentifiers': contractInput['functionHashes'],
'gasEstimates': translatedGasEstimates
}
};
if (!ret['contracts'][fileName]) {
ret['contracts'][fileName] = {};
}
ret['contracts'][fileName][contractName] = contractOutput;
}
var sourceMap = {};
for (var sourceId in output['sourceList']) {
sourceMap[output['sourceList'][sourceId]] = sourceId;
}
ret['sources'] = {};
for (var source in output['sources']) {
ret['sources'][source] = {
id: sourceMap[source],
legacyAST: output['sources'][source].AST
};
}
return ret;
}
function escapeString (text) {
return text
.replace('\n', '\\n', 'g')
.replace('\r', '\\r', 'g')
.replace('\t', '\\t', 'g');
}
function formatAssemblyText (asm, prefix, source) {
if (typeof asm === typeof '' || asm === null || asm === undefined) {
return prefix + (asm || '') + '\n';
}
var text = prefix + '.code\n';
asm['.code'].forEach(function (item, i) {
var v = item.value === undefined ? '' : item.value;
var src = '';
if (source !== undefined && item.begin !== undefined && item.end !== undefined) {
src = escapeString(source.slice(item.begin, item.end));
}
if (src.length > 30) {
src = src.slice(0, 30) + '...';
}
if (item.name !== 'tag') {
text += ' ';
}
text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n';
});
text += prefix + '.data\n';
var asmData = asm['.data'] || [];
for (var i in asmData) {
var item = asmData[i];
text += ' ' + prefix + '' + i + ':\n';
text += formatAssemblyText(item, prefix + ' ', source);
}
return text;
}
function prettyPrintLegacyAssemblyJSON (assembly, source) {
return formatAssemblyText(assembly, '', source);
}
module.exports = {
versionToSemver: versionToSemver,
translateJsonCompilerOutput: translateJsonCompilerOutput,
prettyPrintLegacyAssemblyJSON: prettyPrintLegacyAssemblyJSON
};