Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@types/pako": "^2.0.4",
"@types/papaparse": "^5.5.2",
"@types/three": "^0.182.0",
"@yowasp/clang": "^22.0.0-git20542-10",
"bson": "^7.2.0",
"celaria-formats": "^1.0.2",
"chess.js": "^1.4.0",
Expand Down
100 changes: 100 additions & 0 deletions src/handlers/clang-wasi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import xcursorHandler from "./xcursor.ts";
import shToElfHandler from "./shToElf.ts";
import cssHandler from "./css.ts";
import TypstHandler from "./typst.ts";
import clangWasiHandler from "./clang-wasi.ts";

const handlers: FormatHandler[] = [];
try { handlers.push(new svgTraceHandler()) } catch (_) { };
Expand Down Expand Up @@ -150,5 +151,6 @@ try { handlers.push(new xcursorHandler()) } catch (_) { };
try { handlers.push(new shToElfHandler()) } catch (_) { };
try { handlers.push(new cssHandler()) } catch (_) { };
try { handlers.push(new TypstHandler()) } catch (_) { };
try { handlers.push(new clangWasiHandler()) } catch (_) { };

export default handlers;
3 changes: 2 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default defineConfig({
exclude: [
"@ffmpeg/ffmpeg",
"@sqlite.org/sqlite-wasm",
"@bokuweb/zstd-wasm"
"@bokuweb/zstd-wasm",
"@yowasp/clang",
]
},
base: "/convert/",
Expand Down
Loading