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 some chartable functions to Rust #277

Merged
merged 1 commit into from Jul 31, 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
43 changes: 43 additions & 0 deletions rust_src/remacs-sys/lib.rs
Expand Up @@ -677,6 +677,48 @@ pub struct thread_state {
// next_thread.
}

/// Lisp_Char_Table
#[repr(C)]
#[allow(dead_code)]
enum ChartabSize {
Bits0 = 6,
Bits1 = 4,
Bits2 = 5,
Bits3 = 7,
}

#[repr(C)]
pub struct Lisp_Char_Table {
/// HEADER.SIZE is the vector's size field, which also holds the
/// pseudovector type information. It holds the size, too.
/// The size counts the defalt, parent, purpose, ascii,
/// contents, and extras slots.
pub header: Lisp_Vectorlike_Header,

/// This holds a default value,
/// which is used whenever the value for a specific character is nil.
pub default: Lisp_Object,

/// This points to another char table, which we inherit from when the
/// value for a specific character is nil. The `defalt' slot takes
/// precedence over this.
pub parent: Lisp_Object,

/// This is a symbol which says what kind of use this char-table is
/// meant for.
pub purpose: Lisp_Object,

/// The bottom sub char-table for characters of the range 0..127. It
/// is nil if none of ASCII character has a specific value.
pub ascii: Lisp_Object,

pub contents: [Lisp_Object; 1 << ChartabSize::Bits0 as u8],

/// These hold additional data. It is a vector.
// actually any number of items
pub extras: [Lisp_Object; 1],
}

/// Represents the global state of the editor.
///
/// This has been factored out to a single struct in C Emacs to help
Expand Down Expand Up @@ -1253,6 +1295,7 @@ extern "C" {
pub static Qvectorp: Lisp_Object;
pub static Qsequencep: Lisp_Object;
pub static Qcharacterp: Lisp_Object;
pub static Qchar_table_p: Lisp_Object;
pub static Qminus: Lisp_Object;

pub static Qinteger: Lisp_Object;
Expand Down
45 changes: 45 additions & 0 deletions rust_src/src/chartable.rs
@@ -0,0 +1,45 @@
//! char table related functions

use remacs_sys::Lisp_Char_Table;
use remacs_macros::lisp_fn;
use lisp::{ExternalPtr, LispObject};

pub type LispCharTableRef = ExternalPtr<Lisp_Char_Table>;

/// Return the subtype of char-table CHARTABLE. The value is a symbol.
#[lisp_fn]
fn char_table_subtype(chartable: LispObject) -> LispObject {
let table = chartable.as_char_table_or_error();
LispObject::from_raw(table.purpose)
}

/// Return the parent char-table of CHARTABLE.
/// The value is either nil or another char-table.
/// If CHAR-TABLE holds nil for a given character,
/// then the actual applicable value is inherited from the parent char-table
/// (or from its parents, if necessary).
#[lisp_fn]
fn char_table_parent(chartable: LispObject) -> LispObject {
let table = chartable.as_char_table_or_error();
LispObject::from_raw(table.parent)
}

/// Set the parent char-table of CHARTABLE to PARENT.
/// Return PARENT. PARENT must be either nil or another char-table.
#[lisp_fn]
fn set_char_table_parent(chartable: LispObject, parent: LispObject) -> LispObject {
let mut curr_table = chartable.as_char_table_or_error();

if parent.is_not_nil() {
let mut temp = parent;
while temp.is_not_nil() {
if chartable.eq(temp) {
error!("Attempt to make a chartable to be its own parent");
}
temp = char_table_parent(temp);
}
}

curr_table.parent = parent.to_raw();
parent
}
7 changes: 7 additions & 0 deletions rust_src/src/lib.rs
Expand Up @@ -47,6 +47,7 @@ mod interactive;
mod process;
mod fonts;
mod threads;
mod chartable;

#[cfg(all(not(test), target_os = "macos"))]
use alloc_unexecmacosx::OsxUnexecAlloc;
Expand Down Expand Up @@ -119,6 +120,9 @@ pub use lists::merge;
pub use buffers::Fget_buffer;
pub use buffers::Fcurrent_buffer;

// used in chartab.c
pub use chartable::Fset_char_table_parent;

// Used in process.c
pub use str2sig::str2sig;
pub use process::Fget_process;
Expand Down Expand Up @@ -253,6 +257,9 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*crypto::Ssecure_hash);
defsubr(&*crypto::Sbuffer_hash);
defsubr(&*interactive::Sprefix_numeric_value);
defsubr(&*chartable::Schar_table_subtype);
defsubr(&*chartable::Schar_table_parent);
defsubr(&*chartable::Sset_char_table_parent);

defsubr(&*floatfns::Sisnan);
defsubr(&*floatfns::Sacos);
Expand Down
17 changes: 15 additions & 2 deletions rust_src/src/lisp.rs
Expand Up @@ -19,14 +19,15 @@ use windows::LispWindowRef;
use marker::LispMarkerRef;
use hashtable::LispHashTableRef;
use fonts::LispFontRef;
use chartable::LispCharTableRef;

