From 232ba2754893c8a058007ed34784470710dd47f3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 8 Nov 2020 18:15:23 +0800 Subject: [PATCH] Add NativeCallContext::new. --- doc/src/language/fn-ptr.md | 14 ++++++-------- src/fn_native.rs | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/doc/src/language/fn-ptr.md b/doc/src/language/fn-ptr.md index 40fd8bcd..11ca1f4d 100644 --- a/doc/src/language/fn-ptr.md +++ b/doc/src/language/fn-ptr.md @@ -236,7 +236,7 @@ This type is normally provided by the [`Engine`] (e.g. when using [`Engine::regi However, it may also be manually constructed from a tuple: ```rust -use rhai::{Engine, FnPtr}; +use rhai::{Engine, FnPtr, NativeCallContext}; let engine = Engine::new(); @@ -254,13 +254,11 @@ let fn_ptr = engine.eval_ast::(&ast)?; // Get rid of the script, retaining only functions ast.retain_functions(|_, _, _| true); -// Create native call context via a tuple -let context = - ( - &engine, // the 'Engine' - &[ast.as_ref()] // function namespace from the 'AST' - // as a one-element slice - ).into(); +// Create native call context +let context = NativeCallContext::new( + &engine, // the 'Engine' + &[ast.as_ref()] // function namespace from the 'AST' +); // 'f' captures: the engine, the AST, and the closure let f = move |x: i64| fn_ptr.call_dynamic(context, None, [x.into()]); diff --git a/src/fn_native.rs b/src/fn_native.rs index ef66eff3..d6805814 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -81,6 +81,31 @@ impl<'e, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'m M) } impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> { + /// Create a new `NativeCallContext`. + #[inline(always)] + pub fn new(engine: &'e Engine, lib: &'m impl AsRef<[&'pm Module]>) -> Self { + Self { + engine, + mods: None, + lib: lib.as_ref(), + } + } + /// _[INTERNALS]_ Create a new `NativeCallContext`. + /// Available under the `internals` feature only. + #[cfg(feature = "internals")] + #[cfg(not(feature = "no_module"))] + #[inline(always)] + pub fn new_with_imports( + engine: &'e Engine, + mods: &'a mut Imports, + lib: &'m impl AsRef<[&'pm Module]>, + ) -> Self { + Self { + engine, + mods: Some(mods), + lib: lib.as_ref(), + } + } /// The current `Engine`. #[inline(always)] pub fn engine(&self) -> &'e Engine {