Skip to content

Commit

Permalink
Merge pull request #212 from tkbky/gh-179/integer-size
Browse files Browse the repository at this point in the history
Impl. Integer#size in Rust
  • Loading branch information
lopopolo committed Aug 30, 2019
2 parents 24d8191 + 300c5fa commit aab5c78
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion artichoke-backend/src/extn/core/integer/mod.rs
Expand Up @@ -5,7 +5,7 @@ use std::mem;
use crate::convert::{Convert, TryConvert};
use crate::def::{ClassLike, Define};
use crate::eval::Eval;
use crate::extn::core::error::{ArgumentError, RangeError, RubyException};
use crate::extn::core::error::{ArgumentError, RangeError, RubyException, RuntimeError};
use crate::sys;
use crate::types::Int;
use crate::value::Value;
Expand All @@ -24,6 +24,10 @@ pub fn init(interp: &Artichoke) -> Result<(), ArtichokeError> {
.borrow_mut()
.add_method("chr", Integer::chr, sys::mrb_args_opt(1));

integer
.borrow_mut()
.add_method("size", Integer::size, sys::mrb_args_none());

integer
.borrow()
.define(interp)
Expand Down Expand Up @@ -108,4 +112,18 @@ impl Integer {
}
}
}

pub unsafe extern "C" fn size(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
let interp = unwrap_interpreter!(mrb);
let result = Int::try_convert(&interp, Value::new(&interp, slf));
if result.is_ok() {
if let Ok(size) = Int::try_from(mem::size_of::<Int>()) {
Value::convert(&interp, size).inner()
} else {
RuntimeError::raise(interp, "fatal Integer#size error")
}
} else {
RuntimeError::raise(interp, "fatal Integer#size error")
}
}
}

0 comments on commit aab5c78

Please sign in to comment.