@@ -26,11 +26,17 @@ export class TRuby {
2626 }
2727
2828 /** Compile T-Ruby source code to Ruby */
29- async compile ( source : string , filename = "input.trb" ) : Promise < CompileResult > {
29+ async compile ( source : string , _filename = "input.trb" ) : Promise < CompileResult > {
3030 this . ensureInit ( ) ;
3131 try {
32+ // First check if TRuby::Compiler is defined
33+ const compilerDefined = await this . vm ! . evalAsync ( "defined?(TRuby::Compiler)" ) ;
34+ if ( ! compilerDefined || String ( compilerDefined ) === "nil" ) {
35+ return { success : false , errors : [ { message : "TRuby::Compiler is not defined. Initialization may have failed." } ] } ;
36+ }
37+
3238 const r = await this . evalJson < CompileResult > (
33- `TRuby::Compiler.compile (${ escapeRubyString ( source ) } , filename: ${ escapeRubyString ( filename ) } ).to_json`
39+ `TRuby::Compiler.new.compile_string (${ escapeRubyString ( source ) } ).to_json`
3440 ) ;
3541 return { ...r , success : ! r . errors ?. length } ;
3642 } catch ( e ) {
@@ -59,10 +65,13 @@ export class TRuby {
5965 /** Get version information */
6066 async getVersion ( ) : Promise < VersionInfo > {
6167 this . ensureInit ( ) ;
62- const [ tRuby , ruby ] = await Promise . all ( [
63- this . vm ! . evalAsync ( "TRuby::VERSION" ) as Promise < string > ,
64- this . vm ! . evalAsync ( "RUBY_VERSION" ) as Promise < string > ,
68+ const [ tRubyVal , rubyVal ] = await Promise . all ( [
69+ this . vm ! . evalAsync ( "TRuby::VERSION" ) ,
70+ this . vm ! . evalAsync ( "RUBY_VERSION" ) ,
6571 ] ) ;
72+ // evalAsync returns RbValue, need to convert to JS string
73+ const tRuby = typeof tRubyVal === 'string' ? tRubyVal : String ( tRubyVal ) ;
74+ const ruby = typeof rubyVal === 'string' ? rubyVal : String ( rubyVal ) ;
6675 return { tRuby, ruby, rubyWasm : RUBY_WASM_VERSION } ;
6776 }
6877
@@ -76,8 +85,13 @@ export class TRuby {
7685
7786 private async loadWasm ( ) : Promise < RubyVM > {
7887 const { DefaultRubyVM } = await import ( "@ruby/wasm-wasi/dist/browser" ) ;
79- const url = new URL ( "@ruby/3.4-wasm-wasi/dist/ruby+stdlib.wasm" , import . meta. url ) ;
80- const mod = await WebAssembly . compileStreaming ( fetch ( url ) ) ;
88+ // Use CDN for WASM file to ensure cross-bundler compatibility
89+ const wasmUrl = `https://cdn.jsdelivr.net/npm/@ruby/3.4-wasm-wasi@${ RUBY_WASM_VERSION } /dist/ruby+stdlib.wasm` ;
90+ const response = await fetch ( wasmUrl ) ;
91+ if ( ! response . ok ) {
92+ throw new Error ( `Failed to fetch WASM: ${ response . status } ${ response . statusText } ` ) ;
93+ }
94+ const mod = await WebAssembly . compileStreaming ( response ) ;
8195 const result = await DefaultRubyVM ( mod ) ;
8296 return result . vm as RubyVM ;
8397 }
0 commit comments