diff --git a/CHANGELOG.md b/CHANGELOG.md index d69eb007a..65b7425cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Potentially breaking changes * `ImmutableString` now derefs to `&str` instead of `&SmartString`. Normally this should not be a breaking change. * Traits implemented by `ImmutableString` are cleaned up. However, I cannot guarantee that there are absolutely no breaking changes, although I try to be careful. * `EvalContext::new`, `FloatWrapper` and `ConditionalExpr` are now exported only under `internals`. +* `AST::clear_doc` is removed. Deprecated API's ---------------- diff --git a/src/api/call_fn.rs b/src/api/call_fn.rs index bba9a2586..05962f8de 100644 --- a/src/api/call_fn.rs +++ b/src/api/call_fn.rs @@ -224,10 +224,8 @@ impl Engine { global.lib.push(ast.shared_lib().clone()); #[cfg(not(feature = "no_module"))] - let orig_embedded_module_resolver = std::mem::replace( - &mut global.embedded_module_resolver, - ast.resolver().cloned(), - ); + let orig_embedded_module_resolver = + std::mem::replace(&mut global.embedded_module_resolver, ast.resolver.clone()); let rewind_scope = options.rewind_scope; diff --git a/src/api/compile.rs b/src/api/compile.rs index d60c368ff..b36c07158 100644 --- a/src/api/compile.rs +++ b/src/api/compile.rs @@ -100,7 +100,7 @@ impl Engine { resolver: &StaticModuleResolver, imports: &mut BTreeSet, ) { - ast.walk(&mut |path| match path.last().unwrap() { + ast._walk(&mut |path| match path.last().unwrap() { // Collect all `import` statements with a string constant path ASTNode::Stmt(Stmt::Import(x, ..)) => match x.0 { Expr::StringConstant(ref s, ..) @@ -144,7 +144,7 @@ impl Engine { resolver.insert(path, module); } - ast.set_resolver(resolver); + ast.resolver = Some(resolver.into()); } Ok(ast) @@ -234,7 +234,10 @@ impl Engine { let state = &mut ParseState::new(scope, interned_strings, tc); let mut _ast = self.parse(stream.peekable(), state, optimization_level)?; #[cfg(feature = "metadata")] - _ast.set_doc(&state.tokenizer_control.borrow().global_comments); + { + let global_comments = &state.tokenizer_control.borrow().global_comments; + _ast.doc = global_comments.into(); + } Ok(_ast) } /// Compile a string containing an expression into an [`AST`], diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index 995db5f01..4dc624cdf 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -263,7 +263,7 @@ impl Engine { // Make it a custom keyword/symbol if it is disabled or reserved if (self.is_symbol_disabled(s) || token.as_ref().map_or(false, Token::is_reserved)) - && !self.is_custom_keyword(s) + && !self.custom_keywords.contains_key(s) { self.custom_keywords.insert(s.into(), None); } @@ -289,7 +289,7 @@ impl Engine { // Make it a custom keyword/symbol if it is disabled or reserved if self.is_symbol_disabled(s) || (token.as_ref().map_or(false, Token::is_reserved) - && !self.is_custom_keyword(s)) + && !self.custom_keywords.contains_key(s)) { self.custom_keywords.insert(s.into(), None); } diff --git a/src/api/definitions/mod.rs b/src/api/definitions/mod.rs index cd4f29c28..ea634ea7c 100644 --- a/src/api/definitions/mod.rs +++ b/src/api/definitions/mod.rs @@ -457,7 +457,8 @@ impl Module { !f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name); #[cfg(not(feature = "no_custom_syntax"))] - let operator = operator || def.engine.is_custom_keyword(&f.metadata.name); + let operator = + operator || def.engine.custom_keywords.contains_key(&f.metadata.name); f.write_definition(writer, def, operator)?; } diff --git a/src/api/eval.rs b/src/api/eval.rs index 3a20cf142..6a68fde4c 100644 --- a/src/api/eval.rs +++ b/src/api/eval.rs @@ -240,10 +240,8 @@ impl Engine { global.lib.push(ast.shared_lib().clone()); #[cfg(not(feature = "no_module"))] - let orig_embedded_module_resolver = mem::replace( - &mut global.embedded_module_resolver, - ast.resolver().cloned(), - ); + let orig_embedded_module_resolver = + mem::replace(&mut global.embedded_module_resolver, ast.resolver.clone()); defer! { global => move |g| { #[cfg(not(feature = "no_module"))] diff --git a/src/api/formatting.rs b/src/api/formatting.rs index b7b51cd07..1f93a63f5 100644 --- a/src/api/formatting.rs +++ b/src/api/formatting.rs @@ -260,8 +260,8 @@ impl Engine { #[inline(never)] #[must_use] pub(crate) fn make_type_mismatch_err(&self, typ: &str, pos: Position) -> RhaiError { - let t = self.map_type_name(type_name::()).into(); - ERR::ErrorMismatchDataType(t, typ.into(), pos).into() + ERR::ErrorMismatchDataType(self.map_type_name(type_name::()).into(), typ.into(), pos) + .into() } /// Compact a script to eliminate insignificant whitespaces and comments. diff --git a/src/api/mod.rs b/src/api/mod.rs index edb7b46fc..96a7908a4 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -211,14 +211,6 @@ impl Engine { Ok(self) } - /// Is a keyword registered as a custom keyword? - /// - /// Not available under `no_custom_syntax`. - #[cfg(not(feature = "no_custom_syntax"))] - #[inline(always)] - pub(crate) fn is_custom_keyword(&self, keyword: &str) -> bool { - self.custom_keywords.contains_key(keyword) - } /// Get the default value of the custom state for each evaluation run. #[inline(always)] diff --git a/src/api/optimize.rs b/src/api/optimize.rs index 5e3f1dbad..301a49748 100644 --- a/src/api/optimize.rs +++ b/src/api/optimize.rs @@ -64,7 +64,9 @@ impl Engine { ); #[cfg(feature = "metadata")] - _new_ast.set_doc(std::mem::take(ast.doc_mut())); + { + _new_ast.doc = std::mem::take(&mut ast.doc); + } _new_ast } diff --git a/src/api/run.rs b/src/api/run.rs index a677b0acf..4492bfa60 100644 --- a/src/api/run.rs +++ b/src/api/run.rs @@ -135,7 +135,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] { - global.embedded_module_resolver = ast.resolver().cloned(); + global.embedded_module_resolver = ast.resolver.clone(); } let _ = self.eval_global_statements(global, caches, scope, ast.statements(), true)?; diff --git a/src/ast/ast.rs b/src/ast/ast.rs index c60468e8c..681378838 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -28,10 +28,10 @@ pub struct AST { lib: crate::SharedModule, /// Embedded module resolver, if any. #[cfg(not(feature = "no_module"))] - resolver: Option>, + pub(crate) resolver: Option>, /// [`AST`] documentation. #[cfg(feature = "metadata")] - doc: crate::SmartString, + pub(crate) doc: crate::SmartString, } impl Default for AST { @@ -206,28 +206,6 @@ impl AST { pub fn doc(&self) -> &str { &self.doc } - /// Get a mutable reference to the documentation. - #[cfg(feature = "metadata")] - #[inline(always)] - #[must_use] - #[allow(dead_code)] - pub(crate) fn doc_mut(&mut self) -> &mut crate::SmartString { - &mut self.doc - } - /// Set the documentation. - #[cfg(feature = "metadata")] - #[inline(always)] - pub(crate) fn set_doc(&mut self, doc: impl Into) { - self.doc = doc.into(); - } - /// Clear the documentation. - /// Exported under the `metadata` feature only. - #[cfg(feature = "metadata")] - #[inline(always)] - pub fn clear_doc(&mut self) -> &mut Self { - self.doc.clear(); - self - } /// Get the statements. #[cfg(not(feature = "internals"))] #[inline(always)] @@ -278,16 +256,6 @@ impl AST { pub const fn shared_lib(&self) -> &crate::SharedModule { &self.lib } - /// Get the embedded [module resolver][crate::ModuleResolver]. - #[cfg(not(feature = "internals"))] - #[cfg(not(feature = "no_module"))] - #[inline(always)] - #[must_use] - pub(crate) const fn resolver( - &self, - ) -> Option<&crate::Shared> { - self.resolver.as_ref() - } /// _(internals)_ Get the embedded [module resolver][crate::ModuleResolver]. /// Exported under the `internals` feature only. /// @@ -301,16 +269,6 @@ impl AST { ) -> Option<&crate::Shared> { self.resolver.as_ref() } - /// Set the embedded [module resolver][crate::ModuleResolver]. - #[cfg(not(feature = "no_module"))] - #[inline(always)] - pub(crate) fn set_resolver( - &mut self, - resolver: impl Into>, - ) -> &mut Self { - self.resolver = Some(resolver.into()); - self - } /// Clone the [`AST`]'s functions into a new [`AST`]. /// No statements are cloned. /// @@ -574,23 +532,18 @@ impl AST { #[cfg(not(feature = "no_module"))] match ( - self.resolver().map_or(true, |r| r.is_empty()), - other.resolver().map_or(true, |r| r.is_empty()), + self.resolver.as_deref().map_or(true, |r| r.is_empty()), + other.resolver.as_deref().map_or(true, |r| r.is_empty()), ) { (true, true) => (), - (false, true) => { - _ast.set_resolver(self.resolver().unwrap().clone()); - } - (true, false) => { - _ast.set_resolver(other.resolver().unwrap().clone()); - } + (false, true) => _ast.resolver = self.resolver.clone(), + (true, false) => _ast.resolver = other.resolver.clone(), (false, false) => { - let mut resolver = self.resolver().unwrap().as_ref().clone(); - let other_resolver = other.resolver().unwrap().as_ref().clone(); - for (k, v) in other_resolver { - resolver.insert(k, crate::func::shared_take_or_clone(v)); + let mut resolver = self.resolver.as_deref().unwrap().clone(); + for (k, v) in other.resolver.as_deref().unwrap() { + resolver.insert(k.clone(), v.as_ref().clone()); } - _ast.set_resolver(resolver); + _ast.resolver = Some(resolver.into()); } } @@ -673,13 +626,11 @@ impl AST { ) -> &mut Self { #[cfg(not(feature = "no_module"))] match ( - self.resolver().map_or(true, |r| r.is_empty()), - other.resolver().map_or(true, |r| r.is_empty()), + self.resolver.as_deref().map_or(true, |r| r.is_empty()), + other.resolver.as_deref().map_or(true, |r| r.is_empty()), ) { (_, true) => (), - (true, false) => { - self.set_resolver(other.resolver.unwrap()); - } + (true, false) => self.resolver = other.resolver.clone(), (false, false) => { let resolver = crate::func::shared_make_mut(self.resolver.as_mut().unwrap()); let other_resolver = crate::func::shared_take_or_clone(other.resolver.unwrap()); @@ -876,14 +827,6 @@ impl AST { _ => None, }) } - /// Recursively walk the [`AST`], including function bodies (if any). - /// Return `false` from the callback to terminate the walk. - #[cfg(not(feature = "internals"))] - #[cfg(not(feature = "no_module"))] - #[inline(always)] - pub(crate) fn walk(&self, on_node: &mut (impl FnMut(&[ASTNode]) -> bool + ?Sized)) -> bool { - self._walk(on_node) - } /// _(internals)_ Recursively walk the [`AST`], including function bodies (if any). /// Return `false` from the callback to terminate the walk. /// Exported under the `internals` feature only. @@ -894,7 +837,7 @@ impl AST { } /// Recursively walk the [`AST`], including function bodies (if any). /// Return `false` from the callback to terminate the walk. - fn _walk(&self, on_node: &mut (impl FnMut(&[ASTNode]) -> bool + ?Sized)) -> bool { + pub(crate) fn _walk(&self, on_node: &mut (impl FnMut(&[ASTNode]) -> bool + ?Sized)) -> bool { let path = &mut Vec::new(); for stmt in self.statements() { diff --git a/src/ast/ident.rs b/src/ast/ident.rs index 3641bd577..72c923272 100644 --- a/src/ast/ident.rs +++ b/src/ast/ident.rs @@ -20,7 +20,10 @@ impl fmt::Debug for Ident { #[inline(never)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.name)?; - self.pos.debug_print(f) + if !self.pos.is_none() { + write!(f, " @ {:?}", self.pos)?; + } + Ok(()) } } diff --git a/src/eval/eval_context.rs b/src/eval/eval_context.rs index 08fd970cd..9d5ae4ca2 100644 --- a/src/eval/eval_context.rs +++ b/src/eval/eval_context.rs @@ -123,7 +123,7 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> { #[cfg(not(feature = "no_function"))] #[inline] pub fn iter_namespaces(&self) -> impl Iterator { - self.global.lib.iter().map(AsRef::as_ref) + self.global.lib.iter().map(<_>::as_ref) } /// _(internals)_ The current set of namespaces containing definitions of all script-defined functions. /// Exported under the `internals` feature only. diff --git a/src/eval/expr.rs b/src/eval/expr.rs index c9c6dd699..67ef23b7a 100644 --- a/src/eval/expr.rs +++ b/src/eval/expr.rs @@ -71,9 +71,13 @@ pub fn search_scope_only<'s>( .flat_map(|m| m.iter_script_fn()) .find_map(|(_, _, f, _, func)| if f == &v.3 { Some(func) } else { None }) { - let mut fn_ptr = crate::FnPtr::new_unchecked(v.3.clone(), Vec::new()); - fn_ptr.set_fn_def(Some(fn_def.clone())); - let val: Dynamic = fn_ptr.into(); + let val: Dynamic = crate::FnPtr { + name: v.3.clone(), + curry: Vec::new(), + environ: None, + fn_def: Some(fn_def.clone()), + } + .into(); return Ok(val.into()); } diff --git a/src/func/call.rs b/src/func/call.rs index 267ccc996..ef467e603 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -744,14 +744,14 @@ impl Engine { let _fn_def = (); #[cfg(not(feature = "no_function"))] - let _fn_def = fn_ptr.fn_def(); + let _fn_def = fn_ptr.fn_def.as_deref(); match _fn_def { // Linked to scripted function - short-circuit #[cfg(not(feature = "no_function"))] Some(fn_def) if fn_def.params.len() == args.len() => { let scope = &mut Scope::new(); - let environ = fn_ptr.encapsulated_environ().map(<_>::as_ref); + let environ = fn_ptr.environ.as_ref().map(<_>::as_ref); self.call_script_fn( global, caches, scope, None, environ, fn_def, args, true, pos, @@ -801,13 +801,20 @@ impl Engine { })?; #[cfg(not(feature = "no_function"))] - let (is_anon, (fn_name, fn_curry, environ, fn_def)) = - (fn_ptr.is_anonymous(), fn_ptr.take_data()); + let ( + is_anon, + FnPtr { + name, + curry, + environ, + fn_def, + }, + ) = (fn_ptr.is_anonymous(), fn_ptr); #[cfg(feature = "no_function")] - let (is_anon, (fn_name, fn_curry, _), fn_def) = (false, fn_ptr.take_data(), ()); + let (is_anon, FnPtr { name, curry, .. }, fn_def) = (false, fn_ptr, ()); // Adding the curried arguments and the remaining arguments - let mut curry = fn_curry.into_iter().collect::>(); + let mut curry = curry.into_iter().collect::>(); let args = &mut FnArgsVec::with_capacity(curry.len() + call_args.len()); args.extend(curry.iter_mut()); args.extend(call_args.iter_mut().skip(1)); @@ -838,27 +845,25 @@ impl Engine { // Recalculate hash let num_args = args.len(); - let new_hash = if !is_anon && !is_valid_function_name(&fn_name) { - FnCallHashes::from_native_only(calc_fn_hash(None, &fn_name, num_args)) + let new_hash = if !is_anon && !is_valid_function_name(&name) { + FnCallHashes::from_native_only(calc_fn_hash(None, &name, num_args)) } else { #[cfg(not(feature = "no_function"))] { FnCallHashes::from_script_and_native( - calc_fn_hash(None, &fn_name, num_args - 1), - calc_fn_hash(None, &fn_name, num_args), + calc_fn_hash(None, &name, num_args - 1), + calc_fn_hash(None, &name, num_args), ) } #[cfg(feature = "no_function")] { - FnCallHashes::from_native_only(calc_fn_hash( - None, &fn_name, num_args, - )) + FnCallHashes::from_native_only(calc_fn_hash(None, &name, num_args)) } }; // Map it to name(args) in function-call style self.exec_fn_call( - global, caches, None, &fn_name, None, new_hash, args, is_ref_mut, true, + global, caches, None, &name, None, new_hash, args, is_ref_mut, true, pos, ) } @@ -917,16 +922,13 @@ impl Engine { let _fn_def = (); #[cfg(not(feature = "no_function"))] - let _fn_def = fn_ptr.fn_def(); + let _fn_def = fn_ptr.fn_def.as_deref(); match _fn_def { // Linked to scripted function #[cfg(not(feature = "no_function"))] Some(fn_def) if fn_def.params.len() == call_args.len() => { - _linked = Some(( - fn_def.clone(), - fn_ptr.encapsulated_environ().cloned(), - )) + _linked = Some((fn_def.clone(), fn_ptr.environ.clone())) } _ => { let _is_anon = false; @@ -1020,11 +1022,11 @@ impl Engine { let mut args_expr = args_expr; let mut num_args = usize::from(first_arg.is_some()) + args_expr.len(); let mut curry = FnArgsVec::new_const(); - let mut name = fn_name; + let mut fn_name = fn_name; let mut hashes = hashes; let redirected; // Handle call() - Redirect function call - match name { + match fn_name { _ if op_token.is_some() => (), // Handle call(fn_ptr, ...) @@ -1041,12 +1043,26 @@ impl Engine { })?; #[cfg(not(feature = "no_function"))] - let (is_anon, (fn_name, fn_curry, _environ, fn_def)) = - (fn_ptr.is_anonymous(), fn_ptr.take_data()); + let ( + is_anon, + FnPtr { + name, + curry: extra_curry, + environ, + fn_def, + }, + ) = (fn_ptr.is_anonymous(), fn_ptr); #[cfg(feature = "no_function")] - let (is_anon, (fn_name, fn_curry, _environ)) = (false, fn_ptr.take_data()); + let ( + is_anon, + FnPtr { + name, + curry: extra_curry, + .. + }, + ) = (false, fn_ptr); - curry.extend(fn_curry.into_iter()); + curry.extend(extra_curry.into_iter()); // Linked to scripted function - short-circuit #[cfg(not(feature = "no_function"))] @@ -1064,7 +1080,7 @@ impl Engine { } let args = &mut arg_values.iter_mut().collect::>(); let scope = &mut Scope::new(); - let environ = _environ.as_deref(); + let environ = environ.as_deref(); return self.call_script_fn( global, caches, scope, None, environ, &fn_def, args, true, pos, @@ -1073,8 +1089,8 @@ impl Engine { } // Redirect function name - redirected = fn_name; - name = &redirected; + redirected = name; + fn_name = &redirected; // Shift the arguments first_arg = args_expr.get(0); @@ -1086,10 +1102,10 @@ impl Engine { // Recalculate hash let args_len = num_args + curry.len(); - hashes = if !is_anon && !is_valid_function_name(name) { - FnCallHashes::from_native_only(calc_fn_hash(None, name, args_len)) + hashes = if !is_anon && !is_valid_function_name(fn_name) { + FnCallHashes::from_native_only(calc_fn_hash(None, fn_name, args_len)) } else { - FnCallHashes::from_hash(calc_fn_hash(None, name, args_len)) + FnCallHashes::from_hash(calc_fn_hash(None, fn_name, args_len)) }; } @@ -1292,7 +1308,7 @@ impl Engine { return self .exec_fn_call( - global, caches, scope, name, op_token, hashes, &mut args, is_ref_mut, false, + global, caches, scope, fn_name, op_token, hashes, &mut args, is_ref_mut, false, pos, ) .map(|(v, ..)| v); @@ -1370,7 +1386,7 @@ impl Engine { args.extend(arg_values.iter_mut()); self.exec_fn_call( - global, caches, None, name, op_token, hashes, &mut args, is_ref_mut, false, pos, + global, caches, None, fn_name, op_token, hashes, &mut args, is_ref_mut, false, pos, ) .map(|(v, ..)| v) } diff --git a/src/func/native.rs b/src/func/native.rs index 3e855c9c3..60d0a581a 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -277,7 +277,7 @@ impl<'a> NativeCallContext<'a> { #[cfg(not(feature = "no_function"))] #[inline] pub fn iter_namespaces(&self) -> impl Iterator { - self.global.lib.iter().map(AsRef::as_ref) + self.global.lib.iter().map(<_>::as_ref) } /// _(internals)_ The current stack of namespaces containing definitions of all script-defined functions. /// Exported under the `internals` feature only. diff --git a/src/module/mod.rs b/src/module/mod.rs index 4c954a889..2e83cd562 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -2246,7 +2246,7 @@ impl Module { value.deep_scan(|v| { if let Some(fn_ptr) = v.downcast_mut::() { - fn_ptr.set_encapsulated_environ(Some(environ.clone())); + fn_ptr.environ = Some(environ.clone()); } }); diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 8bcbb8b33..14bacb6cc 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -1270,7 +1270,13 @@ pub mod array_functions { /// print(x); // prints "[1, 2, 3, 4, 3, 2, 1]" /// ``` pub fn dedup(ctx: NativeCallContext, array: &mut Array) { - let comparer = FnPtr::new_unchecked(OP_EQUALS, Vec::new()); + let comparer = FnPtr { + name: ctx.engine().get_interned_string(OP_EQUALS), + curry: Vec::new(), + environ: None, + #[cfg(not(feature = "no_function"))] + fn_def: None, + }; dedup_by_comparer(ctx, array, comparer); } /// Remove duplicated _consecutive_ elements from the array that return `true` when applied the diff --git a/src/parser.rs b/src/parser.rs index 6ee5accf7..7558ddc0c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2449,7 +2449,7 @@ impl Engine { } #[cfg(not(feature = "no_custom_syntax"))] - Token::Custom(s) if self.is_custom_keyword(&s) => { + Token::Custom(s) if self.custom_keywords.contains_key(&*s) => { op_base.hashes = if native_only { FnCallHashes::from_native_only(calc_fn_hash(None, &s, 2)) } else { @@ -3884,8 +3884,13 @@ impl Engine { comments: <_>::default(), }); - let mut fn_ptr = crate::FnPtr::new_unchecked(fn_name, Vec::new()); - fn_ptr.set_fn_def(Some(script.clone())); + let fn_ptr = crate::FnPtr { + name: fn_name, + curry: Vec::new(), + environ: None, + #[cfg(not(feature = "no_function"))] + fn_def: Some(script.clone()), + }; let expr = Expr::DynamicConstant(Box::new(fn_ptr.into()), settings.pos); #[cfg(not(feature = "no_closure"))] diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 3028842cd..bc5743bd7 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -2516,7 +2516,7 @@ impl<'a> Iterator for TokenIterator<'a> { Some((Token::Reserved(s), pos)) => (match (s.as_str(), #[cfg(not(feature = "no_custom_syntax"))] - self.engine.is_custom_keyword(&s), + self.engine.custom_keywords.contains_key(&*s), #[cfg(feature = "no_custom_syntax")] false ) @@ -2562,12 +2562,12 @@ impl<'a> Iterator for TokenIterator<'a> { }, pos), // Custom keyword #[cfg(not(feature = "no_custom_syntax"))] - Some((Token::Identifier(s), pos)) if self.engine.is_custom_keyword(&s) => { + Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.contains_key(&*s) => { (Token::Custom(s), pos) } // Custom keyword/symbol - must be disabled #[cfg(not(feature = "no_custom_syntax"))] - Some((token, pos)) if token.is_literal() && self.engine.is_custom_keyword(token.literal_syntax()) => { + Some((token, pos)) if token.is_literal() && self.engine.custom_keywords.contains_key(token.literal_syntax()) => { if self.engine.is_symbol_disabled(token.literal_syntax()) { // Disabled standard keyword/symbol (Token::Custom(Box::new(token.literal_syntax().into())), pos) diff --git a/src/types/fn_ptr.rs b/src/types/fn_ptr.rs index 0c7967ad6..2e2f2cc12 100644 --- a/src/types/fn_ptr.rs +++ b/src/types/fn_ptr.rs @@ -23,11 +23,11 @@ use std::{ /// to be passed onto a function during a call. #[derive(Clone)] pub struct FnPtr { - name: ImmutableString, - curry: Vec, - environ: Option>, + pub(crate) name: ImmutableString, + pub(crate) curry: Vec, + pub(crate) environ: Option>, #[cfg(not(feature = "no_function"))] - fn_def: Option>, + pub(crate) fn_def: Option>, } impl Hash for FnPtr { @@ -71,19 +71,6 @@ impl FnPtr { pub fn new(name: impl Into) -> RhaiResultOf { name.into().try_into() } - /// Create a new function pointer without checking its parameters. - #[inline(always)] - #[must_use] - #[allow(dead_code)] - pub(crate) fn new_unchecked(name: impl Into, curry: Vec) -> Self { - Self { - name: name.into(), - curry, - environ: None, - #[cfg(not(feature = "no_function"))] - fn_def: None, - } - } /// Get the name of the function. #[inline(always)] #[must_use] @@ -96,33 +83,6 @@ impl FnPtr { pub(crate) const fn fn_name_raw(&self) -> &ImmutableString { &self.name } - /// Get the underlying data of the function pointer. - #[cfg(not(feature = "no_function"))] - #[inline(always)] - #[must_use] - pub(crate) fn take_data( - self, - ) -> ( - ImmutableString, - Vec, - Option>, - Option>, - ) { - (self.name, self.curry, self.environ, self.fn_def) - } - /// Get the underlying data of the function pointer. - #[cfg(feature = "no_function")] - #[inline(always)] - #[must_use] - pub(crate) fn take_data( - self, - ) -> ( - ImmutableString, - Vec, - Option>, - ) { - (self.name, self.curry, self.environ) - } /// Get the curried arguments. #[inline(always)] pub fn curry(&self) -> &[Dynamic] { @@ -306,7 +266,7 @@ impl FnPtr { caches, &mut crate::Scope::new(), this_ptr, - self.encapsulated_environ().map(AsRef::as_ref), + self.environ.as_deref(), fn_def, args, true, @@ -324,35 +284,6 @@ impl FnPtr { context.call_fn_raw(self.fn_name(), is_method, is_method, args) } - /// Get a reference to the [encapsulated environment][EncapsulatedEnviron]. - #[inline(always)] - #[must_use] - #[allow(dead_code)] - pub(crate) const fn encapsulated_environ(&self) -> Option<&Shared> { - self.environ.as_ref() - } - /// Set a reference to the [encapsulated environment][EncapsulatedEnviron]. - #[inline(always)] - #[allow(dead_code)] - pub(crate) fn set_encapsulated_environ( - &mut self, - value: Option>>, - ) { - self.environ = value.map(Into::into); - } - /// Get a reference to the linked [`ScriptFnDef`][crate::ast::ScriptFnDef]. - #[cfg(not(feature = "no_function"))] - #[inline(always)] - #[must_use] - pub(crate) const fn fn_def(&self) -> Option<&Shared> { - self.fn_def.as_ref() - } - /// Set a reference to the linked [`ScriptFnDef`][crate::ast::ScriptFnDef]. - #[cfg(not(feature = "no_function"))] - #[inline(always)] - pub(crate) fn set_fn_def(&mut self, value: Option>>) { - self.fn_def = value.map(Into::into); - } /// Make a call to a function pointer with either a specified number of arguments, or with extra /// arguments attached. @@ -389,7 +320,8 @@ impl FnPtr { } } /// _(internals)_ Make a call to a function pointer with either a specified number of arguments, - /// or with extra arguments attached. Exported under the `internals` feature only. + /// or with extra arguments attached. + /// Exported under the `internals` feature only. /// /// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`. /// @@ -433,7 +365,7 @@ impl FnPtr { move_this_ptr_to_args: usize, ) -> RhaiResult { #[cfg(not(feature = "no_function"))] - if let Some(arity) = self.fn_def().map(|f| f.params.len()) { + if let Some(arity) = self.fn_def.as_deref().map(|f| f.params.len()) { if arity == N + self.curry().len() { return self.call_raw(ctx, this_ptr, args); } diff --git a/src/types/immutable_string.rs b/src/types/immutable_string.rs index 41f04453c..e70d81091 100644 --- a/src/types/immutable_string.rs +++ b/src/types/immutable_string.rs @@ -731,7 +731,7 @@ impl ImmutableString { self.0.as_str() } /// Strong count of references to the underlying string. - pub(crate) fn strong_count(&self) -> usize { + pub fn strong_count(&self) -> usize { Shared::strong_count(&self.0) } /// Consume the [`ImmutableString`] and convert it into a [`String`]. @@ -749,12 +749,13 @@ impl ImmutableString { /// If there are other references to the same string, a cloned copy is used. #[inline(always)] #[must_use] - pub(crate) fn make_mut(&mut self) -> &mut SmartString { + pub fn make_mut(&mut self) -> &mut SmartString { shared_make_mut(&mut self.0) } - /// Return a mutable reference to the [`SmartString`] wrapped by the [`ImmutableString`]. + /// Return a mutable reference to the [`SmartString`] wrapped by the [`ImmutableString`] + /// if there are no other outstanding references to it. #[inline(always)] - pub(crate) fn get_mut(&mut self) -> Option<&mut SmartString> { + pub fn get_mut(&mut self) -> Option<&mut SmartString> { shared_get_mut(&mut self.0) } /// Returns `true` if the two [`ImmutableString`]'s point to the same allocation. diff --git a/src/types/position.rs b/src/types/position.rs index dde988dd0..f6ede26fd 100644 --- a/src/types/position.rs +++ b/src/types/position.rs @@ -130,14 +130,6 @@ impl Position { self } } - /// Print this [`Position`] for debug purposes. - #[inline] - pub(crate) fn debug_print(self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - if !self.is_none() { - write!(_f, " @ {self:?}")?; - } - Ok(()) - } } impl Default for Position { diff --git a/src/types/position_none.rs b/src/types/position_none.rs index e6a65d164..8958f0a66 100644 --- a/src/types/position_none.rs +++ b/src/types/position_none.rs @@ -74,11 +74,6 @@ impl Position { pub const fn or_else(self, pos: Self) -> Self { pos } - /// Print this [`Position`] for debug purposes. - #[inline(always)] - pub(crate) fn debug_print(self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - Ok(()) - } } impl fmt::Display for Position {