Skip to content

Commit 61d9156

Browse files
committed
fix: WASM 초기화 및 컴파일 오류 수정 (#2)
- CDN에서 WASM 파일 로드하도록 변경 (번들러 호환성 개선) - RUBY_WASM_VERSION 2.7.0 → 2.8.1 업데이트 - Ruby interpolation (#{}) 이스케이프 추가 - Compiler.compile → Compiler.new.compile_string 호출 수정 - 초기화 스크립트에 디버그 로깅 추가 - release workflow 경로 수정
1 parent 94c2715 commit 61d9156

6 files changed

Lines changed: 36 additions & 12 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ jobs:
7373
- name: Copy t-ruby lib to vendor
7474
run: |
7575
mkdir -p vendor/t-ruby
76-
cp -r /tmp/t-ruby/lib/* vendor/t-ruby/
76+
cp -r /tmp/t-ruby/lib vendor/t-ruby/
7777
echo "Copied t-ruby lib files:"
78-
ls -la vendor/t-ruby/
78+
ls -la vendor/t-ruby/lib/
7979
8080
- name: Bundle t-ruby source
8181
run: node scripts/bundle-t-ruby.mjs

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@t-ruby/wasm",
3-
"version": "0.0.46-patch1",
3+
"version": "0.0.46-patch2",
44
"description": "T-Ruby compiler running in WebAssembly for browser environments",
55
"type": "module",
66
"main": "dist/index.cjs",

src/TRuby.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
*/
66

77
/** Current Ruby WASM runtime version */
8-
export const RUBY_WASM_VERSION = "2.7.0";
8+
export const RUBY_WASM_VERSION = "2.8.1";

src/utils/escapeRubyString.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function escapeRubyString(str: string): string {
2121
const escaped = str
2222
.replace(/\\/g, "\\\\")
2323
.replace(/"/g, '\\"')
24+
.replace(/#\{/g, '\\#{') // Escape Ruby interpolation
2425
.replace(/\n/g, "\\n")
2526
.replace(/\r/g, "\\r")
2627
.replace(/\t/g, "\\t");

src/vm/TRubyInitScript.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,19 @@ end unless defined?(FileUtils) && FileUtils.respond_to?(:mkdir_p)
181181

182182
// Load each bundled file in dependency order by directly evaling the content
183183
// Using heredoc syntax to avoid complex escaping issues
184+
script += `
185+
puts "[T-Ruby WASM] Starting file loading..."
186+
puts "[T-Ruby WASM] Bundle has #{${Object.keys(T_RUBY_BUNDLE).length}} files"
187+
`;
188+
184189
for (let i = 0; i < T_RUBY_LOAD_ORDER.length; i++) {
185190
const path = T_RUBY_LOAD_ORDER[i];
186191
const content = T_RUBY_BUNDLE[path];
187-
if (!content) continue;
192+
if (!content) {
193+
script += `puts "[T-Ruby WASM] SKIP: ${path} (not in bundle)"\n`;
194+
continue;
195+
}
196+
script += `puts "[T-Ruby WASM] Loading: ${path}"\n`;
188197

189198
// Process the content: remove frozen_string_literal and require_relative
190199
const processedContent = content

0 commit comments

Comments
 (0)