Skip to content

Instantly share code, notes, and snippets.

@keplersj
Created July 25, 2015 21:00
Show Gist options
  • Save keplersj/3bd3aadd71db206e828f to your computer and use it in GitHub Desktop.
Save keplersj/3bd3aadd71db206e828f to your computer and use it in GitHub Desktop.

Revisions

  1. keplersj created this gist Jul 25, 2015.
    13 changes: 13 additions & 0 deletions logged.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #[link(name = "logger")]
    extern {
    fn CrystalLog(text: *const u8);
    }

    fn log(text: &'static str) {
    unsafe{ CrystalLog(text.as_bytes().as_ptr()) };
    }

    fn main() {
    log("Hello Crystal, from Rust!");
    log("Logging from Crystal, using Rust, totally works twice, by the way.");
    }
    16 changes: 16 additions & 0 deletions logger.cr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    require "colorize"

    fun log = CrystalLog(text: UInt8*): Void
    # We need to initialize the GC
    GC.init

    # We need to invoke Crystal's "main" function, the one that initializes
    # all constants and runs the top-level code (none in this case, but without
    # constants like STDOUT and others the last line will crash).
    # We pass 0 and null to argc and argv.
    LibCrystalMain.__crystal_main(0, Pointer(Pointer(UInt8)).null)

    #puts "#{"Pointer Recieved:".colorize.bold} #{text}"
    #puts "#{"Value of Pointer:".colorize.bold} #{String.new(text)}"
    puts String.new(text)
    end
    3 changes: 3 additions & 0 deletions run.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    crystal logger.cr --link-flags "-dynamiclib" -o lib/x86_64-apple-darwin/liblogger.dylib
    rustc logged.rs
    ./logged