UNPKG

6.94 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const linker_1 = __importDefault(require("./linker"));
6/// Translate old style version numbers to semver.
7/// Old style: 0.3.6-3fc68da5/Release-Emscripten/clang
8/// 0.3.5-371690f0/Release-Emscripten/clang/Interpreter
9/// 0.3.5-0/Release-Emscripten/clang/Interpreter
10/// 0.2.0-e7098958/.-Emscripten/clang/int linked to libethereum-1.1.1-bbb80ab0/.-Emscripten/clang/int
11/// 0.1.3-0/.-/clang/int linked to libethereum-0.9.92-0/.-/clang/int
12/// 0.1.2-5c3bfd4b*/.-/clang/int
13/// 0.1.1-6ff4cd6b/RelWithDebInfo-Emscripten/clang/int
14/// New style: 0.4.5+commit.b318366e.Emscripten.clang
15function versionToSemver(version) {
16 // FIXME: parse more detail, but this is a good start
17 const parsed = version.match(/^([0-9]+\.[0-9]+\.[0-9]+)-([0-9a-f]{8})[/*].*$/);
18 if (parsed) {
19 return parsed[1] + '+commit.' + parsed[2];
20 }
21 if (version.indexOf('0.1.3-0') !== -1) {
22 return '0.1.3';
23 }
24 if (version.indexOf('0.3.5-0') !== -1) {
25 return '0.3.5';
26 }
27 // assume it is already semver compatible
28 return version;
29}
30function translateErrors(ret, errors) {
31 for (const error in errors) {
32 let type = 'error';
33 let extractType = /^(.*):(\d+):(\d+):(.*):/;
34 extractType = extractType.exec(errors[error]);
35 if (extractType) {
36 type = extractType[4].trim();
37 }
38 else if (errors[error].indexOf(': Warning:')) {
39 type = 'Warning';
40 }
41 else if (errors[error].indexOf(': Error:')) {
42 type = 'Error';
43 }
44 ret.push({
45 type: type,
46 component: 'general',
47 severity: (type === 'Warning') ? 'warning' : 'error',
48 message: errors[error],
49 formattedMessage: errors[error]
50 });
51 }
52}
53function translateGasEstimates(gasEstimates) {
54 if (gasEstimates === null) {
55 return 'infinite';
56 }
57 if (typeof gasEstimates === 'number') {
58 return gasEstimates.toString();
59 }
60 const gasEstimatesTranslated = {};
61 for (const func in gasEstimates) {
62 gasEstimatesTranslated[func] = translateGasEstimates(gasEstimates[func]);
63 }
64 return gasEstimatesTranslated;
65}
66function translateJsonCompilerOutput(output, libraries) {
67 const ret = {};
68 ret.errors = [];
69 let errors;
70 if (output.error) {
71 errors = [output.error];
72 }
73 else {
74 errors = output.errors;
75 }
76 translateErrors(ret.errors, errors);
77 ret.contracts = {};
78 for (const contract in output.contracts) {
79 // Split name first, can be `contract`, `:contract` or `filename:contract`
80 const tmp = contract.match(/^((.*):)?([^:]+)$/);
81 if (tmp.length !== 4) {
82 // Force abort
83 return null;
84 }
85 let fileName = tmp[2];
86 if (fileName === undefined) {
87 // this is the case of `contract`
88 fileName = '';
89 }
90 const contractName = tmp[3];
91 const contractInput = output.contracts[contract];
92 const gasEstimates = contractInput.gasEstimates;
93 const translatedGasEstimates = {};
94 if (gasEstimates.creation) {
95 translatedGasEstimates.creation = {
96 codeDepositCost: translateGasEstimates(gasEstimates.creation[1]),
97 executionCost: translateGasEstimates(gasEstimates.creation[0])
98 };
99 }
100 if (gasEstimates.internal) {
101 translatedGasEstimates.internal = translateGasEstimates(gasEstimates.internal);
102 }
103 if (gasEstimates.external) {
104 translatedGasEstimates.external = translateGasEstimates(gasEstimates.external);
105 }
106 const contractOutput = {
107 abi: JSON.parse(contractInput.interface),
108 metadata: contractInput.metadata,
109 evm: {
110 legacyAssembly: contractInput.assembly,
111 bytecode: {
112 object: contractInput.bytecode && linker_1.default.linkBytecode(contractInput.bytecode, libraries || {}),
113 opcodes: contractInput.opcodes,
114 sourceMap: contractInput.srcmap,
115 linkReferences: contractInput.bytecode && linker_1.default.findLinkReferences(contractInput.bytecode)
116 },
117 deployedBytecode: {
118 object: contractInput.runtimeBytecode && linker_1.default.linkBytecode(contractInput.runtimeBytecode, libraries || {}),
119 sourceMap: contractInput.srcmapRuntime,
120 linkReferences: contractInput.runtimeBytecode && linker_1.default.findLinkReferences(contractInput.runtimeBytecode)
121 },
122 methodIdentifiers: contractInput.functionHashes,
123 gasEstimates: translatedGasEstimates
124 }
125 };
126 if (!ret.contracts[fileName]) {
127 ret.contracts[fileName] = {};
128 }
129 ret.contracts[fileName][contractName] = contractOutput;
130 }
131 const sourceMap = {};
132 for (const sourceId in output.sourceList) {
133 sourceMap[output.sourceList[sourceId]] = sourceId;
134 }
135 ret.sources = {};
136 for (const source in output.sources) {
137 ret.sources[source] = {
138 id: sourceMap[source],
139 legacyAST: output.sources[source].AST
140 };
141 }
142 return ret;
143}
144function escapeString(text) {
145 return text
146 .replace(/\n/g, '\\n')
147 .replace(/\r/g, '\\r')
148 .replace(/\t/g, '\\t');
149}
150// 'asm' can be an object or a string
151function formatAssemblyText(asm, prefix, source) {
152 if (typeof asm === 'string' || asm === null || asm === undefined) {
153 return prefix + (asm || '') + '\n';
154 }
155 let text = prefix + '.code\n';
156 asm['.code'].forEach(function (item, i) {
157 const v = item.value === undefined ? '' : item.value;
158 let src = '';
159 if (source !== undefined && item.begin !== undefined && item.end !== undefined) {
160 src = escapeString(source.slice(item.begin, item.end));
161 }
162 if (src.length > 30) {
163 src = src.slice(0, 30) + '...';
164 }
165 if (item.name !== 'tag') {
166 text += ' ';
167 }
168 text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n';
169 });
170 text += prefix + '.data\n';
171 const asmData = asm['.data'] || [];
172 for (const i in asmData) {
173 const item = asmData[i];
174 text += ' ' + prefix + '' + i + ':\n';
175 text += formatAssemblyText(item, prefix + ' ', source);
176 }
177 return text;
178}
179function prettyPrintLegacyAssemblyJSON(assembly, source) {
180 return formatAssemblyText(assembly, '', source);
181}
182module.exports = {
183 versionToSemver,
184 translateJsonCompilerOutput,
185 prettyPrintLegacyAssemblyJSON
186};