Skip to content
Open
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
56 changes: 52 additions & 4 deletions CSharpWasmExpo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,60 @@ import { dotnet } from "./wwwroot/_framework/dotnet.js";
import methods from "./splashKitMethods.generated.js";

const parseMethods = (methods) => {
if (!methods || typeof methods !== "string") {
console.error("[SplashKit WASM] Invalid method list received.");
return {};
}

const methodList = methods
.split(",")
.map((method) => method.trim().replace("\n", ""))
.filter(Boolean);

const bindingsFunctions = {};
const missingFunctions = [];
const errorFunctions = [];

for (const name of methodList) {
try {
bindingsFunctions[name] = eval(name);
if (typeof window[name] === "function") {
bindingsFunctions[name] = window[name];
} else {
missingFunctions.push(name);
}
} catch (e) {
console.warn(e);
errorFunctions.push(name);
}
}

if (missingFunctions.length > 0) {
console.warn(
`[SplashKit WASM] ${missingFunctions.length} SplashKit bindings are missing.`
);

console.groupCollapsed("[SplashKit WASM] View missing bindings");

missingFunctions.forEach((name) => {
console.warn(name);
});

console.groupEnd();
}

if (errorFunctions.length > 0) {
console.warn(
`[SplashKit WASM] ${errorFunctions.length} SplashKit bindings could not be loaded.`
);

console.groupCollapsed("[SplashKit WASM] View binding loading errors");

errorFunctions.forEach((name) => {
console.warn(name);
});

console.groupEnd();
}

return bindingsFunctions;
};

Expand All @@ -28,6 +67,14 @@ const loadDotNet = async () => {

const skFunctions = parseMethods(methods);

skFunctions.process_events = () => {
if (typeof __sko_process_events === "function") {
return __sko_process_events();
}

console.warn("[SplashKit WASM] process_events is not wired properly.");
};

setModuleImports("main.js", {
window: {
location: {
Expand All @@ -46,12 +93,14 @@ const CompileAndRun = async (code, reportError) => {
try {
const exports = await loadDotNet();
const result = await exports.CSharpCodeRunner.CompileAndRun(code);

if (result.includes("Compilation failed")) {
const errors = result.split(":");
const errorLine = errors[1].split("Line");

const indexCorrector = 1;
const filePath = "__USERCODE__/code/main.cs";

reportError(
filePath,
result,
Expand All @@ -65,7 +114,6 @@ const CompileAndRun = async (code, reportError) => {
}
};

// This event will be trigger by the csharp compiler
document.addEventListener("compileAndRun", (ev) => {
CompileAndRun(ev.detail.program[0].source, ev.detail.reportError);
});
});
Loading