Add source to contexts.
This commit is contained in:
parent
5ac83f0f46
commit
ff67efc6d5
@ -37,6 +37,8 @@ New features
|
|||||||
* `AST::iter_functions` now returns `ScriptFnMetadata` which includes, among others, _doc-comments_ for functions prefixed by `///` or `/**`.
|
* `AST::iter_functions` now returns `ScriptFnMetadata` which includes, among others, _doc-comments_ for functions prefixed by `///` or `/**`.
|
||||||
* _Doc-comments_ can be enabled/disabled with the new `Engine::set_doc_comments` method.
|
* _Doc-comments_ can be enabled/disabled with the new `Engine::set_doc_comments` method.
|
||||||
* A new feature `metadata` is added that pulls in `serde_json` and enables `Engine::gen_fn_metadata_to_json` which exports the full list of functions metadata (including those inside an `AST`) in JSON format.
|
* A new feature `metadata` is added that pulls in `serde_json` and enables `Engine::gen_fn_metadata_to_json` which exports the full list of functions metadata (including those inside an `AST`) in JSON format.
|
||||||
|
* `Engine::on_debug` provides two additional parameters: `source: Option<&str>` and `pos: Position`.
|
||||||
|
* `NativeCallContext` and `EvalContext` both expose `source()` which returns the current source, if any.
|
||||||
|
|
||||||
Enhancements
|
Enhancements
|
||||||
------------
|
------------
|
||||||
|
@ -122,6 +122,7 @@ where:
|
|||||||
| • `scope()` | `&Scope` | reference to the current [`Scope`] |
|
| • `scope()` | `&Scope` | reference to the current [`Scope`] |
|
||||||
| • `scope_mut()` | `&mut Scope` | mutable reference to the current [`Scope`]; variables can be added to/removed from it |
|
| • `scope_mut()` | `&mut Scope` | mutable reference to the current [`Scope`]; variables can be added to/removed from it |
|
||||||
| • `engine()` | `&Engine` | reference to the current [`Engine`] |
|
| • `engine()` | `&Engine` | reference to the current [`Engine`] |
|
||||||
|
| • `source()` | `Option<&str>` | reference to the current source, if any |
|
||||||
| • `imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
|
| • `imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
|
||||||
| • `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
| • `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||||
| • `this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
|
| • `this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
|
||||||
|
@ -74,6 +74,7 @@ where:
|
|||||||
| `context` | `&EvalContext` | reference to the current evaluation _context_ |
|
| `context` | `&EvalContext` | reference to the current evaluation _context_ |
|
||||||
| • `scope()` | `&Scope` | reference to the current [`Scope`] |
|
| • `scope()` | `&Scope` | reference to the current [`Scope`] |
|
||||||
| • `engine()` | `&Engine` | reference to the current [`Engine`] |
|
| • `engine()` | `&Engine` | reference to the current [`Engine`] |
|
||||||
|
| • `source()` | `Option<&str>` | reference to the current source, if any |
|
||||||
| • `imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
|
| • `imports()` | `&Imports` | reference to the current stack of [modules] imported via `import` statements |
|
||||||
| • `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
| • `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||||
| • `this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
|
| • `this_ptr()` | `Option<&Dynamic>` | reference to the current bound [`this`] pointer, if any |
|
||||||
|
@ -228,6 +228,7 @@ of the particular call to a registered Rust function. It is a type that exposes
|
|||||||
| Field | Type | Description |
|
| Field | Type | Description |
|
||||||
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||||
|
| `source()` | `Option<&str>` | reference to the current source, if any |
|
||||||
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||||
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||||
|
|
||||||
@ -254,11 +255,11 @@ let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
|
|||||||
// Get rid of the script, retaining only functions
|
// Get rid of the script, retaining only functions
|
||||||
ast.retain_functions(|_, _, _| true);
|
ast.retain_functions(|_, _, _| true);
|
||||||
|
|
||||||
|
// Create function namespace from the 'AST'
|
||||||
|
let lib = [ast.as_ref()];
|
||||||
|
|
||||||
// Create native call context
|
// Create native call context
|
||||||
let context = NativeCallContext::new(
|
let context = NativeCallContext::new(&engine, &lib);
|
||||||
&engine, // the 'Engine'
|
|
||||||
&[ast.as_ref()] // function namespace from the 'AST'
|
|
||||||
);
|
|
||||||
|
|
||||||
// 'f' captures: the engine, the AST, and the closure
|
// 'f' captures: the engine, the AST, and the closure
|
||||||
let f = move |x: i64| fn_ptr.call_dynamic(context, None, [x.into()]);
|
let f = move |x: i64| fn_ptr.call_dynamic(context, None, [x.into()]);
|
||||||
|
@ -89,6 +89,7 @@ specially by the plugins system.
|
|||||||
| Field | Type | Description |
|
| Field | Type | Description |
|
||||||
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||||
|
| `source()` | `Option<&str>` | reference to the current source, if any |
|
||||||
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||||
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||||
|
|
||||||
|
@ -400,6 +400,7 @@ specially by the plugins system.
|
|||||||
| Field | Type | Description |
|
| Field | Type | Description |
|
||||||
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| ------------------- | :-----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
| `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||||
|
| `source()` | `Option<&str>` | reference to the current source, if any |
|
||||||
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
| `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||||
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
| `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||||
|
|
||||||
|
@ -70,6 +70,7 @@ where:
|
|||||||
| `T` | `impl Clone` | return type of the function |
|
| `T` | `impl Clone` | return type of the function |
|
||||||
| `context` | `NativeCallContext` | the current _native call context_ |
|
| `context` | `NativeCallContext` | the current _native call context_ |
|
||||||
| • `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
| • `engine()` | `&Engine` | the current [`Engine`], with all configurations and settings.<br/>This is sometimes useful for calling a script-defined function within the same evaluation context using [`Engine::call_fn`][`call_fn`], or calling a [function pointer]. |
|
||||||
|
| • `source()` | `Option<&str>` | reference to the current source, if any |
|
||||||
| • `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
| • `imports()` | `Option<&Imports>` | reference to the current stack of [modules] imported via `import` statements (if any) |
|
||||||
| • `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
| • `iter_namespaces()` | `impl Iterator<Item = &Module>` | iterator of the namespaces (as [modules]) containing all script-defined functions |
|
||||||
| `args` | `&mut [&mut Dynamic]` | a slice containing `&mut` references to [`Dynamic`] values.<br/>The slice is guaranteed to contain enough arguments _of the correct types_. |
|
| `args` | `&mut [&mut Dynamic]` | a slice containing `&mut` references to [`Dynamic`] values.<br/>The slice is guaranteed to contain enough arguments _of the correct types_. |
|
||||||
|
@ -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 {
|
pub fn engine(&self) -> &'e Engine {
|
||||||
self.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`].
|
/// The current [`Scope`].
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn scope(&self) -> &Scope<'px> {
|
pub fn scope(&self) -> &Scope<'px> {
|
||||||
@ -2002,9 +2007,12 @@ impl Engine {
|
|||||||
// Overriding exact implementation
|
// Overriding exact implementation
|
||||||
if func.is_plugin_fn() {
|
if func.is_plugin_fn() {
|
||||||
func.get_plugin_fn()
|
func.get_plugin_fn()
|
||||||
.call((self, &*mods, lib).into(), args)?;
|
.call((self, &state.source, &*mods, lib).into(), args)?;
|
||||||
} else {
|
} 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
|
// Built-in op-assignment function
|
||||||
|
@ -198,9 +198,10 @@ impl Engine {
|
|||||||
|
|
||||||
// Run external function
|
// Run external function
|
||||||
let result = if func.is_plugin_fn() {
|
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 {
|
} 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
|
// Restore the original reference
|
||||||
@ -534,10 +535,13 @@ impl Engine {
|
|||||||
// Get function
|
// Get function
|
||||||
let (func, mut source) = lib
|
let (func, mut source) = lib
|
||||||
.iter()
|
.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.global_namespace.get_fn(hash_script, pub_only))
|
||||||
.or_else(|| self.packages.get_fn(hash_script).map(|f| (f, None)))
|
.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();
|
.unwrap();
|
||||||
|
|
||||||
if func.is_script() {
|
if func.is_script() {
|
||||||
@ -1176,7 +1180,7 @@ impl Engine {
|
|||||||
let new_scope = &mut Default::default();
|
let new_scope = &mut Default::default();
|
||||||
let fn_def = f.get_fn_def().clone();
|
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);
|
mem::swap(&mut state.source, &mut source);
|
||||||
|
|
||||||
let result = self.call_script_fn(
|
let result = self.call_script_fn(
|
||||||
@ -1190,7 +1194,7 @@ impl Engine {
|
|||||||
Some(f) if f.is_plugin_fn() => f
|
Some(f) if f.is_plugin_fn() => f
|
||||||
.get_plugin_fn()
|
.get_plugin_fn()
|
||||||
.clone()
|
.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() => {
|
Some(f) if f.is_native() => {
|
||||||
if !f.is_method() {
|
if !f.is_method() {
|
||||||
// Clone first argument
|
// 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!(),
|
Some(_) => unreachable!(),
|
||||||
None if def_val.is_some() => Ok(def_val.unwrap().clone()),
|
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.
|
/// Context of a native Rust function call.
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct NativeCallContext<'e, 'a, 'm, 'pm: 'm> {
|
pub struct NativeCallContext<'e, 's, 'a, 'm, 'pm: 'm> {
|
||||||
engine: &'e Engine,
|
engine: &'e Engine,
|
||||||
|
source: Option<&'s str>,
|
||||||
pub(crate) mods: Option<&'a Imports>,
|
pub(crate) mods: Option<&'a Imports>,
|
||||||
pub(crate) lib: &'m [&'pm Module],
|
pub(crate) lib: &'m [&'pm Module],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'e, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'a Imports, &'m M)>
|
impl<'e, 's, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized>
|
||||||
for NativeCallContext<'e, 'a, 'm, 'pm>
|
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 {
|
Self {
|
||||||
engine: value.0,
|
engine: value.0,
|
||||||
mods: Some(value.1),
|
source: value.1.as_ref().map(|s| s.as_str()),
|
||||||
lib: value.2.as_ref(),
|
mods: Some(value.2),
|
||||||
|
lib: value.3.as_ref(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'e, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'m M)>
|
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 {
|
fn from(value: (&'e Engine, &'m M)) -> Self {
|
||||||
Self {
|
Self {
|
||||||
engine: value.0,
|
engine: value.0,
|
||||||
|
source: None,
|
||||||
mods: None,
|
mods: None,
|
||||||
lib: value.1.as_ref(),
|
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`].
|
/// Create a new [`NativeCallContext`].
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn new(engine: &'e Engine, lib: &'m impl AsRef<[&'pm Module]>) -> Self {
|
pub fn new(engine: &'e Engine, lib: &'m impl AsRef<[&'pm Module]>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
engine,
|
engine,
|
||||||
|
source: None,
|
||||||
mods: None,
|
mods: None,
|
||||||
lib: lib.as_ref(),
|
lib: lib.as_ref(),
|
||||||
}
|
}
|
||||||
@ -92,13 +97,15 @@ impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
|
|||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn new_with_imports(
|
pub fn new_with_all_fields(
|
||||||
engine: &'e Engine,
|
engine: &'e Engine,
|
||||||
|
source: &'s Option<ImmutableString>,
|
||||||
mods: &'a mut Imports,
|
mods: &'a mut Imports,
|
||||||
lib: &'m impl AsRef<[&'pm Module]>,
|
lib: &'m impl AsRef<[&'pm Module]>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
engine,
|
engine,
|
||||||
|
source: source.as_ref().map(|s| s.as_str()),
|
||||||
mods: Some(mods),
|
mods: Some(mods),
|
||||||
lib: lib.as_ref(),
|
lib: lib.as_ref(),
|
||||||
}
|
}
|
||||||
@ -108,6 +115,11 @@ impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
|
|||||||
pub fn engine(&self) -> &'e Engine {
|
pub fn engine(&self) -> &'e Engine {
|
||||||
self.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.
|
/// _(INTERNALS)_ The current set of modules imported via `import` statements.
|
||||||
/// Available under the `internals` feature only.
|
/// Available under the `internals` feature only.
|
||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
|
@ -244,8 +244,8 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the ID of the module, if any.
|
/// Get the ID of the module, if any.
|
||||||
pub(crate) fn clone_id(&self) -> Option<ImmutableString> {
|
pub(crate) fn id_raw(&self) -> &Option<ImmutableString> {
|
||||||
self.id.clone()
|
&self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the ID of the module.
|
/// Set the ID of the module.
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#![cfg(not(feature = "no_function"))]
|
#![cfg(not(feature = "no_function"))]
|
||||||
use rhai::{Engine, EvalAltResult, FnPtr, ParseErrorType, RegisterFn, Scope, INT};
|
use rhai::{
|
||||||
|
Engine, EvalAltResult, FnPtr, NativeCallContext, ParseErrorType, RegisterFn, Scope, INT,
|
||||||
|
};
|
||||||
use std::any::TypeId;
|
use std::any::TypeId;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::mem::take;
|
use std::mem::take;
|
||||||
@ -269,8 +271,14 @@ fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
|
|||||||
// Get rid of the script, retaining only functions
|
// Get rid of the script, retaining only functions
|
||||||
ast.retain_functions(|_, _, _, _| true);
|
ast.retain_functions(|_, _, _, _| true);
|
||||||
|
|
||||||
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
// Create function namespace from the 'AST'
|
||||||
let f = move |x: INT| fn_ptr.call_dynamic((&engine, &[ast.as_ref()]).into(), None, [x.into()]);
|
let lib = [ast.as_ref()];
|
||||||
|
|
||||||
|
// Create native call context
|
||||||
|
let context = NativeCallContext::new(&engine, &lib);
|
||||||
|
|
||||||
|
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
||||||
|
let f = move |x: INT| fn_ptr.call_dynamic(context, None, [x.into()]);
|
||||||
|
|
||||||
assert_eq!(f(42)?.as_str(), Ok("hello42"));
|
assert_eq!(f(42)?.as_str(), Ok("hello42"));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user