use remacs_sys::{EmacsInt, EmacsUint, EmacsDouble, VALMASK, VALBITS, INTTYPEBITS, INTMASK,
USE_LSB_TAG, MOST_POSITIVE_FIXNUM, MOST_NEGATIVE_FIXNUM, Lisp_Type,
Lisp_Misc_Any, Lisp_Misc_Type, Lisp_Float, Lisp_Cons, Lisp_Object, lispsym,
make_float, circular_list, internal_equal, Fcons, CHECK_IMPURE, Qnil, Qt,
Qnumberp, Qfloatp, Qstringp, Qsymbolp, Qnumber_or_marker_p, Qwholenump, Qvectorp,
Qcharacterp, Qlistp, Qintegerp, Qhash_table_p, Qconsp, SYMBOL_NAME,
PseudovecType, EqualKind};
Qcharacterp, Qlistp, Qintegerp, Qhash_table_p, Qchar_table_p, Qconsp,
SYMBOL_NAME, PseudovecType, EqualKind};

// TODO: tweak Makefile to rebuild C files if this changes.

Expand Down Expand Up @@ -440,6 +441,18 @@ impl LispObject {
})
}

pub fn as_char_table(self) -> Option<LispCharTableRef> {
self.as_vectorlike().and_then(|v| v.as_char_table())
}

pub fn as_char_table_or_error(self) -> LispCharTableRef {
if let Some(chartable) = self.as_char_table() {
chartable
} else {
wrong_type!(Qchar_table_p, self)
}
}

pub fn is_bool_vector(self) -> bool {
self.as_vectorlike().map_or(
false,
Expand Down
10 changes: 10 additions & 0 deletions rust_src/src/vectors.rs
Expand Up @@ -12,6 +12,7 @@ use multibyte::MAX_CHAR;
use lists::{sort_list, inorder};
use buffers::LispBufferRef;
use windows::LispWindowRef;
use chartable::LispCharTableRef;
use remacs_sys::{Qsequencep, EmacsInt, PSEUDOVECTOR_FLAG, PVEC_TYPE_MASK, PSEUDOVECTOR_AREA_BITS,
PSEUDOVECTOR_SIZE_MASK, PseudovecType, Lisp_Vectorlike, Lisp_Vector,
Lisp_Bool_Vector, MOST_POSITIVE_FIXNUM};
Expand Down Expand Up @@ -73,6 +74,15 @@ impl LispVectorlikeRef {
None
}
}

#[inline]
pub fn as_char_table(&self) -> Option<LispCharTableRef> {
if self.is_pseudovector(PseudovecType::PVEC_CHAR_TABLE) {
Some(unsafe { mem::transmute(*self) })
} else {
None
}
}
}

impl LispVectorRef {
Expand Down
52 changes: 0 additions & 52 deletions src/chartab.c
Expand Up @@ -516,55 +516,6 @@ char_table_set_range (Lisp_Object table, int from, int to, Lisp_Object val)
}


DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
1, 1, 0,
doc: /*
Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
(Lisp_Object char_table)
{
CHECK_CHAR_TABLE (char_table);

return XCHAR_TABLE (char_table)->purpose;
}

DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
1, 1, 0,
doc: /* Return the parent char-table of CHAR-TABLE.
The value is either nil or another char-table.
If CHAR-TABLE holds nil for a given character,
then the actual applicable value is inherited from the parent char-table
\(or from its parents, if necessary). */)
(Lisp_Object char_table)
{
CHECK_CHAR_TABLE (char_table);

return XCHAR_TABLE (char_table)->parent;
}

DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
2, 2, 0,
doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
Return PARENT. PARENT must be either nil or another char-table. */)
(Lisp_Object char_table, Lisp_Object parent)
{
Lisp_Object temp;

CHECK_CHAR_TABLE (char_table);

if (!NILP (parent))
{
CHECK_CHAR_TABLE (parent);

for (temp = parent; !NILP (temp); temp = XCHAR_TABLE (temp)->parent)
if (EQ (temp, char_table))
error ("Attempt to make a chartable be its own parent");
}

set_char_table_parent (char_table, parent);

return parent;
}

DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
2, 2, 0,
doc: /* Return the value of CHAR-TABLE's extra-slot number N. */)
Expand Down Expand Up @@ -1369,9 +1320,6 @@ syms_of_chartab (void)
DEFSYM (Qchar_code_property_table, "char-code-property-table");

defsubr (&Smake_char_table);
defsubr (&Schar_table_parent);
defsubr (&Schar_table_subtype);
defsubr (&Sset_char_table_parent);
defsubr (&Schar_table_extra_slot);
defsubr (&Sset_char_table_extra_slot);
defsubr (&Schar_table_range);
Expand Down