Add source to contexts.
This commit is contained in:
@@ -560,6 +560,11 @@ impl<'e, 'x, 'px, 'a, 's, 'm, 'pm, 't, 'pt> EvalContext<'e, 'x, 'px, 'a, 's, 'm,
|
||||
pub fn engine(&self) -> &'e Engine {
|
||||
self.engine
|
||||
}
|
||||
/// The current source.
|
||||
#[inline(always)]
|
||||
pub fn source<'z: 's>(&'z self) -> Option<&'s str> {
|
||||
self.state.source.as_ref().map(|s| s.as_str())
|
||||
}
|
||||
/// The current [`Scope`].
|
||||
#[inline(always)]
|
||||
pub fn scope(&self) -> &Scope<'px> {
|
||||
@@ -2002,9 +2007,12 @@ impl Engine {
|
||||
// Overriding exact implementation
|
||||
if func.is_plugin_fn() {
|
||||
func.get_plugin_fn()
|
||||
.call((self, &*mods, lib).into(), args)?;
|
||||
.call((self, &state.source, &*mods, lib).into(), args)?;
|
||||
} else {
|
||||
func.get_native_fn()((self, &*mods, lib).into(), args)?;
|
||||
func.get_native_fn()(
|
||||
(self, &state.source, &*mods, lib).into(),
|
||||
args,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
// Built-in op-assignment function
|
||||
|
@@ -198,9 +198,10 @@ impl Engine {
|
||||
|
||||
// Run external function
|
||||
let result = if func.is_plugin_fn() {
|
||||
func.get_plugin_fn().call((self, mods, lib).into(), args)
|
||||
func.get_plugin_fn()
|
||||
.call((self, &state.source, mods, lib).into(), args)
|
||||
} else {
|
||||
func.get_native_fn()((self, mods, lib).into(), args)
|
||||
func.get_native_fn()((self, &state.source, mods, lib).into(), args)
|
||||
};
|
||||
|
||||
// Restore the original reference
|
||||
@@ -534,10 +535,13 @@ impl Engine {
|
||||
// Get function
|
||||
let (func, mut source) = lib
|
||||
.iter()
|
||||
.find_map(|&m| m.get_fn(hash_script, pub_only).map(|f| (f, m.clone_id())))
|
||||
.find_map(|&m| {
|
||||
m.get_fn(hash_script, pub_only)
|
||||
.map(|f| (f, m.id_raw().clone()))
|
||||
})
|
||||
//.or_else(|| self.global_namespace.get_fn(hash_script, pub_only))
|
||||
.or_else(|| self.packages.get_fn(hash_script).map(|f| (f, None)))
|
||||
//.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.clone_id()))))
|
||||
//.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.id_raw().clone()))))
|
||||
.unwrap();
|
||||
|
||||
if func.is_script() {
|
||||
@@ -1176,7 +1180,7 @@ impl Engine {
|
||||
let new_scope = &mut Default::default();
|
||||
let fn_def = f.get_fn_def().clone();
|
||||
|
||||
let mut source = module.clone_id();
|
||||
let mut source = module.id_raw().clone();
|
||||
mem::swap(&mut state.source, &mut source);
|
||||
|
||||
let result = self.call_script_fn(
|
||||
@@ -1190,7 +1194,7 @@ impl Engine {
|
||||
Some(f) if f.is_plugin_fn() => f
|
||||
.get_plugin_fn()
|
||||
.clone()
|
||||
.call((self, &*mods, lib).into(), args.as_mut()),
|
||||
.call((self, module.id_raw(), &*mods, lib).into(), args.as_mut()),
|
||||
Some(f) if f.is_native() => {
|
||||
if !f.is_method() {
|
||||
// Clone first argument
|
||||
@@ -1201,7 +1205,7 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
f.get_native_fn()((self, &*mods, lib).into(), args.as_mut())
|
||||
f.get_native_fn()((self, module.id_raw(), &*mods, lib).into(), args.as_mut())
|
||||
}
|
||||
Some(_) => unreachable!(),
|
||||
None if def_val.is_some() => Ok(def_val.unwrap().clone()),
|
||||
|
@@ -47,42 +47,47 @@ pub type Locked<T> = crate::stdlib::sync::RwLock<T>;
|
||||
|
||||
/// Context of a native Rust function call.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct NativeCallContext<'e, 'a, 'm, 'pm: 'm> {
|
||||
pub struct NativeCallContext<'e, 's, 'a, 'm, 'pm: 'm> {
|
||||
engine: &'e Engine,
|
||||
source: Option<&'s str>,
|
||||
pub(crate) mods: Option<&'a Imports>,
|
||||
pub(crate) lib: &'m [&'pm Module],
|
||||
}
|
||||
|
||||
impl<'e, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'a Imports, &'m M)>
|
||||
for NativeCallContext<'e, 'a, 'm, 'pm>
|
||||
impl<'e, 's, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized>
|
||||
From<(&'e Engine, &'s Option<ImmutableString>, &'a Imports, &'m M)>
|
||||
for NativeCallContext<'e, 's, 'a, 'm, 'pm>
|
||||
{
|
||||
fn from(value: (&'e Engine, &'a Imports, &'m M)) -> Self {
|
||||
fn from(value: (&'e Engine, &'s Option<ImmutableString>, &'a Imports, &'m M)) -> Self {
|
||||
Self {
|
||||
engine: value.0,
|
||||
mods: Some(value.1),
|
||||
lib: value.2.as_ref(),
|
||||
source: value.1.as_ref().map(|s| s.as_str()),
|
||||
mods: Some(value.2),
|
||||
lib: value.3.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'e, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'m M)>
|
||||
for NativeCallContext<'e, '_, 'm, 'pm>
|
||||
for NativeCallContext<'e, '_, '_, 'm, 'pm>
|
||||
{
|
||||
fn from(value: (&'e Engine, &'m M)) -> Self {
|
||||
Self {
|
||||
engine: value.0,
|
||||
source: None,
|
||||
mods: None,
|
||||
lib: value.1.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
|
||||
impl<'e, 's, 'a, 'm, 'pm> NativeCallContext<'e, 's, 'a, 'm, 'pm> {
|
||||
/// Create a new [`NativeCallContext`].
|
||||
#[inline(always)]
|
||||
pub fn new(engine: &'e Engine, lib: &'m impl AsRef<[&'pm Module]>) -> Self {
|
||||
Self {
|
||||
engine,
|
||||
source: None,
|
||||
mods: None,
|
||||
lib: lib.as_ref(),
|
||||
}
|
||||
@@ -92,13 +97,15 @@ impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
|
||||
#[cfg(feature = "internals")]
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline(always)]
|
||||
pub fn new_with_imports(
|
||||
pub fn new_with_all_fields(
|
||||
engine: &'e Engine,
|
||||
source: &'s Option<ImmutableString>,
|
||||
mods: &'a mut Imports,
|
||||
lib: &'m impl AsRef<[&'pm Module]>,
|
||||
) -> Self {
|
||||
Self {
|
||||
engine,
|
||||
source: source.as_ref().map(|s| s.as_str()),
|
||||
mods: Some(mods),
|
||||
lib: lib.as_ref(),
|
||||
}
|
||||
@@ -108,6 +115,11 @@ impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
|
||||
pub fn engine(&self) -> &'e Engine {
|
||||
self.engine
|
||||
}
|
||||
/// The current source.
|
||||
#[inline(always)]
|
||||
pub fn source<'z: 's>(&'z self) -> Option<&'s str> {
|
||||
self.source
|
||||
}
|
||||
/// _(INTERNALS)_ The current set of modules imported via `import` statements.
|
||||
/// Available under the `internals` feature only.
|
||||
#[cfg(feature = "internals")]
|
||||
|
@@ -244,8 +244,8 @@ impl Module {
|
||||
}
|
||||
|
||||
/// Get the ID of the module, if any.
|
||||
pub(crate) fn clone_id(&self) -> Option<ImmutableString> {
|
||||
self.id.clone()
|
||||
pub(crate) fn id_raw(&self) -> &Option<ImmutableString> {
|
||||
&self.id
|
||||
}
|
||||
|
||||
/// Set the ID of the module.
|
||||
|
Reference in New Issue
Block a user