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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,364 @@
1
+ [![CircleCI](https://img.shields.io/circleci/project/github/ethereum/solc-js/master.svg?style=flat-square)](https://circleci.com/gh/ethereum/solc-js/tree/master)
2
+ [![Coverage Status](https://img.shields.io/coveralls/ethereum/solc-js.svg?style=flat-square)](https://coveralls.io/r/ethereum/solc-js)
3
+
4
+ # solc-js
5
+
6
+ JavaScript bindings for the [Solidity compiler](https://github.com/ethereum/solidity).
7
+
8
+ Uses the Emscripten compiled Solidity found in the [solc-bin repository](https://github.com/ethereum/solc-bin).
9
+
10
+ ## Node.js Usage
11
+
12
+ To use the latest stable version of the Solidity compiler via Node.js you can install it via npm:
13
+
14
+ ```bash
15
+ npm install solc
16
+ ```
17
+
18
+ ### Usage on the Command-Line
19
+
20
+ If this package is installed globally (`npm install -g solc`), a command-line tool called `solcjs` will be available.
21
+
22
+ To see all the supported features, execute:
23
+
24
+ ```bash
25
+ solcjs --help
26
+ ```
27
+
28
+ To compile a contract that imports other contracts via relative paths:
29
+ ```bash
30
+ solcjs --bin --include-path node_modules/ --base-path . MainContract.sol
31
+ ```
32
+ Use the ``--base-path`` and ``--include-path`` options to describe the layout of your project.
33
+ ``--base-path`` represents the root of your own source tree while ``--include-path`` allows you to
34
+ specify extra locations containing external code (e.g. libraries installed with a package manager).
35
+
36
+ Note: ensure that all the files you specify on the command line are located inside the base path or
37
+ one of the include paths.
38
+ The compiler refers to files from outside of these directories using absolute paths.
39
+ Having absolute paths in contract metadata will result in your bytecode being reproducible only
40
+ when it's placed in these exact absolute locations.
41
+
42
+ Note: this commandline interface is not compatible with `solc` provided by the Solidity compiler package and thus cannot be
43
+ used in combination with an Ethereum client via the `eth.compile.solidity()` RPC method. Please refer to the
44
+ [Solidity compiler documentation](https://solidity.readthedocs.io/) for instructions to install `solc`.
45
+ Furthermore, the commandline interface to solc-js provides fewer features than the binary release.
46
+
47
+ ### Usage in Projects
48
+
49
+ There are two ways to use `solc`:
50
+
51
+ 1. Through a high-level API giving a uniform interface to all compiler versions
52
+ 2. Through a low-level API giving access to all the compiler interfaces, which depend on the version of the compiler
53
+
54
+ #### High-level API
55
+
56
+ The high-level API consists of a single method, `compile`, which expects the [Compiler Standard Input and Output JSON](https://solidity.readthedocs.io/en/v0.5.0/using-the-compiler.html#compiler-input-and-output-json-description).
57
+
58
+ It also accepts an optional set of callback functions, which include the ``import`` and the ``smtSolver`` callbacks.
59
+ Starting 0.6.0 it only accepts an object in place of the callback to supply the callbacks.
60
+
61
+ The ``import`` callback function is used to resolve unmet dependencies.
62
+ This callback receives a path and must synchronously return either an error or the content of the dependency
63
+ as a string. It cannot be used together with callback-based, asynchronous,
64
+ filesystem access. A workaround is to collect the names of dependencies, return
65
+ an error, and keep re-running the compiler until all of them are resolved.
66
+
67
+ #### Example usage without the import callback
68
+
69
+ Example:
70
+
71
+ ```javascript
72
+ var solc = require('solc');
73
+
74
+ var input = {
75
+ language: 'Solidity',
76
+ sources: {
77
+ 'test.sol': {
78
+ content: 'contract C { function f() public { } }'
79
+ }
80
+ },
81
+ settings: {
82
+ outputSelection: {
83
+ '*': {
84
+ '*': ['*']
85
+ }
86
+ }
87
+ }
88
+ };
89
+
90
+ var output = JSON.parse(solc.compile(JSON.stringify(input)));
91
+
92
+ // `output` here contains the JSON output as specified in the documentation
93
+ for (var contractName in output.contracts['test.sol']) {
94
+ console.log(
95
+ contractName +
96
+ ': ' +
97
+ output.contracts['test.sol'][contractName].evm.bytecode.object
98
+ );
99
+ }
100
+ ```
101
+
102
+ #### Example usage with import callback
103
+
104
+ ```javascript
105
+ var solc = require('solc');
106
+
107
+ var input = {
108
+ language: 'Solidity',
109
+ sources: {
110
+ 'test.sol': {
111
+ content: 'import "lib.sol"; contract C { function f() public { L.f(); } }'
112
+ }
113
+ },
114
+ settings: {
115
+ outputSelection: {
116
+ '*': {
117
+ '*': ['*']
118
+ }
119
+ }
120
+ }
121
+ };
122
+
123
+ function findImports(path) {
124
+ if (path === 'lib.sol')
125
+ return {
126
+ contents:
127
+ 'library L { function f() internal returns (uint) { return 7; } }'
128
+ };
129
+ else return { error: 'File not found' };
130
+ }
131
+
132
+ // New syntax (supported from 0.5.12, mandatory from 0.6.0)
133
+ var output = JSON.parse(
134
+ solc.compile(JSON.stringify(input), { import: findImports })
135
+ );
136
+
137
+ // `output` here contains the JSON output as specified in the documentation
138
+ for (var contractName in output.contracts['test.sol']) {
139
+ console.log(
140
+ contractName +
141
+ ': ' +
142
+ output.contracts['test.sol'][contractName].evm.bytecode.object
143
+ );
144
+ }
145
+ ```
146
+
147
+ Since version 0.5.1, the ``smtSolver`` callback function is used to solve SMT queries generated by
148
+ Solidity's SMTChecker. If you have an SMT solver installed locally, it can
149
+ be used to solve the given queries, where the callback must synchronously
150
+ return either an error or the result from the solver. A default
151
+ ``smtSolver`` callback is included in this package via the module
152
+ ``smtchecker.js`` which exports the ``smtCallback`` function that takes 1) a
153
+ function that takes queries and returns the solving result, and 2) a solver
154
+ configuration object. The module ``smtsolver.js`` has a few predefined solver
155
+ configurations, and relies on Z3, Eldarica or cvc5 being installed locally. It
156
+ exports the list of locally found solvers and a function that invokes a given
157
+ solver.
158
+
159
+ The API of the SMT callback is **experimental** and can change at any time.
160
+ The last change was in version 0.8.11.
161
+
162
+ #### Example usage with smtSolver callback
163
+
164
+ ```javascript
165
+ var solc = require('solc');
166
+ const smtchecker = require('solc/smtchecker');
167
+ const smtsolver = require('solc/smtsolver');
168
+ // Note that this example only works via node and not in the browser.
169
+
170
+ var input = {
171
+ language: 'Solidity',
172
+ sources: {
173
+ 'test.sol': {
174
+ content: 'contract C { function f(uint x) public { assert(x > 0); } }'
175
+ }
176
+ },
177
+ settings: {
178
+ modelChecker: {
179
+ engine: "chc",
180
+ solvers: [ "smtlib2" ]
181
+ }
182
+ }
183
+ };
184
+
185
+ var output = JSON.parse(
186
+ solc.compile(
187
+ JSON.stringify(input),
188
+ { smtSolver: smtchecker.smtCallback(smtsolver.smtSolver, smtsolver.availableSolvers[0]) }
189
+ )
190
+ );
191
+
192
+ ```
193
+ The assertion is clearly false, and an ``assertion failure`` warning
194
+ should be returned, together with a counterexample.
195
+
196
+ #### Low-level API
197
+
198
+ The low-level API is as follows:
199
+
200
+ - `solc.lowlevel.compileSingle`: the original entry point, supports only a single file
201
+ - `solc.lowlevel.compileMulti`: this supports multiple files, introduced in 0.1.6
202
+ - `solc.lowlevel.compileCallback`: this supports callbacks, introduced in 0.2.1
203
+ - `solc.lowlevel.compileStandard`: this works just like `compile` above, but is only present in compilers after (and including) 0.4.11
204
+
205
+ For examples how to use them, please refer to the README of the above mentioned solc-js releases.
206
+
207
+ **Note**: These low-level functions remain available for compatibility reasons.
208
+ However, they were superseded by the `compile()` function and are no longer required.
209
+ Starting from version `0.5.0+commit.1d4f565a`, the functions `compileSingle`, `compileMulti`, and `compileCallback` are always `null` when using newer solc binary versions.
210
+ It is recommended to use the latest release of solc-js, but it should also handle all the older solc binaries down to `0.1.x`.
211
+
212
+ ### Using with Electron
213
+
214
+ **Note:**
215
+ If you are using Electron, `nodeIntegration` is on for `BrowserWindow` by default. If it is on, Electron will provide a `require` method which will not behave as expected and this may cause calls, such as `require('solc')`, to fail.
216
+
217
+ To turn off `nodeIntegration`, use the following:
218
+
219
+ ```javascript
220
+ new BrowserWindow({
221
+ webPreferences: {
222
+ nodeIntegration: false
223
+ }
224
+ });
225
+ ```
226
+
227
+ ### Using a Legacy Version
228
+
229
+ In order to compile contracts using a specific version of Solidity, the `solc.loadRemoteVersion(version, callback)` method is available. This returns a new `solc` object that uses a version of the compiler specified.
230
+
231
+ You can also load the "binary" manually and use `setupMethods` to create the familiar wrapper functions described above:
232
+ `var solc = solc.setupMethods(require("/my/local/soljson.js"))`.
233
+
234
+ ### Using the Latest Development Snapshot
235
+
236
+ By default, the npm version is only created for releases. This prevents people from deploying contracts with non-release versions because they are less stable and harder to verify. If you would like to use the latest development snapshot (at your own risk!), you may use the following example code.
237
+
238
+ ```javascript
239
+ var solc = require('solc');
240
+
241
+ // getting the development snapshot
242
+ solc.loadRemoteVersion('latest', function(err, solcSnapshot) {
243
+ if (err) {
244
+ // An error was encountered, display and quit
245
+ } else {
246
+ // NOTE: Use `solcSnapshot` here with the same interface `solc` has
247
+ // For example:
248
+ const output = solcSnapshot.compile(/* ... */)
249
+ }
250
+ });
251
+ ```
252
+
253
+ The version **must** be in the long format.
254
+ Thus, if you would like to use version `v0.8.17` you need to include the commit hash of the release.
255
+ You can extract the long version string for each version from the [publicly available release list](https://binaries.soliditylang.org/bin/list.json).
256
+
257
+ ```javascript
258
+ solc.loadRemoteVersion('v0.8.17+commit.8df45f5f', function(err, solcSnapshot) { /* ... */ });
259
+ ```
260
+
261
+ ### Linking Bytecode
262
+
263
+ When using libraries, the resulting bytecode will contain placeholders for the real addresses of the referenced libraries. These have to be updated, via a process called linking, before deploying the contract.
264
+
265
+ The `linker` module (`require('solc/linker')`) offers helpers to accomplish this.
266
+
267
+ The `linkBytecode` method provides a simple helper for linking:
268
+
269
+ ```javascript
270
+ var linker = require('solc/linker');
271
+
272
+ bytecode = linker.linkBytecode(bytecode, { MyLibrary: '0x123456...' });
273
+ ```
274
+
275
+ As of Solidity 0.4.11 the compiler supports [standard JSON input and output](https://solidity.readthedocs.io/en/develop/using-the-compiler.html#compiler-input-and-output-json-description) which outputs a _link references_ map. This gives a map of library names to offsets in the bytecode to replace the addresses at. It also doesn't have the limitation on library file and contract name lengths.
276
+
277
+ There is a method available in the `linker` module called `findLinkReferences` which can find such link references in bytecode produced by an older compiler:
278
+
279
+ ```javascript
280
+ var linker = require('solc/linker');
281
+
282
+ var linkReferences = linker.findLinkReferences(bytecode);
283
+ ```
284
+
285
+ ### Updating the ABI
286
+
287
+ The ABI generated by Solidity versions can differ slightly, due to new features introduced. There is a tool included which aims to translate the ABI generated by an older Solidity version to conform to the latest standard.
288
+
289
+ It can be used as:
290
+
291
+ ```javascript
292
+ var abi = require('solc/abi');
293
+
294
+ var inputABI = [
295
+ {
296
+ constant: false,
297
+ inputs: [],
298
+ name: 'hello',
299
+ outputs: [{ name: '', type: 'string' }],
300
+ payable: false,
301
+ type: 'function'
302
+ }
303
+ ];
304
+ var outputABI = abi.update('0.3.6', inputABI);
305
+ // Output contains: [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":true,"type":"function"},{"type":"fallback","payable":true}]
306
+ ```
307
+
308
+ ### Formatting old JSON assembly output
309
+
310
+ There is a helper available to format old JSON assembly output into a text familiar to earlier users of Remix IDE.
311
+
312
+ ```
313
+ var translate = require('solc/translate')
314
+
315
+ // assemblyJSON refers to the JSON of the given assembly and sourceCode is the source of which the assembly was generated from
316
+ var output = translate.prettyPrintLegacyAssemblyJSON(assemblyJSON, sourceCode)
317
+ ```
318
+
319
+ ## Browser Usage
320
+
321
+ Compilation is generally a long-running and resource intensive task that cannot reasonably be performed in the main thread of the browser.
322
+ Some browsers even disallow synchronous compilation on the main thread if the module is larger than 4KB.
323
+ Thus, the only supported way to use `solc` in a web browser is through a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).
324
+
325
+ ### Loading solc with web workers
326
+
327
+ Web Workers allow you to run javascript in the background in the browser, letting the browser's main thread free to do whatever it needs to do.
328
+ Please, see the minimal example of how to use `solc` with web workers below or check out this [repository](https://github.com/r0qs/solcjs-webworker-example) for a full demo.
329
+
330
+ * index.html
331
+ ```html
332
+ <!DOCTYPE html>
333
+ <html>
334
+
335
+ <head>
336
+ <meta charset="utf-8" />
337
+ </head>
338
+
339
+ <body>
340
+ <script>
341
+ var worker = new Worker('./dist/bundle.js');
342
+ worker.addEventListener('message', function (e) {
343
+ console.log(e.data.version)
344
+ }, false);
345
+
346
+ worker.postMessage({})
347
+ </script>
348
+ </body>
349
+
350
+ </html>
351
+ ```
352
+
353
+ * worker.js:
354
+ ```javascript
355
+ importScripts('https://binaries.soliditylang.org/bin/soljson-v0.8.19+commit.7dd6d404.js')
356
+ import wrapper from 'solc/wrapper';
357
+
358
+ self.addEventListener('message', () => {
359
+ const compiler = wrapper(self.Module)
360
+ self.postMessage({
361
+ version: compiler.version()
362
+ })
363
+ }, false)
364
+ ```
package/abi.js ADDED
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ const semver = __importStar(require("semver"));
26
+ function update(compilerVersion, abi) {
27
+ let hasConstructor = false;
28
+ let hasFallback = false;
29
+ for (let i = 0; i < abi.length; i++) {
30
+ const item = abi[i];
31
+ if (item.type === 'constructor') {
32
+ hasConstructor = true;
33
+ // <0.4.5 assumed every constructor to be payable
34
+ if (semver.lt(compilerVersion, '0.4.5')) {
35
+ item.payable = true;
36
+ }
37
+ }
38
+ else if (item.type === 'fallback') {
39
+ hasFallback = true;
40
+ }
41
+ if (item.type !== 'event') {
42
+ // add 'payable' to everything, except constant functions
43
+ if (!item.constant && semver.lt(compilerVersion, '0.4.0')) {
44
+ item.payable = true;
45
+ }
46
+ // add stateMutability field
47
+ if (semver.lt(compilerVersion, '0.4.16')) {
48
+ if (item.payable) {
49
+ item.stateMutability = 'payable';
50
+ }
51
+ else if (item.constant) {
52
+ item.stateMutability = 'view';
53
+ }
54
+ else {
55
+ item.stateMutability = 'nonpayable';
56
+ }
57
+ }
58
+ }
59
+ }
60
+ // 0.1.2 from Aug 2015 had it. The code has it since May 2015 (e7931ade)
61
+ if (!hasConstructor && semver.lt(compilerVersion, '0.1.2')) {
62
+ abi.push({
63
+ type: 'constructor',
64
+ payable: true,
65
+ stateMutability: 'payable',
66
+ inputs: []
67
+ });
68
+ }
69
+ if (!hasFallback && semver.lt(compilerVersion, '0.4.0')) {
70
+ abi.push({
71
+ type: 'fallback',
72
+ payable: true,
73
+ stateMutability: 'payable'
74
+ });
75
+ }
76
+ return abi;
77
+ }
78
+ module.exports = {
79
+ update
80
+ };
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.setupCompile = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ const helpers_1 = require("../common/helpers");
9
+ const helpers_2 = require("./helpers");
10
+ function setupCompile(solJson, core) {
11
+ return {
12
+ compileJson: bindCompileJson(solJson),
13
+ compileJsonCallback: bindCompileJsonCallback(solJson, core),
14
+ compileJsonMulti: bindCompileJsonMulti(solJson),
15
+ compileStandard: bindCompileStandard(solJson, core)
16
+ };
17
+ }
18
+ exports.setupCompile = setupCompile;
19
+ /**********************
20
+ * COMPILE
21
+ **********************/
22
+ /**
23
+ * Returns a binding to the solidity compileJSON method.
24
+ * input (text), optimize (bool) -> output (jsontext)
25
+ *
26
+ * @param solJson The Emscripten compiled Solidity object.
27
+ */
28
+ function bindCompileJson(solJson) {
29
+ return (0, helpers_2.bindSolcMethod)(solJson, 'compileJSON', 'string', ['string', 'number'], null);
30
+ }
31
+ /**
32
+ * Returns a binding to the solidity compileJSONMulti method.
33
+ * input (jsontext), optimize (bool) -> output (jsontext)
34
+ *
35
+ * @param solJson The Emscripten compiled Solidity object.
36
+ */
37
+ function bindCompileJsonMulti(solJson) {
38
+ return (0, helpers_2.bindSolcMethod)(solJson, 'compileJSONMulti', 'string', ['string', 'number'], null);
39
+ }
40
+ /**
41
+ * Returns a binding to the solidity compileJSONCallback method.
42
+ * input (jsontext), optimize (bool), callback (ptr) -> output (jsontext)
43
+ *
44
+ * @param solJson The Emscripten compiled Solidity object.
45
+ * @param coreBindings The core bound Solidity methods.
46
+ */
47
+ function bindCompileJsonCallback(solJson, coreBindings) {
48
+ const compileInternal = (0, helpers_2.bindSolcMethod)(solJson, 'compileJSONCallback', 'string', ['string', 'number', 'number'], null);
49
+ if ((0, helpers_1.isNil)(compileInternal))
50
+ return null;
51
+ return function (input, optimize, readCallback) {
52
+ return runWithCallbacks(solJson, coreBindings, readCallback, compileInternal, [input, optimize]);
53
+ };
54
+ }
55
+ /**
56
+ * Returns a binding to the solidity solidity_compile method with a fallback to
57
+ * compileStandard.
58
+ * input (jsontext), callback (optional >= v6 only - ptr) -> output (jsontext)
59
+ *
60
+ * @param solJson The Emscripten compiled Solidity object.
61
+ * @param coreBindings The core bound Solidity methods.
62
+ */
63
+ function bindCompileStandard(solJson, coreBindings) {
64
+ let boundFunctionStandard = null;
65
+ let boundFunctionSolidity = null;
66
+ // input (jsontext), callback (ptr) -> output (jsontext)
67
+ const compileInternal = (0, helpers_2.bindSolcMethod)(solJson, 'compileStandard', 'string', ['string', 'number'], null);
68
+ if (coreBindings.isVersion6OrNewer) {
69
+ // input (jsontext), callback (ptr), callback_context (ptr) -> output (jsontext)
70
+ boundFunctionSolidity = (0, helpers_2.bindSolcMethod)(solJson, 'solidity_compile', 'string', ['string', 'number', 'number'], null);
71
+ }
72
+ else {
73
+ // input (jsontext), callback (ptr) -> output (jsontext)
74
+ boundFunctionSolidity = (0, helpers_2.bindSolcMethod)(solJson, 'solidity_compile', 'string', ['string', 'number'], null);
75
+ }
76
+ if (!(0, helpers_1.isNil)(compileInternal)) {
77
+ boundFunctionStandard = function (input, readCallback) {
78
+ return runWithCallbacks(solJson, coreBindings, readCallback, compileInternal, [input]);
79
+ };
80
+ }
81
+ if (!(0, helpers_1.isNil)(boundFunctionSolidity)) {
82
+ boundFunctionStandard = function (input, callbacks) {
83
+ return runWithCallbacks(solJson, coreBindings, callbacks, boundFunctionSolidity, [input]);
84
+ };
85
+ }
86
+ return boundFunctionStandard;
87
+ }
88
+ /**********************
89
+ * CALL BACKS
90
+ **********************/
91
+ function wrapCallback(coreBindings, callback) {
92
+ (0, assert_1.default)(typeof callback === 'function', 'Invalid callback specified.');
93
+ return function (data, contents, error) {
94
+ const result = callback(coreBindings.copyFromCString(data));
95
+ if (typeof result.contents === 'string') {
96
+ coreBindings.copyToCString(result.contents, contents);
97
+ }
98
+ if (typeof result.error === 'string') {
99
+ coreBindings.copyToCString(result.error, error);
100
+ }
101
+ };
102
+ }
103
+ function wrapCallbackWithKind(coreBindings, callback) {
104
+ (0, assert_1.default)(typeof callback === 'function', 'Invalid callback specified.');
105
+ return function (context, kind, data, contents, error) {
106
+ // Must be a null pointer.
107
+ (0, assert_1.default)(context === 0, 'Callback context must be null.');
108
+ const result = callback(coreBindings.copyFromCString(kind), coreBindings.copyFromCString(data));
109
+ if (typeof result.contents === 'string') {
110
+ coreBindings.copyToCString(result.contents, contents);
111
+ }
112
+ if (typeof result.error === 'string') {
113
+ coreBindings.copyToCString(result.error, error);
114
+ }
115
+ };
116
+ }
117
+ // calls compile() with args || cb
118
+ function runWithCallbacks(solJson, coreBindings, callbacks, compile, args) {
119
+ if (callbacks) {
120
+ (0, assert_1.default)(typeof callbacks === 'object', 'Invalid callback object specified.');
121
+ }
122
+ else {
123
+ callbacks = {};
124
+ }
125
+ let readCallback = callbacks.import;
126
+ if (readCallback === undefined) {
127
+ readCallback = function (data) {
128
+ return {
129
+ error: 'File import callback not supported'
130
+ };
131
+ };
132
+ }
133
+ let singleCallback;
134
+ if (coreBindings.isVersion6OrNewer) {
135
+ // After 0.6.x multiple kind of callbacks are supported.
136
+ let smtSolverCallback = callbacks.smtSolver;
137
+ if (smtSolverCallback === undefined) {
138
+ smtSolverCallback = function (data) {
139
+ return {
140
+ error: 'SMT solver callback not supported'
141
+ };
142
+ };
143
+ }
144
+ singleCallback = function (kind, data) {
145
+ if (kind === 'source') {
146
+ return readCallback(data);
147
+ }
148
+ else if (kind === 'smt-query') {
149
+ return smtSolverCallback(data);
150
+ }
151
+ else {
152
+ (0, assert_1.default)(false, 'Invalid callback kind specified.');
153
+ }
154
+ };
155
+ singleCallback = wrapCallbackWithKind(coreBindings, singleCallback);
156
+ }
157
+ else {
158
+ // Old Solidity version only supported imports.
159
+ singleCallback = wrapCallback(coreBindings, readCallback);
160
+ }
161
+ const cb = coreBindings.addFunction(singleCallback, 'viiiii');
162
+ let output;
163
+ try {
164
+ args.push(cb);
165
+ if (coreBindings.isVersion6OrNewer) {
166
+ // Callback context.
167
+ args.push(null);
168
+ }
169
+ output = compile(...args);
170
+ }
171
+ finally {
172
+ coreBindings.removeFunction(cb);
173
+ }
174
+ if (coreBindings.reset) {
175
+ // Explicitly free memory.
176
+ //
177
+ // NOTE: cwrap() of "compile" will copy the returned pointer into a
178
+ // Javascript string and it is not possible to call free() on it.
179
+ // reset() however will clear up all allocations.
180
+ coreBindings.reset();
181
+ }
182
+ return output;
183
+ }