Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Modify clangService.ts for multiple sources compilation #504

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/compilerServices/clangService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { CompilerService, ServiceInput, ServiceOutput, Language } from "./types";
import { CompilerService, ServiceInput, ServiceOutput, Language, InputFile } from "./types";
import { sendRequestJSON, ServiceTypes } from "./sendRequest";
import { decodeBinary } from "./utils";

Expand All @@ -31,23 +31,25 @@ export class ClangService implements CompilerService {

async compile(input: ServiceInput): Promise<ServiceOutput> {
const files = Object.values(input.files);
if (files.length !== 1) {
throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`);
}
const [ fileRef ] = Object.keys(input.files);
const src = files[0].content;
const from = this.lang;
const [fileRef] = Object.keys(input.files);
const inputFile = Object.keys(input.files);
const File = function(inputFile: string[], files: InputFile[]) {
const compileFile = [];
for (let i = 0; i < inputFile.length; i++) {
const f = {
type: inputFile[i].split(".").slice(-1)[0],
name: inputFile[i].split("/").slice(-1)[0],
options: input.options,
src: files[i].content,
};
compileFile.push(f);
}
return compileFile;
};
const project = {
output: "wasm",
compress: true,
files: [
{
type: from,
name: "file." + from,
options: input.options,
src
}
]
files: File(inputFile, files)
};
const result = await sendRequestJSON(project, ServiceTypes.Clang);
const items: any = {};
Expand Down
32 changes: 26 additions & 6 deletions tests/compilerServices/clangService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("Tests for clangService", () => {
expect(sendRequestJSON).toHaveBeenCalledWith({
output: "wasm",
compress: true,
files: [{ type: "c", name: "file.c", options: "options", src: "a" }]
files: [{ type: "c", name: "a.c", options: "options", src: "a" }]
}, 2);
expect(decodeBinary).toHaveBeenCalledWith("out");
expect(output).toEqual({
Expand All @@ -56,11 +56,31 @@ describe("Tests for clangService", () => {
console: "error"
});
});
it("should throw an error when trying to compile more than one file", async () => {
it("should compile when trying to compile more than one file", async () => {
const clangService = await createCompilerService(Language.C, Language.Wasm);
const input = { files: { "a.c": { content: "a" }, "b.c": { content: "b" }}};
await expect(clangService.compile(input))
.rejects
.toThrow("Supporting compilation of a single file, but 2 file(s) found");
sendRequestJSON.mockImplementation(() => ({
success: true,
output: "out",
tasks: [{ console }],
message: "response-message"
}));
const input = { files: { "a.c": { content: "a" }, "b.c": { content: "b" }}, options: "options" };
const console = { log: jest.fn() };
const output = await clangService.compile(input);
expect(sendRequestJSON).toHaveBeenCalledWith({
output: "wasm",
compress: true,
files: [
{ type: "c", name: "a.c", options: "options", src: "a" },
{ type: "c", name: "b.c", options: "options", src: "b" }
]
}, 2);
expect(decodeBinary).toHaveBeenCalledWith("out");
expect(output).toEqual({
success: true,
items: { "a.wasm": { content: "out", fileRef: "a.c", console, }},
console: "response-message"
});
decodeBinary.mockClear();
});
});