Native only if function call is not a valid identifier.

This commit is contained in:
Stephen Chung 2020-12-18 23:03:56 +08:00
parent 5454ff0cd5
commit db7b9cb0f9
4 changed files with 32 additions and 14 deletions

View File

@ -125,16 +125,24 @@ impl Imports {
/// Does the specified function hash key exist in this stack of imported [modules][Module]? /// Does the specified function hash key exist in this stack of imported [modules][Module]?
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains_fn(&self, hash: u64) -> bool { pub fn contains_fn(&self, hash: u64) -> bool {
if hash == 0 {
false
} else {
self.0.as_ref().map_or(false, |x| { self.0.as_ref().map_or(false, |x| {
x.iter().any(|(_, m)| m.contains_qualified_fn(hash)) x.iter().any(|(_, m)| m.contains_qualified_fn(hash))
}) })
} }
}
/// Get specified function via its hash key. /// Get specified function via its hash key.
pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> { pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> {
if hash == 0 {
None
} else {
self.0 self.0
.as_ref() .as_ref()
.and_then(|x| x.iter().rev().find_map(|(_, m)| m.get_qualified_fn(hash))) .and_then(|x| x.iter().rev().find_map(|(_, m)| m.get_qualified_fn(hash)))
} }
}
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in this stack of imported [modules][Module]? /// Does the specified [`TypeId`][std::any::TypeId] iterator exist in this stack of imported [modules][Module]?
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains_iter(&self, id: TypeId) -> bool { pub fn contains_iter(&self, id: TypeId) -> bool {

View File

@ -443,8 +443,8 @@ impl Engine {
// NOTE: We skip script functions for global_namespace and packages, and native functions for lib // NOTE: We skip script functions for global_namespace and packages, and native functions for lib
// First check script-defined functions // First check script-defined functions
lib.iter().any(|&m| m.contains_fn(hash_script, pub_only)) (hash_script != 0 && lib.iter().any(|&m| m.contains_fn(hash_script, pub_only)))
//|| lib.iter().any(|&m| m.contains_fn(hash_fn, pub_only)) //|| (hash_fn != 0 && lib.iter().any(|&m| m.contains_fn(hash_fn, pub_only)))
// Then check registered functions // Then check registered functions
//|| self.global_namespace.contains_fn(hash_script, pub_only) //|| self.global_namespace.contains_fn(hash_script, pub_only)
|| self.global_namespace.contains_fn(hash_fn, false) || self.global_namespace.contains_fn(hash_fn, false)

View File

@ -64,16 +64,24 @@ impl PackagesCollection {
/// Does the specified function hash key exist in the [`PackagesCollection`]? /// Does the specified function hash key exist in the [`PackagesCollection`]?
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains_fn(&self, hash: u64) -> bool { pub fn contains_fn(&self, hash: u64) -> bool {
if hash == 0 {
false
} else {
self.0 self.0
.as_ref() .as_ref()
.map_or(false, |x| x.iter().any(|p| p.contains_fn(hash, false))) .map_or(false, |x| x.iter().any(|p| p.contains_fn(hash, false)))
} }
}
/// Get specified function via its hash key. /// Get specified function via its hash key.
pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> { pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> {
if hash == 0 {
None
} else {
self.0 self.0
.as_ref() .as_ref()
.and_then(|x| x.iter().find_map(|p| p.get_fn(hash, false))) .and_then(|x| x.iter().find_map(|p| p.get_fn(hash, false)))
} }
}
/// Does the specified [`TypeId`] iterator exist in the [`PackagesCollection`]? /// Does the specified [`TypeId`] iterator exist in the [`PackagesCollection`]?
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains_iter(&self, id: TypeId) -> bool { pub fn contains_iter(&self, id: TypeId) -> bool {

View File

@ -357,6 +357,7 @@ fn parse_fn_call(
return Ok(Expr::FnCall( return Ok(Expr::FnCall(
Box::new(FnCallExpr { Box::new(FnCallExpr {
name: id.to_string().into(), name: id.to_string().into(),
native_only: !is_valid_identifier(id.chars()), // script functions can only be valid identifiers
capture, capture,
namespace, namespace,
hash: hash_script, hash: hash_script,
@ -404,6 +405,7 @@ fn parse_fn_call(
return Ok(Expr::FnCall( return Ok(Expr::FnCall(
Box::new(FnCallExpr { Box::new(FnCallExpr {
name: id.to_string().into(), name: id.to_string().into(),
native_only: !is_valid_identifier(id.chars()), // script functions can only be valid identifiers
capture, capture,
namespace, namespace,
hash: hash_script, hash: hash_script,