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

A few more fns from fns.c #235

Merged
merged 2 commits into from
Jul 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust_src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust_src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ remacs-sys = { version = "0.1.0", path = "remacs-sys/" }
remacs-lib = { version = "0.1.0", path = "remacs-lib/" }
remacs-macros = { version = "0.1.0", path = "remacs-macros" }
libc = "0.2"
rand = "0.3.15"
rustc-serialize = "0.3"
sha1 = "0.2.0"
sha2 = "0.4.2"
Expand Down
7 changes: 7 additions & 0 deletions rust_src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern crate remacs_sys;
extern crate remacs_lib;
extern crate remacs_macros;
extern crate libc;
extern crate rand;
extern crate sha1;
extern crate sha2;

Expand Down Expand Up @@ -83,10 +84,13 @@ pub use lists::Fdelq;
pub use lists::Fplist_get;
pub use lists::Fplist_member;
pub use lists::Fplist_put;
pub use lists::Fget;
pub use lists::Fput;
pub use lists::Flist;
pub use lists::Fmake_list;
pub use floatfns::extract_float;
pub use floatfns::fmod_float;
pub use numbers::Frandom;
pub use objects::Fequal;
pub use objects::Fequal_including_properties;
pub use symbols::Fsymbolp;
Expand Down Expand Up @@ -161,6 +165,7 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*numbers::Snatnump);
defsubr(&*numbers::Snumber_or_marker_p);
defsubr(&*numbers::Snumberp);
defsubr(&*numbers::Srandom);
defsubr(&*objects::Snull);
defsubr(&*objects::Seq);
defsubr(&*objects::Seql);
Expand Down Expand Up @@ -193,6 +198,8 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*lists::Splist_member);
defsubr(&*lists::Splist_put);
defsubr(&*lists::Slax_plist_put);
defsubr(&*lists::Sget);
defsubr(&*lists::Sput);
defsubr(&*lists::Slist);
defsubr(&*lists::Smake_list);
defsubr(&*lists::Ssafe_length);
Expand Down
10 changes: 8 additions & 2 deletions rust_src/src/lisp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use std::cmp::max;
use std::mem;
use std::slice;
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
use std::fmt::{Debug, Formatter, Error};
use libc::{c_void, intptr_t};

Expand Down Expand Up @@ -95,7 +95,7 @@ const VAL_MAX: EmacsInt = EMACS_INT_MAX >> (GCTYPEBITS - 1);

const VALMASK: EmacsInt = [VAL_MAX, -(1 << GCTYPEBITS)][USE_LSB_TAG as usize];

const INTMASK: EmacsInt = (EMACS_INT_MAX >> (INTTYPEBITS - 1));
pub const INTMASK: EmacsInt = (EMACS_INT_MAX >> (INTTYPEBITS - 1));

/// Bit pattern used in the least significant bits of a lisp object,
/// to denote its type.
Expand Down Expand Up @@ -268,6 +268,12 @@ impl<T> Deref for ExternalPtr<T> {
}
}

impl<T> DerefMut for ExternalPtr<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.0 }
}
}

pub type LispMiscRef = ExternalPtr<LispMiscAny>;

// Supertype of all Misc types.
Expand Down
18 changes: 18 additions & 0 deletions rust_src/src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,24 @@ fn lax_plist_put(plist: LispObject, prop: LispObject, val: LispObject) -> LispOb
internal_plist_put(plist, prop, val, LispObject::equal)
}

/// Return the value of SYMBOL's PROPNAME property.
/// This is the last value stored with `(put SYMBOL PROPNAME VALUE)'.
#[lisp_fn]
fn get(symbol: LispObject, propname: LispObject) -> LispObject {
let sym = symbol.as_symbol_or_error();
plist_get(sym.get_plist(), propname)
}

/// Store SYMBOL's PROPNAME property with value VALUE.
/// It can be retrieved with `(get SYMBOL PROPNAME)'.
#[lisp_fn]
fn put(symbol: LispObject, propname: LispObject, value: LispObject) -> LispObject {
let mut sym = symbol.as_symbol_or_error();
let new_plist = plist_put(sym.get_plist(), propname, value);
sym.set_plist(new_plist);
value
}

/// Return a newly created list with specified arguments as elements.
/// Any number of arguments, even zero arguments, are allowed.
/// usage: (list &rest OBJECTS)
Expand Down
47 changes: 46 additions & 1 deletion rust_src/src/numbers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
//! Functions operating on numbers.

use lisp::LispObject;
use std::sync::Mutex;
use rand::{StdRng, Rng, SeedableRng};

use lisp::{LispObject, INTMASK};
use remacs_sys::EmacsInt;
use remacs_macros::lisp_fn;

lazy_static! {
static ref RNG: Mutex<StdRng> = Mutex::new(StdRng::new().unwrap());
}


