-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
88 lines (74 loc) · 2.89 KB
/
app.js
File metadata and controls
88 lines (74 loc) · 2.89 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
// Wait for the module to be ready
const status = document.getElementById('status');
const output = document.getElementById('output');
// Initialize CodeMirror
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
lineNumbers: true,
mode: 'text/x-c++src',
indentUnit: 4
});
// Initialize the application
async function initialize() {
try {
status.textContent = 'Loading WebAssembly...';
console.log("Loading WebAssembly module...");
// Wait for the module to be ready
const Module = await window.loomModulePromise;
console.log("WebAssembly module loaded:", Module);
// Test the module
try {
console.log("Testing module functions...");
// Get the function pointer
const testResult = Module.ccall(
'testFunction', // function name
'string', // return type
[], // argument types
[] // arguments
);
console.log("testFunction result:", testResult);
output.textContent = `Test successful! ${testResult}\n\n`;
// Store the module for later use
window.loomModule = Module;
status.textContent = 'Ready';
document.getElementById('run-btn').disabled = false;
console.log("Initialization complete");
} catch (e) {
console.error("Error testing module:", e);
throw new Error(`Module test failed: ${e}`);
}
} catch (e) {
const errorMsg = `Initialization failed: ${e}`;
console.error(errorMsg, e);
status.textContent = errorMsg;
output.textContent = `Error details: ${e.stack || e}`;
}
}
// Run button handler
document.getElementById('run-btn').addEventListener('click', async () => {
const status = document.getElementById('status');
const output = document.getElementById('output');
status.textContent = 'Running...';
try {
if (!window.loomModule) {
throw new Error("WebAssembly module not loaded");
}
const code = editor.getValue();
console.log("Running with code:", code);
// Call the C++ function using ccall
const result = window.loomModule.ccall(
'processLoomCode', // function name
'string', // return type
['string'], // argument types
[code] // arguments
);
output.textContent += `> ${code}\n${result}\n\n`;
status.textContent = 'Done';
} catch (e) {
const errorMsg = `Error: ${e}`;
console.error(errorMsg, e);
status.textContent = errorMsg;
output.textContent += `Error: ${e.message || e}\n\n`;
}
});
// Start the application
initialize();