avoid allocating unnecessary errors for getting functions

This commit is contained in:
Connor Skees 2020-07-08 20:35:15 -04:00
parent 05b6b539ad
commit ca370eb9b0
3 changed files with 9 additions and 12 deletions

View File

@ -188,8 +188,8 @@ fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
}, },
parser.global_scope, parser.global_scope,
) { ) {
Ok(f) => SassFunction::UserDefined(Box::new(f), name), Some(f) => SassFunction::UserDefined(Box::new(f), name),
Err(..) => match GLOBAL_FUNCTIONS.get(name.as_str()) { None => match GLOBAL_FUNCTIONS.get(name.as_str()) {
Some(f) => SassFunction::Builtin(f.clone(), name), Some(f) => SassFunction::Builtin(f.clone(), name),
None => return Err((format!("Function not found: {}", name), args.span()).into()), None => return Err((format!("Function not found: {}", name), args.span()).into()),
}, },

View File

@ -263,8 +263,8 @@ impl<'a> Parser<'a> {
}, },
self.global_scope, self.global_scope,
) { ) {
Ok(f) => f, Some(f) => f,
Err(_) => { None => {
if let Some(f) = GLOBAL_FUNCTIONS.get(as_ident.as_str()) { if let Some(f) = GLOBAL_FUNCTIONS.get(as_ident.as_str()) {
return Ok(IntermediateValue::Value(HigherIntermediateValue::Function( return Ok(IntermediateValue::Value(HigherIntermediateValue::Function(
SassFunction::Builtin(f.clone(), as_ident), SassFunction::Builtin(f.clone(), as_ident),

View File

@ -57,11 +57,8 @@ impl Scope {
self.mixins.contains_key(name) self.mixins.contains_key(name)
} }
fn get_fn(&self, name: Spanned<&Identifier>) -> SassResult<Function> { fn get_fn(&self, name: &Identifier) -> Option<Function> {
match self.functions.get(name.node) { self.functions.get(name).cloned()
Some(v) => Ok(v.clone()),
None => Err(("Undefined function.", name.span).into()),
}
} }
pub fn insert_fn<T: Into<Identifier>>(&mut self, s: T, v: Function) -> Option<Function> { pub fn insert_fn<T: Into<Identifier>>(&mut self, s: T, v: Function) -> Option<Function> {
@ -218,13 +215,13 @@ impl Scopes {
&'a self, &'a self,
name: Spanned<&Identifier>, name: Spanned<&Identifier>,
global_scope: &'a Scope, global_scope: &'a Scope,
) -> SassResult<Function> { ) -> Option<Function> {
for scope in self.0.iter().rev() { for scope in self.0.iter().rev() {
if scope.fn_exists(&name.node) { if scope.fn_exists(&name.node) {
return scope.get_fn(name); return scope.get_fn(name.node);
} }
} }
global_scope.get_fn(name) global_scope.get_fn(name.node)
} }
pub fn fn_exists(&self, name: &Identifier, global_scope: &Scope) -> bool { pub fn fn_exists(&self, name: &Identifier, global_scope: &Scope) -> bool {