Skip to content

Commit

Permalink
add vec index methods
Browse files Browse the repository at this point in the history
  • Loading branch information
makspll committed Apr 1, 2024
1 parent 8346ec3 commit 0442b7d
Show file tree
Hide file tree
Showing 18 changed files with 1,232 additions and 995 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"/home/makspll/git/bevy_mod_scripting/check.sh"
],
"rust-analyzer.showUnlinkedFileNotification": false,
"rust-analyzer.semanticHighlighting.operator.enable": false
// "rust-analyzer.semanticHighlighting.operator.enable": false
}
2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ unset RUSTUP_TOOLCHAIN
CURRENT_DIR=$(basename "$PWD")

if [[ "$CURRENT_DIR" == "bevy_api_gen" ]]; then
cargo clippy --message-format=json
cargo clippy --message-format=json
else
cargo clippy --message-format=json --features="lua54, lua_script_api, rhai, rhai_script_api, teal, rune"
fi
4 changes: 4 additions & 0 deletions crates/bevy_api_gen/src/passes/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub(crate) fn codegen(ctxt: &mut BevyCtxt<'_>, args: &Args) -> bool {
mod tests {
use std::collections::HashSet;

use strum::VariantNames;

use crate::TEMPLATE_DIR;

use super::*;

#[test]
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_api_gen/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub static TEMPLATE_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/templates");
Deserialize,
)]
pub enum TemplateKind {
// Note: order here matters, macros need to be loaded first as they are used in other templates
#[strum(to_string = "macros.tera")]
Macros,
#[strum(to_string = "mod.tera")]
SharedModule,
#[strum(to_string = "crate.tera")]
Expand Down Expand Up @@ -155,7 +158,6 @@ pub fn configure_tera(
.expect("Missing template kind file in the binary")
.contents_utf8()
.unwrap();

tera.add_raw_template(template_filename, content)
.expect("Could not load built-in template");
}
Expand Down
9 changes: 1 addition & 8 deletions crates/bevy_api_gen/templates/footer.tera
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@

{% if args.self_is_bevy_script_api %}
crate::impl_tealr_generic!(pub(crate) struct T);
{% else %}
bevy_script_api::impl_tealr_generic!(pub(crate) struct T);
{% endif %}

#[derive(Default)]
pub(crate) struct Globals;

Expand All @@ -15,7 +8,7 @@ impl bevy_mod_scripting_lua::tealr::mlu::ExportInstances for Globals {
) -> bevy_mod_scripting_lua::tealr::mlu::mlua::Result<()> {
{% for item in items %}
{% if item.has_static_methods %}
instances.add_instance("{{ item.ident | prefix_lua }}",
instances.add_instance("{{ item.ident }}",
bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<{{item.ident | prefix_lua}}>::new)?;
{% endif %}
{% endfor %}
Expand Down
26 changes: 26 additions & 0 deletions crates/bevy_api_gen/templates/item.tera
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% import "macros.tera" as macros -%}
{% for line in item.docstrings %}
///{{ line }}
{% endfor %}
Expand All @@ -19,6 +20,31 @@ functions[
---
{%- include "function.tera" -%}
{%- endfor -%}
{% if item.import_path is matching("bevy::math::[^B]?Vec.?") %}
---
r#"
{%- set vec_type = item.import_path | split(pat="::") | last -%}

{%- if vec_type is starting_with("V") -%}
{%- set num_type = "f32" -%}
{%- elif vec_type is starting_with("U16") -%}
{%- set num_type = "u64" -%}
{%- elif vec_type is starting_with("UV") -%}
{%- set num_type = "u32" -%}
{%- elif vec_type is starting_with("U16") -%}
{%- set num_type = "u16" -%}
{%- elif vec_type is starting_with("DV") -%}
{%- set num_type = "f64" -%}
{%- elif vec_type is starting_with("IV") -%}
{%- set num_type = "i32" -%}
{%- elif vec_type is starting_with("I16") -%}
{%- set num_type = "i16" -%}
{%- elif vec_type is starting_with("I64") -%}
{%- set num_type = "i64" -%}
{%- endif -%}
{{- macros::vector_index(num_type=num_type) -}}
"#
{% endif %}
{%- endfilter -%}
]
)]
Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_api_gen/templates/macros.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% macro vector_index(num_type) %}
#[lua(kind="Method", raw , metamethod="Index")]
fn index(&self, lua: &Lua, idx: crate::lua::util::LuaIndex) -> Result<{{ num_type }},_> {
Ok(self.inner()?[*idx])
}
{% endmacro vector_index %}

