Created
July 25, 2015 21:00
-
-
Save keplersj/3bd3aadd71db206e828f to your computer and use it in GitHub Desktop.
Gist demonstrating the ability to run Crystal code from Rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crystal logger.cr --link-flags "-dynamiclib" -o lib/x86_64-apple-darwin/liblogger.dylib | |
rustc logged.rs | |
./logged |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment