Skip to content

Commit 0621f1e

Browse files
committed
wasm: refactor preparing for Process reuse
Each DocumentLinter creates and manages its own Process (WebAssembly memory space). I want DocumentLinter-s to share a single Process. Refactor DocumentLinter to take a DocumentProcessManager which will, in the future, store the single Process. This commit should not change behavior.
1 parent 664a06e commit 0621f1e

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

plugin/vscode/extension.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ let vscode = require("vscode");
77
let {
88
DiagnosticSeverity,
99
DocumentLinter,
10-
createProcessFactoryAsync,
10+
DocumentProcessManager,
1111
} = require("quick-lint-js-wasm/quick-lint-js.js");
1212

13-
// TODO(strager): Allow developers to reload the .wasm file.
14-
let processFactoryPromise = createProcessFactoryAsync();
15-
1613
class VSCodeDocumentLinter {
1714
constructor(document, diagnosticCollection) {
1815
this._documentLinter = new DocumentLinter(
@@ -30,7 +27,7 @@ class VSCodeDocumentLinter {
3027
diagnosticCollection.delete(document.uri);
3128
},
3229
},
33-
processFactoryPromise
30+
new DocumentProcessManager()
3431
);
3532
}
3633

wasm/quick-lint-js.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class DocumentLinter {
4747
// getText(): string;
4848
// setDiagnostics(diagnostics: Object[]): void;
4949
// removeDiagnostics(): void;
50-
constructor(document, processFactoryPromise) {
50+
constructor(document, documentProcessManager) {
5151
this._document = document;
52-
this._processFactoryPromise = processFactoryPromise;
52+
this._documentProcessManager = documentProcessManager;
5353
this._state = DocumentLinterState.NO_PARSER;
5454

5555
// Used only in states: CREATING_PARSER
@@ -69,7 +69,7 @@ class DocumentLinter {
6969
assertEqual(this._state, DocumentLinterState.NO_PARSER);
7070
this._state = DocumentLinterState.CREATING_PARSER;
7171
this._parserPromise = (async () => {
72-
let factory = await this._processFactoryPromise;
72+
let factory = await this._documentProcessManager._processFactoryPromise;
7373
// TODO(strager): Reuse processes across documents.
7474
let process = await factory.createProcessAsync();
7575
let parser = await process.createDocumentForVSCodeAsync();
@@ -252,7 +252,7 @@ class DocumentLinter {
252252
let diags;
253253
try {
254254
// TODO(strager): Reuse processes across documents.
255-
let factory = await this._processFactoryPromise;
255+
let factory = await this._documentProcessManager._processFactoryPromise;
256256
let process = await factory.createProcessAsync();
257257
let parser = await process.createDocumentForVSCodeAsync();
258258

@@ -289,6 +289,14 @@ class DocumentLinter {
289289
}
290290
exports.DocumentLinter = DocumentLinter;
291291

292+
class DocumentProcessManager {
293+
constructor() {
294+
// TODO(strager): Allow developers to reload the .wasm file.
295+
this._processFactoryPromise = createProcessFactoryAsync();
296+
}
297+
}
298+
exports.DocumentProcessManager = DocumentProcessManager;
299+
292300
async function createProcessFactoryAsync() {
293301
if (typeof window === "undefined") {
294302
// Node.js.

wasm/test-document.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ let {
1010
DiagnosticSeverity,
1111
DocumentLinter,
1212
DocumentLinterDisposed,
13+
DocumentProcessManager,
1314
LintingCrashed,
1415
ProcessCrashed,
1516
createProcessFactoryAsync,
1617
} = qljs;
1718

18-
let processFactoryPromise = createProcessFactoryAsync();
19-
2019
describe("DocumentLinter", () => {
2120
let toDisposeAfterTest = [];
2221
function disposeAfterTest(disposable) {
@@ -38,7 +37,7 @@ describe("DocumentLinter", () => {
3837
it("diagnostic severity", async () => {
3938
let document = new MockDocument("let x;let x;\nundeclaredVariable");
4039
let linter = disposeAfterTest(
41-
new DocumentLinter(document, processFactoryPromise)
40+
new DocumentLinter(document, new DocumentProcessManager())
4241
);
4342
await linter.editorChangedVisibilityAsync();
4443

@@ -63,7 +62,7 @@ describe("DocumentLinter", () => {
6362
it("opening editor lints", async () => {
6463
let document = new MockDocument("let x;let x;");
6564
let linter = disposeAfterTest(
66-
new DocumentLinter(document, processFactoryPromise)
65+
new DocumentLinter(document, new DocumentProcessManager())
6766
);
6867

6968
await linter.editorChangedVisibilityAsync();
@@ -76,7 +75,7 @@ describe("DocumentLinter", () => {
7675
it("applying change to unopened editor lints", async () => {
7776
let document = new MockDocument("let x;");
7877
let linter = disposeAfterTest(
79-
new DocumentLinter(document, processFactoryPromise)
78+
new DocumentLinter(document, new DocumentProcessManager())
8079
);
8180
assert.deepStrictEqual(document.diagnostics, []);
8281

@@ -104,7 +103,7 @@ describe("DocumentLinter", () => {
104103

105104
let document = new MockDocument("let x;");
106105
let linter = disposeAfterTest(
107-
new DocumentLinter(document, processFactoryPromise)
106+
new DocumentLinter(document, new DocumentProcessManager())
108107
);
109108
await linter.editorChangedVisibilityAsync();
110109
assert.deepStrictEqual(document.diagnostics, []);
@@ -133,14 +132,14 @@ describe("DocumentLinter", () => {
133132

134133
it("dispose unused linter", async () => {
135134
let document = new MockDocument("let x;");
136-
let linter = new DocumentLinter(document, processFactoryPromise);
135+
let linter = new DocumentLinter(document, new DocumentProcessManager());
137136
// Should not throw.
138137
await linter.disposeAsync();
139138
});
140139

141140
it("dispose initializing linter", async () => {
142141
let document = new MockDocument("hello.js", "let x;");
143-
let linter = new DocumentLinter(document, processFactoryPromise);
142+
let linter = new DocumentLinter(document, new DocumentProcessManager());
144143
let promise = linter.editorChangedVisibilityAsync();
145144

146145
// Should not throw.
@@ -151,7 +150,7 @@ describe("DocumentLinter", () => {
151150
it("concurrent edits are applied in order of calls", async () => {
152151
let document = new MockDocument("let x;");
153152
let linter = disposeAfterTest(
154-
new DocumentLinter(document, processFactoryPromise)
153+
new DocumentLinter(document, new DocumentProcessManager())
155154
);
156155
await linter.editorChangedVisibilityAsync();
157156

@@ -223,7 +222,7 @@ describe("DocumentLinter", () => {
223222
let linter = null;
224223
try {
225224
let document = new MockDocument("let x;let x;\n");
226-
linter = new DocumentLinter(document, processFactoryPromise);
225+
linter = new DocumentLinter(document, new DocumentProcessManager());
227226

228227
let crashedOpeningEditor = await didLintingCrashAsync(async () => {
229228
await linter.editorChangedVisibilityAsync();
@@ -304,7 +303,7 @@ describe("DocumentLinter", () => {
304303
let linter;
305304
try {
306305
let document = new MockDocument("const x = 10;");
307-
linter = new DocumentLinter(document, processFactoryPromise);
306+
linter = new DocumentLinter(document, new DocumentProcessManager());
308307
let shouldOpenEditorBeforeChanges = rng.nextCoinFlip();
309308
if (shouldOpenEditorBeforeChanges) {
310309
await linter.editorChangedVisibilityAsync();

0 commit comments

Comments
 (0)