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
96 changes: 76 additions & 20 deletions playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -270,32 +270,85 @@ <h3>Interactive Playground <span id="chai-status" class="loading">Loading&hellip
outputEl.scrollTop = outputEl.scrollHeight;
}

var Module = {
print: function(text) {
appendOutput(text);
},
printErr: function(text) {
appendOutput(text, 'chai-output-error');
},
onRuntimeInitialized: function() {
statusEl.textContent = 'Ready';
statusEl.className = 'ready';
btnRun.disabled = false;
runtimeReady = true;
selectExample(0);
var engineAborted = false;
var engineScript = null;
var pendingCode = null;
var firstLoad = true;

function setEngineStatus(text, className) {
statusEl.textContent = text;
statusEl.className = className;
}

function formatError(e) {
if (typeof e === 'string') { return e; }
if (e && e.message) { return e.message; }
try { return String(e); } catch (_) { return 'Unknown error'; }
}

function executePendingCode() {
if (!pendingCode) { return; }
var code = pendingCode;
pendingCode = null;
try {
Module.eval(code);
} catch (e) {
appendOutput('Error: ' + formatError(e), 'chai-output-error');
}
}

function createModuleConfig() {
return {
print: function(text) {
appendOutput(text);
},
printErr: function(text) {
appendOutput(text, 'chai-output-error');
},
onAbort: function(what) {
engineAborted = true;
setEngineStatus('Error \u2014 click Run to retry', 'error');
appendOutput('Engine error: ' + formatError(what), 'chai-output-error');
},
onRuntimeInitialized: function() {
engineAborted = false;
setEngineStatus('Ready', 'ready');
btnRun.disabled = false;
runtimeReady = true;
if (firstLoad) {
firstLoad = false;
selectExample(0);
} else {
executePendingCode();
}
}
};
}

var Module = createModuleConfig();

function resetEngine() {
runtimeReady = false;
btnRun.disabled = true;
setEngineStatus('Resetting\u2026', 'loading');
if (engineScript) {
engineScript.parentNode.removeChild(engineScript);
engineScript = null;
}
};
Module = createModuleConfig();
window.Module = Module;
engineScript = document.createElement('script');
engineScript.src = '/playground/chaiscript.js';
document.body.appendChild(engineScript);
}

function runCode() {
var code = inputEl.value;
if (!code.trim()) { return; }

outputEl.innerHTML = '';
try {
Module.eval(code);
} catch (e) {
appendOutput('Error: ' + e.message, 'chai-output-error');
}
pendingCode = code;
resetEngine();
}

function scheduleLiveRun() {
Expand Down Expand Up @@ -331,7 +384,10 @@ <h3>Interactive Playground <span id="chai-status" class="loading">Loading&hellip
});

buildSidebar();

engineScript = document.createElement('script');
engineScript.src = '/playground/chaiscript.js';
document.body.appendChild(engineScript);
</script>
<script src="/playground/chaiscript.js"></script>

</body>
10 changes: 9 additions & 1 deletion test_playground.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ assert_file_contains "playground.html" "example-item"
assert_file_contains "playground.html" "debounceTimer"
assert_file_contains "playground.html" "addEventListener.*input"

# 9. Playground examples cover major ChaiScript features
# 9. Playground engine is reset between runs (issue #14)
assert_file_contains "playground.html" "resetEngine"
assert_file_contains "playground.html" "onAbort"

# 10. Playground catches and displays exceptions with detail (issue #14)
assert_file_contains "playground.html" "chai-output-error"
assert_file_contains "playground.html" "engineAborted"

# 11. Playground examples cover major ChaiScript features
assert_file_contains "playground.html" "Variables &amp; Types"
assert_file_contains "playground.html" "Functions"
assert_file_contains "playground.html" "Loops"
Expand Down