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

Commit d40c8cc

Browse files
committed
Now We can use Header File.
1 parent d9e9288 commit d40c8cc

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

src/compilerServices/clangService.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,29 @@ export class ClangService implements CompilerService {
3131

3232
async compile(input: ServiceInput): Promise<ServiceOutput> {
3333
const files = Object.values(input.files);
34-
if (files.length !== 1) {
35-
throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`);
36-
}
34+
// if (files.length !== 1) {
35+
// throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`);
36+
// }
3737
const [ fileRef ] = Object.keys(input.files);
3838
const src = files[0].content;
39+
const header = files[1].content;
3940
const from = this.lang;
4041
const project = {
4142
output: "wasm",
4243
compress: true,
4344
files: [
4445
{
4546
type: from,
46-
name: "file." + from,
47-
options: input.options,
48-
src
49-
}
47+
name: "main."+from,
48+
options:input.options,
49+
src,
50+
},
51+
{
52+
type:Language.h,
53+
name:"header."+ Language.h,
54+
options:input.options,
55+
src:header,
56+
},
5057
]
5158
};
5259
const result = await sendRequestJSON(project, ServiceTypes.Clang);

src/compilerServices/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export enum Language {
3030
JavaScript = "javascript",
3131
TypeScript = "typescript",
3232
Toml = "toml",
33-
Text = "text"
33+
Text = "text",
34+
h = "h",
35+
hpp = "hpp"
3436
}
3537

3638
export interface InputFile {

src/service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export { Language } from "./compilerServices";
6161

6262
function getProjectFilePath(file: File): string {
6363
const project = file.getProject();
64-
return file.getPath(project);
64+
const result = file.getPath(project);
65+
return result;
6566
}
6667

6768
export class ServiceWorker {
@@ -197,18 +198,17 @@ export class Service {
197198
return annotations;
198199
}
199200

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

203204
const service = await createCompilerService(from, to);
204-
205-
const fileNameMap: {[name: string]: File} = files.reduce((acc: any, f: File) => {
205+
const fileNameMap: {[name: string]: File} = files[0].reduce((acc: any, f: any) => {
206206
acc[getProjectFilePath(f)] = f;
207207
return acc;
208208
}, {} as any);
209209

210210
const input = {
211-
files: files.reduce((acc: any, f: File) => {
211+
files: files[0].reduce((acc: any, f: File) => {
212212
acc[getProjectFilePath(f)] = {
213213
content: f.getData(),
214214
};
@@ -218,7 +218,7 @@ export class Service {
218218
};
219219
const result = await service.compile(input);
220220

221-
for (const file of files) {
221+
for (const file of files[0]) {
222222
file.setProblems([]);
223223
}
224224

templates/hello_world_c/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as gulp from "gulp";
22
import { Service, project } from "@wasm/studio-utils";
33

44
gulp.task("build", async () => {
5-
const data = await Service.compileFile(project.getFile("src/main.c"), "c", "wasm", "-g -O3");
5+
const data = await Service.compileFile([project.getFile("src/main.c"),project.getFile("src/header.h")], "c", "wasm", "-g -O3");
66
const outWasm = project.newFile("out/main.wasm", "wasm", true);
77
outWasm.setData(data);
88
});

templates/hello_world_c/src/header.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int hello(int a,int b){
2+
return a+b;
3+
}

templates/hello_world_c/src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include <stdio.h>
22
#include <sys/uio.h>
3+
#include "header.h"
34

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

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

0 commit comments

Comments
 (0)