Include hashes in JSON output.

This commit is contained in:
Stephen Chung 2021-12-06 11:12:54 +08:00
parent 206b5051eb
commit cc98e82ea1
3 changed files with 41 additions and 60 deletions

View File

@ -31,6 +31,7 @@ Enhancements
* Added `into_array` and `into_typed_array` for `Dynamic`. * Added `into_array` and `into_typed_array` for `Dynamic`.
* Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer. * Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer.
* BLob's can now be deserialized (using `from_dynamic`) into `Vec<u8>` via [`serde_bytes`](https://crates.io/crates/serde_bytes). * BLob's can now be deserialized (using `from_dynamic`) into `Vec<u8>` via [`serde_bytes`](https://crates.io/crates/serde_bytes).
* A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call.
Deprecated and Gated API's Deprecated and Gated API's
-------------------------- --------------------------

View File

@ -106,7 +106,7 @@ impl FuncInfo {
/// ///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain. /// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline] #[inline]
fn calc_native_fn_hash( pub fn calc_native_fn_hash(
modules: impl Iterator<Item = impl AsRef<str>>, modules: impl Iterator<Item = impl AsRef<str>>,
fn_name: impl AsRef<str>, fn_name: impl AsRef<str>,
params: &[TypeId], params: &[TypeId],
@ -179,8 +179,7 @@ impl fmt::Debug for Module {
d.field( d.field(
"functions", "functions",
&self &self
.functions .iter_fn()
.values()
.map(|f| f.func.to_string()) .map(|f| f.func.to_string())
.collect::<BTreeSet<_>>(), .collect::<BTreeSet<_>>(),
); );
@ -358,9 +357,8 @@ impl Module {
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
#[inline] #[inline]
pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ { pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
self.functions self.iter_fn()
.values() .filter(|&f| match f.access {
.filter(|f| match f.access {
FnAccess::Public => true, FnAccess::Public => true,
FnAccess::Private => false, FnAccess::Private => false,
}) })
@ -501,8 +499,7 @@ impl Module {
} else { } else {
let name = name.as_ref(); let name = name.as_ref();
self.functions self.iter_fn()
.values()
.find(|f| f.params == num_params && f.name == name) .find(|f| f.params == num_params && f.name == name)
.and_then(|f| f.func.get_script_fn_def()) .and_then(|f| f.func.get_script_fn_def())
} }
@ -1232,7 +1229,7 @@ impl Module {
other other
.functions .functions
.iter() .iter()
.filter(|(_, f)| { .filter(|&(_, f)| {
_filter( _filter(
f.namespace, f.namespace,
f.access, f.access,
@ -1331,18 +1328,15 @@ impl Module {
&Shared<crate::ast::ScriptFnDef>, &Shared<crate::ast::ScriptFnDef>,
), ),
> + '_ { > + '_ {
self.functions self.iter_fn().filter(|&f| f.func.is_script()).map(|f| {
.values() (
.filter(|f| f.func.is_script()) f.namespace,
.map(|f| { f.access,
( f.name.as_str(),
f.namespace, f.params,
f.access, f.func.get_script_fn_def().expect("scripted function"),
f.name.as_str(), )
f.params, })
f.func.get_script_fn_def().expect("scripted function"),
)
})
} }
/// Get an iterator over all script-defined functions in the [`Module`]. /// Get an iterator over all script-defined functions in the [`Module`].
@ -1358,9 +1352,8 @@ impl Module {
pub fn iter_script_fn_info( pub fn iter_script_fn_info(
&self, &self,
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize)> { ) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize)> {
self.functions self.iter_fn()
.values() .filter(|&f| f.func.is_script())
.filter(|f| f.func.is_script())
.map(|f| (f.namespace, f.access, f.name.as_str(), f.params)) .map(|f| (f.namespace, f.access, f.name.as_str(), f.params))
} }
@ -1461,13 +1454,12 @@ impl Module {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
if ast.has_functions() { if ast.has_functions() {
ast.shared_lib() ast.shared_lib()
.functions .iter_fn()
.values() .filter(|&f| match f.access {
.filter(|f| match f.access {
FnAccess::Public => true, FnAccess::Public => true,
FnAccess::Private => false, FnAccess::Private => false,
}) })
.filter(|f| f.func.is_script()) .filter(|&f| f.func.is_script())
.for_each(|f| { .for_each(|f| {
// Encapsulate AST environment // Encapsulate AST environment
let mut func = f let mut func = f

View File

@ -1,8 +1,9 @@
use crate::{Engine, AST}; use crate::module::calc_native_fn_hash;
use crate::{calc_fn_hash, Engine, AST};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{cmp::Ordering, collections::BTreeMap}; use std::{cmp::Ordering, collections::BTreeMap, iter::empty};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -83,6 +84,8 @@ impl Ord for FnParam {
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct FnMetadata { struct FnMetadata {
pub base_hash: u64,
pub full_hash: u64,
pub namespace: FnNamespace, pub namespace: FnNamespace,
pub access: FnAccess, pub access: FnAccess,
pub name: String, pub name: String,
@ -118,15 +121,23 @@ impl Ord for FnMetadata {
impl From<&crate::module::FuncInfo> for FnMetadata { impl From<&crate::module::FuncInfo> for FnMetadata {
fn from(info: &crate::module::FuncInfo) -> Self { fn from(info: &crate::module::FuncInfo) -> Self {
let base_hash = calc_fn_hash(&info.name, info.params);
let (typ, full_hash) = if info.func.is_script() {
(FnType::Script, base_hash)
} else {
(
FnType::Native,
calc_native_fn_hash(empty::<&str>(), &info.name, &info.param_types),
)
};
Self { Self {
base_hash,
full_hash,
namespace: info.namespace.into(), namespace: info.namespace.into(),
access: info.access.into(), access: info.access.into(),
name: info.name.to_string(), name: info.name.to_string(),
typ: if info.func.is_script() { typ,
FnType::Script
} else {
FnType::Native
},
num_params: info.params, num_params: info.params,
params: info params: info
.param_names .param_names
@ -169,30 +180,6 @@ impl From<&crate::module::FuncInfo> for FnMetadata {
} }
} }
#[cfg(not(feature = "no_function"))]
impl From<crate::ast::ScriptFnMetadata<'_>> for FnMetadata {
fn from(info: crate::ast::ScriptFnMetadata) -> Self {
Self {
namespace: FnNamespace::Global,
access: info.access.into(),
name: info.name.to_string(),
typ: FnType::Script,
num_params: info.params.len(),
params: info
.params
.iter()
.map(|&s| FnParam {
name: s.into(),
typ: Some("Dynamic".into()),
})
.collect(),
return_type: Some("Dynamic".into()),
signature: info.to_string(),
doc_comments: info.comments.iter().map(|&s| s.into()).collect(),
}
}
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ModuleMetadata { struct ModuleMetadata {
@ -263,7 +250,8 @@ impl Engine {
.for_each(|f| global.functions.push(f.into())); .for_each(|f| global.functions.push(f.into()));
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
_ast.iter_functions() _ast.shared_lib()
.iter_fn()
.for_each(|f| global.functions.push(f.into())); .for_each(|f| global.functions.push(f.into()));
global.functions.sort(); global.functions.sort();