Expose `RB_NIL_P` and `RTEST` in libcruby

These seem to be the appropriate way to check for `nil` and truthiness
of an object using the C API. I would assume that additional changes
have to happen since this touches the runtime extension, but I'm not
sure what to do.
This commit is contained in:
Sean Griffin 2017-11-08 10:43:03 -07:00
parent 60cb64d615
commit b5965c87d1
5 changed files with 40 additions and 0 deletions

View File

@ -113,6 +113,12 @@ extern "C" {
#[link_name = "HELIX_RB_TYPE_P"]
pub fn RB_TYPE_P(val: VALUE, rb_type: isize) -> bool;
#[link_name = "HELIX_RB_NIL_P"]
pub fn RB_NIL_P(val: VALUE) -> bool;
#[link_name = "HELIX_RTEST"]
pub fn RTEST(val: VALUE) -> bool;
#[link_name = "HELIX_TYPE"]
pub fn TYPE(val: VALUE) -> isize;

View File

@ -44,6 +44,14 @@ bool HELIX_RB_TYPE_P(VALUE v, int type) {
return RB_TYPE_P(v, type);
}
bool HELIX_RB_NIL_P(VALUE v) {
return RB_NIL_P(v);
}
bool HELIX_RTEST(VALUE v) {
return RTEST(v);
}
VALUE HELIX_INT2FIX(int c_int) {
return INT2FIX(c_int);
}

View File

@ -58,7 +58,9 @@ HELIX_EXTERN const void* HELIX_RARRAY_CONST_PTR(VALUE array);
HELIX_EXTERN long HELIX_RHASH_SIZE(VALUE hash);
HELIX_EXTERN bool HELIX_RB_TYPE_P(VALUE v, int type);
HELIX_EXTERN bool HELIX_RB_NIL_P(VALUE v);
HELIX_EXTERN int HELIX_TYPE(VALUE v);
HELIX_EXTERN bool HELIX_RTEST(VALUE v);
HELIX_EXTERN VALUE HELIX_INT2FIX(int c_int);
HELIX_EXTERN VALUE HELIX_FIX2INT(VALUE fix);

View File

@ -80,6 +80,18 @@ describe HelixRuntime do
expect(Dummy.RHASH_SIZE({a: 1, b: 2, c: 3, d: 4, e: 5})).to equal(5)
end
it 'exports the RB_NIL_P macro' do
expect(Dummy.RB_NIL_P(nil)).to eq(true)
expect(Dummy.RB_NIL_P(:foo)).to eq(false)
end
it 'exports the RTEST macro' do
expect(Dummy.RTEST(true)).to eq(true)
expect(Dummy.RTEST(:foo)).to eq(true)
expect(Dummy.RTEST(false)).to eq(false)
expect(Dummy.RTEST(nil)).to eq(false)
end
describe 'coercions' do
it "(INT2FIX)" do
expect(Dummy.INT2FIX(10)).to eq(10)

View File

@ -48,6 +48,16 @@ static VALUE TEST_RB_TYPE_P(VALUE _self, VALUE val, VALUE type) {
return result ? Qtrue : Qfalse;
}
static VALUE TEST_RB_NIL_P(VALUE _self, VALUE val) {
int result = HELIX_RB_NIL_P(val);
return result ? Qtrue : Qfalse;
}
static VALUE TEST_RTEST(VALUE _self, VALUE val) {
int result = HELIX_RTEST(val);
return result ? Qtrue : Qfalse;
}
static VALUE TEST_TYPE(VALUE _self, VALUE val) {
return INT2FIX(HELIX_TYPE(val));
}
@ -192,9 +202,11 @@ void Init_dummy() {
EXPORT_RUBY_FUNC(RARRAY_CONST_PTR, 1);
EXPORT_FUNC(RHASH_SIZE, 1);
EXPORT_FUNC(RB_TYPE_P, 2);
EXPORT_FUNC(RB_NIL_P, 1);
EXPORT_FUNC(TYPE, 1);
EXPORT_FUNC(INT2FIX, 1);
EXPORT_FUNC(FIX2INT, 1);
EXPORT_FUNC(RTEST, 1);
EXPORT_FUNC(NUM2U64, 1);
EXPORT_FUNC(U642NUM, 1);