Skip to content

Commit

Permalink
Doc and api tweaks
Browse files Browse the repository at this point in the history
Registering functions and modules now takes str slices without .into()
  • Loading branch information
GsLogiMaker committed Dec 12, 2023
1 parent 19c8949 commit 4f0cfdb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
12 changes: 6 additions & 6 deletions qu/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
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,
Expand All @@ -81,15 +81,15 @@ pub struct ModuleBuilder<'a> {
pub fn add_class_static_function(
&mut self,
for_class: ClassId,
name: String,
name: impl Into<String>,
args: &[ClassId],
out: ClassId,
body: &'static ExternalFunctionPointer,
) -> Result<(), QuMsg> {
self.definitions.register_static_function_in_class(
for_class,
ExternalFunctionDefinition {
name: name,
name: name.into(),
pointer: body,
parameters: Vec::from(args),
return_type: out,
Expand Down Expand Up @@ -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<String>,
body:&ModuleBody
) -> Result<ModuleId, QuMsg> {
self.definitions.define_module(name, body)
self.definitions.define_module(name.into(), body)
}
}

Expand Down
47 changes: 45 additions & 2 deletions qu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<MyClass>()?;
/// 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> {
Expand All @@ -146,7 +190,6 @@ pub struct Qu<'a> {
/// # Example
///
/// ```
///
/// # use qu::QuMsg;
/// # fn main(){example().unwrap()}
/// # fn example() -> Result<(), QuMsg> {
Expand Down
22 changes: 11 additions & 11 deletions qu/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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::<QuVoid>()?;
let int = m.add_class::<i32>()?;
Expand All @@ -77,23 +77,23 @@ 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| {
api.set(0);
Ok(())
}
)?;
m.add_class_static_function(int, CONSTRUCTOR_NAME.into(),
m.add_class_static_function(int, CONSTRUCTOR_NAME,
&[int],
int,
&|api| {
api.set(*api.get::<i32>(0)?);
Ok(())
}
)?;
m.add_class_static_function(int, CONSTRUCTOR_NAME.into(),
m.add_class_static_function(int, CONSTRUCTOR_NAME,
&[bool],
int,
&|api| {
Expand Down Expand Up @@ -173,23 +173,23 @@ 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| {
api.set(Bool::from(false));
Ok(())
}
)?;
m.add_class_static_function(bool, CONSTRUCTOR_NAME.into(),
m.add_class_static_function(bool, CONSTRUCTOR_NAME,
&[bool],
bool,
&|api| {
api.set(*api.get::<Bool>(0)?);
Ok(())
}
)?;
m.add_class_static_function(bool, CONSTRUCTOR_NAME.into(),
m.add_class_static_function(bool, CONSTRUCTOR_NAME,
&[int],
bool,
&|api| {
Expand Down Expand Up @@ -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::<Vector2>()?;
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| {
Expand Down

0 comments on commit 4f0cfdb

Please sign in to comment.