@@ -270,32 +270,85 @@ <h3>Interactive Playground <span id="chai-status" class="loading">Loading&hellip
270270 outputEl . scrollTop = outputEl . scrollHeight ;
271271 }
272272
273- var Module = {
274- print : function ( text ) {
275- appendOutput ( text ) ;
276- } ,
277- printErr : function ( text ) {
278- appendOutput ( text , 'chai-output-error' ) ;
279- } ,
280- onRuntimeInitialized : function ( ) {
281- statusEl . textContent = 'Ready' ;
282- statusEl . className = 'ready' ;
283- btnRun . disabled = false ;
284- runtimeReady = true ;
285- selectExample ( 0 ) ;
273+ var engineAborted = false ;
274+ var engineScript = null ;
275+ var pendingCode = null ;
276+ var firstLoad = true ;
277+
278+ function setEngineStatus ( text , className ) {
279+ statusEl . textContent = text ;
280+ statusEl . className = className ;
281+ }
282+
283+ function formatError ( e ) {
284+ if ( typeof e === 'string' ) { return e ; }
285+ if ( e && e . message ) { return e . message ; }
286+ try { return String ( e ) ; } catch ( _ ) { return 'Unknown error' ; }
287+ }
288+
289+ function executePendingCode ( ) {
290+ if ( ! pendingCode ) { return ; }
291+ var code = pendingCode ;
292+ pendingCode = null ;
293+ try {
294+ Module . eval ( code ) ;
295+ } catch ( e ) {
296+ appendOutput ( 'Error: ' + formatError ( e ) , 'chai-output-error' ) ;
297+ }
298+ }
299+
300+ function createModuleConfig ( ) {
301+ return {
302+ print : function ( text ) {
303+ appendOutput ( text ) ;
304+ } ,
305+ printErr : function ( text ) {
306+ appendOutput ( text , 'chai-output-error' ) ;
307+ } ,
308+ onAbort : function ( what ) {
309+ engineAborted = true ;
310+ setEngineStatus ( 'Error \u2014 click Run to retry' , 'error' ) ;
311+ appendOutput ( 'Engine error: ' + formatError ( what ) , 'chai-output-error' ) ;
312+ } ,
313+ onRuntimeInitialized : function ( ) {
314+ engineAborted = false ;
315+ setEngineStatus ( 'Ready' , 'ready' ) ;
316+ btnRun . disabled = false ;
317+ runtimeReady = true ;
318+ if ( firstLoad ) {
319+ firstLoad = false ;
320+ selectExample ( 0 ) ;
321+ } else {
322+ executePendingCode ( ) ;
323+ }
324+ }
325+ } ;
326+ }
327+
328+ var Module = createModuleConfig ( ) ;
329+
330+ function resetEngine ( ) {
331+ runtimeReady = false ;
332+ btnRun . disabled = true ;
333+ setEngineStatus ( 'Resetting\u2026' , 'loading' ) ;
334+ if ( engineScript ) {
335+ engineScript . parentNode . removeChild ( engineScript ) ;
336+ engineScript = null ;
286337 }
287- } ;
338+ Module = createModuleConfig ( ) ;
339+ window . Module = Module ;
340+ engineScript = document . createElement ( 'script' ) ;
341+ engineScript . src = '/playground/chaiscript.js' ;
342+ document . body . appendChild ( engineScript ) ;
343+ }
288344
289345 function runCode ( ) {
290346 var code = inputEl . value ;
291347 if ( ! code . trim ( ) ) { return ; }
292348
293349 outputEl . innerHTML = '' ;
294- try {
295- Module . eval ( code ) ;
296- } catch ( e ) {
297- appendOutput ( 'Error: ' + e . message , 'chai-output-error' ) ;
298- }
350+ pendingCode = code ;
351+ resetEngine ( ) ;
299352 }
300353
301354 function scheduleLiveRun ( ) {
@@ -331,7 +384,10 @@ <h3>Interactive Playground <span id="chai-status" class="loading">Loading&hellip
331384 } ) ;
332385
333386 buildSidebar ( ) ;
387+
388+ engineScript = document . createElement ( 'script' ) ;
389+ engineScript . src = '/playground/chaiscript.js' ;
390+ document . body . appendChild ( engineScript ) ;
334391</ script >
335- < script src ="/playground/chaiscript.js "> </ script >
336392
337393</ body >
0 commit comments