Merge pull request #24 from jhwgh1968/plugins
Cleanup, breakage fixes, and CI fixes for codegen integration
This commit is contained in:
commit
1de13b7a03
1
.github/workflows/build.yml
vendored
1
.github/workflows/build.yml
vendored
@ -38,6 +38,7 @@ jobs:
|
|||||||
- {toolchain: stable, os: macos-latest, experimental: false, flags: ""}
|
- {toolchain: stable, os: macos-latest, experimental: false, flags: ""}
|
||||||
- {toolchain: beta, os: ubuntu-latest, experimental: false, flags: ""}
|
- {toolchain: beta, os: ubuntu-latest, experimental: false, flags: ""}
|
||||||
- {toolchain: nightly, os: ubuntu-latest, experimental: true, flags: ""}
|
- {toolchain: nightly, os: ubuntu-latest, experimental: true, flags: ""}
|
||||||
|
fail-fast: false
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
@ -220,7 +220,7 @@ impl ExportedFn {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let downcast_span = quote_spanned!(
|
let downcast_span = quote_spanned!(
|
||||||
arg_type.span()=> &mut *args[0usize].write_lock::<#arg_type>().unwrap());
|
arg_type.span()=> &mut args[0usize].write_lock::<#arg_type>().unwrap());
|
||||||
unpack_stmts.push(
|
unpack_stmts.push(
|
||||||
syn::parse2::<syn::Stmt>(quote! {
|
syn::parse2::<syn::Stmt>(quote! {
|
||||||
let #var: &mut _ = #downcast_span;
|
let #var: &mut _ = #downcast_span;
|
||||||
@ -784,7 +784,7 @@ mod generate_tests {
|
|||||||
args.len(), 2usize), Position::none())));
|
args.len(), 2usize), Position::none())));
|
||||||
}
|
}
|
||||||
let arg1 = args[1usize].downcast_clone::<usize>().unwrap();
|
let arg1 = args[1usize].downcast_clone::<usize>().unwrap();
|
||||||
let arg0: &mut _ = args[0usize].write_lock::<usize>().unwrap();
|
let arg0: &mut _ = &mut args[0usize].write_lock::<usize>().unwrap();
|
||||||
Ok(Dynamic::from(increment(arg0, arg1)))
|
Ok(Dynamic::from(increment(arg0, arg1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,7 +667,7 @@ mod generate_tests {
|
|||||||
format!("wrong arg count: {} != {}",
|
format!("wrong arg count: {} != {}",
|
||||||
args.len(), 1usize), Position::none())));
|
args.len(), 1usize), Position::none())));
|
||||||
}
|
}
|
||||||
let arg0: &mut _ = args[0usize].write_lock::<FLOAT>().unwrap();
|
let arg0: &mut _ = &mut args[0usize].write_lock::<FLOAT>().unwrap();
|
||||||
Ok(Dynamic::from(increment(arg0)))
|
Ok(Dynamic::from(increment(arg0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ pub(crate) fn generate_body(
|
|||||||
let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
|
let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
|
||||||
for function in fns {
|
for function in fns {
|
||||||
let fn_token_name = syn::Ident::new(
|
let fn_token_name = syn::Ident::new(
|
||||||
&format!("{}_Token", function.name().to_string()),
|
&format!("{}_token", function.name().to_string()),
|
||||||
function.name().span(),
|
function.name().span(),
|
||||||
);
|
);
|
||||||
let fn_literal =
|
let fn_literal =
|
||||||
|
@ -26,25 +26,36 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use rhai::{FLOAT, INT, Module, ModuleResolver, RegisterFn, RegisterPlugin};
|
/// # #[cfg(not(feature = "no_float"))]
|
||||||
|
/// use rhai::FLOAT as NUMBER;
|
||||||
|
/// # #[cfg(feature = "no_float")]
|
||||||
|
/// use rhai::INT as NUMBER;
|
||||||
|
/// # #[cfg(not(feature = "no_module"))]
|
||||||
|
/// use rhai::{Module, ModuleResolver, RegisterFn, RegisterPlugin};
|
||||||
|
/// # #[cfg(not(feature = "no_module"))]
|
||||||
/// use rhai::plugin::*;
|
/// use rhai::plugin::*;
|
||||||
|
/// # #[cfg(not(feature = "no_module"))]
|
||||||
/// use rhai::module_resolvers::*;
|
/// use rhai::module_resolvers::*;
|
||||||
///
|
///
|
||||||
/// // A function we want to expose to Rhai.
|
/// // A function we want to expose to Rhai.
|
||||||
/// #[derive(Copy, Clone)]
|
/// #[derive(Copy, Clone)]
|
||||||
/// struct DistanceFunction();
|
/// struct DistanceFunction();
|
||||||
///
|
///
|
||||||
|
/// # #[cfg(not(feature = "no_module"))]
|
||||||
/// impl PluginFunction for DistanceFunction {
|
/// impl PluginFunction for DistanceFunction {
|
||||||
/// fn is_method_call(&self) -> bool { false }
|
/// fn is_method_call(&self) -> bool { false }
|
||||||
/// fn is_varadic(&self) -> bool { false }
|
/// fn is_varadic(&self) -> bool { false }
|
||||||
///
|
///
|
||||||
/// fn call(&self, args: &mut[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>> {
|
/// fn call(&self, args: &mut[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
/// let x1: &FLOAT = args[0].downcast_ref::<FLOAT>().unwrap();
|
/// let x1: NUMBER = args[0].downcast_clone::<NUMBER>().unwrap();
|
||||||
/// let y1: &FLOAT = args[1].downcast_ref::<FLOAT>().unwrap();
|
/// let y1: NUMBER = args[1].downcast_clone::<NUMBER>().unwrap();
|
||||||
/// let x2: &FLOAT = args[2].downcast_ref::<FLOAT>().unwrap();
|
/// let x2: NUMBER = args[2].downcast_clone::<NUMBER>().unwrap();
|
||||||
/// let y2: &FLOAT = args[3].downcast_ref::<FLOAT>().unwrap();
|
/// let y2: NUMBER = args[3].downcast_clone::<NUMBER>().unwrap();
|
||||||
|
/// # #[cfg(not(feature = "no_float"))]
|
||||||
/// let square_sum = (y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0);
|
/// let square_sum = (y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0);
|
||||||
/// Ok(Dynamic::from(square_sum.sqrt()))
|
/// # #[cfg(feature = "no_float")]
|
||||||
|
/// let square_sum = (y2 - y1).abs().pow(2) + (x2 -x1).abs().pow(2);
|
||||||
|
/// Ok(Dynamic::from(square_sum))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn clone_boxed(&self) -> Box<dyn PluginFunction> {
|
/// fn clone_boxed(&self) -> Box<dyn PluginFunction> {
|
||||||
@ -52,10 +63,10 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn input_types(&self) -> Box<[std::any::TypeId]> {
|
/// fn input_types(&self) -> Box<[std::any::TypeId]> {
|
||||||
/// vec![std::any::TypeId::of::<FLOAT>(),
|
/// vec![std::any::TypeId::of::<NUMBER>(),
|
||||||
/// std::any::TypeId::of::<FLOAT>(),
|
/// std::any::TypeId::of::<NUMBER>(),
|
||||||
/// std::any::TypeId::of::<FLOAT>(),
|
/// std::any::TypeId::of::<NUMBER>(),
|
||||||
/// std::any::TypeId::of::<FLOAT>()].into_boxed_slice()
|
/// std::any::TypeId::of::<NUMBER>()].into_boxed_slice()
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
@ -63,10 +74,11 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
|
|||||||
/// #[derive(Copy, Clone)]
|
/// #[derive(Copy, Clone)]
|
||||||
/// pub struct AdvancedMathPlugin();
|
/// pub struct AdvancedMathPlugin();
|
||||||
///
|
///
|
||||||
|
/// # #[cfg(not(feature = "no_module"))]
|
||||||
/// impl Plugin for AdvancedMathPlugin {
|
/// impl Plugin for AdvancedMathPlugin {
|
||||||
/// fn register_contents(self, engine: &mut Engine) {
|
/// fn register_contents(self, engine: &mut Engine) {
|
||||||
/// // Plugins are allowed to have side-effects on the engine.
|
/// // Plugins are allowed to have side-effects on the engine.
|
||||||
/// engine.register_fn("get_mystic_number", || { 42 as FLOAT });
|
/// engine.register_fn("get_mystic_number", || { 42 as NUMBER });
|
||||||
///
|
///
|
||||||
/// // Main purpose: create a module to expose the functions to Rhai.
|
/// // Main purpose: create a module to expose the functions to Rhai.
|
||||||
/// //
|
/// //
|
||||||
@ -74,10 +86,10 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
|
|||||||
/// // modules.
|
/// // modules.
|
||||||
/// let mut m = Module::new();
|
/// let mut m = Module::new();
|
||||||
/// m.set_fn("euclidean_distance".to_string(), FnAccess::Public,
|
/// m.set_fn("euclidean_distance".to_string(), FnAccess::Public,
|
||||||
/// &[std::any::TypeId::of::<FLOAT>(),
|
/// &[std::any::TypeId::of::<NUMBER>(),
|
||||||
/// std::any::TypeId::of::<FLOAT>(),
|
/// std::any::TypeId::of::<NUMBER>(),
|
||||||
/// std::any::TypeId::of::<FLOAT>(),
|
/// std::any::TypeId::of::<NUMBER>(),
|
||||||
/// std::any::TypeId::of::<FLOAT>()],
|
/// std::any::TypeId::of::<NUMBER>()],
|
||||||
/// CallableFunction::from_plugin(DistanceFunction()));
|
/// CallableFunction::from_plugin(DistanceFunction()));
|
||||||
/// let mut r = StaticModuleResolver::new();
|
/// let mut r = StaticModuleResolver::new();
|
||||||
/// r.insert("Math::Advanced".to_string(), m);
|
/// r.insert("Math::Advanced".to_string(), m);
|
||||||
@ -88,12 +100,19 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
|
|||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
|
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
|
||||||
///
|
///
|
||||||
|
/// # #[cfg(not(feature = "no_module"))] {
|
||||||
/// let mut engine = Engine::new();
|
/// let mut engine = Engine::new();
|
||||||
/// engine.register_plugin(AdvancedMathPlugin());
|
/// engine.register_plugin(AdvancedMathPlugin());
|
||||||
///
|
///
|
||||||
/// assert_eq!(engine.eval::<FLOAT>(
|
/// # #[cfg(feature = "no_float")]
|
||||||
|
/// assert_eq!(engine.eval::<NUMBER>(
|
||||||
/// r#"import "Math::Advanced" as math;
|
/// r#"import "Math::Advanced" as math;
|
||||||
/// let x = math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number()); x"#)?, 41.0);
|
/// let x = math::euclidean_distance(0, 1, 0, get_mystic_number()); x"#)?, 1681);
|
||||||
|
/// # #[cfg(not(feature = "no_float"))]
|
||||||
|
/// assert_eq!(engine.eval::<NUMBER>(
|
||||||
|
/// r#"import "Math::Advanced" as math;
|
||||||
|
/// let x = math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number()); x"#)?, 1681.0);
|
||||||
|
/// # } // end cfg
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -102,6 +102,8 @@ pub use rhai_codegen::*;
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
pub use parser::FnAccess;
|
pub use parser::FnAccess;
|
||||||
|
#[cfg(feature = "no_function")]
|
||||||
|
pub use parser::FnAccess;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
pub use fn_func::Func;
|
pub use fn_func::Func;
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![cfg(not(any(feature = "no_index", feature = "no_module")))]
|
||||||
|
|
||||||
use rhai::plugin::*;
|
use rhai::plugin::*;
|
||||||
use rhai::{Engine, EvalAltResult, INT};
|
use rhai::{Engine, EvalAltResult, INT};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user