~tim/scheme-vm

e51ac2aa281881c25b60eaa2144791a8ac0d7b95 — Tim Morgan 4 years ago 5272163
Use FFI instead of ruru
3 files changed, 19 insertions(+), 23 deletions(-)

M Cargo.toml
M src/lib.rs
M test.rb
M Cargo.toml => Cargo.toml +1 -1
@@ 8,7 8,7 @@ build = "build.rs"
crate-type = ["dylib"]

[dependencies]
ruru = "0.9.0"
libc = "0.2.x"

[build-dependencies]
peg = { git = "https://github.com/kevinmehall/rust-peg", branch = "master" }

M src/lib.rs => src/lib.rs +16 -17
@@ 1,7 1,4 @@
#[macro_use]
extern crate ruru;

use ruru::{Boolean, Class, Object, RString};
extern crate libc;

mod values;
mod tests;


@@ 10,18 7,20 @@ mod lisp {
    include!(concat!(env!("OUT_DIR"), "/lisp.rs"));
}

methods!(
   RString,
   itself,

   fn string_test_rule() -> Boolean {
       Boolean::new(lisp::program(&itself.to_string()).is_ok())
   }
);

#[no_mangle]
pub extern fn initialize_string() {
    Class::from_existing("String").define(|itself| {
        itself.def("test_rule", string_test_rule);
    });
pub extern "C" fn is_ok(program_ptr: *const libc::c_char) -> libc::int8_t {
    let program = string_from_c_ptr(program_ptr);
    if lisp::program(&program).is_ok() {
        1
    } else {
        0
    }
}

fn string_from_c_ptr(c_ptr: *const libc::c_char) -> String {
    let c_str = unsafe {
        assert!(!c_ptr.is_null());
        std::ffi::CStr::from_ptr(c_ptr)
    };
    c_str.to_str().unwrap().to_string()
}

M test.rb => test.rb +2 -5
@@ 1,9 1,6 @@
require 'fiddle'

library = Fiddle::dlopen('target/release/libscheme_vm.dylib')
is_ok = Fiddle::Function.new(library['is_ok'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_SHORT)

Fiddle::Function.new(library['initialize_string'], [], Fiddle::TYPE_VOIDP).call

p "(foo bar)".test_rule
p "()".test_rule
p "(".test_rule
p is_ok.call(ARGV.first) == 1