Change function call name into ImmutableString from Cow<str>.
This commit is contained in:
parent
7bdc2e3d20
commit
241f5abe10
@ -4,7 +4,6 @@ use crate::dynamic::{AccessMode, Union};
|
|||||||
use crate::fn_native::shared_make_mut;
|
use crate::fn_native::shared_make_mut;
|
||||||
use crate::module::NamespaceRef;
|
use crate::module::NamespaceRef;
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
borrow::Cow,
|
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
collections::{BTreeMap, BTreeSet},
|
collections::{BTreeMap, BTreeSet},
|
||||||
fmt,
|
fmt,
|
||||||
@ -1398,16 +1397,14 @@ pub struct FnCallExpr {
|
|||||||
pub hash: FnCallHash,
|
pub hash: FnCallHash,
|
||||||
/// Does this function call capture the parent scope?
|
/// Does this function call capture the parent scope?
|
||||||
pub capture: bool,
|
pub capture: bool,
|
||||||
/// List of function call arguments.
|
/// List of function call argument expressions.
|
||||||
pub args: StaticVec<Expr>,
|
pub args: StaticVec<Expr>,
|
||||||
/// List of function call arguments that are constants.
|
/// List of function call arguments that are constants.
|
||||||
pub constant_args: StaticVec<(Dynamic, Position)>,
|
pub constant_args: StaticVec<(Dynamic, Position)>,
|
||||||
/// Namespace of the function, if any. Boxed because it occurs rarely.
|
/// Namespace of the function, if any. Boxed because it occurs rarely.
|
||||||
pub namespace: Option<NamespaceRef>,
|
pub namespace: Option<NamespaceRef>,
|
||||||
/// Function name.
|
/// Function name.
|
||||||
/// Use [`Cow<'static, str>`][Cow] because a lot of operators (e.g. `==`, `>=`) are implemented as
|
pub name: ImmutableString,
|
||||||
/// function calls and the function names are predictable, so no need to allocate a new [`String`].
|
|
||||||
pub name: Cow<'static, str>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FnCallExpr {
|
impl FnCallExpr {
|
||||||
|
@ -346,16 +346,18 @@ fn parse_fn_call(
|
|||||||
calc_fn_hash(empty(), &id, 0)
|
calc_fn_hash(empty(), &id, 0)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let hash = if is_valid_identifier(id.chars()) {
|
||||||
|
FnCallHash::from_script(hash)
|
||||||
|
} else {
|
||||||
|
FnCallHash::from_native(hash)
|
||||||
|
};
|
||||||
|
|
||||||
return Ok(Expr::FnCall(
|
return Ok(Expr::FnCall(
|
||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
name: id.to_string().into(),
|
name: state.get_interned_string(id),
|
||||||
capture,
|
capture,
|
||||||
namespace,
|
namespace,
|
||||||
hash: if is_valid_identifier(id.chars()) {
|
hash,
|
||||||
FnCallHash::from_script(hash)
|
|
||||||
} else {
|
|
||||||
FnCallHash::from_native(hash)
|
|
||||||
},
|
|
||||||
args,
|
args,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
@ -389,16 +391,18 @@ fn parse_fn_call(
|
|||||||
calc_fn_hash(empty(), &id, args.len())
|
calc_fn_hash(empty(), &id, args.len())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let hash = if is_valid_identifier(id.chars()) {
|
||||||
|
FnCallHash::from_script(hash)
|
||||||
|
} else {
|
||||||
|
FnCallHash::from_native(hash)
|
||||||
|
};
|
||||||
|
|
||||||
return Ok(Expr::FnCall(
|
return Ok(Expr::FnCall(
|
||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
name: id.to_string().into(),
|
name: state.get_interned_string(id),
|
||||||
capture,
|
capture,
|
||||||
namespace,
|
namespace,
|
||||||
hash: if is_valid_identifier(id.chars()) {
|
hash,
|
||||||
FnCallHash::from_script(hash)
|
|
||||||
} else {
|
|
||||||
FnCallHash::from_native(hash)
|
|
||||||
},
|
|
||||||
args,
|
args,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
@ -1276,14 +1280,13 @@ fn parse_unary(
|
|||||||
|
|
||||||
// Call negative function
|
// Call negative function
|
||||||
expr => {
|
expr => {
|
||||||
let op = "-";
|
|
||||||
let mut args = StaticVec::new();
|
let mut args = StaticVec::new();
|
||||||
args.push(expr);
|
args.push(expr);
|
||||||
|
|
||||||
Ok(Expr::FnCall(
|
Ok(Expr::FnCall(
|
||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
name: op.into(),
|
name: state.get_interned_string("-"),
|
||||||
hash: FnCallHash::from_native(calc_fn_hash(empty(), op, 1)),
|
hash: FnCallHash::from_native(calc_fn_hash(empty(), "-", 1)),
|
||||||
args,
|
args,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
@ -1303,14 +1306,13 @@ fn parse_unary(
|
|||||||
|
|
||||||
// Call plus function
|
// Call plus function
|
||||||
expr => {
|
expr => {
|
||||||
let op = "+";
|
|
||||||
let mut args = StaticVec::new();
|
let mut args = StaticVec::new();
|
||||||
args.push(expr);
|
args.push(expr);
|
||||||
|
|
||||||
Ok(Expr::FnCall(
|
Ok(Expr::FnCall(
|
||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
name: op.into(),
|
name: state.get_interned_string("+"),
|
||||||
hash: FnCallHash::from_native(calc_fn_hash(empty(), op, 1)),
|
hash: FnCallHash::from_native(calc_fn_hash(empty(), "+", 1)),
|
||||||
args,
|
args,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
@ -1326,12 +1328,10 @@ fn parse_unary(
|
|||||||
let expr = parse_unary(input, state, lib, settings.level_up())?;
|
let expr = parse_unary(input, state, lib, settings.level_up())?;
|
||||||
args.push(expr);
|
args.push(expr);
|
||||||
|
|
||||||
let op = "!";
|
|
||||||
|
|
||||||
Ok(Expr::FnCall(
|
Ok(Expr::FnCall(
|
||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
name: op.into(),
|
name: state.get_interned_string("!"),
|
||||||
hash: FnCallHash::from_native(calc_fn_hash(empty(), op, 1)),
|
hash: FnCallHash::from_native(calc_fn_hash(empty(), "!", 1)),
|
||||||
args,
|
args,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
@ -1673,7 +1673,7 @@ fn parse_binary_op(
|
|||||||
let hash = calc_fn_hash(empty(), &op, 2);
|
let hash = calc_fn_hash(empty(), &op, 2);
|
||||||
|
|
||||||
let op_base = FnCallExpr {
|
let op_base = FnCallExpr {
|
||||||
name: op,
|
name: state.get_interned_string(op.as_ref()),
|
||||||
hash: FnCallHash::from_native(hash),
|
hash: FnCallHash::from_native(hash),
|
||||||
capture: false,
|
capture: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -1741,7 +1741,7 @@ fn parse_binary_op(
|
|||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
hash: FnCallHash::from_script(hash),
|
hash: FnCallHash::from_script(hash),
|
||||||
args,
|
args,
|
||||||
name: OP_CONTAINS.into(),
|
name: state.get_interned_string(OP_CONTAINS),
|
||||||
..op_base
|
..op_base
|
||||||
}),
|
}),
|
||||||
pos,
|
pos,
|
||||||
@ -2739,7 +2739,7 @@ fn parse_fn(
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(ScriptFnDef {
|
Ok(ScriptFnDef {
|
||||||
name: name.into(),
|
name: state.get_interned_string(&name),
|
||||||
access,
|
access,
|
||||||
params,
|
params,
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
@ -2755,7 +2755,12 @@ fn parse_fn(
|
|||||||
/// Creates a curried expression from a list of external variables
|
/// Creates a curried expression from a list of external variables
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Position) -> Expr {
|
fn make_curry_from_externals(
|
||||||
|
state: &mut ParseState,
|
||||||
|
fn_expr: Expr,
|
||||||
|
externals: StaticVec<Ident>,
|
||||||
|
pos: Position,
|
||||||
|
) -> Expr {
|
||||||
// If there are no captured variables, no need to curry
|
// If there are no captured variables, no need to curry
|
||||||
if externals.is_empty() {
|
if externals.is_empty() {
|
||||||
return fn_expr;
|
return fn_expr;
|
||||||
@ -2770,12 +2775,14 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Po
|
|||||||
args.push(Expr::Variable(Box::new((None, None, x.clone()))));
|
args.push(Expr::Variable(Box::new((None, None, x.clone()))));
|
||||||
});
|
});
|
||||||
|
|
||||||
let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY;
|
|
||||||
|
|
||||||
let expr = Expr::FnCall(
|
let expr = Expr::FnCall(
|
||||||
Box::new(FnCallExpr {
|
Box::new(FnCallExpr {
|
||||||
name: curry_func.into(),
|
name: state.get_interned_string(crate::engine::KEYWORD_FN_PTR_CURRY),
|
||||||
hash: FnCallHash::from_native(calc_fn_hash(empty(), curry_func, num_externals + 1)),
|
hash: FnCallHash::from_native(calc_fn_hash(
|
||||||
|
empty(),
|
||||||
|
crate::engine::KEYWORD_FN_PTR_CURRY,
|
||||||
|
num_externals + 1,
|
||||||
|
)),
|
||||||
args,
|
args,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
@ -2880,7 +2887,8 @@ fn parse_anon_fn(
|
|||||||
body.hash(hasher);
|
body.hash(hasher);
|
||||||
let hash = hasher.finish();
|
let hash = hasher.finish();
|
||||||
|
|
||||||
let fn_name: ImmutableString = format!("{}{:016x}", crate::engine::FN_ANONYMOUS, hash).into();
|
let fn_name =
|
||||||
|
state.get_interned_string(&(format!("{}{:016x}", crate::engine::FN_ANONYMOUS, hash)));
|
||||||
|
|
||||||
// Define the function
|
// Define the function
|
||||||
let script = ScriptFnDef {
|
let script = ScriptFnDef {
|
||||||
@ -2899,7 +2907,7 @@ fn parse_anon_fn(
|
|||||||
let expr = Expr::FnPointer(fn_name, settings.pos);
|
let expr = Expr::FnPointer(fn_name, settings.pos);
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
let expr = make_curry_from_externals(expr, externals, settings.pos);
|
let expr = make_curry_from_externals(state, expr, externals, settings.pos);
|
||||||
|
|
||||||
Ok((expr, script))
|
Ok((expr, script))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user