@@ -5,7 +5,7 @@ import { expose } from "comlink";
55import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/browser" ;
66import type { RubyVM } from "@ruby/wasm-wasi/dist/vm" ;
77import type { WorkerCapabilities } from "./runtime" ;
8- import type { ReplOutput } from "../repl" ;
8+ import type { ReplOutput , ReplOutputType } from "../repl" ;
99
1010import init_rb from "./ruby/init.rb?raw" ;
1111
@@ -21,29 +21,26 @@ declare global {
2121self . stdout = {
2222 write ( str : string ) {
2323 stdoutBuffer += str ;
24- // If buffer contains newlines, flush complete lines immediately
25- if ( stdoutBuffer . includes ( "\n" ) ) {
26- const lines = stdoutBuffer . split ( "\n" ) ;
27- for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
28- currentOutputCallback ?.( { type : "stdout" , message : lines [ i ] } ) ;
29- }
30- stdoutBuffer = lines [ lines . length - 1 ] ;
31- }
24+ stdoutBuffer = handleBatchOutput ( stdoutBuffer , "stdout" ) ;
3225 } ,
3326} ;
3427self . stderr = {
3528 write ( str : string ) {
3629 stderrBuffer += str ;
37- // If buffer contains newlines, flush complete lines immediately
38- if ( stderrBuffer . includes ( "\n" ) ) {
39- const lines = stderrBuffer . split ( "\n" ) ;
40- for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
41- currentOutputCallback ?.( { type : "stderr" , message : lines [ i ] } ) ;
42- }
43- stderrBuffer = lines [ lines . length - 1 ] ;
44- }
30+ stderrBuffer = handleBatchOutput ( stderrBuffer , "stderr" ) ;
4531 } ,
4632} ;
33+ function handleBatchOutput ( buffer : string , type : ReplOutputType ) : string {
34+ // If buffer contains newlines, flush complete lines immediately
35+ if ( buffer . includes ( "\n" ) ) {
36+ const lines = buffer . split ( "\n" ) ;
37+ for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
38+ currentOutputCallback ?.( { type, message : lines [ i ] } ) ;
39+ }
40+ return lines [ lines . length - 1 ] ;
41+ }
42+ return buffer ;
43+ }
4744
4845async function init ( /*_interruptBuffer?: Uint8Array*/ ) : Promise < {
4946 capabilities : WorkerCapabilities ;
@@ -122,6 +119,9 @@ async function runCode(
122119
123120 const resultStr = await result . callAsync ( "inspect" ) ;
124121
122+ // Flush any buffered output
123+ flushOutput ( ) ;
124+
125125 // Add result to output if it's not nil and not empty
126126 onOutput ( {
127127 type : "return" ,
@@ -130,13 +130,14 @@ async function runCode(
130130 } catch ( e ) {
131131 console . log ( e ) ;
132132
133+ // Flush any buffered output
134+ flushOutput ( ) ;
135+
133136 onOutput ( {
134137 type : "error" ,
135138 message : formatRubyError ( e , false ) ,
136139 } ) ;
137140 } finally {
138- // Flush any buffered output
139- flushOutput ( ) ;
140141 currentOutputCallback = null ;
141142 }
142143
@@ -175,16 +176,20 @@ async function runFile(
175176
176177 // Run the specified file
177178 rubyVM . eval ( `load ${ JSON . stringify ( name ) } ` ) ;
179+
180+ // Flush any buffered output
181+ flushOutput ( ) ;
178182 } catch ( e ) {
179183 console . log ( e ) ;
180184
185+ // Flush any buffered output
186+ flushOutput ( ) ;
187+
181188 onOutput ( {
182189 type : "error" ,
183190 message : formatRubyError ( e , true ) ,
184191 } ) ;
185192 } finally {
186- // Flush any buffered output
187- flushOutput ( ) ;
188193 currentOutputCallback = null ;
189194 }
190195
0 commit comments