-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathclang-wasi.ts
More file actions
100 lines (91 loc) · 2.4 KB
/
Copy pathclang-wasi.ts
File metadata and controls
100 lines (91 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// file: clang-wasi.ts
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";
import { commands } from '@yowasp/clang';
class clangWasiHandler implements FormatHandler {
public name: string = "clang-wasi";
public supportedFormats: FileFormat[] = [
{
name: "C Source File",
format: "c",
extension: "c",
mime: "text/x-c",
from: true,
to: false,
internal: "c",
category: Category.CODE,
lossless: false,
},
{
name: "C++ Source File",
format: "cpp",
extension: "cpp",
mime: "text/x-c++src",
from: true,
to: false,
internal: "cpp",
category: Category.CODE,
lossless: false,
},
{
name: "Assembly Source File",
format: "asm",
extension: "s",
mime: "text/x-asm",
from: true,
to: false,
internal: "asm",
category: Category.CODE,
lossless: false,
},
{
name: "WebAssembly Binary (Wasm)",
format: "wasm",
extension: "wasm",
mime: "application/wasm",
from: false,
to: true,
internal: "wasm",
category: Category.CODE,
lossless: true,
},
];
public ready: boolean = false;
async init () {
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
for (const inputFile of inputFiles) {
const output = await commands
[inputFormat.internal === "cpp" ? "clang++" : "clang"]
(
[inputFile.name, "-o", "out.wasm", "-O3", "-fno-exceptions"],
// this build specifically excludes exceptions for some reason
{
[inputFile.name]: inputFile.bytes
}
);
if (!output) throw new Error("clang did not return any files?");
const data = output["out.wasm"];
let bytes;
if (data instanceof Uint8Array) { // js wtf is this ??
bytes = data;
} else if (typeof data === "string") {
bytes = new TextEncoder().encode(data);
} else {
throw new Error("clang output was not a file");
}
outputFiles.push({
name: inputFile.name.replace(/\.[^.]+$/, "") + `.wasm`,
bytes,
});
}
return outputFiles;
}
}
export default clangWasiHandler;