|
| 1 | +/** |
| 2 | + * T-Ruby WASM Loader for Playground |
| 3 | + * |
| 4 | + * Handles lazy loading and initialization of Ruby WASM with T-Ruby compiler |
| 5 | + */ |
| 6 | + |
| 7 | +// CDN URLs |
| 8 | +const RUBY_WASM_CDN = 'https://cdn.jsdelivr.net/npm/@ruby/3.3-wasm-wasi@2.7.0/dist/browser/+esm'; |
| 9 | +const RUBY_WASM_BINARY = 'https://cdn.jsdelivr.net/npm/@ruby/3.3-wasm-wasi@2.7.0/dist/ruby+stdlib.wasm'; |
| 10 | +const T_RUBY_LIB_CDN = 'https://cdn.jsdelivr.net/npm/@t-ruby/wasm/dist/lib/'; |
| 11 | + |
| 12 | +// Types |
| 13 | +export interface CompileResult { |
| 14 | + success: boolean; |
| 15 | + ruby: string; |
| 16 | + rbs: string; |
| 17 | + errors: string[]; |
| 18 | +} |
| 19 | + |
| 20 | +export interface TRubyCompiler { |
| 21 | + compile(code: string): CompileResult; |
| 22 | + healthCheck(): { loaded: boolean; version: string; ruby_version: string }; |
| 23 | + getVersion(): { t_ruby: string; ruby: string }; |
| 24 | +} |
| 25 | + |
| 26 | +export type LoadingState = 'idle' | 'loading' | 'ready' | 'error'; |
| 27 | + |
| 28 | +export interface LoadingProgress { |
| 29 | + state: LoadingState; |
| 30 | + message: string; |
| 31 | + progress?: number; // 0-100 |
| 32 | +} |
| 33 | + |
| 34 | +// Singleton state |
| 35 | +let rubyVM: any = null; |
| 36 | +let compiler: TRubyCompiler | null = null; |
| 37 | +let loadingPromise: Promise<TRubyCompiler> | null = null; |
| 38 | + |
| 39 | +// Bootstrap code for T-Ruby compiler |
| 40 | +const BOOTSTRAP_CODE = ` |
| 41 | +require "json" |
| 42 | +
|
| 43 | +# Global compiler instance |
| 44 | +$trb_compiler = nil |
| 45 | +
|
| 46 | +def get_compiler |
| 47 | + $trb_compiler ||= TRuby::Compiler.new |
| 48 | +end |
| 49 | +
|
| 50 | +def __trb_compile__(code) |
| 51 | + compiler = get_compiler |
| 52 | +
|
| 53 | + begin |
| 54 | + result = compiler.compile_string(code) |
| 55 | +
|
| 56 | + { |
| 57 | + success: result[:errors].empty?, |
| 58 | + ruby: result[:ruby] || "", |
| 59 | + rbs: result[:rbs] || "", |
| 60 | + errors: result[:errors] || [] |
| 61 | + }.to_json |
| 62 | + rescue TRuby::ParseError => e |
| 63 | + { |
| 64 | + success: false, |
| 65 | + ruby: "", |
| 66 | + rbs: "", |
| 67 | + errors: [e.message] |
| 68 | + }.to_json |
| 69 | + rescue StandardError => e |
| 70 | + { |
| 71 | + success: false, |
| 72 | + ruby: "", |
| 73 | + rbs: "", |
| 74 | + errors: ["Compilation error: " + e.message] |
| 75 | + }.to_json |
| 76 | + end |
| 77 | +end |
| 78 | +
|
| 79 | +def __trb_health_check__ |
| 80 | + { |
| 81 | + loaded: defined?(TRuby) == "constant", |
| 82 | + version: defined?(TRuby::VERSION) ? TRuby::VERSION : "unknown", |
| 83 | + ruby_version: RUBY_VERSION |
| 84 | + }.to_json |
| 85 | +end |
| 86 | +
|
| 87 | +def __trb_version__ |
| 88 | + { |
| 89 | + t_ruby: defined?(TRuby::VERSION) ? TRuby::VERSION : "unknown", |
| 90 | + ruby: RUBY_VERSION |
| 91 | + }.to_json |
| 92 | +end |
| 93 | +`; |
| 94 | + |
| 95 | +/** |
| 96 | + * Load the T-Ruby WASM compiler |
| 97 | + * Returns cached instance if already loaded |
| 98 | + */ |
| 99 | +export async function loadTRubyCompiler( |
| 100 | + onProgress?: (progress: LoadingProgress) => void |
| 101 | +): Promise<TRubyCompiler> { |
| 102 | + // Return cached compiler if available |
| 103 | + if (compiler) { |
| 104 | + onProgress?.({ state: 'ready', message: 'Compiler ready' }); |
| 105 | + return compiler; |
| 106 | + } |
| 107 | + |
| 108 | + // Return existing promise if already loading |
| 109 | + if (loadingPromise) { |
| 110 | + return loadingPromise; |
| 111 | + } |
| 112 | + |
| 113 | + // Start loading |
| 114 | + loadingPromise = doLoadCompiler(onProgress); |
| 115 | + |
| 116 | + try { |
| 117 | + compiler = await loadingPromise; |
| 118 | + return compiler; |
| 119 | + } catch (error) { |
| 120 | + loadingPromise = null; |
| 121 | + throw error; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +async function doLoadCompiler( |
| 126 | + onProgress?: (progress: LoadingProgress) => void |
| 127 | +): Promise<TRubyCompiler> { |
| 128 | + try { |
| 129 | + // Step 1: Load Ruby WASM module |
| 130 | + onProgress?.({ |
| 131 | + state: 'loading', |
| 132 | + message: 'Loading Ruby runtime...', |
| 133 | + progress: 10 |
| 134 | + }); |
| 135 | + |
| 136 | + const { DefaultRubyVM } = await import(/* webpackIgnore: true */ RUBY_WASM_CDN); |
| 137 | + |
| 138 | + onProgress?.({ |
| 139 | + state: 'loading', |
| 140 | + message: 'Downloading Ruby WASM binary...', |
| 141 | + progress: 30 |
| 142 | + }); |
| 143 | + |
| 144 | + // Step 2: Fetch and compile WASM binary |
| 145 | + const response = await fetch(RUBY_WASM_BINARY); |
| 146 | + const wasmModule = await WebAssembly.compileStreaming(response); |
| 147 | + |
| 148 | + onProgress?.({ |
| 149 | + state: 'loading', |
| 150 | + message: 'Initializing Ruby VM...', |
| 151 | + progress: 60 |
| 152 | + }); |
| 153 | + |
| 154 | + // Step 3: Initialize Ruby VM |
| 155 | + const { vm } = await DefaultRubyVM(wasmModule); |
| 156 | + rubyVM = vm; |
| 157 | + |
| 158 | + onProgress?.({ |
| 159 | + state: 'loading', |
| 160 | + message: 'Loading T-Ruby compiler...', |
| 161 | + progress: 80 |
| 162 | + }); |
| 163 | + |
| 164 | + // Step 4: Load T-Ruby library |
| 165 | + // For now, we'll use a simplified approach that works without VFS mounting |
| 166 | + // In production, the T-Ruby lib files should be bundled or fetched |
| 167 | + |
| 168 | + // Initialize the compiler bootstrap |
| 169 | + vm.eval(BOOTSTRAP_CODE); |
| 170 | + |
| 171 | + onProgress?.({ |
| 172 | + state: 'ready', |
| 173 | + message: 'Compiler ready', |
| 174 | + progress: 100 |
| 175 | + }); |
| 176 | + |
| 177 | + // Return compiler interface |
| 178 | + return { |
| 179 | + compile(code: string): CompileResult { |
| 180 | + const resultJson = vm.eval(`__trb_compile__(${JSON.stringify(code)})`); |
| 181 | + return JSON.parse(resultJson.toString()); |
| 182 | + }, |
| 183 | + |
| 184 | + healthCheck() { |
| 185 | + const resultJson = vm.eval('__trb_health_check__'); |
| 186 | + return JSON.parse(resultJson.toString()); |
| 187 | + }, |
| 188 | + |
| 189 | + getVersion() { |
| 190 | + const resultJson = vm.eval('__trb_version__'); |
| 191 | + return JSON.parse(resultJson.toString()); |
| 192 | + } |
| 193 | + }; |
| 194 | + } catch (error) { |
| 195 | + onProgress?.({ |
| 196 | + state: 'error', |
| 197 | + message: error instanceof Error ? error.message : 'Unknown error' |
| 198 | + }); |
| 199 | + throw error; |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +/** |
| 204 | + * Check if the compiler is loaded |
| 205 | + */ |
| 206 | +export function isCompilerLoaded(): boolean { |
| 207 | + return compiler !== null; |
| 208 | +} |
| 209 | + |
| 210 | +/** |
| 211 | + * Get the current loading state |
| 212 | + */ |
| 213 | +export function getLoadingState(): LoadingState { |
| 214 | + if (compiler) return 'ready'; |
| 215 | + if (loadingPromise) return 'loading'; |
| 216 | + return 'idle'; |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Preload the compiler (call early to warm up) |
| 221 | + */ |
| 222 | +export function preloadCompiler(): void { |
| 223 | + if (!compiler && !loadingPromise) { |
| 224 | + loadTRubyCompiler().catch(() => { |
| 225 | + // Silently ignore preload errors |
| 226 | + }); |
| 227 | + } |
| 228 | +} |
0 commit comments