Update playground
This commit is contained in:
@@ -16,12 +16,10 @@
|
||||
<body>
|
||||
<div class="box">
|
||||
<h1>Solstice Playground</h1>
|
||||
<h2>Broken right now. Sorry!</h2>
|
||||
|
||||
<textarea class="code" id="editor" spellcheck="false" placeholder="Enter Solstice code here..." rows="20">
|
||||
puts "Hello from Solstice via WebAssembly!"
|
||||
puts "Let's do some math:"
|
||||
puts 10 + 20 + 12</textarea>
|
||||
</textarea>
|
||||
|
||||
<div id="controls">
|
||||
<button id="runBtn" style="font-size: 20px;">Loading WASM...</button>
|
||||
@@ -30,57 +28,32 @@ puts 10 + 20 + 12</textarea>
|
||||
<h3>Output:</h3>
|
||||
<pre class="code" id="output">Output will appear here. Click "Run Code" to run!</pre>
|
||||
|
||||
<script type="module">
|
||||
import createSolsticeModule from './playground.js';
|
||||
|
||||
let solsticeModule = null;
|
||||
<script src="playground.js"></script>
|
||||
<script>
|
||||
const outputDiv = document.getElementById('output');
|
||||
const codeArea = document.getElementById('editor');
|
||||
const runBtn = document.getElementById('runBtn');
|
||||
const editor = document.getElementById('editor');
|
||||
|
||||
// Initialize the Emscripten module
|
||||
createSolsticeModule({
|
||||
// Redirect stdout to our output div
|
||||
print: (text) => {
|
||||
outputDiv.textContent += text + "\n";
|
||||
console.log(text);
|
||||
},
|
||||
// Redirect stderr to our output div (and console)
|
||||
printErr: (text) => {
|
||||
outputDiv.textContent += "[stderr] " + text + "\n";
|
||||
console.error(text);
|
||||
}
|
||||
}).then((instance) => {
|
||||
solsticeModule = instance;
|
||||
runBtn.textContent = "Run Code";
|
||||
runBtn.disabled = false;
|
||||
console.log("Solstice WASM module loaded successfully");
|
||||
}).catch(err => {
|
||||
console.error("Failed to load WASM module:", err);
|
||||
outputDiv.innerHTML = '<span class="error">Error loading WASM module. See console for details.</span>';
|
||||
runBtn.textContent = "Load Failed";
|
||||
});
|
||||
// Emscripten provides a 'Module' object that fires 'onRuntimeInitialized'
|
||||
Module.onRuntimeInitialized = () => {
|
||||
outputDiv.innerText = "Ready to run.";
|
||||
runBtn.innerText = "Run";
|
||||
|
||||
runBtn.addEventListener('click', () => {
|
||||
if (!solsticeModule) return;
|
||||
runBtn.addEventListener('click', () => {
|
||||
const source = codeArea.value;
|
||||
|
||||
outputDiv.textContent = ""; // Clear previous output
|
||||
const code = editor.value;
|
||||
|
||||
try {
|
||||
// Call the C++ function 'run_source' exported via EMSCRIPTEN_KEEPALIVE
|
||||
// int run_source(char* code)
|
||||
solsticeModule.ccall(
|
||||
'run_source', // function name
|
||||
'number', // return type
|
||||
['string'], // argument types
|
||||
[code] // arguments
|
||||
// Use 'ccall' to call the C function:
|
||||
// ccall(name, returnType, [argTypes], [args])
|
||||
const result = Module.ccall(
|
||||
'solstice_run', // C function name
|
||||
'string', // Return type
|
||||
['string'], // Argument types
|
||||
[source] // Arguments
|
||||
);
|
||||
} catch (e) {
|
||||
outputDiv.innerHTML += `\n<span class="error">[Runtime Error] ${e}</span>`;
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
|
||||
outputDiv.innerText = result;
|
||||
});
|
||||
};
|
||||
</script>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user