Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Dec 2, 2024
1 parent 2a3892f commit fbf20d8
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ impl InternalCompilationBuilder {

pub fn add_file(
&mut self,
id: String,
file_id: String,
contents: &str,
) -> Result<AddFileResponse, AddFileError> {
let parse_output = self.parser.parse(Parser::ROOT_KIND, contents);

let import_strings =
extract_imports(parse_output.create_tree_cursor()).map_err(AddFileError::Internal)?;

let file = File::new(id.clone(), parse_output);
self.files.insert(id, file);
let file = File::new(file_id.clone(), parse_output);
self.files.insert(file_id, file);

Ok(AddFileResponse { import_strings })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface compilation {
create: static func(version: string) -> result<internal-compilation-builder, string>;

/// Adds a source file to the compilation unit.
add-file: func(id: string, contents: string) -> result<add-file-response, string>;
add-file: func(file-id: string, contents: string) -> result<add-file-response, string>;

/// Adds an import relationship between files.
add-import: func(file-id: string, import-string: string, imported-file-id: string) -> result<_, string>;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ define_refcell_wrapper! { InternalCompilationBuilder {
.map_err(|e| e.to_string())
}

fn add_file(&self, id: String, contents: String) -> Result<ffi::AddFileResponse, String> {
fn add_file(&self, file_id: String, contents: String) -> Result<ffi::AddFileResponse, String> {
self._borrow_mut_ffi()
.add_file(id, &contents)
.add_file(file_id, &contents)
.map(IntoFFI::_into_ffi)
.map_err(|e| e.to_string())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as generated from "../../../wasm/index.mjs";

const InternalCompilationBuilder = generated.compilation.InternalCompilationBuilder;
type InternalCompilationBuilder = generated.compilation.InternalCompilationBuilder;

export interface CompilationOptions {
/**
* The language version to parse files with.
*/
version: string;

resolveImport: (fileId: string, importString: string) => Promise<string | undefined>;

/**
* Callback used by this builder to load the contents of a file.
* The user is responsible for fetching the file from the filesystem.
* If the file is not found, the callback should return undefined.
* If the file cannot be read, the callback should throw an error, and it will be propagated to the caller.
*/
loadFileContents: (fileId: string) => Promise<string | undefined>;
}

/**
* A builder for creating compilation units.
* Allows incrementally building a transitive list of all files and their imports.
*/
export class CompilationBuilder {
private readonly pendingTasks: Promise<void>[] = [];
private readonly resolvedFileIds: Set<string> = new Set();

private constructor(
private readonly options: CompilationOptions,
private readonly internal: InternalCompilationBuilder,
) {}

/**
* Creates a new compilation builder for the specified language version.
*/
public create(options: CompilationOptions): CompilationBuilder {
const internal = InternalCompilationBuilder.create(options.version);
return new CompilationBuilder(options, internal);
}

/**
* Adds a source file to the compilation unit.
*/
public async addFile(fileId: string, contents: string): Promise<void> {
const { importStrings } = this.internal.addFile(fileId, contents);

for (const importString of importStrings) {
this.pendingTasks.push(this.addImport(fileId, importString));
}

await this.executePendingTasks();
}

private async addImport(fileId: string, importString: string): Promise<void> {
const importedFileId = await this.options.resolveImport(fileId, importString);
if (importedFileId === undefined) {
return;
}

this.internal.addImport(fileId, importString, importedFileId);
}

private async executePendingTasks(): Promise<void> {
while (this.pendingTasks.length > 0) {
const pendingTasks = this.pendingTasks.splice(0);
await Promise.all(pendingTasks);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as generated from "../../../wasm/index.mjs";
import * as builder from "./builder.mjs";

// This internal implementation is exposed via an async wrapper below:
const InternalCompilationBuilder = generated.compilation.InternalCompilationBuilder;
type InternalCompilationBuilder = generated.compilation.InternalCompilationBuilder;
// This generated 'InternalCompilationBuilder' is exposed via an async wrapper here:
export const CompilationBuilder = builder.CompilationBuilder;
export type CompilationBuilder = builder.CompilationBuilder;

export const CompilationUnit = generated.compilation.CompilationUnit;
export type CompilationUnit = generated.compilation.CompilationUnit;
Expand All @@ -12,29 +13,3 @@ export type FilesIterator = generated.compilation.FilesIterator;

export const File = generated.compilation.File;
export type File = generated.compilation.File;

/**
* A builder for creating compilation units.
* Allows incrementally building a transitive list of all files and their imports.
*/
export class CompilationBuilder {
private constructor(private readonly internal: InternalCompilationBuilder) {}

/**
* Creates a new compilation builder for the specified language version.
*/
public create(version: string): CompilationBuilder {
return new CompilationBuilder(InternalCompilationBuilder.create(version));
}

/**
* Adds a source file to the compilation unit.
*/
public async addFile(id: string, contents: string): Promise<void> {
const queue = new PromiseQueue();

this.enqueueFileAdditions(queue, id, contents);

await queue.everything();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fbf20d8

Please sign in to comment.