Skip to content

Commit 0411cb6

Browse files
some more refactors and review comment adaptions.
1 parent 2353e82 commit 0411cb6

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Since version 0.8.11, the solidity compiler natively supports the
210210
language server protocol. With solc-js, you can now use it as follows:
211211

212212
```javascript
213-
var solc = require('solc');
213+
const solc = require('solc');
214214

215215
// Callback to be invoked when additional files have to be opened during
216216
// source code analysis stage.
@@ -223,16 +223,15 @@ function fileReadCallback(path)
223223
contents: 'library L { function f() internal returns (uint) { return 7; } }';
224224
};
225225
}
226-
else
227-
return { error: 'File not found' };
226+
return { error: 'File not found' };
228227
}
229228

230229
// Put solcjs into LSP mode.
231230
// Needs to be called only once before the actual LSP I/O calls.
232-
solc.lspStart(fileReadCallback);
231+
solc.lsp.start(fileReadCallback);
233232

234233
// Send some LSP JSON-RPC message and optionally receive a reply.
235-
var lspInitializationMessage = {
234+
const lspInitializationMessage = {
236235
'jsonrpc': '2.0',
237236
'method': 'initialize',
238237
'params': {
@@ -251,14 +250,14 @@ var lspInitializationMessage = {
251250
}
252251
}
253252
};
254-
solc.lspSendReceive(JSON.stringify(lspInitializationMessage)));
255-
solc.lspSendReceive(JSON.stringify({'jsonrpc': '2.0', 'method': 'initialized'}));
253+
solc.lsp.sendReceive(JSON.stringify(lspInitializationMessage)));
254+
solc.lsp.sendReceive(JSON.stringify({'jsonrpc': '2.0', 'method': 'initialized'}));
256255

257256
// Now, with the LSP server, being set up the following
258257
// can be called as often as needed.
259258
function lspRoundtrip(jsonRpcInputObject)
260259
{
261-
return JSON.parse(solc.lspSendReceive(JSON.stringify(jsonRpcInputObject)));
260+
return JSON.parse(solc.lsp.sendReceive(JSON.stringify(jsonRpcInputObject)));
262261
}
263262
```
264263

common/interfaces.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface ReadCallbackReply {
2+
error?: string;
3+
contents?: string
4+
}
5+
6+
export interface Callbacks {
7+
import(path: string): ReadCallbackReply;
8+
}
9+

wrapper.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@ import { https } from 'follow-redirects';
33
import MemoryStream from 'memorystream';
44
import assert from 'assert';
55
import * as semver from 'semver';
6+
import { Callbacks } from './common/interfaces';
67

78
const Module = module.constructor as any;
89

9-
interface ReadCallbackReply {
10-
error?: string;
11-
contents?: string
12-
}
13-
14-
interface Callbacks {
15-
import(path: string): ReadCallbackReply;
16-
}
17-
1810
function setupMethods (soljson) {
1911
let version;
2012
if ('_solidity_version' in soljson) {
@@ -73,23 +65,23 @@ function setupMethods (soljson) {
7365
const createWrappedLspSend = function() {
7466
const wrappedLspSend = soljson.cwrap('solidity_lsp_send', 'number', ['string']);
7567
return function (input: String) {
76-
let args = [];
68+
const args = [];
7769
args.push(JSON.stringify(input));
7870
return wrappedLspSend.apply(undefined, args);
7971
};
8072
};
8173

8274
// Creates a wrapper around `int solidity_lsp_start(callbacks: Callbacks)`.
8375
const createWrappedLspStart = function() {
84-
const wrappedLspStart = soljson.cwrap('solidity_lsp_start', 'number', []);
85-
return function (callbacks: Callbacks) {
76+
const wrappedLspStart = soljson.cwrap('solidity_lsp_start', 'number', []);
77+
return function (callbacks: Callbacks) {
8678
let readCallback = callbacks.import;
8779
assert(typeof readCallback === 'function', 'Invalid callback specified.');
8880
const copyFromCString = soljson.UTF8ToString || soljson.Pointer_stringify;
8981

9082
const wrappedReadCallback = function (path: string, contents: string, error: string) {
91-
// Calls the user-supplied file read callback and passes the return values
92-
// accordingly to either @p contents or into @p error on failure.
83+
// Calls the user-supplied file read callback and passes the return values
84+
// accordingly to either @p contents or into @p error on failure.
9385
const result = readCallback(copyFromCString(path));
9486
if (typeof result.contents === 'string') {
9587
copyToCString(result.contents, contents);
@@ -104,7 +96,7 @@ function setupMethods (soljson) {
10496
const wrappedFunctionId = addFunction(wrappedReadCallback, 'ii');
10597

10698
try {
107-
// call solidity_lsp_start(callbacks)
99+
// call solidity_lsp_start(callbacks)
108100
let args = [];
109101
args.push(wrappedFunctionId);
110102
let output = wrappedLspStart.apply(undefined, args);
@@ -119,7 +111,7 @@ function setupMethods (soljson) {
119111
// safe to be released.
120112
// Probably by clearly defining semantics and memory lifetimes
121113
// of output strings.
122-
};
114+
};
123115
};
124116

125117
// This is to support multiple versions of Emscripten.

0 commit comments

Comments
 (0)