Skip to content

Commit

Permalink
Ordering and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
makspll committed Apr 1, 2024
1 parent 0442b7d commit 9961a9e
Show file tree
Hide file tree
Showing 14 changed files with 1,713 additions and 865 deletions.
9 changes: 7 additions & 2 deletions crates/bevy_api_gen/src/passes/find_methods_and_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ pub(crate) fn find_methods_and_fields(ctxt: &mut BevyCtxt<'_>, _args: &Args) ->
ty_ctxt.valid_functions = Some(Vec::default());

// filter the list of all methods and select candidates applicable to proxy generation
for impl_did in ctxt
let mut all_impls = ctxt
.tcx
.inherent_impls(def_id)
.unwrap()
.iter()
.chain(trait_impls_for_ty.iter())
{
.collect::<Vec<_>>();

// sort them to avoid unnecessary diffs, we can use hashes here as they are forever stable (touch wood)
all_impls.sort_by_cached_key(|a| ctxt.tcx.def_path_hash(**a));

for impl_did in all_impls {
let functions = ctxt
.tcx
.associated_items(impl_did)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_api_gen/src/passes/find_reflect_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub(crate) fn find_reflect_types(ctxt: &mut BevyCtxt<'_>, args: &Args) -> bool {
}

ctxt.reflect_types
.sort_by_cached_key(|did, _| did.index.as_u32() + did.krate.as_u32());
.sort_by_cached_key(|did, _| tcx.item_name(*did));

if args.cmd.is_list_types() {
for did in ctxt.reflect_types.keys() {
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_api_gen/templates/function.tera
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ as_trait="{{ function.from_trait_path }}",

{% if function.has_self and not is_op %}
kind="{% if function.args.0.ty is starting_with("&mut") %}Mutating{% endif %}Method",
{% elif is_op %}
kind="MetaFunction",
{% else %}
kind="Function",
{% endif %}
Expand All @@ -27,7 +29,6 @@ output(proxy),
composite="{{ function.ident }}",
{% endif %}

{# forgive me father, this probably should be a filter #}
{% if function.from_trait_path == "std::ops::Neg" %}
metamethod="Unm",
{% elif function.from_trait_path == "std::ops::Mul" %}
Expand All @@ -40,7 +41,7 @@ metamethod="Sub",
metamethod="Div",
{% elif function.from_trait_path == "std::ops::Rem" %}
metamethod="Mod",
{% elif function.from_trait_path == "std::cmp:PartialEq" %}
{% elif function.from_trait_path == "std::cmp::PartialEq" %}
metamethod="Eq",
{% endif %}

Expand Down
12 changes: 8 additions & 4 deletions crates/bevy_api_gen/templates/item.tera
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ 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 -%}

{%- 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") -%}
Expand All @@ -42,8 +39,15 @@ functions[
{%- elif vec_type is starting_with("I64") -%}
{%- set num_type = "i64" -%}
{%- endif -%}

---
r#"
{{- macros::vector_index(num_type=num_type) -}}
"#
---
r#"
{{- macros::vector_newindex(num_type=num_type) -}}
"#
{% endif %}
{%- endfilter -%}
]
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_api_gen/templates/macros.tera
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{% macro vector_index(num_type) %}
#[lua(kind="Method", raw , metamethod="Index")]
#[lua(kind="MetaMethod", 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 }},_> {
#[lua(kind="MutatingMetaMethod", raw , metamethod="NewIndex")]
fn index(&mut self, lua: &Lua, idx: crate::lua::util::LuaIndex, val: {{ num_type }}) -> Result<(),_> {
self.val_mut(|s| Ok(s[*idx] = val))?
}
{% endmacro vector_newindex %}
7 changes: 6 additions & 1 deletion crates/bevy_script_api/src/providers/bevy_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
#[lua(
as_trait = "std::cmp::PartialEq",
kind = "MetaFunction",
composite = "eq",
metamethod = "Eq",
)]
fn eq(&self, #[proxy] other: &name::Name) -> bool;
"#]
Expand Down
39 changes: 27 additions & 12 deletions crates/bevy_script_api/src/providers/bevy_ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,25 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
fn clone(&self) -> bevy::ecs::component::ComponentId;
#[lua(as_trait = "std::cmp::Eq", kind = "Method")]
fn assert_receiver_is_total_eq(&self) -> ();
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
#[lua(
as_trait = "std::cmp::PartialEq",
kind = "MetaFunction",
composite = "eq",
metamethod = "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) -> ();
#[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
fn clone(&self) -> bevy::ecs::component::ComponentId;
"#]
)]
Expand Down Expand Up @@ -145,12 +150,6 @@ functions[r#"
this_run: bevy::ecs::component::Tick,
) -> bool;
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
fn eq(&self, #[proxy] other: &component::Tick) -> bool;
"#,
r#"
Expand All @@ -163,6 +162,17 @@ functions[r#"
#[lua(as_trait = "std::cmp::Eq", kind = "Method")]
fn assert_receiver_is_total_eq(&self) -> ();
"#,
r#"
#[lua(
as_trait = "std::cmp::PartialEq",
kind = "MetaFunction",
composite = "eq",
metamethod = "Eq",
)]
fn eq(&self, #[proxy] other: &component::Tick) -> bool;
"#]
)]

Expand Down Expand Up @@ -442,7 +452,12 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
#[lua(
as_trait = "std::cmp::PartialEq",
kind = "MetaFunction",
composite = "eq",
metamethod = "Eq",
)]
fn eq(&self, #[proxy] other: &entity::Entity) -> bool;
"#,
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_script_api/src/providers/bevy_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ functions[r#"
"#,
r#"
#[lua(as_trait = "std::cmp::PartialEq", kind = "Function", composite = "eq")]
#[lua(
as_trait = "std::cmp::PartialEq",
kind = "MetaFunction",
composite = "eq",
metamethod = "Eq",
)]
fn eq(&self, #[proxy] other: &components::parent::Parent) -> bool;
"#,
Expand Down
Loading

0 comments on commit 9961a9e

Please sign in to comment.