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

Now We can Use Header File . #365

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/compilerServices/clangService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@ 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 inputFile=Object.keys(input.files);
const File = function(inputFile:any,file:any){
const compileFile =[];
for(let i=0;i<inputFile.length;i++){
const f={
type:inputFile[i].split("/")[1].split(".")[1],
name:inputFile[i].split("/")[1],
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
4 changes: 3 additions & 1 deletion src/compilerServices/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export enum Language {
JavaScript = "javascript",
TypeScript = "typescript",
Toml = "toml",
Text = "text"
Text = "text",
h = "h",
hpp = "hpp"
}

export interface InputFile {
Expand Down
12 changes: 6 additions & 6 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export { Language } from "./compilerServices";

function getProjectFilePath(file: File): string {
const project = file.getProject();
return file.getPath(project);
const result = file.getPath(project);
return result;
}

export class ServiceWorker {
Expand Down Expand Up @@ -197,18 +198,17 @@ export class Service {
return annotations;
}

static async compileFiles(files: File[], from: Language, to: Language, options = ""): Promise<{ [name: string]: (string|ArrayBuffer); }> {
static async compileFiles(files: Array<any>, from: Language, to: Language, options = ""): Promise<{ [name: string]: (string|ArrayBuffer); }> {
gaEvent("compile", "Service", `${from}->${to}`);

const service = await createCompilerService(from, to);

const fileNameMap: {[name: string]: File} = files.reduce((acc: any, f: File) => {
const fileNameMap: {[name: string]: File} = files[0].reduce((acc: any, f: any) => {
acc[getProjectFilePath(f)] = f;
return acc;
}, {} as any);

const input = {
files: files.reduce((acc: any, f: File) => {
files: files[0].reduce((acc: any, f: File) => {
acc[getProjectFilePath(f)] = {
content: f.getData(),
};
Expand All @@ -218,7 +218,7 @@ export class Service {
};
const result = await service.compile(input);

for (const file of files) {
for (const file of files[0]) {
file.setProblems([]);
}

Expand Down
2 changes: 1 addition & 1 deletion templates/hello_world_c/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as gulp from "gulp";
import { Service, project } from "@wasm/studio-utils";

gulp.task("build", async () => {
const data = await Service.compileFile(project.getFile("src/main.c"), "c", "wasm", "-g -O3");
const data = await Service.compileFile([project.getFile("src/main.c"),project.getFile("src/header.h")], "c", "wasm", "-g -O3");
const outWasm = project.newFile("out/main.wasm", "wasm", true);
outWasm.setData(data);
});
Expand Down
3 changes: 3 additions & 0 deletions templates/hello_world_c/src/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int hello(int a,int b){
return a+b;
}
3 changes: 3 additions & 0 deletions templates/hello_world_c/src/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <stdio.h>
#include <sys/uio.h>
#include "header.h"

#define WASM_EXPORT __attribute__((visibility("default")))

WASM_EXPORT
int main(void) {
int c= hello(3,2);//you can define any function in headerfile and use it here.It is just an example.
printf("%d",c);
printf("Hello World\n");
}

Expand Down