intuicio-core 0.53.0

Core module for Intuicio scripting platform
Documentation
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! The catalogue of everything scripts and host can reach.
//!
//! See [`Registry`].
use crate::{
    function::{Function, FunctionHandle, FunctionQuery},
    object::Object,
    types::{Type, TypeHandle, TypeQuery, struct_type::NativeStructBuilder},
};
use intuicio_data::managed::{
    DynamicManaged, DynamicManagedLazy, DynamicManagedRef, DynamicManagedRefMut,
    gc::DynamicManagedGc,
};
use std::{
    collections::BTreeMap,
    sync::{Arc, RwLock},
};

/// Shared registry, as a [`crate::host::Host`] holds it.
pub type RegistryHandle = Arc<Registry>;

/// Everything both sides of a scripting solution know about: types and
/// functions.
///
/// Nothing can be called or constructed unless it is in here first, so a
/// registry is filled during setup and then treated as read only: a function
/// call takes `&Registry`, which means the registry cannot change while any
/// script is running.
///
/// # Lookups
///
/// Items are found by query rather than by key, since a script may know only
/// part of what it is looking for. See [`FunctionQuery`] and [`TypeQuery`].
///
/// Queries scan linearly by default. Two fields turn on a cache of resolved
/// queries: [`Registry::index_capacity`] sets how many results to remember,
/// and [`Registry::use_indexing_threshold`] how many items must be registered
/// before the cache is worth using.
///
/// ```
/// # use intuicio_core::{registry::Registry, types::TypeQuery};
/// let registry = Registry::default().with_basic_types();
/// assert!(registry.find_type(TypeQuery::of::<i32>()).is_some());
/// ```
#[derive(Debug, Default)]
pub struct Registry {
    functions: Vec<FunctionHandle>,
    types: Vec<TypeHandle>,
    /// How many resolved queries to keep cached. `0` disables the cache.
    pub index_capacity: usize,
    /// How many registered items are needed before the cache is used at all.
    pub use_indexing_threshold: usize,
    functions_index: RwLock<BTreeMap<u64, FunctionHandle>>,
    types_index: RwLock<BTreeMap<u64, TypeHandle>>,
}

impl Clone for Registry {
    fn clone(&self) -> Self {
        Self {
            functions: self.functions.clone(),
            types: self.types.clone(),
            index_capacity: self.index_capacity,
            use_indexing_threshold: self.use_indexing_threshold,
            functions_index: RwLock::new(
                self.functions_index
                    .read()
                    .ok()
                    .map(|items| items.clone())
                    .unwrap_or_default(),
            ),
            types_index: RwLock::new(
                self.types_index
                    .read()
                    .ok()
                    .map(|items| items.clone())
                    .unwrap_or_default(),
            ),
        }
    }
}

