Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Compile it with: crystal build --prelude=empty --emit llvm-ir noprelude_exception.cr
- lib LibC
- alias SizeT = UInt64
- fun printf(format : UInt8*, ...) : Int32
- fun exit(exit_code : Int32) : NoReturn
- end
- require "callstack/lib_unwind"
- struct UInt64
- def self.zero
- 0
- end
- end
- class String
- def to_unsafe
- pointerof(@c)
- end
- end
- fun __crystal_personality(version : Int32, actions : LibUnwind::Action, exception_class : UInt64, exception_object : LibUnwind::Exception*, context : Void*) : LibUnwind::ReasonCode
- return LibUnwind::ReasonCode::CONTINUE_UNWIND
- end
- @[Raises]
- fun __crystal_raise(unwind_ex : LibUnwind::Exception*) : NoReturn
- ret = LibUnwind.raise_exception(unwind_ex)
- LibC.printf "Failed to raise an exception: %d\n".to_unsafe, ret
- #CallStack.print_backtrace
- LibC.exit(ret.value)
- end
- def raise(str : String) : NoReturn
- unwind_ex = LibUnwind::Exception.new
- unwind_ex.exception_class = LibC::SizeT.zero
- unwind_ex.exception_cleanup = LibC::SizeT.zero
- unwind_ex.exception_object = str.object_id
- unwind_ex.exception_type_id = str.crystal_type_id
- __crystal_raise(pointerof(unwind_ex))
- end
- begin
- var = 2
- LibC.printf("in begin: var = %d\n", var)
- raise "I'm an exception!"
- ensure
- var = 3
- LibC.printf("in ensure: var = %d\n", var)
- end
- LibC.printf("after: var = %d\n", var)
Add Comment
Please, Sign In to add comment