File size: 4,511 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | use std::{fmt::Debug, hash::Hash, num::NonZeroU64, ops::Deref, sync::RwLock};
use dashmap::mapref::entry::Entry;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use crate::{
FxDashMap, TraitType, ValueType,
id::{TraitTypeId, ValueTypeId},
id_factory::IdFactory,
native_function::NativeFunction,
no_move_vec::NoMoveVec,
};
static NAME_TO_FUNCTION: Lazy<RwLock<FxHashMap<&'static str, &'static NativeFunction>>> =
Lazy::new(RwLock::default);
static VALUE_TYPE_ID_FACTORY: IdFactory<ValueTypeId> = IdFactory::new_const(
ValueTypeId::MIN.to_non_zero_u64(),
ValueTypeId::MAX.to_non_zero_u64(),
);
static VALUE_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, ValueTypeId>> =
Lazy::new(FxDashMap::default);
static VALUE_TYPES_BY_VALUE: Lazy<FxDashMap<&'static ValueType, ValueTypeId>> =
Lazy::new(FxDashMap::default);
static VALUE_TYPES: Lazy<NoMoveVec<(&'static ValueType, &'static str)>> = Lazy::new(NoMoveVec::new);
static TRAIT_TYPE_ID_FACTORY: IdFactory<TraitTypeId> = IdFactory::new_const(
TraitTypeId::MIN.to_non_zero_u64(),
TraitTypeId::MAX.to_non_zero_u64(),
);
static TRAIT_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, TraitTypeId>> =
Lazy::new(FxDashMap::default);
static TRAIT_TYPES_BY_VALUE: Lazy<FxDashMap<&'static TraitType, TraitTypeId>> =
Lazy::new(FxDashMap::default);
static TRAIT_TYPES: Lazy<NoMoveVec<(&'static TraitType, &'static str)>> = Lazy::new(NoMoveVec::new);
/// Registers the value and returns its id if this is the initial
fn register_thing<
K: Copy + Deref<Target = u32> + TryFrom<NonZeroU64>,
V: Copy + Hash + Eq,
const INITIAL_CAPACITY_BITS: u32,
>(
global_name: &'static str,
value: V,
id_factory: &IdFactory<K>,
store: &NoMoveVec<(V, &'static str), INITIAL_CAPACITY_BITS>,
map_by_name: &FxDashMap<&'static str, K>,
map_by_value: &FxDashMap<V, K>,
) -> Option<K> {
if let Entry::Vacant(e) = map_by_value.entry(value) {
let new_id = id_factory.get();
// SAFETY: this is a fresh id
unsafe {
store.insert(*new_id as usize, (value, global_name));
}
map_by_name.insert(global_name, new_id);
e.insert(new_id);
Some(new_id)
} else {
None
}
}
fn get_thing_id<K, V>(value: V, map_by_value: &FxDashMap<V, K>) -> K
where
V: Hash + Eq + Debug,
K: Clone,
{
if let Some(id) = map_by_value.get(&value) {
id.clone()
} else {
panic!("Use of unregistered {value:?}");
}
}
/// Registers a function so it is available for persistence
pub fn register_function(global_name: &'static str, func: &'static NativeFunction) {
let prev = NAME_TO_FUNCTION.write().unwrap().insert(global_name, func);
debug_assert!(
prev.is_none(),
"registration mappings for {global_name} are inconsistent!"
);
}
pub fn get_function_by_global_name(global_name: &str) -> &'static NativeFunction {
NAME_TO_FUNCTION.read().unwrap().get(global_name).unwrap()
}
pub fn register_value_type(
global_name: &'static str,
ty: &'static ValueType,
) -> Option<ValueTypeId> {
register_thing(
global_name,
ty,
&VALUE_TYPE_ID_FACTORY,
&VALUE_TYPES,
&VALUE_TYPES_BY_NAME,
&VALUE_TYPES_BY_VALUE,
)
}
pub fn get_value_type_id(func: &'static ValueType) -> ValueTypeId {
get_thing_id(func, &VALUE_TYPES_BY_VALUE)
}
pub fn get_value_type_id_by_global_name(global_name: &str) -> Option<ValueTypeId> {
VALUE_TYPES_BY_NAME.get(global_name).map(|x| *x)
}
pub fn get_value_type(id: ValueTypeId) -> &'static ValueType {
VALUE_TYPES.get(*id as usize).unwrap().0
}
pub fn get_value_type_global_name(id: ValueTypeId) -> &'static str {
VALUE_TYPES.get(*id as usize).unwrap().1
}
pub fn register_trait_type(global_name: &'static str, ty: &'static TraitType) {
register_thing(
global_name,
ty,
&TRAIT_TYPE_ID_FACTORY,
&TRAIT_TYPES,
&TRAIT_TYPES_BY_NAME,
&TRAIT_TYPES_BY_VALUE,
);
}
pub fn get_trait_type_id(func: &'static TraitType) -> TraitTypeId {
get_thing_id(func, &TRAIT_TYPES_BY_VALUE)
}
pub fn get_trait_type_id_by_global_name(global_name: &str) -> Option<TraitTypeId> {
TRAIT_TYPES_BY_NAME.get(global_name).map(|x| *x)
}
pub fn get_trait(id: TraitTypeId) -> &'static TraitType {
TRAIT_TYPES.get(*id as usize).unwrap().0
}
pub fn get_trait_type_global_name(id: TraitTypeId) -> &'static str {
TRAIT_TYPES.get(*id as usize).unwrap().1
}
|