diff --git a/runtime/src/capi.rs b/runtime/src/capi.rs index 3884bb8..45b16cc 100644 --- a/runtime/src/capi.rs +++ b/runtime/src/capi.rs @@ -11,3 +11,9 @@ pub extern "C" fn scm_from_utf8_string ( let bytes = unsafe { slice::from_raw_parts (ptr, len) }; scm::make_string (str::from_utf8 (bytes).unwrap ()) } + +#[unsafe(no_mangle)] +pub extern "C" fn scm_hash (ptr : *const u8, len : usize) -> u64 { + let bytes = unsafe { slice::from_raw_parts (ptr, len) }; + crate::obarray::hash (str::from_utf8 (bytes).unwrap ()) +} diff --git a/runtime/src/obarray.rs b/runtime/src/obarray.rs index 30dd04f..04a8886 100644 --- a/runtime/src/obarray.rs +++ b/runtime/src/obarray.rs @@ -3,8 +3,24 @@ use crate::scm::scm_bits; use crate::scm; mod fnv1a; +use fnv1a::{FNV1a, SymbolHash}; -pub struct Obarray (HashMap ); +pub struct Obarray (HashMap ); -pub fn hash () { +pub const fn new () -> Obarray { + Obarray (HashMap::with_hasher (SymbolHash ())) } + +pub fn hash (s : &str) -> u64 { + let mut a : FNV1a = Default::default (); + a.write (s.as_bytes ()); + return a.finish () +} + +impl Obarray { + pub fn insert (&mut self) { + + } +} + +pub static symbols : Obarray = new (); diff --git a/runtime/src/obarray/fnv1a.rs b/runtime/src/obarray/fnv1a.rs index 3d95577..9c0e015 100644 --- a/runtime/src/obarray/fnv1a.rs +++ b/runtime/src/obarray/fnv1a.rs @@ -1,7 +1,13 @@ use std::{collections::HashMap, hash::{BuildHasher, Hasher}}; pub struct FNV1a (u64); -pub struct SymbolHash; +pub struct SymbolHash (); + +impl Default for FNV1a { + fn default () -> Self { + FNV1a (offset_basis) + } +} impl Hasher for FNV1a { fn finish (&self) -> u64 { @@ -24,5 +30,5 @@ impl BuildHasher for SymbolHash { } } -const offset_basis : u64 = 0xcbf29ce484222325; -const prime : u64 = 0x100000001b3; +pub const offset_basis : u64 = 0xcbf29ce484222325; +pub const prime : u64 = 0x100000001b3; diff --git a/runtime/src/scm.rs b/runtime/src/scm.rs index 41d5c6e..31abd45 100644 --- a/runtime/src/scm.rs +++ b/runtime/src/scm.rs @@ -9,7 +9,7 @@ pub type scm_bits = u64; pub const tc2_int : u64 = 2; pub const tc3_cons : u64 = 0; -pub const tc7_weak_set : u64 = 0x55; +pub const tc7_obarray : u64 = 0x55; pub const tc7_symbol : u64 = 0x05; pub const tc7_string : u64 = 0x15;