-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbootstrap.rb
More file actions
82 lines (69 loc) · 1.85 KB
/
bootstrap.rb
File metadata and controls
82 lines (69 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
# T-Ruby WASM Bootstrap
# This file initializes the T-Ruby compiler in a WebAssembly environment
require "json"
# Add T-Ruby lib to load path
$LOAD_PATH.unshift("/t-ruby/lib")
# Load T-Ruby compiler
require "t_ruby"
# Global compiler instance (lazy initialized)
$trb_compiler = nil
def get_compiler
$trb_compiler ||= TRuby::Compiler.new
end
# Compile T-Ruby code to Ruby and RBS
#
# @param code [String] T-Ruby source code
# @return [String] JSON result with success, ruby, rbs, errors keys
def __trb_compile__(code)
compiler = get_compiler
begin
result = compiler.compile_string(code)
{
success: result[:errors].empty?,
ruby: result[:ruby] || "",
rbs: result[:rbs] || "",
errors: result[:errors] || []
}.to_json
rescue TRuby::ParseError => e
{
success: false,
ruby: "",
rbs: "",
errors: [format_error(e)]
}.to_json
rescue StandardError => e
{
success: false,
ruby: "",
rbs: "",
errors: ["Compilation error: #{e.message}"]
}.to_json
end
end
# Format error message with line/column info if available
def format_error(error)
if error.respond_to?(:line) && error.respond_to?(:column)
"Line #{error.line}, Column #{error.column}: #{error.message}"
else
error.message
end
end
# Check if T-Ruby is properly loaded
def __trb_health_check__
{
loaded: defined?(TRuby) == "constant",
version: defined?(TRuby::VERSION) ? TRuby::VERSION : "unknown",
ruby_version: RUBY_VERSION
}.to_json
end
# Version info
def __trb_version__
{
t_ruby: defined?(TRuby::VERSION) ? TRuby::VERSION : "unknown",
ruby: RUBY_VERSION
}.to_json
end
puts "[T-Ruby WASM] Bootstrap loaded successfully"
puts "[T-Ruby WASM] Ruby version: #{RUBY_VERSION}"
puts "[T-Ruby WASM] T-Ruby version: #{TRuby::VERSION}" if defined?(TRuby::VERSION)