scol 0.8.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/translate.js ADDED
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const 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
15
+ function 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
+ }
30
+ function 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
+ }
53
+ function 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
+ }
66
+ function 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
+ }
144
+ function 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
151
+ function 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
+ }
179
+ function prettyPrintLegacyAssemblyJSON(assembly, source) {
180
+ return formatAssemblyText(assembly, '', source);
181
+ }
182
+ module.exports = {
183
+ versionToSemver,
184
+ translateJsonCompilerOutput,
185
+ prettyPrintLegacyAssemblyJSON
186
+ };
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __importDefault = (this && this.__importDefault) || function (mod) {
27
+ return (mod && mod.__esModule) ? mod : { "default": mod };
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ const semver = __importStar(require("semver"));
31
+ const _1 = __importDefault(require("./"));
32
+ const { version: packageVersion } = require('./package.json');
33
+ const solcVersion = _1.default.version();
34
+ console.log('solcVersion: ' + solcVersion);
35
+ console.log('packageVersion: ' + packageVersion);
36
+ if (semver.eq(packageVersion, solcVersion)) {
37
+ console.log('Version matching');
38
+ process.exit(0);
39
+ }
40
+ else {
41
+ console.log('Version mismatch');
42
+ process.exit(1);
43
+ }
package/wrapper.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const memorystream_1 = __importDefault(require("memorystream"));
6
+ const follow_redirects_1 = require("follow-redirects");
7
+ const formatters_1 = require("./formatters");
8
+ const helpers_1 = require("./common/helpers");
9
+ const bindings_1 = __importDefault(require("./bindings"));
10
+ const translate_1 = __importDefault(require("./translate"));
11
+ const Module = module.constructor;
12
+ function wrapper(soljson) {
13
+ const { coreBindings, compileBindings, methodFlags } = (0, bindings_1.default)(soljson);
14
+ return {
15
+ version: coreBindings.version,
16
+ semver: coreBindings.versionToSemver,
17
+ license: coreBindings.license,
18
+ lowlevel: {
19
+ compileSingle: compileBindings.compileJson,
20
+ compileMulti: compileBindings.compileJsonMulti,
21
+ compileCallback: compileBindings.compileJsonCallback,
22
+ compileStandard: compileBindings.compileStandard
23
+ },
24
+ features: {
25
+ legacySingleInput: methodFlags.compileJsonStandardSupported,
26
+ multipleInputs: methodFlags.compileJsonMultiSupported || methodFlags.compileJsonStandardSupported,
27
+ importCallback: methodFlags.compileJsonCallbackSuppported || methodFlags.compileJsonStandardSupported,
28
+ nativeStandardJSON: methodFlags.compileJsonStandardSupported
29
+ },
30
+ compile: compileStandardWrapper.bind(this, compileBindings),
31
+ // Loads the compiler of the given version from the github repository
32
+ // instead of from the local filesystem.
33
+ loadRemoteVersion,
34
+ // Use this if you want to add wrapper functions around the pure module.
35
+ setupMethods: wrapper
36
+ };
37
+ }
38
+ function loadRemoteVersion(versionString, callback) {
39
+ const memoryStream = new memorystream_1.default(null, { readable: false });
40
+ const url = `https://binaries.soliditylang.org/bin/soljson-${versionString}.js`;
41
+ follow_redirects_1.https.get(url, response => {
42
+ if (response.statusCode !== 200) {
43
+ callback(new Error(`Error retrieving binary: ${response.statusMessage}`));
44
+ }
45
+ else {
46
+ response.pipe(memoryStream);
47
+ response.on('end', () => {
48
+ // Based on the require-from-string package.
49
+ const soljson = new Module();
50
+ soljson._compile(memoryStream.toString(), `soljson-${versionString}.js`);
51
+ if (module.parent && module.parent.children) {
52
+ // Make sure the module is plugged into the hierarchy correctly to have parent
53
+ // properly garbage collected.
54
+ module.parent.children.splice(module.parent.children.indexOf(soljson), 1);
55
+ }
56
+ callback(null, wrapper(soljson.exports));
57
+ });
58
+ }
59
+ }).on('error', function (error) {
60
+ callback(error);
61
+ });
62
+ }
63
+ // Expects a Standard JSON I/O but supports old compilers
64
+ function compileStandardWrapper(compile, inputRaw, readCallback) {
65
+ if (!(0, helpers_1.isNil)(compile.compileStandard)) {
66
+ return compile.compileStandard(inputRaw, readCallback);
67
+ }
68
+ let input;
69
+ try {
70
+ input = JSON.parse(inputRaw);
71
+ }
72
+ catch (e) {
73
+ return (0, formatters_1.formatFatalError)(`Invalid JSON supplied: ${e.message}`);
74
+ }
75
+ if (input.language !== 'Solidity') {
76
+ return (0, formatters_1.formatFatalError)('Only "Solidity" is supported as a language.');
77
+ }
78
+ // NOTE: this is deliberately `== null`
79
+ if ((0, helpers_1.isNil)(input.sources) || input.sources.length === 0) {
80
+ return (0, formatters_1.formatFatalError)('No input sources specified.');
81
+ }
82
+ const sources = translateSources(input);
83
+ const optimize = isOptimizerEnabled(input);
84
+ const libraries = librariesSupplied(input);
85
+ if ((0, helpers_1.isNil)(sources) || Object.keys(sources).length === 0) {
86
+ return (0, formatters_1.formatFatalError)('Failed to process sources.');
87
+ }
88
+ // Try to wrap around old versions
89
+ if (!(0, helpers_1.isNil)(compile.compileJsonCallback)) {
90
+ const inputJson = JSON.stringify({ sources: sources });
91
+ const output = compile.compileJsonCallback(inputJson, optimize, readCallback);
92
+ return translateOutput(output, libraries);
93
+ }
94
+ if (!(0, helpers_1.isNil)(compile.compileJsonMulti)) {
95
+ const output = compile.compileJsonMulti(JSON.stringify({ sources: sources }), optimize);
96
+ return translateOutput(output, libraries);
97
+ }
98
+ // Try our luck with an ancient compiler
99
+ if (!(0, helpers_1.isNil)(compile.compileJson)) {
100
+ if (Object.keys(sources).length > 1) {
101
+ return (0, formatters_1.formatFatalError)('Multiple sources provided, but compiler only supports single input.');
102
+ }
103
+ const input = sources[Object.keys(sources)[0]];
104
+ const output = compile.compileJson(input, optimize);
105
+ return translateOutput(output, libraries);
106
+ }
107
+ return (0, formatters_1.formatFatalError)('Compiler does not support any known interface.');
108
+ }
109
+ function isOptimizerEnabled(input) {
110
+ return input.settings && input.settings.optimizer && input.settings.optimizer.enabled;
111
+ }
112
+ function translateSources(input) {
113
+ const sources = {};
114
+ for (const source in input.sources) {
115
+ if (input.sources[source].content !== null) {
116
+ sources[source] = input.sources[source].content;
117
+ }
118
+ else {
119
+ // force failure
120
+ return null;
121
+ }
122
+ }
123
+ return sources;
124
+ }
125
+ function librariesSupplied(input) {
126
+ if (!(0, helpers_1.isNil)(input.settings))
127
+ return input.settings.libraries;
128
+ }
129
+ function translateOutput(outputRaw, libraries) {
130
+ let parsedOutput;
131
+ try {
132
+ parsedOutput = JSON.parse(outputRaw);
133
+ }
134
+ catch (e) {
135
+ return (0, formatters_1.formatFatalError)(`Compiler returned invalid JSON: ${e.message}`);
136
+ }
137
+ const output = translate_1.default.translateJsonCompilerOutput(parsedOutput, libraries);
138
+ if ((0, helpers_1.isNil)(output)) {
139
+ return (0, formatters_1.formatFatalError)('Failed to process output.');
140
+ }
141
+ return JSON.stringify(output);
142
+ }
143
+ module.exports = wrapper;