{% macro vector_newindex(num_type) %}
#[lua(kind="MutatingMethod", raw , metamethod="NewIndex")]
fn index(&mut self, lua: &Lua, (idx, val): (crate::lua::util::LuaIndex,{{ num_type }})) -> Result<{{ num_type }},_> {
self.val_mut(|s| Ok(s[*idx] = val))?
}
{% endmacro vector_newindex %}
114 changes: 114 additions & 0 deletions crates/bevy_script_api/src/core_providers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::lua::RegisterForeignLuaType;

pub struct CoreBevyAPIProvider;

#[derive(Default)]
pub(crate) struct CoreBevyGlobals;

crate::impl_tealr_generic!(pub(crate) struct T);

impl bevy_mod_scripting_lua::tealr::mlu::ExportInstances for CoreBevyGlobals {
fn add_instances<'lua, T: bevy_mod_scripting_lua::tealr::mlu::InstanceCollector<'lua>>(
self,
instances: &mut T,
) -> bevy_mod_scripting_lua::tealr::mlu::mlua::Result<()> {
instances.add_instance(
"world",
crate::lua::util::DummyTypeName::<crate::lua::bevy::LuaWorld>::new,
)?;
instances.add_instance(
"script",
crate::lua::util::DummyTypeName::<crate::lua::bevy::LuaScriptData>::new,
)?;
instances.add_instance(
"entity",
crate::lua::util::DummyTypeName::<crate::providers::bevy_ecs::LuaEntity>::new,
)?;
Ok(())
}
}

impl bevy_mod_scripting_core::hosts::APIProvider for CoreBevyAPIProvider {
type APITarget = std::sync::Mutex<bevy_mod_scripting_lua::tealr::mlu::mlua::Lua>;
type ScriptContext = std::sync::Mutex<bevy_mod_scripting_lua::tealr::mlu::mlua::Lua>;
type DocTarget = bevy_mod_scripting_lua::docs::LuaDocFragment;

fn attach_api(
&mut self,
ctx: &mut Self::APITarget,
) -> Result<(), bevy_mod_scripting_core::error::ScriptError> {
let ctx = ctx
.get_mut()
.expect("Unable to acquire lock on Lua context");
bevy_mod_scripting_lua::tealr::mlu::set_global_env(CoreBevyGlobals, ctx)
.map_err(|e| bevy_mod_scripting_core::error::ScriptError::Other(e.to_string()))
}

fn get_doc_fragment(&self) -> Option<Self::DocTarget> {
Some(bevy_mod_scripting_lua::docs::LuaDocFragment::new(
"CoreBevyAPI",
|tw| {
tw
.document_global_instance::<CoreBevyGlobals>().expect("Something went wrong documenting globals")
.process_type::<crate::lua::bevy::LuaWorld>()
.process_type::<bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<crate::lua::bevy::LuaWorld>>()
.process_type::<crate::lua::bevy::LuaScriptData>()
.process_type::<bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<crate::lua::bevy::LuaScriptData>>()
.process_type::<crate::lua::bevy::LuaTypeRegistration>()
.process_type::<crate::lua::std::LuaVec<T>>()
},
))
}

fn setup_script(
&mut self,
script_data: &bevy_mod_scripting_core::hosts::ScriptData,
ctx: &mut Self::ScriptContext,
) -> Result<(), bevy_mod_scripting_core::error::ScriptError> {
let ctx = ctx.get_mut().expect("Could not get context");
let globals = ctx.globals();
globals
.set(
"entity",
crate::providers::bevy_ecs::LuaEntity::new(script_data.entity),
)
.map_err(bevy_mod_scripting_core::error::ScriptError::new_other)?;
globals
.set::<_, crate::lua::bevy::LuaScriptData>("script", script_data.into())
.map_err(bevy_mod_scripting_core::error::ScriptError::new_other)?;

Ok(())
}

fn setup_script_runtime(
&mut self,
world_ptr: bevy_mod_scripting_core::world::WorldPointer,
_script_data: &bevy_mod_scripting_core::hosts::ScriptData,
ctx: &mut Self::ScriptContext,
) -> Result<(), bevy_mod_scripting_core::error::ScriptError> {
let ctx = ctx.get_mut().expect("Could not get context");
let globals = ctx.globals();
globals
.set("world", crate::lua::bevy::LuaWorld::new(world_ptr))
.map_err(bevy_mod_scripting_core::error::ScriptError::new_other)
}

fn register_with_app(&self, app: &mut bevy::app::App) {
app.register_foreign_lua_type::<usize>();
app.register_foreign_lua_type::<isize>();
app.register_foreign_lua_type::<f32>();
app.register_foreign_lua_type::<f64>();
app.register_foreign_lua_type::<u128>();
app.register_foreign_lua_type::<u64>();
app.register_foreign_lua_type::<u32>();
app.register_foreign_lua_type::<u16>();
app.register_foreign_lua_type::<u8>();
app.register_foreign_lua_type::<i128>();
app.register_foreign_lua_type::<i64>();
app.register_foreign_lua_type::<i32>();
app.register_foreign_lua_type::<i16>();
app.register_foreign_lua_type::<i8>();
app.register_foreign_lua_type::<String>();
app.register_foreign_lua_type::<bool>();
}
}
2 changes: 2 additions & 0 deletions crates/bevy_script_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod rhai;

