Skip to content

Commit 7431059

Browse files
committed
wasm: create workspace to hold all open documents (JS half)
In the VS Code extension, in order to have a .js file lint using an open config file, we need to be able to correlate documents together. Do this by using the C API's workspace abstraction to hold all the documents. This commit should not change behavior.
1 parent 48c9fec commit 7431059

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

wasm/quick-lint-js.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class AbstractSyncedDocument {
6363
assertEqual(this._state, DocumentLinterState.NO_WASM_DOC);
6464
this._state = DocumentLinterState.CREATING_WASM_DOC;
6565
this._wasmDocPromise = (async () => {
66-
let process;
6766
let wasmDoc;
6867
try {
69-
process = await this._documentProcessManager.getOrCreateProcessAsync();
70-
wasmDoc = process.createDocumentForVSCode();
68+
let workspace =
69+
await this._documentProcessManager.getOrCreateWorkspaceAsync();
70+
wasmDoc = workspace.createDocument();
7171
} catch (e) {
7272
if (e instanceof ProcessCrashed) {
7373
throw new LintingCrashed(e);
@@ -250,11 +250,10 @@ class AbstractSyncedDocument {
250250
);
251251
this._state = DocumentLinterState.RECOVERING;
252252
this._recoveryPromise = (async () => {
253-
let diags;
254-
let process;
255253
try {
256-
process = await this._documentProcessManager.getOrCreateProcessAsync();
257-
let wasmDoc = process.createDocumentForVSCode();
254+
let workspace =
255+
await this._documentProcessManager.getOrCreateWorkspaceAsync();
256+
let wasmDoc = workspace.createDocument();
258257

259258
// BEGIN CRITICAL SECTION (no awaiting below)
260259
assertEqual(this._state, DocumentLinterState.RECOVERING);
@@ -332,32 +331,35 @@ class DocumentProcessManager {
332331
// TODO(strager): Allow developers to reload the .wasm file.
333332
this._processFactoryPromise = createProcessFactoryAsync();
334333
this._processesCreated = 0;
335-
this._processPromise = null;
334+
this._processAndWorkspacePromise = null;
336335
}
337336

338-
async _createNewProcessAsync() {
337+
async _createNewProcessAndWorkspaceAsync() {
339338
let processFactory = await this._processFactoryPromise;
340339
let process = await processFactory.createProcessAsync();
341340
this._processesCreated += 1;
342-
return process;
341+
let workspace = process.createWorkspaceForVSCode();
342+
return { process, workspace };
343343
}
344344

345-
async getOrCreateProcessAsync() {
345+
async getOrCreateWorkspaceAsync() {
346346
// BEGIN CRITICAL SECTION (no awaiting below)
347-
if (this._processPromise === null) {
348-
this._processPromise = this._createNewProcessAsync();
347+
if (this._processAndWorkspacePromise === null) {
348+
this._processAndWorkspacePromise =
349+
this._createNewProcessAndWorkspaceAsync();
349350
// END CRITICAL SECTION (no awaiting above)
350351
} else {
351352
// END CRITICAL SECTION (no awaiting above)
352353
}
353-
let process = await this._processPromise;
354+
let { process, workspace } = await this._processAndWorkspacePromise;
354355
// BEGIN CRITICAL SECTION (no awaiting below)
355356
while (process.isTainted()) {
356-
this._processPromise = this._createNewProcessAsync();
357+
this._processAndWorkspacePromise =
358+
this._createNewProcessAndWorkspaceAsync();
357359
// END CRITICAL SECTION (no awaiting above)
358-
process = await this._processPromise;
360+
({ process, workspace } = await this._processAndWorkspacePromise);
359361
}
360-
return process;
362+
return workspace;
361363
}
362364

363365
// For testing only.
@@ -546,21 +548,41 @@ class Process {
546548
return `Process(id=${this._idForDebugging})`;
547549
}
548550

549-
createDocumentForVSCode() {
550-
return new DocumentForVSCode(this);
551+
createWorkspaceForVSCode() {
552+
return new WorkspaceForVSCode(this);
551553
}
552554

553555
async createDocumentForWebDemoAsync() {
554556
return new DocumentForWebDemo(this);
555557
}
556558
}
557559

558-
class DocumentForVSCode {
560+
class WorkspaceForVSCode {
559561
constructor(process) {
560562
this._process = process;
561563
this._wasmWorkspace = this._process._vscodeCreateWorkspace();
564+
}
565+
566+
// Returns a DocumentForVSCode
567+
createDocument() {
568+
return new DocumentForVSCode(this);
569+
}
570+
571+
dispose() {
572+
this._process._vscodeDestroyWorkspace(this._wasmWorkspace);
573+
this._wasmWorkspace = null;
574+
}
575+
}
576+
577+
class DocumentForVSCode {
578+
// Private. Call WorkspaceForVSCode#createDocument instead.
579+
constructor(workspace) {
580+
if (!(workspace instanceof WorkspaceForVSCode)) {
581+
throw new TypeError(`Expected WorkspaceForVSCode, but got ${workspace}`);
582+
}
583+
this._process = workspace._process;
562584
this._wasmDoc = this._process._vscodeCreateSourceDocument(
563-
this._wasmWorkspace
585+
workspace._wasmWorkspace
564586
);
565587
}
566588

0 commit comments

Comments
 (0)