Use locked_read.
This commit is contained in:
parent
84b8e1ed87
commit
7068775f19
@ -1,7 +1,7 @@
|
|||||||
//! Module implementing custom syntax for [`Engine`].
|
//! Module implementing custom syntax for [`Engine`].
|
||||||
|
|
||||||
use crate::ast::Expr;
|
use crate::ast::Expr;
|
||||||
use crate::func::native::SendSync;
|
use crate::func::SendSync;
|
||||||
use crate::parser::ParseResult;
|
use crate::parser::ParseResult;
|
||||||
use crate::tokenizer::{is_valid_identifier, Token};
|
use crate::tokenizer::{is_valid_identifier, Token};
|
||||||
use crate::types::dynamic::Variant;
|
use crate::types::dynamic::Variant;
|
||||||
|
@ -991,7 +991,7 @@ impl Engine {
|
|||||||
if !name.contains(separator.as_ref()) {
|
if !name.contains(separator.as_ref()) {
|
||||||
if !module.is_indexed() {
|
if !module.is_indexed() {
|
||||||
// Index the module (making a clone copy if necessary) if it is not indexed
|
// Index the module (making a clone copy if necessary) if it is not indexed
|
||||||
let mut module = crate::func::native::shared_take_or_clone(module);
|
let mut module = crate::func::shared_take_or_clone(module);
|
||||||
module.build_index();
|
module.build_index();
|
||||||
root.insert(name.into(), module.into());
|
root.insert(name.into(), module.into());
|
||||||
} else {
|
} else {
|
||||||
@ -1009,7 +1009,7 @@ impl Engine {
|
|||||||
root.insert(sub_module.into(), m.into());
|
root.insert(sub_module.into(), m.into());
|
||||||
} else {
|
} else {
|
||||||
let m = root.remove(sub_module).expect("contains sub-module");
|
let m = root.remove(sub_module).expect("contains sub-module");
|
||||||
let mut m = crate::func::native::shared_take_or_clone(m);
|
let mut m = crate::func::shared_take_or_clone(m);
|
||||||
register_static_module_raw(m.sub_modules_mut(), remainder, module);
|
register_static_module_raw(m.sub_modules_mut(), remainder, module);
|
||||||
m.build_index();
|
m.build_index();
|
||||||
root.insert(sub_module.into(), m.into());
|
root.insert(sub_module.into(), m.into());
|
||||||
|
@ -615,8 +615,7 @@ impl AST {
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
if !other.lib.is_empty() {
|
if !other.lib.is_empty() {
|
||||||
crate::func::native::shared_make_mut(&mut self.lib)
|
crate::func::shared_make_mut(&mut self.lib).merge_filtered(&other.lib, &_filter);
|
||||||
.merge_filtered(&other.lib, &_filter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
@ -629,10 +628,8 @@ impl AST {
|
|||||||
self.set_resolver(other.resolver.unwrap());
|
self.set_resolver(other.resolver.unwrap());
|
||||||
}
|
}
|
||||||
(_, _) => {
|
(_, _) => {
|
||||||
let resolver =
|
let resolver = crate::func::shared_make_mut(self.resolver.as_mut().unwrap());
|
||||||
crate::func::native::shared_make_mut(self.resolver.as_mut().unwrap());
|
let other_resolver = crate::func::shared_take_or_clone(other.resolver.unwrap());
|
||||||
let other_resolver =
|
|
||||||
crate::func::native::shared_take_or_clone(other.resolver.unwrap());
|
|
||||||
for (k, v) in other_resolver {
|
for (k, v) in other_resolver {
|
||||||
resolver.insert(k, crate::func::shared_take_or_clone(v));
|
resolver.insert(k, crate::func::shared_take_or_clone(v));
|
||||||
}
|
}
|
||||||
@ -673,7 +670,7 @@ impl AST {
|
|||||||
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
|
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
if !self.lib.is_empty() {
|
if !self.lib.is_empty() {
|
||||||
crate::func::native::shared_make_mut(&mut self.lib).retain_script_functions(filter);
|
crate::func::shared_make_mut(&mut self.lib).retain_script_functions(filter);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -1222,7 +1222,7 @@ impl Engine {
|
|||||||
let (mut target, _pos) =
|
let (mut target, _pos) =
|
||||||
self.search_namespace(scope, global, lib, this_ptr, first_expr, level)?;
|
self.search_namespace(scope, global, lib, this_ptr, first_expr, level)?;
|
||||||
|
|
||||||
if target.as_ref().is_read_only() {
|
if target.is_read_only() {
|
||||||
target = target.into_owned();
|
target = target.into_owned();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,8 +22,8 @@ pub use hashing::{
|
|||||||
combine_hashes, get_hasher,
|
combine_hashes, get_hasher,
|
||||||
};
|
};
|
||||||
pub use native::{
|
pub use native::{
|
||||||
locked_read, locked_write, shared_make_mut, shared_take, shared_take_or_clone, shared_try_take,
|
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,
|
||||||
FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,
|
shared_try_take, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,
|
||||||
};
|
};
|
||||||
pub use plugin::PluginFunction;
|
pub use plugin::PluginFunction;
|
||||||
pub use register::RegisterNativeFunction;
|
pub use register::RegisterNativeFunction;
|
||||||
|
@ -307,12 +307,7 @@ impl FileModuleResolver {
|
|||||||
let file_path = self.get_file_path(path, source_path);
|
let file_path = self.get_file_path(path, source_path);
|
||||||
|
|
||||||
if self.is_cache_enabled() {
|
if self.is_cache_enabled() {
|
||||||
#[cfg(not(feature = "sync"))]
|
if let Some(module) = locked_read(&self.cache).get(&file_path) {
|
||||||
let c = self.cache.borrow();
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
let c = self.cache.read().unwrap();
|
|
||||||
|
|
||||||
if let Some(module) = c.get(&file_path) {
|
|
||||||
return Ok(module.clone());
|
return Ok(module.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::eval::GlobalRuntimeState;
|
use crate::eval::GlobalRuntimeState;
|
||||||
use crate::func::native::SendSync;
|
use crate::func::SendSync;
|
||||||
use crate::{Engine, Module, Position, RhaiResultOf, Shared, AST};
|
use crate::{Engine, Module, Position, RhaiResultOf, Shared, AST};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
|
@ -1343,7 +1343,7 @@ pub fn optimize_into_ast(
|
|||||||
let lib2 = &[&lib2];
|
let lib2 = &[&lib2];
|
||||||
|
|
||||||
for fn_def in functions {
|
for fn_def in functions {
|
||||||
let mut fn_def = crate::func::native::shared_take_or_clone(fn_def);
|
let mut fn_def = crate::func::shared_take_or_clone(fn_def);
|
||||||
|
|
||||||
// Optimize the function body
|
// Optimize the function body
|
||||||
let body = mem::take(&mut *fn_def.body);
|
let body = mem::take(&mut *fn_def.body);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Helper module which defines the [`Dynamic`] data type and the
|
//! Helper module which defines the [`Dynamic`] data type and the
|
||||||
//! [`Any`] trait to to allow custom type handling.
|
//! [`Any`] trait to to allow custom type handling.
|
||||||
|
|
||||||
use crate::func::native::SendSync;
|
use crate::func::{locked_read, SendSync};
|
||||||
use crate::{reify, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT};
|
use crate::{reify, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
@ -26,7 +26,7 @@ pub use instant::Instant;
|
|||||||
const CHECKED: &str = "data type was checked";
|
const CHECKED: &str = "data type was checked";
|
||||||
|
|
||||||
mod private {
|
mod private {
|
||||||
use crate::func::native::SendSync;
|
use crate::func::SendSync;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
/// A sealed trait that prevents other crates from implementing [`Variant`].
|
/// A sealed trait that prevents other crates from implementing [`Variant`].
|
||||||
@ -384,12 +384,7 @@ impl Dynamic {
|
|||||||
Union::Variant(ref v, ..) => (***v).type_id(),
|
Union::Variant(ref v, ..) => (***v).type_id(),
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
#[cfg(not(feature = "sync"))]
|
Union::Shared(ref cell, ..) => (*locked_read(cell)).type_id(),
|
||||||
Union::Shared(ref cell, ..) => (*cell.borrow()).type_id(),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
Union::Shared(ref cell, ..) => (*cell.read().unwrap()).type_id(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Get the name of the type of the value held by this [`Dynamic`].
|
/// Get the name of the type of the value held by this [`Dynamic`].
|
||||||
@ -463,12 +458,7 @@ impl Hash for Dynamic {
|
|||||||
Union::FnPtr(ref f, ..) => f.hash(state),
|
Union::FnPtr(ref f, ..) => f.hash(state),
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
#[cfg(not(feature = "sync"))]
|
Union::Shared(ref cell, ..) => (*locked_read(cell)).hash(state),
|
||||||
Union::Shared(ref cell, ..) => (*cell.borrow()).hash(state),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
Union::Shared(ref cell, ..) => (*cell.read().unwrap()).hash(state),
|
|
||||||
|
|
||||||
Union::Variant(..) => unimplemented!("{} cannot be hashed", self.type_name()),
|
Union::Variant(..) => unimplemented!("{} cannot be hashed", self.type_name()),
|
||||||
|
|
||||||
@ -1025,16 +1015,8 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Shared(.., ReadOnly) => return true,
|
Union::Shared(.., ReadOnly) => return true,
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
|
||||||
Union::Shared(ref cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
return match cell.borrow().access_mode() {
|
return match locked_read(cell).access_mode() {
|
||||||
ReadWrite => false,
|
|
||||||
ReadOnly => true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
Union::Shared(ref cell, ..) => {
|
|
||||||
return match cell.read().unwrap().access_mode() {
|
|
||||||
ReadWrite => false,
|
ReadWrite => false,
|
||||||
ReadOnly => true,
|
ReadOnly => true,
|
||||||
}
|
}
|
||||||
@ -1066,12 +1048,7 @@ impl Dynamic {
|
|||||||
Union::Map(..) => true,
|
Union::Map(..) => true,
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
#[cfg(not(feature = "sync"))]
|
Union::Shared(ref cell, ..) => locked_read(cell).is_hashable(),
|
||||||
Union::Shared(ref cell, ..) => cell.borrow().is_hashable(),
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
Union::Shared(ref cell, ..) => cell.read().unwrap().is_hashable(),
|
|
||||||
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@ -1322,11 +1299,7 @@ impl Dynamic {
|
|||||||
pub fn flatten_clone(&self) -> Self {
|
pub fn flatten_clone(&self) -> Self {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
#[cfg(not(feature = "sync"))]
|
Union::Shared(ref cell, ..) => locked_read(cell).clone(),
|
||||||
Union::Shared(ref cell, ..) => cell.borrow().clone(),
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
Union::Shared(ref cell, ..) => cell.read().unwrap().clone(),
|
|
||||||
_ => self.clone(),
|
_ => self.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1341,12 +1314,8 @@ impl Dynamic {
|
|||||||
pub fn flatten(self) -> Self {
|
pub fn flatten(self) -> Self {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(cell, ..) => crate::func::native::shared_try_take(cell).map_or_else(
|
Union::Shared(cell, ..) => crate::func::shared_try_take(cell).map_or_else(
|
||||||
#[cfg(not(feature = "sync"))]
|
|ref cell| locked_read(cell).clone(),
|
||||||
|cell| cell.borrow().clone(),
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
|cell| cell.read().unwrap().clone(),
|
|
||||||
#[cfg(not(feature = "sync"))]
|
|
||||||
|value| value.into_inner(),
|
|value| value.into_inner(),
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
|value| value.into_inner().unwrap(),
|
|value| value.into_inner().unwrap(),
|
||||||
@ -1366,11 +1335,8 @@ impl Dynamic {
|
|||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(ref mut cell, ..) => {
|
Union::Shared(ref mut cell, ..) => {
|
||||||
let cell = mem::take(cell);
|
let cell = mem::take(cell);
|
||||||
*self = crate::func::native::shared_try_take(cell).map_or_else(
|
*self = crate::func::shared_try_take(cell).map_or_else(
|
||||||
#[cfg(not(feature = "sync"))]
|
|ref cell| locked_read(cell).clone(),
|
||||||
|cell| cell.borrow().clone(),
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
|cell| cell.read().unwrap().clone(),
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
|value| value.into_inner(),
|
|value| value.into_inner(),
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
@ -1422,10 +1388,7 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(ref cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
#[cfg(not(feature = "sync"))]
|
let value = locked_read(cell);
|
||||||
let value = cell.borrow();
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
let value = cell.read().unwrap();
|
|
||||||
|
|
||||||
if (*value).type_id() != TypeId::of::<T>()
|
if (*value).type_id() != TypeId::of::<T>()
|
||||||
&& TypeId::of::<Dynamic>() != TypeId::of::<T>()
|
&& TypeId::of::<Dynamic>() != TypeId::of::<T>()
|
||||||
@ -1457,7 +1420,7 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(ref cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
let guard = crate::func::native::locked_write(cell);
|
let guard = crate::func::locked_write(cell);
|
||||||
|
|
||||||
if (*guard).type_id() != TypeId::of::<T>()
|
if (*guard).type_id() != TypeId::of::<T>()
|
||||||
&& TypeId::of::<Dynamic>() != TypeId::of::<T>()
|
&& TypeId::of::<Dynamic>() != TypeId::of::<T>()
|
||||||
@ -1772,11 +1735,8 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Str(s, ..) => Ok(s),
|
Union::Str(s, ..) => Ok(s),
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
#[cfg(not(feature = "sync"))]
|
let value = locked_read(cell);
|
||||||
let value = cell.borrow();
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
let value = cell.read().unwrap();
|
|
||||||
|
|
||||||
match value.0 {
|
match value.0 {
|
||||||
Union::Str(ref s, ..) => Ok(s.clone()),
|
Union::Str(ref s, ..) => Ok(s.clone()),
|
||||||
@ -1794,11 +1754,8 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Array(a, ..) => Ok(*a),
|
Union::Array(a, ..) => Ok(*a),
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
#[cfg(not(feature = "sync"))]
|
let value = locked_read(cell);
|
||||||
let value = cell.borrow();
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
let value = cell.read().unwrap();
|
|
||||||
|
|
||||||
match value.0 {
|
match value.0 {
|
||||||
Union::Array(ref a, ..) => Ok(a.as_ref().clone()),
|
Union::Array(ref a, ..) => Ok(a.as_ref().clone()),
|
||||||
@ -1832,11 +1789,8 @@ impl Dynamic {
|
|||||||
.collect(),
|
.collect(),
|
||||||
Union::Blob(..) if TypeId::of::<T>() == TypeId::of::<u8>() => Ok(self.cast::<Vec<T>>()),
|
Union::Blob(..) if TypeId::of::<T>() == TypeId::of::<u8>() => Ok(self.cast::<Vec<T>>()),
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
#[cfg(not(feature = "sync"))]
|
let value = locked_read(cell);
|
||||||
let value = cell.borrow();
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
let value = cell.read().unwrap();
|
|
||||||
|
|
||||||
match value.0 {
|
match value.0 {
|
||||||
Union::Array(ref a, ..) => {
|
Union::Array(ref a, ..) => {
|
||||||
@ -1873,11 +1827,8 @@ impl Dynamic {
|
|||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Blob(a, ..) => Ok(*a),
|
Union::Blob(a, ..) => Ok(*a),
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
Union::Shared(cell, ..) => {
|
Union::Shared(ref cell, ..) => {
|
||||||
#[cfg(not(feature = "sync"))]
|
let value = locked_read(cell);
|
||||||
let value = cell.borrow();
|
|
||||||
#[cfg(feature = "sync")]
|
|
||||||
let value = cell.read().unwrap();
|
|
||||||
|
|
||||||
match value.0 {
|
match value.0 {
|
||||||
Union::Blob(ref a, ..) => Ok(a.as_ref().clone()),
|
Union::Blob(ref a, ..) => Ok(a.as_ref().clone()),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! The `ImmutableString` type.
|
//! The `ImmutableString` type.
|
||||||
|
|
||||||
use crate::func::native::{shared_get_mut, shared_make_mut, shared_take};
|
use crate::func::{shared_get_mut, shared_make_mut, shared_take};
|
||||||
use crate::{Shared, SmartString};
|
use crate::{Shared, SmartString};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user