Skip to content

Commit 473414f

Browse files
committed
simple refactor to resolve linting and legacy variables
1 parent 646b4dd commit 473414f

File tree

2 files changed

+76
-72
lines changed

2 files changed

+76
-72
lines changed

common/interfaces.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
export interface ReadCallbackReply {
22
error?: string;
3-
contents?: string
3+
contents?: string;
44
}
55

66
export interface Callbacks {
7-
import(path: string): ReadCallbackReply;
7+
import (path: string): ReadCallbackReply;
88
}
9-

wrapper.ts

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -62,79 +62,84 @@ function setupMethods (soljson) {
6262
soljson.setValue(ptr, buffer, '*');
6363
};
6464

65-
// Creates a wrapper around `int solidity_lsp_start(callbacks: Callbacks)`.
66-
const createWrappedLspStart = function() {
67-
if (!('_solidity_lsp_start' in soljson))
68-
return null;
69-
const wrappedLspStart = soljson.cwrap('solidity_lsp_start', 'number', []);
70-
return function (callbacks: Callbacks) {
71-
let readCallback = callbacks.import;
72-
assert(typeof readCallback === 'function', 'Invalid callback specified.');
73-
const copyFromCString = soljson.UTF8ToString || soljson.Pointer_stringify;
74-
75-
const wrappedReadCallback = function (path: string, contents: string, error: string) {
76-
// Calls the user-supplied file read callback and passes the return values
77-
// accordingly to either @p contents or into @p error on failure.
78-
const result = readCallback(copyFromCString(path));
79-
if (typeof result.contents === 'string') {
80-
copyToCString(result.contents, contents);
81-
}
82-
if (typeof result.error === 'string') {
83-
copyToCString(result.error, error);
84-
}
85-
};
86-
87-
const addFunction = soljson.addFunction || soljson.Runtime.addFunction;
88-
const removeFunction = soljson.removeFunction || soljson.Runtime.removeFunction;
89-
const wrappedFunctionId = addFunction(wrappedReadCallback, 'ii');
90-
91-
try {
92-
// call solidity_lsp_start(callbacks)
93-
let args = [];
94-
args.push(wrappedFunctionId);
95-
let output = wrappedLspStart.apply(undefined, args);
96-
removeFunction(wrappedFunctionId);
97-
return output;
98-
} catch (e) {
99-
removeFunction(wrappedFunctionId);
100-
throw e;
101-
}
102-
// NOTE: We MUST NOT reset the compiler here.
103-
// We instead could try to make sure to only release memory that is
104-
// safe to be released.
105-
// Probably by clearly defining semantics and memory lifetimes
106-
// of output strings.
107-
};
108-
};
109-
110-
// C signature : int solidity_lsp_send(char const* jsonRpcInputObject);
111-
// TS signature : int send(object jsonRpcInputObject);
112-
const createWrappedLspSend = function() {
113-
if (!('_solidity_lsp_send' in soljson))
114-
return null;
115-
const wrappedLspSend = soljson.cwrap('solidity_lsp_send', 'number', ['string']);
116-
return function (input: String) {
117-
const args = [];
118-
args.push(JSON.stringify(input));
119-
return wrappedLspSend.apply(undefined, args);
65+
// Creates a wrapper around `int solidity_lsp_start(callbacks: Callbacks)`.
66+
const createWrappedLspStart = function () {
67+
if (!('_solidity_lsp_start' in soljson)) {
68+
return () => {
69+
throw new Error('lsp is not supported on this version.');
70+
};
71+
}
72+
73+
const wrappedLspStart = soljson.cwrap('solidity_lsp_start', 'number', []);
74+
75+
return function (callbacks: Callbacks) {
76+
const readCallback = callbacks.import;
77+
78+
assert(typeof readCallback === 'function', 'Invalid callback specified.');
79+
const copyFromCString = soljson.UTF8ToString || soljson.Pointer_stringify;
80+
81+
const wrappedReadCallback = function (path: string, contents: string, error: string) {
82+
// Calls the user-supplied file read callback and passes the return values
83+
// accordingly to either @p contents or into @p error on failure.
84+
const result = readCallback(copyFromCString(path));
85+
86+
if (typeof result.contents === 'string') {
87+
copyToCString(result.contents, contents);
88+
}
89+
90+
if (typeof result.error === 'string') {
91+
copyToCString(result.error, error);
92+
}
93+
};
94+
95+
const addFunction = soljson.addFunction || soljson.Runtime.addFunction;
96+
const removeFunction = soljson.removeFunction || soljson.Runtime.removeFunction;
97+
const wrappedFunctionId = addFunction(wrappedReadCallback, 'ii');
98+
99+
try {
100+
// call solidity_lsp_start(callbacks)
101+
const output = wrappedLspStart(wrappedFunctionId);
102+
removeFunction(wrappedFunctionId);
103+
return output;
104+
} catch (e) {
105+
removeFunction(wrappedFunctionId);
106+
throw e;
107+
}
108+
109+
// NOTE: We MUST NOT reset the compiler here.
110+
// We instead could try to make sure to only release memory that is safe
111+
// to be released. Probably by clearly defining semantics and memory
112+
// lifetimes of output strings.
120113
};
121114
};
122115

123-
// C signature : char* solidity_lsp_send_receive(char const* jsonRpcInputObject);
124-
// TS signature : object sendReceive(object jsonRpcInputObject);
125-
//
126-
// sendReceive send one message to the LSP server (notification or method call).
127-
// The method call may reply with zero or one message that is going to be returned.
128-
const createWrappedLspSendReceive = function() {
129-
if (!('_solidity_lsp_send_receive' in soljson))
130-
return null;
116+
// C signature : int solidity_lsp_send(char const* jsonRpcInputObject);
117+
// TS signature : int send(object jsonRpcInputObject);
118+
const createWrappedLspSend = function () {
119+
if (!('_solidity_lsp_send' in soljson)) {
120+
return () => {
121+
throw new Error('lsp is not supported on this version.');
122+
};
123+
}
124+
125+
const wrappedLspSend = soljson.cwrap('solidity_lsp_send', 'number', ['string']);
126+
return (input: string) => wrappedLspSend(JSON.stringify(input));
127+
};
128+
129+
// C signature : char* solidity_lsp_send_receive(char const* jsonRpcInputObject);
130+
// TS signature : object sendReceive(object jsonRpcInputObject);
131+
//
132+
// sendReceive send one message to the LSP server (notification or method call).
133+
// The method call may reply with zero or one message that is going to be returned.
134+
const createWrappedLspSendReceive = function () {
135+
if (!('_solidity_lsp_send_receive' in soljson)) {
136+
return () => {
137+
throw new Error('lsp is not supported on this version.');
138+
};
139+
}
140+
131141
const wrappedLspSendReceive = soljson.cwrap('solidity_lsp_send_receive', 'string', ['string']);
132-
return function (input: String) {
133-
const args = [];
134-
args.push(JSON.stringify(input));
135-
const reply = wrappedLspSendReceive.apply(undefined, args);
136-
return JSON.parse(reply);
137-
};
142+
return (input: string) => JSON.parse(wrappedLspSendReceive(JSON.stringify(input)));
138143
};
139144

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

0 commit comments

Comments
 (0)