Skip to content

Commit

Permalink
Showing 5 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions opal/corelib/error.rb
Original file line number Diff line number Diff line change
@@ -76,3 +76,14 @@ def self.new
end
end
end

class UncaughtThrowError < ArgumentError
attr_reader :sym, :arg

def initialize(args)
@sym = args[0]
@arg = args[1] if args.length > 1

super("uncaught throw #{@sym.inspect}")
end
end
11 changes: 11 additions & 0 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -1104,6 +1104,17 @@ def to_proc
def to_s
"#<#{self.class}:0x#{__id__.to_s(16)}>"
end

def catch(sym)
yield
rescue UncaughtThrowError => e
return e.arg if e.sym == sym
raise
end

def throw(*args)
raise UncaughtThrowError.new(args)
end
end

class Object
1 change: 1 addition & 0 deletions spec/filters/unsupported/symbol.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
opal_filter "Symbol" do
fails "Numeric#coerce raises a TypeError when passed a Symbol"
fails "Fixnum#coerce raises a TypeError when given an Object that does not respond to #to_f"
fails "The throw keyword does not convert strings to a symbol"
end
2 changes: 2 additions & 0 deletions spec/filters/unsupported/thread.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
opal_filter "Thread" do
fails "StandardError is a superclass of ThreadError"
fails "The throw keyword raises an ArgumentError if used to exit a thread"
fails "The throw keyword clears the current exception"
end
1 change: 0 additions & 1 deletion spec/rubyspecs
Original file line number Diff line number Diff line change
@@ -129,7 +129,6 @@ rubyspec/language
!rubyspec/language/send_spec
!rubyspec/language/string_spec
!rubyspec/language/symbol_spec
!rubyspec/language/throw_spec
!rubyspec/language/variables_spec
!rubyspec/language/while_spec
!rubyspec/language/yield_spec

0 comments on commit 71ddd1f

Please sign in to comment.