module Crystal

Defined in:


Constant Summary

BUILD_COMMIT = "f5abb5b"
BUILD_DATE = "2017-09-20"
CACHE_DIR = "/tmp/crystal"
DEFAULT_PATH = "/mnt/src"
DESCRIPTION = "Crystal 0.18.4+108 [f5abb5b] (2017-09-20)\n\nLLVM: 3.8.0\nDefault target: x86_64-pc-linux-gnu"
LLVM_VERSION = "3.8.0"
PATH = "/mnt/src:lib"
VERSION = "0.18.4+108"

Class Method Summary

Class Method Detail

def self.main(&block) #

Defines the main routine run by normal Crystal programs:

  • Initializes the GC
  • Invokes the given block
  • Handles unhandled exceptions
  • Invokes at_exit handlers
  • Flushes STDOUT and STDERR

This method can be invoked if you need to define a custom main (as in C main) function, doing all the above steps.

For example:

fun main(argc : Int32, argv : UInt8**) : Int32
  Crystal.main do
    time =
    Crystal.main_user_code(argc, argv)
    puts "Time to execute program: #{ - time}"

Note that the above is really just an example, almost the same can be accomplished with at_exit. But in some cases redefinition of C's main is needed.

[View source]
def self.main(argc : Int32, argv : Pointer(Pointer(UInt8))) #

Main method run by all Crystal programs at startup.

This setups up the GC, invokes your program, rescuing any handled exception, and then runs at_exit handlers.

This method is automatically invoked for you, so you don't need to invoke it.

However, if you need to define a special main C function, you can redefine main and invoke Crystal.main from it:

fun main(argc : Int32, argv : UInt8**) : Int32
  # some setup before Crystal main
  Crystal.main(argc, argv)
  # some cleanup logic after Crystal main

The Crystal.main can also be passed as a callback:

fun main(argc : Int32, argv : UInt8**) : Int32
  LibFoo.init_foo_and_invoke_main(argc, argv, ->Crystal.main)

Note that before Crystal.main is invoked the GC is not setup yet, so nothing that allocates memory in Crystal (like new for classes) can be used.

[View source]
def self.main_user_code(argc : Int32, argv : Pointer(Pointer(UInt8))) #

Executes the main user code. This normally is executed after initializing the GC and before executing at_exit handlers.

You should never invoke this method unless you need to redefine C's main function. See Crystal.main for more details.

[View source]