schungx / rhai

Rhai - An embedded scripting language for Rust [dev repo, may regularly force-push, pull from https://github.com/rhaiscript/rhai for a stable build]
https://github.com/rhaiscript/rhai
Apache License 2.0
9 stars 3 forks source link

codegen: add rhai_mod and submodule support #40

Closed jhwgh1968 closed 4 years ago

jhwgh1968 commented 4 years ago

This allows modules to be nested, and set attributes with rhai_mod, similar to rhai_fn:

#[export_module]
pub mod advanced_math {
    use rhai::plugin::*;
    #[rhai_mod(name = "constants")]
    pub mod my_module {
        use rhai::plugin::*;
        use rhai::FLOAT;
        #[rhai_fn(return_raw)]
        pub fn get_mystic_number() -> Result<Dynamic, Box<EvalAltResult>> {
            Ok(Dynamic::from(42.0 as FLOAT))
        }
    }
}

However, this does not implement #[cfg(...)] block handling yet. That will be complex enough to be its own PR.

schungx commented 4 years ago

I find that rhai_mod must have a parameters list. That is, #[rhai_mod] is an error, but #[rhai_mod()] is accepted.

jhwgh1968 commented 4 years ago

That is currently not a surprise, since there is no reason to have it without attributes in the current version.

I do intend to change that, once I implement a series of attributes for fine-grained "does this export" behavior.

schungx commented 4 years ago

That is currently not a surprise, since there is no reason to have it without attributes in the current version.

So, will a sub-module be always exported if it is pub mod? The rhai_mod attribute is only used to rename it?

jhwgh1968 commented 4 years ago

So, will a sub-module be always exported if it is pub mod? The rhai_mod attribute is only used to rename it?

Currently, that is correct.

schungx commented 4 years ago

Don't forget a variation of rhai_generate that adds the function directly into an existing module instead of creating a new one. This will help cut down a lot on unnecessary module creation.

jhwgh1968 commented 4 years ago

Don't forget a variation of rhai_generate that adds the function directly into an existing module instead of creating a new one. This will help cut down a lot on unnecessary module creation.

I have it on my list (it's the item about adding to an existing module). I have some questions, but I'll ask them when I get there.