/// Return t if OBJECT is a floating point number.
#[lisp_fn]
Expand Down Expand Up @@ -39,3 +47,40 @@ fn numberp(object: LispObject) -> LispObject {
fn number_or_marker_p(object: LispObject) -> LispObject {
LispObject::from_bool(object.is_number() || object.is_marker())
}

/// Return a pseudo-random number.
/// All integers representable in Lisp, i.e. between `most-negative-fixnum'
/// and `most-positive-fixnum', inclusive, are equally likely.
///
/// With positive integer LIMIT, return random number in interval [0,LIMIT).
/// With argument t, set the random number seed from the system's entropy
/// pool if available, otherwise from less-random volatile data such as the time.
/// With a string argument, set the seed based on the string's contents.
/// Other values of LIMIT are ignored.
///
/// See Info node `(elisp)Random Numbers' for more details.
#[lisp_fn(min = "0")]
fn random(limit: LispObject) -> LispObject {
let mut rng = RNG.lock().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume that this will change the output of the random number generator in Remacs, even when seeded with the same value as GNU Emacs? If so, we should mention it in REMACS_COMPATIBILITY.md

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, although as far as I can see Emacs uses the C library's generator, which I assume does not guarantee any specific sequence either.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, OK.

if limit == LispObject::constant_t() {
*rng = StdRng::new().unwrap();
} else if let Some(s) = limit.as_string() {
let values: Vec<usize> = s.as_slice().iter().map(|&x| x as usize).collect();
rng.reseed(&values);
}

if let Some(limit) = limit.as_fixnum() {
// Return the remainder, except reject the rare case where
// get_random returns a number so close to INTMASK that the
// remainder isn't random.
loop {
let val: EmacsInt = rng.gen();
let remainder = val.abs() % limit;
if val - remainder <= INTMASK - limit + 1 {
return LispObject::from_fixnum(remainder);
}
}
} else {
LispObject::from_fixnum_truncated(rng.gen())
}
}
4 changes: 4 additions & 0 deletions rust_src/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl LispSymbolRef {
pub fn get_plist(&self) -> LispObject {
LispObject::from_raw(self.plist)
}

pub fn set_plist(&mut self, plist: LispObject) {
self.plist = plist.to_raw();
}
}

/// Return t if OBJECT is a symbol.
Expand Down
60 changes: 0 additions & 60 deletions src/fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,6 @@ DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0,
return arg;
}

DEFUN ("random", Frandom, Srandom, 0, 1, 0,
doc: /* Return a pseudo-random number.
All integers representable in Lisp, i.e. between `most-negative-fixnum'
and `most-positive-fixnum', inclusive, are equally likely.

With positive integer LIMIT, return random number in interval [0,LIMIT).
With argument t, set the random number seed from the system's entropy
pool if available, otherwise from less-random volatile data such as the time.
With a string argument, set the seed based on the string's contents.
Other values of LIMIT are ignored.

See Info node `(elisp)Random Numbers' for more details. */)
(Lisp_Object limit)
{
EMACS_INT val;

if (EQ (limit, Qt))
init_random ();
else if (STRINGP (limit))
seed_random (SSDATA (limit), SBYTES (limit));

val = get_random ();
if (INTEGERP (limit) && 0 < XINT (limit))
while (true)
{
/* Return the remainder, except reject the rare case where
get_random returns a number so close to INTMASK that the
remainder isn't random. */
EMACS_INT remainder = val % XINT (limit);
if (val - remainder <= INTMASK - XINT (limit) + 1)
return make_number (remainder);
val = get_random ();
}
return make_number (val);
}

/* Random data-structure functions. */

DEFUN ("compare-strings", Fcompare_strings, Scompare_strings, 6, 7, 0,
Expand Down Expand Up @@ -1387,27 +1351,6 @@ See also the function `nreverse', which is used more often. */)
wrong_type_argument (Qsequencep, seq);
return new;
}


DEFUN ("get", Fget, Sget, 2, 2, 0,
doc: /* Return the value of SYMBOL's PROPNAME property.
This is the last value stored with `(put SYMBOL PROPNAME VALUE)'. */)
(Lisp_Object symbol, Lisp_Object propname)
{
CHECK_SYMBOL (symbol);
return Fplist_get (XSYMBOL (symbol)->plist, propname);
}

DEFUN ("put", Fput, Sput, 3, 3, 0,
doc: /* Store SYMBOL's PROPNAME property with value VALUE.
It can be retrieved with `(get SYMBOL PROPNAME)'. */)
(Lisp_Object symbol, Lisp_Object propname, Lisp_Object value)
{
CHECK_SYMBOL (symbol);
set_symbol_plist
(symbol, Fplist_put (XSYMBOL (symbol)->plist, propname, value));
return value;
}

/* Return true if O1 and O2 are equal. Do not quit or check for cycles.
Use this only on arguments that are cycle-free and not too large and
Expand Down Expand Up @@ -4017,7 +3960,6 @@ this variable. */);
use_file_dialog = 1;

defsubr (&Sidentity);
defsubr (&Srandom);
defsubr (&Scompare_strings);
defsubr (&Sstring_version_lessp);
defsubr (&Sstring_collate_lessp);
Expand All @@ -4036,8 +3978,6 @@ this variable. */);
defsubr (&Sdelete);
defsubr (&Snreverse);
defsubr (&Sreverse);
defsubr (&Sget);
defsubr (&Sput);
defsubr (&Sfillarray);
defsubr (&Sclear_string);
defsubr (&Snconc);
Expand Down