pub mod common;

pub(crate) mod core_providers;
pub mod script_ref;
pub mod sub_reflect;
pub mod wrappers;
Expand All @@ -17,6 +18,7 @@ pub use {script_ref::*, sub_reflect::*};
pub mod prelude {
#[cfg(feature = "lua")]
pub use crate::{
core_providers::CoreBevyAPIProvider,
lua::{std::LuaVec, FromLuaProxy, IntoLuaProxy, LuaProxyable, ReflectLuaProxyable},
providers::BevyAPIProvider,
LuaProxy,
Expand Down
5 changes: 0 additions & 5 deletions crates/bevy_script_api/src/providers/bevy_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ pub struct Name{
}




crate::impl_tealr_generic!(pub(crate) struct T);


#[derive(Default)]
pub(crate) struct Globals;

Expand Down
39 changes: 17 additions & 22 deletions crates/bevy_script_api/src/providers/bevy_ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ functions[r#"
#[lua(kind = "Method")]
fn index(self) -> usize;
"#,
r#"
#[lua(as_trait = "std::cmp::Eq", kind = "Method")]
fn assert_receiver_is_total_eq(&self) -> ();
"#,
r#"
Expand All @@ -88,6 +82,12 @@ functions[r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
fn eq(&self, #[proxy] other: &component::ComponentId) -> bool;
"#,
r#"
#[lua(as_trait = "std::cmp::Eq", kind = "Method")]
fn assert_receiver_is_total_eq(&self) -> ();
"#]
)]

Expand Down Expand Up @@ -148,8 +148,8 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::cmp::Eq", kind = "Method")]
fn assert_receiver_is_total_eq(&self) -> ();
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
fn eq(&self, #[proxy] other: &component::Tick) -> bool;
"#,
r#"
Expand All @@ -160,8 +160,8 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
fn eq(&self, #[proxy] other: &component::Tick) -> bool;
#[lua(as_trait = "std::cmp::Eq", kind = "Method")]
fn assert_receiver_is_total_eq(&self) -> ();
"#]
)]
Expand Down Expand Up @@ -442,14 +442,14 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
fn clone(&self) -> bevy::ecs::entity::Entity;
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
fn eq(&self, #[proxy] other: &entity::Entity) -> bool;
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
fn eq(&self, #[proxy] other: &entity::Entity) -> bool;
#[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
fn clone(&self) -> bevy::ecs::entity::Entity;
"#]
)]
Expand All @@ -468,11 +468,6 @@ pub struct Entity{
}




crate::impl_tealr_generic!(pub(crate) struct T);


#[derive(Default)]
pub(crate) struct Globals;

Expand All @@ -483,12 +478,12 @@ impl bevy_mod_scripting_lua::tealr::mlu::ExportInstances for Globals {
) -> bevy_mod_scripting_lua::tealr::mlu::mlua::Result<()> {


instances.add_instance("LuaComponentId",
instances.add_instance("ComponentId",
bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaComponentId>::new)?;



instances.add_instance("LuaTick",
instances.add_instance("Tick",
bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaTick>::new)?;


Expand All @@ -497,7 +492,7 @@ impl bevy_mod_scripting_lua::tealr::mlu::ExportInstances for Globals {



instances.add_instance("LuaEntity",
instances.add_instance("Entity",
bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaEntity>::new)?;


Expand Down
Loading

0 comments on commit 0442b7d

Please sign in to comment.