Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port keywordp to rust #334

Merged
merged 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions rust_src/remacs-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ pub blv: *mut c_void, // @TODO implement Lisp_Buffer_Local_Value
pub fwd: *mut c_void, // @TODO implement Lisp_Fwd
}

/// Interned state of a symbol.
#[repr(C)]
pub enum Symbol_Interned {
Uninterned = 0,
Interned = 1,
InternedInInitialObarray = 2,
}

/// This struct has 4 bytes of padding, representing the bitfield that
/// lives at the top of a Lisp_Symbol. The first 10 bits of this field are
/// used.
Expand Down
2 changes: 2 additions & 0 deletions rust_src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub use symbols::Fsymbol_name;
pub use symbols::Ffboundp;
pub use symbols::Fsymbol_function;
pub use symbols::Fsymbol_plist;
pub use symbols::Fkeywordp;
pub use strings::Fstring_equal;
pub use strings::Fstring_as_multibyte;
pub use strings::Fstring_to_multibyte;
Expand Down Expand Up @@ -280,6 +281,7 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*symbols::Ssymbol_plist);
defsubr(&*symbols::Ssetplist);
defsubr(&*symbols::Sfmakunbound);
defsubr(&*symbols::Skeywordp);
defsubr(&*lists::Sconsp);
defsubr(&*lists::Ssetcar);
defsubr(&*lists::Ssetcdr);
Expand Down
5 changes: 5 additions & 0 deletions rust_src/src/multibyte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ impl LispStringRef {
pub fn as_mut_slice(&self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.data as *mut u8, self.len_bytes() as usize) }
}

#[inline]
pub fn byte_at(&self, index: ptrdiff_t) -> u8 {
unsafe { *self.const_data_ptr().offset(index) }
}
}

pub struct LispStringRefIterator<'a> {
Expand Down
28 changes: 27 additions & 1 deletion rust_src/src/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use remacs_macros::lisp_fn;
use lisp::{LispObject, ExternalPtr};
use remacs_sys::{Lisp_Symbol, Qsetting_constant};
use remacs_sys::{Lisp_Symbol, Symbol_Interned, Qsetting_constant};

pub type LispSymbolRef = ExternalPtr<Lisp_Symbol>;

const FLAG_INTERNED: u32 = 0b11 << 6; // 7 and 8 bits

impl LispSymbolRef {
pub fn symbol_name(&self) -> LispObject {
LispObject::from_raw(self.name)
Expand All @@ -24,6 +26,11 @@ impl LispSymbolRef {
pub fn set_function(&mut self, function: LispObject) {
self.function = function.to_raw();
}

pub fn is_interned_in_initial_obarray(&self) -> bool {
self.symbol_bitfield & FLAG_INTERNED ==
(Symbol_Interned::InternedInInitialObarray as u32) << 6
}
}

/// Return t if OBJECT is a symbol.
Expand Down Expand Up @@ -81,3 +88,22 @@ fn fmakunbound(symbol: LispObject) -> LispObject {
sym.set_function(LispObject::constant_nil());
symbol
}


// Define this in Rust to avoid unnecessarily consing up the symbol
// name.

/// Return t if OBJECT is a keyword.
/// This means that it is a symbol with a print name beginning with `:'
/// interned in the initial obarray.
#[lisp_fn]
fn keywordp(object: LispObject) -> LispObject {
if let Some(sym) = object.as_symbol() {
let name = sym.symbol_name().as_string_or_error();
LispObject::from_bool(
name.byte_at(0) == b':' && sym.is_interned_in_initial_obarray(),
)
} else {
LispObject::constant_nil()
}
}
16 changes: 0 additions & 16 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,6 @@ for example, (type-of 1) returns `integer'. */)
}
}

/* Define this in C to avoid unnecessarily consing up the symbol
name. */
DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
doc: /* Return t if OBJECT is a keyword.
This means that it is a symbol with a print name beginning with `:'
interned in the initial obarray. */)
(Lisp_Object object)
{
if (SYMBOLP (object)
&& SREF (SYMBOL_NAME (object), 0) == ':'
&& SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
return Qt;
return Qnil;
}

#ifdef HAVE_MODULES
DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
doc: /* Return t if OBJECT is a module user pointer. */)
Expand Down Expand Up @@ -2777,7 +2762,6 @@ syms_of_data (void)
defsubr (&Sindirect_variable);
defsubr (&Sinteractive_form);
defsubr (&Stype_of);
defsubr (&Skeywordp);
defsubr (&Smodule_function_p);
defsubr (&Sindirect_function);
defsubr (&Smakunbound);
Expand Down