Skip to content

Commit

Permalink
Implemented StableApiDefinition::rstring_interned_p methods for Truff…
Browse files Browse the repository at this point in the history
…leRuby. (#456)

* Implemented StableApiDefinition::rstring_interned_p.

* Added tests. Also, C is not Rust xD.

* Use ternary.

* A bit more succint.

* Removed file included by my LSP.
  • Loading branch information
goyox86 authored Dec 4, 2024
1 parent 9013c95 commit 61fdb43
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 0 deletions.
18 changes: 18 additions & 0 deletions crates/rb-sys-tests/src/stable_api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,21 @@ parity_test!(
},
expected: false
);

parity_test!(
name: test_rb_string_interned_p,
func: rstring_interned_p,
data_factory: {
ruby_eval!("'foo'")
},
expected: false
);

parity_test!(
name: test_rb_string_interned_p_frozen_str,
func: rstring_interned_p,
data_factory: {
ruby_eval!("'foo'.freeze")
},
expected: true
);
8 changes: 8 additions & 0 deletions crates/rb-sys/src/stable_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ pub trait StableApiDefinition {
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
unsafe fn rb_type(&self, obj: VALUE) -> crate::ruby_value_type;

/// Check if a Ruby string is interned (akin to `RSTRING_FSTR`).
///
/// # Safety
/// This function is unsafe because it dereferences a raw pointer to get
/// access to underlying flags of the RString. The caller must ensure that
/// the `VALUE` is a valid pointer to an RString.
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool;
}

#[cfg(stable_api_enable_compiled_mod)]
Expand Down
7 changes: 7 additions & 0 deletions crates/rb-sys/src/stable_api/compiled.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ int
impl_integer_type_p(VALUE obj) {
return RB_INTEGER_TYPE_P(obj);
}

int
impl_rstring_interned_p(VALUE obj) {
Check_Type(obj, T_STRING);

return !(FL_TEST(obj, RSTRING_FSTR) == 0);
}
7 changes: 7 additions & 0 deletions crates/rb-sys/src/stable_api/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ extern "C" {

#[link_name = "impl_integer_type_p"]
fn impl_integer_type_p(obj: VALUE) -> bool;

#[link_name = "impl_rstring_interned_p"]
fn impl_rstring_interned_p(obj: VALUE) -> bool;
}

pub struct Definition;
Expand Down Expand Up @@ -154,4 +157,8 @@ impl StableApiDefinition for Definition {
unsafe fn integer_type_p(&self, obj: VALUE) -> bool {
impl_integer_type_p(obj)
}

unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
impl_rstring_interned_p(obj)
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_2_6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_2_7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}
10 changes: 10 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,14 @@ impl StableApiDefinition for Definition {
self.builtin_type(obj) == value_type::RUBY_T_BIGNUM
}
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
assert!(self.type_p(obj, value_type::RUBY_T_STRING));

let rstring: &RString = &*(obj as *const RString);
let flags = rstring.basic.flags;

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}
}

0 comments on commit 61fdb43

Please sign in to comment.