impl Registry {
    /// Registers the Rust primitives and [`String`].
    ///
    /// Almost every setup wants these, since function signatures are built from
    /// types looked up in the registry.
    pub fn with_basic_types(self) -> Self {
        unsafe {
            self.with_type(
                NativeStructBuilder::new::<()>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<bool>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<i8>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<i16>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<i32>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<i64>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<i128>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<isize>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<u8>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<u16>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<u32>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<u64>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<u128>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<usize>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<f32>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<f64>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new::<char>()
                    .override_send(true)
                    .override_sync(true)
                    .override_copy(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new_named::<String>("String")
                    .override_send(true)
                    .override_sync(true)
                    .build(),
            )
        }
    }

    /// Registers the type-erased value boxes, so scripts can hold values whose
    /// type they do not know.
    pub fn with_erased_types(self) -> Self {
        unsafe {
            self.with_type(
                NativeStructBuilder::new_named_uninitialized::<DynamicManaged>("DynamicManaged")
                    .override_send(true)
                    .override_sync(true)
                    .build(),
            )
            .with_type(
                NativeStructBuilder::new_named_uninitialized::<DynamicManagedLazy>(
                    "DynamicManagedLazy",
                )
                .override_send(true)
                .override_sync(true)
                .build(),
            )
            .with_type(
                NativeStructBuilder::new_named_uninitialized::<DynamicManagedRef>(
                    "DynamicManagedRef",
                )
                .override_send(true)
                .override_sync(true)
                .build(),
            )
            .with_type(
                NativeStructBuilder::new_named_uninitialized::<DynamicManagedRefMut>(
                    "DynamicManagedRefMut",
                )
                .override_send(true)
                .override_sync(true)
                .build(),
            )
            .with_type(
                NativeStructBuilder::new_named_uninitialized::<DynamicManagedGc>(
                    "DynamicManagedGc",
                )
                .override_send(true)
                .override_sync(true)
                .build(),
            )
            .with_type(
                NativeStructBuilder::new_named_uninitialized::<Object>("Object")
                    .override_send(true)
                    .override_sync(true)
                    .build(),
            )
        }
    }

    /// Sets [`Registry::index_capacity`], builder style.
    pub fn with_index_capacity(mut self, capacity: usize) -> Self {
        self.index_capacity = capacity;
        self
    }

    /// Caches every resolved query, never evicting.
    pub fn with_max_index_capacity(mut self) -> Self {
        self.index_capacity = usize::MAX;
        self
    }

    /// Sets [`Registry::use_indexing_threshold`], builder style.
    pub fn with_use_indexing_threshold(mut self, threshold: usize) -> Self {
        self.use_indexing_threshold = threshold;
        self
    }

    /// Runs a setup closure, builder style. Handy for grouping a library's
    /// registrations into one function.
    pub fn with_install(mut self, f: impl FnOnce(&mut Self)) -> Self {
        self.install(f);
        self
    }

    /// Adds a function, builder style.
    pub fn with_function(mut self, function: Function) -> Self {
        self.add_function(function);
        self
    }

    /// Adds a type, builder style.
    pub fn with_type(mut self, type_: impl Into<Type>) -> Self {
        self.add_type(type_);
        self
    }

    /// Runs a setup closure.
    pub fn install(&mut self, f: impl FnOnce(&mut Self)) {
        f(self);
    }

    /// Adds an already shared function, unless one with the same signature is
    /// registered.
    pub fn add_function_handle(&mut self, function_handle: FunctionHandle) {
        if !self
            .functions
            .iter()
            .any(|handle| handle.signature() == function_handle.signature())
        {
            self.functions.push(function_handle);
        }
    }

    /// Adds a function and returns its handle.
    ///
    /// When a function with the same signature is already registered, that one
    /// is returned and the new one is dropped.
    pub fn add_function(&mut self, function: Function) -> FunctionHandle {
        if let Some(handle) = self
            .functions
            .iter()
            .find(|handle| handle.signature() == function.signature())
        {
            handle.clone()
        } else {
            let handle = FunctionHandle::new(function);
            self.functions.push(handle.clone());
            handle
        }
    }

    /// Removes the function with the same signature as `function_handle`.
    pub fn remove_function(&mut self, function_handle: FunctionHandle) {
        if let Some(position) = self
            .functions
            .iter()
            .position(|handle| handle.signature() == function_handle.signature())
        {
            self.functions.swap_remove(position);
        }
    }

    /// Removes every function the query matches.
    pub fn remove_functions(&mut self, query: FunctionQuery) {
        while let Some(position) = self
            .functions
            .iter()
            .position(|handle| query.is_valid(handle.signature()))
        {
            self.functions.swap_remove(position);
        }
    }

    /// Iterates every registered function.
    pub fn functions(&self) -> impl Iterator<Item = &FunctionHandle> {
        self.functions.iter()
    }

    /// Iterates the functions a query matches, in registration order.
    pub fn find_functions<'a>(
        &'a self,
        query: FunctionQuery<'a>,
    ) -> impl Iterator<Item = FunctionHandle> + 'a {
        self.functions
            .iter()
            .filter(move |handle| query.is_valid(handle.signature()))
            .cloned()
    }

    /// Returns the first function a query matches.
    ///
    /// Goes through the query cache when it is enabled and worth using.
    pub fn find_function<'a>(&'a self, query: FunctionQuery<'a>) -> Option<FunctionHandle> {
        if self.index_capacity == 0 || self.functions.len() < self.use_indexing_threshold {
            self.find_functions(query).next()
        } else if let Ok(mut index) = self.functions_index.try_write() {
            let hash = query.as_hash();
            if let Some(found) = index.get(&hash) {
                Some(found.clone())
            } else if let Some(found) = self.find_functions(query).next() {
                for _ in 0..(index.len().saturating_sub(self.index_capacity)) {
                    if let Some(hash) = index.keys().next().copied() {
                        index.remove(&hash);
                    }
                }
                index.insert(hash, found.clone());
                Some(found)
            } else {
                None
            }
        } else {
            self.find_functions(query).next()
        }
    }

    /// Adds an already shared type, unless an equal one is registered.
    pub fn add_type_handle(&mut self, type_handle: TypeHandle) {
        if !self
            .types
            .iter()
            .any(|handle| handle.as_ref() == type_handle.as_ref())
        {
            self.types.push(type_handle);
        }
    }

    /// Adds a type and returns its handle.
    ///
    /// When an equal type is already registered, that one is returned and the
    /// new one is dropped.
    pub fn add_type(&mut self, type_: impl Into<Type>) -> TypeHandle {
        let type_ = type_.into();
        if let Some(handle) = self.types.iter().find(|handle| handle.as_ref() == &type_) {
            handle.clone()
        } else {
            let handle = TypeHandle::new(type_);
            self.types.push(handle.clone());
            handle
        }
    }

    /// Removes one type.
    pub fn remove_type(&mut self, type_handle: TypeHandle) {
        if let Some(position) = self.types.iter().position(|handle| handle == &type_handle) {
            self.types.swap_remove(position);
        }
    }

    /// Removes every type the query matches.
    pub fn remove_types(&mut self, query: TypeQuery) {
        while let Some(position) = self.types.iter().position(|handle| query.is_valid(handle)) {
            self.types.swap_remove(position);
        }
    }

    /// Iterates every registered type.
    pub fn types(&self) -> impl Iterator<Item = &TypeHandle> {
        self.types.iter()
    }

    /// Iterates the types a query matches, in registration order.
    pub fn find_types<'a>(&'a self, query: TypeQuery<'a>) -> impl Iterator<Item = TypeHandle> + 'a {
        self.types
            .iter()
            .filter(move |handle| query.is_valid(handle))
            .cloned()
    }

    /// Returns the first type a query matches.
    ///
    /// Goes through the query cache when it is enabled and worth using.
    pub fn find_type<'a>(&'a self, query: TypeQuery<'a>) -> Option<TypeHandle> {
        if self.index_capacity == 0 || self.types.len() < self.use_indexing_threshold {
            self.find_types(query).next()
        } else if let Ok(mut index) = self.types_index.try_write() {
            let hash = query.as_hash();
            if let Some(found) = index.get(&hash) {
                Some(found.clone())
            } else if let Some(found) = self.find_types(query).next() {
                for _ in 0..(index.len().saturating_sub(self.index_capacity)) {
                    if let Some(hash) = index.keys().next().copied() {
                        index.remove(&hash);
                    }
                }
                index.insert(hash, found.clone());
                Some(found)
            } else {
                None
            }
        } else {
            self.find_types(query).next()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_async() {
        fn is_async<T: Send + Sync>() {}

        is_async::<Registry>();
        is_async::<String>();
        is_async::<DynamicManaged>();
        is_async::<DynamicManagedLazy>();
        is_async::<DynamicManagedRef>();
        is_async::<DynamicManagedRefMut>();
        is_async::<DynamicManagedGc>();
    }
}