Skip to content

Commit cd68a6a

Browse files
authored
Merge pull request #148 from eregon/default-cruby
Default to CRuby for https://try.ruby-lang.org/
2 parents 2f0f0ff + f067176 commit cd68a6a

58 files changed

Lines changed: 125 additions & 113 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/ruby_engine.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ def run(source)
99
raise NotImplementedError
1010
end
1111

12+
def exception_to_string(err)
13+
# Beautify the backtrace a little bit
14+
backtrace = err.backtrace
15+
backtrace = backtrace.select { |i| i.include? '<anonymous>' }
16+
backtrace = backtrace.map { |i| i.gsub(/.*(<anonymous>)/, '\1') }
17+
backtrace = ["(file)"] if backtrace.empty?
18+
err.set_backtrace(backtrace)
19+
err.full_message
20+
end
21+
1222
def run_with_writer(source, writer, &block)
1323
@writer = writer
1424
@dots = 0
@@ -29,8 +39,6 @@ def loading(part = nil)
2939
# When you update the engines, ensure that they are tested correctly.
3040
# Update the engine list also in spec/playground_spec.
3141
ENGINES = [
32-
# Opal.new,
33-
OpalWebWorker.new("1.7.1"),
3442
CRubyWASI.new(
3543
"https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@0.5.0-2022-12-25-a/dist/ruby.wasm",
3644
"3.2.0"
@@ -41,6 +49,9 @@ def loading(part = nil)
4149
# "https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@next/dist/ruby.wasm",
4250
# "3.3.0dev"
4351
# ),
52+
53+
# Opal.new,
54+
OpalWebWorker.new("1.7.1"),
4455
].each_with_object({}) do |engine, hash|
4556
hash[engine.engine_id] = engine
4657
end

app/ruby_engine/cruby_wasi.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def run(source)
7474
textDecoder = `new TextDecoder("utf-8")`
7575
%x{
7676
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
77-
const text = textDecoder.decode(buffer);
7877
if (fd == 1 || fd == 2) {
78+
const text = textDecoder.decode(buffer);
7979
#{@writer.print_to_output(`text`, "")};
8080
}
8181
return originalWriteSync(fd, buffer, offset, length, position);
@@ -102,11 +102,18 @@ def run(source)
102102
`vm.setInstance(wasmInstance)`.await
103103
`wasi.setMemory(wasmInstance.exports.memory)`
104104
`vm.initialize()`
105+
set_external_encoding = "Encoding.default_external = Encoding::UTF_8"
106+
`vm.eval(set_external_encoding)`
105107
end
106108

107109
yield `vm.eval(source).toString()`
108110
rescue JS::Error => err
109-
@writer.log_error(err)
111+
raise err
112+
end
113+
114+
def exception_to_string(err)
115+
# "...: undefined method `reverse' for 40:Integer (NoMethodError)\n (Exception)\n"
116+
super(err).sub(/\s+\(Exception\)\s*\z/, '')
110117
end
111118
end
112119
end

app/try_ruby.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TryRuby
1414
RUBY
1515
INITIAL_TRY_RESULT = 'Welcome ' * 3
1616

17-
DEFAULT_RUBY_ENGINE = "opal-ww-1.7.1"
17+
DEFAULT_RUBY_ENGINE = "cruby-3.2.0"
1818

1919
def self.start
2020
instance
@@ -397,14 +397,14 @@ def show_result(retval)
397397
# Do not check the answer if there is no regexp matcher
398398
if @current_item && @current_item.answer
399399
# Get last line of output
400-
value_to_check = @output_buffer.length > 0 && !@output_buffer.last.empty? ? @output_buffer.last.chomp : ''
400+
value_to_check = @output_buffer.join.rstrip.lines.last
401401

402402
# Check if output matches the defined answer regexp
403403
# and print status message
404404
print_to_output("\n")
405405
from = count_lines
406406

407-
if !value_to_check.empty? && value_to_check.chomp.match(@current_item.answer)
407+
if value_to_check && !value_to_check.empty? && value_to_check.match(@current_item.answer)
408408
@current_item.ok.each do |line|
409409
print_to_output(line)
410410
end
@@ -431,13 +431,7 @@ def count_lines
431431

432432
def log_error(err)
433433
unless err.is_a? String
434-
# Beautify the backtrace a little bit
435-
backtrace = err.backtrace
436-
backtrace = backtrace.select { |i| i.include? '<anonymous>' }
437-
backtrace = backtrace.map { |i| i.gsub(/.*(<anonymous>)/, '\1') }
438-
backtrace = ["(file)"] if backtrace.empty?
439-
err.set_backtrace(backtrace)
440-
err = err.full_message
434+
err = @engine.exception_to_string(err)
441435
end
442436

443437
from = count_lines

source/try_ruby_en.json

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

source/try_ruby_es.json

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

source/try_ruby_ja.json

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

source/try_ruby_mk.json

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

source/try_ruby_nl.json

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

source/try_ruby_pt-br.json

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

source/try_ruby_ru.json

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)