Skip to content

Commit

Permalink
update emscriptenCompilerService
Browse files Browse the repository at this point in the history
  • Loading branch information
nokotan committed Dec 31, 2024
1 parent 3006662 commit d4d28c6
Showing 1 changed file with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,29 @@ interface IFileContent {
type?: "text" | "binary";
}

interface ICompileResult {
files: IFileContent[];
interface TaskResult {
commandLine: string;

stdout: string;

success: boolean;
}

interface SourceFile {
/** the name of source file */
name: string;
/** source file content */
content: string;

format?: string;
}

type Response = {
tasks: TaskResult[];

output: SourceFile[];
};

export class EmscriptenService implements CompilerService {

async compile(input: ServiceInput): Promise<ServiceOutput> {
Expand All @@ -42,51 +61,48 @@ export class EmscriptenService implements CompilerService {

for (let i = 0; i < inputFile.length; i++) {
compiledFiles.push({
type: inputFile[i].split(".").pop(),
name: inputFile[i],
options: input.options,
src: files[i].content,
content: files[i].content,
});
}

const options: string = input.options;

const project = {
output: "wasm",
compress: true,
files: compiledFiles,
link_options: input.options
compileOptions: options.split(" "),
linkOptions: options.split(" "),
};
const result = await sendRequestJSON(project, ServiceTypes.Emscripten);
const result = await sendRequestJSON(project, ServiceTypes.Emscripten) as unknown as Response;
const items: any = {};
if (result.success) {
const content = await decodeBinary(result.output || "");
const success = result.output.length > 0;

if (success) {
const wasm = result.output.filter(i => i.name == "a.wasm")[0];
const content = await decodeBinary(wasm.content);
items["a.wasm"] = { content };
}

const findInputFileFromFileName = (fileName: string): string => {
const maybeFileRef = inputFile.filter(x => x.indexOf(fileName) >= 0);
if (maybeFileRef.length > 0) {
return maybeFileRef[0];
} else {
return "";
}
};
// const findInputFileFromFileName = (fileName: string): string => {
// const maybeFileRef = inputFile.filter(x => x.indexOf(fileName) >= 0);
// if (maybeFileRef.length > 0) {
// return maybeFileRef[0];
// } else {
// return "";
// }
// };

let stdout = "";

if (result.tasks) {
for (const task of result.tasks) {
const fileRef = findInputFileFromFileName(task.file);
items[task.file] = { fileRef, console: task.console };
stdout += task.stdout;
}
}

if (result.wasmBindgenJs) {
items["wasm_bindgen.js"] = {
content: result.wasmBindgenJs,
};
}

return {
success: result.success,
console: result.message,
success: success,
console: stdout,
items,
};
}
Expand Down

0 comments on commit d4d28c6

Please sign in to comment.