From 4f0cfdbe4e0cb625f926550c86f5581de99c8a82 Mon Sep 17 00:00:00 2001 From: GsLogimaker Date: Mon, 11 Dec 2023 21:48:09 -0600 Subject: [PATCH] Doc and api tweaks Registering functions and modules now takes str slices without .into() --- qu/src/import.rs | 12 ++++++------ qu/src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++-- qu/src/objects.rs | 22 +++++++++++----------- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/qu/src/import.rs b/qu/src/import.rs index 7c37477..808bea1 100644 --- a/qu/src/import.rs +++ b/qu/src/import.rs @@ -64,13 +64,13 @@ pub struct ModuleBuilder<'a> { /// Define a function in this module. pub fn add_function( &mut self, - name: String, + name: impl Into, args: &[ClassId], out: ClassId, body: &'static ExternalFunctionPointer, ) -> Result<(), QuMsg> { self.definitions.register_function_in_module(self.module_id, ExternalFunctionDefinition { - name: name, + name: name.into(), pointer: body, parameters: Vec::from(args), return_type: out, @@ -81,7 +81,7 @@ pub struct ModuleBuilder<'a> { pub fn add_class_static_function( &mut self, for_class: ClassId, - name: String, + name: impl Into, args: &[ClassId], out: ClassId, body: &'static ExternalFunctionPointer, @@ -89,7 +89,7 @@ pub struct ModuleBuilder<'a> { self.definitions.register_static_function_in_class( for_class, ExternalFunctionDefinition { - name: name, + name: name.into(), pointer: body, parameters: Vec::from(args), return_type: out, @@ -236,10 +236,10 @@ pub struct Registerer<'a> { pub(crate) definitions: &'a mut Definitions, } impl<'a> Registerer<'a> { pub fn add_module( - &mut self, name:String, + &mut self, name:impl Into, body:&ModuleBody ) -> Result { - self.definitions.define_module(name, body) + self.definitions.define_module(name.into(), body) } } diff --git a/qu/src/lib.rs b/qu/src/lib.rs index ffce2d3..97dcdc3 100644 --- a/qu/src/lib.rs +++ b/qu/src/lib.rs @@ -62,7 +62,6 @@ pub use vm::QuStackId; /// # fn run_test() -> Result<(), qu::QuMsg>{ /// let mut qu = Qu::new(); /// -/// // TODO: Fix functions with no parameters being uncallable. /// qu.run(r#" /// fn adder(a int) int: /// var left int = 2 @@ -124,11 +123,56 @@ pub struct Qu<'a> { /// Registers external items (such as functions, classes, and modules). + /// + /// # Example + /// + /// ``` + /// # use qu::QuMsg; + /// # fn main(){example().unwrap()} + /// # fn example() -> Result<(), QuMsg> { + /// use qu::Qu; + /// use qu::QuRegisterStruct; + /// + /// struct MyClass(i32); + /// impl QuRegisterStruct for MyClass { + /// fn name() -> &'static str { + /// "MyClass" + /// } + /// } + /// + /// let mut qu = Qu::new(); + /// qu.register(&|r| { + /// r.add_module("my_module", &|m| { + /// let my_class_id = m.add_class::()?; + /// m.add_class_static_function( + /// my_class_id, + /// ":new", + /// &[], + /// my_class_id, + /// &|api| { + /// api.set(MyClass(25)); + /// Ok(()) + /// } + /// )?; + /// Ok(()) + /// })?; + /// Ok(()) + /// }).unwrap(); + /// + /// let value:&MyClass = qu.run_and_get(" + /// import my_module.MyClass + /// return MyClass() + /// ").unwrap(); + /// assert_eq!(value.0, 25); + /// # return Ok(()); + /// # } + /// ``` pub fn register(&mut self, body: &RegistrationMethod) -> Result<(), QuMsg>{ self.vm.definitions.register(body) } + /// pub fn read<'b, T:'a + QuRegisterStruct>( &self, at_reg:QuStackId ) -> Result<&T, QuMsg> { @@ -146,7 +190,6 @@ pub struct Qu<'a> { /// # Example /// /// ``` - /// /// # use qu::QuMsg; /// # fn main(){example().unwrap()} /// # fn example() -> Result<(), QuMsg> { diff --git a/qu/src/objects.rs b/qu/src/objects.rs index 31b7457..952ebff 100644 --- a/qu/src/objects.rs +++ b/qu/src/objects.rs @@ -25,7 +25,7 @@ pub(crate) const FUNDAMENTALS_MODULE:&str = "__fundamentals__"; macro_rules! qufn { ($module_builder:ident, $api:ident, $name:ident($($param:ident),*) $return:ident $block:block) => { $module_builder.add_function( - stringify!($name).into(), + stringify!($name), &[$($param),*], $return, &|$api| $block @@ -34,7 +34,7 @@ macro_rules! qufn { ($module_builder:ident, $api:ident, $for_class:ident.$name:ident($($param:ident),*) $return:ident $block:block) => { $module_builder.add_class_static_function( $for_class, - stringify!($name).into(), + stringify!($name), &[$($param),*], $return, &|$api| $block @@ -60,7 +60,7 @@ macro_rules! qufn { /// ``` pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { registerer.add_module( - FUNDAMENTALS_MODULE.into(), + FUNDAMENTALS_MODULE, &|m| { let void = m.add_class::()?; let int = m.add_class::()?; @@ -77,7 +77,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { // int functions { - m.add_class_static_function(int, CONSTRUCTOR_NAME.into(), + m.add_class_static_function(int, CONSTRUCTOR_NAME, &[], int, &|api| { @@ -85,7 +85,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { Ok(()) } )?; - m.add_class_static_function(int, CONSTRUCTOR_NAME.into(), + m.add_class_static_function(int, CONSTRUCTOR_NAME, &[int], int, &|api| { @@ -93,7 +93,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { Ok(()) } )?; - m.add_class_static_function(int, CONSTRUCTOR_NAME.into(), + m.add_class_static_function(int, CONSTRUCTOR_NAME, &[bool], int, &|api| { @@ -173,7 +173,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { // bool functions { - m.add_class_static_function(bool, CONSTRUCTOR_NAME.into(), + m.add_class_static_function(bool, CONSTRUCTOR_NAME, &[], bool, &|api| { @@ -181,7 +181,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { Ok(()) } )?; - m.add_class_static_function(bool, CONSTRUCTOR_NAME.into(), + m.add_class_static_function(bool, CONSTRUCTOR_NAME, &[bool], bool, &|api| { @@ -189,7 +189,7 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { Ok(()) } )?; - m.add_class_static_function(bool, CONSTRUCTOR_NAME.into(), + m.add_class_static_function(bool, CONSTRUCTOR_NAME, &[int], bool, &|api| { @@ -245,13 +245,13 @@ pub fn fundamentals_module(registerer: &mut Registerer) -> Result<(), QuMsg> { pub fn math_module(registerer: &mut Registerer) -> Result<(), QuMsg> { registerer.add_module( - "math".into(), + "math", &|m| { let vector2 = m.add_class::()?; let fundamentals = m.get_module(FUNDAMENTALS_MODULE)?; let int = fundamentals.get_class_id("int")?; m.add_function( - "foo".into(), + "foo", &[int], int, &|api:&mut ArgsAPI| {