Skip to content

Commit 91f1e36

Browse files
committed
Move #raise-related helpers to ExceptionOperations and load it earlier
1 parent 3ed77b7 commit 91f1e36

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
11411141
"/core/string.rb",
11421142
"/core/random.rb",
11431143
"/core/truffle/kernel_operations.rb",
1144+
"/core/truffle/exception_operations.rb",
11441145
"/core/truffle/feature_loader.rb",
11451146
"/core/truffle/gem_util.rb",
11461147
"/core/thread.rb",
@@ -1155,7 +1156,6 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
11551156
"/core/truffle/boot.rb",
11561157
"/core/truffle/debug.rb",
11571158
"/core/truffle/encoding_operations.rb",
1158-
"/core/truffle/exception_operations.rb",
11591159
"/core/truffle/hash_operations.rb",
11601160
"/core/truffle/numeric_operations.rb",
11611161
"/core/truffle/proc_operations.rb",

src/main/ruby/truffleruby/core/kernel.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,14 @@ def raise(exc = undefined, msg = undefined, ctx = nil)
662662
if Primitive.undefined?(exc) and last
663663
exc = last
664664
else
665-
exc = Truffle::KernelOperations.build_exception_for_raise(exc, msg)
665+
exc = Truffle::ExceptionOperations.build_exception_for_raise(exc, msg)
666666

667667
exc.set_context ctx if ctx
668668
exc.capture_backtrace!(1) unless exc.backtrace?
669669
Primitive.exception_set_cause exc, last unless Primitive.object_equal(exc, last)
670670
end
671671

672-
Truffle::KernelOperations.show_exception_for_debug(exc, 1) if $DEBUG
672+
Truffle::ExceptionOperations.show_exception_for_debug(exc, 1) if $DEBUG
673673

674674
Primitive.vm_raise_exception exc
675675
end

src/main/ruby/truffleruby/core/thread.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,12 @@ def inspect
285285
def raise(exc = undefined, msg = undefined, ctx = nil)
286286
return nil unless alive?
287287

288-
exc = Truffle::KernelOperations.build_exception_for_raise(exc, msg)
288+
exc = Truffle::ExceptionOperations.build_exception_for_raise(exc, msg)
289289

290290
exc.set_context ctx if ctx
291291
exc.capture_backtrace!(1) unless exc.backtrace?
292292

293-
Truffle::KernelOperations.show_exception_for_debug(exc, 1) if $DEBUG
293+
Truffle::ExceptionOperations.show_exception_for_debug(exc, 1) if $DEBUG
294294

295295
if self == Thread.current
296296
Primitive.vm_raise_exception exc

src/main/ruby/truffleruby/core/truffle/exception_operations.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@
1010

1111
module Truffle
1212
module ExceptionOperations
13+
def self.build_exception_for_raise(exc, msg)
14+
if Primitive.undefined? exc
15+
::RuntimeError.exception ''
16+
elsif exc.respond_to? :exception
17+
if Primitive.undefined? msg
18+
exc = exc.exception
19+
else
20+
exc = exc.exception msg
21+
end
22+
23+
exception_class_object_expected! unless Primitive.object_kind_of?(exc, ::Exception)
24+
exc
25+
elsif exc.kind_of? ::String
26+
::RuntimeError.exception exc
27+
else
28+
exception_class_object_expected!
29+
end
30+
end
31+
32+
# Avoid using #raise here to prevent infinite recursion
33+
def self.exception_class_object_expected!
34+
exc = ::TypeError.new('exception class/object expected')
35+
exc.capture_backtrace!(1)
36+
37+
show_exception_for_debug(exc, 2) if $DEBUG
38+
39+
Primitive.vm_raise_exception exc
40+
end
41+
42+
def self.show_exception_for_debug(exc, uplevel)
43+
STDERR.puts "Exception: `#{exc.class}' at #{caller(uplevel + 1, 1)[0]} - #{exc.message}\n"
44+
end
45+
1346
def self.class_name(receiver)
1447
Truffle::Type.object_class(receiver).name
1548
end

src/main/ruby/truffleruby/core/truffle/kernel_operations.rb

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -128,39 +128,6 @@ def self.load_error(name)
128128
load_error
129129
end
130130

131-
def self.build_exception_for_raise(exc, msg)
132-
if Primitive.undefined? exc
133-
::RuntimeError.exception ''
134-
elsif exc.respond_to? :exception
135-
if Primitive.undefined? msg
136-
exc = exc.exception
137-
else
138-
exc = exc.exception msg
139-
end
140-
141-
exception_class_object_expected! unless Primitive.object_kind_of?(exc, ::Exception)
142-
exc
143-
elsif exc.kind_of? ::String
144-
::RuntimeError.exception exc
145-
else
146-
exception_class_object_expected!
147-
end
148-
end
149-
150-
# Avoid using #raise here to prevent infinite recursion
151-
def self.exception_class_object_expected!
152-
exc = ::TypeError.new('exception class/object expected')
153-
exc.capture_backtrace!(1)
154-
155-
Truffle::KernelOperations.show_exception_for_debug(exc, 2) if $DEBUG
156-
157-
Primitive.vm_raise_exception exc
158-
end
159-
160-
def self.show_exception_for_debug(exc, uplevel)
161-
STDERR.puts "Exception: `#{exc.class}' at #{caller(uplevel + 1, 1)[0]} - #{exc.message}\n"
162-
end
163-
164131
def self.check_last_line(line)
165132
unless Primitive.object_kind_of? line, String
166133
raise TypeError, "$_ value need to be String (#{Truffle::ExceptionOperations.to_class_name(line)} given)"

0 commit comments

Comments
 (0)