Skip to content

Commit

Permalink
Bump tealr & mlua versions (#96)
Browse files Browse the repository at this point in the history
* initial work

* change readme

* change readme

* re-factor CI

* spaces

* remove goblins

* change ci

* change ci

* change ci

* forgot to save

* add inherits

* clippy

* fmt

* change profile
  • Loading branch information
makspll authored Jan 19, 2024
1 parent 48d8012 commit 85e66a5
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 132 deletions.
23 changes: 14 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jobs:
profile: minimal
toolchain: stable
override: true
- uses: Swatinem/rust-cache@v2.7.0
- name: Rust Cache
uses: Swatinem/rust-cache@v2.7.3
# for x86 builds
- if: matrix.run_args.cross == null
uses: actions-rs/cargo@v1
Expand All @@ -47,7 +48,7 @@ jobs:
with:
command: check
target: ${{ matrix.run_args.cross }}
args: --workspace --features=${{ matrix.run_args.lua }},rhai,teal,lua_script_api,rhai_script_api
args: --workspace --features=${{ matrix.run_args.lua }},rhai,teal,lua_script_api,rhai_script_api --profile=ephemeral-build

fmt:
name: Rustfmt
Expand All @@ -62,7 +63,8 @@ jobs:
components: rustfmt
toolchain: stable
override: true
- uses: Swatinem/rust-cache@v2.7.0
- name: Rust Cache
uses: Swatinem/rust-cache@v2.7.3
- uses: actions-rs/cargo@v1
with:
command: fmt
Expand All @@ -80,11 +82,12 @@ jobs:
toolchain: stable
components: clippy
override: true
- uses: Swatinem/rust-cache@v2.7.0
- name: Rust Cache
uses: Swatinem/rust-cache@v2.7.3
- uses: actions-rs/cargo@v1
with:
command: clippy
args: --features=lua54,rhai,teal,lua_script_api,rhai_script_api -- -D warnings
args: --features=lua54,rhai,teal,lua_script_api,rhai_script_api --profile=ephemeral-build -- -D warnings
tests:
name: Tests
runs-on: ubuntu-latest
Expand All @@ -108,11 +111,12 @@ jobs:
with:
toolchain: stable
override: true
- uses: Swatinem/rust-cache@v2.7.0
- name: Rust Cache
uses: Swatinem/rust-cache@v2.7.3
- uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --features=lua54,rhai,teal,lua_script_api,rhai_script_api
args: --workspace --features=lua54,rhai,teal,lua_script_api,rhai_script_api --profile=ephemeral-build
docs:
name: Docs
runs-on: ubuntu-latest
Expand All @@ -124,11 +128,12 @@ jobs:
with:
toolchain: stable
override: true
- uses: Swatinem/rust-cache@v2.7.0
- name: Rust Cache
uses: Swatinem/rust-cache@v2.7.3
- name: Find docs.rs features
run: echo "DOCS_FEATURES=$(cargo metadata --no-deps | python -c "import sys,json; [print(','.join(x['metadata']['docs.rs']['features'])) for x in json.load(sys.stdin)['packages'] if x['name'] == 'bevy_mod_scripting']")" >> $GITHUB_OUTPUT
id: features
- uses: actions-rs/cargo@v1
with:
command: doc
args: --workspace --features=${{ steps.features.outputs.DOCS_FEATURES }}
args: --workspace --features=${{ steps.features.outputs.DOCS_FEATURES }} --profile=ephemeral-build
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ opt-level = 1
[profile.dev.package."*"]
opt-level = 3

[profile.ephemeral-build]
inherits = "dev"
opt-level = 2
codegen-units = 8


[[example]]
name = "console_integration_lua"
Expand Down
4 changes: 3 additions & 1 deletion bevy_script_api/src/lua/bevy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::common::bevy::{ScriptTypeRegistration, ScriptWorld};
use crate::impl_tealr_type;
use crate::{impl_from_lua_with_clone, impl_tealr_type};

use std::sync::Arc;

Expand All @@ -21,6 +21,7 @@ use super::util::LuaIndex;

pub type LuaTypeRegistration = ScriptTypeRegistration;
impl_tealr_type!(LuaTypeRegistration);
impl_from_lua_with_clone!(LuaTypeRegistration);

impl TealData for LuaTypeRegistration {
fn add_methods<'lua, T: TealDataMethods<'lua, Self>>(methods: &mut T) {
Expand Down Expand Up @@ -66,6 +67,7 @@ impl TealData for LuaScriptData {
pub type LuaWorld = ScriptWorld;

impl_tealr_type!(LuaWorld);
impl_from_lua_with_clone!(LuaWorld);

impl TealData for LuaWorld {
fn add_methods<'lua, T: TealDataMethods<'lua, Self>>(methods: &mut T) {
Expand Down
36 changes: 15 additions & 21 deletions bevy_script_api/src/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ use ::std::any::TypeId;
use ::std::borrow::Cow;

use crate::common::bevy::GetWorld;
use crate::impl_tealr_type;
use crate::{impl_from_lua_with_clone, impl_tealr_type};
use ::bevy::prelude::{App, AppTypeRegistry};

use ::bevy::reflect::{FromType, GetTypeRegistration, Reflect};

use bevy_mod_scripting_core::world::WorldPointer;
use bevy_mod_scripting_lua::tealr;
use bevy_mod_scripting_lua::tealr::{self, ToTypename};

use tealr::mlu::mlua::MetaMethod;
use tealr::mlu::{
mlua::{self, FromLua, Lua, ToLua, UserData, Value},
mlua::{self, FromLua, IntoLua, Lua, UserData, Value},
TealData, TealDataMethods,
};
use tealr::TypeName;

use crate::script_ref::{ReflectedValue, ScriptRef, ValueIndex};

Expand Down Expand Up @@ -126,13 +125,13 @@ impl ApplyLua for ScriptRef {
}
}

impl<'lua> ToLua<'lua> for ScriptRef {
impl<'lua> IntoLua<'lua> for ScriptRef {
/// Converts the LuaRef to the most convenient representation
/// checking conversions in this order:
/// - A primitive or bevy type which has a reflect interface is converted to a custom UserData exposing its API to lua conveniently
/// - A type implementing CustomUserData is converted with its `ref_to_lua` method
/// - Finally the method is represented as a `ReflectedValue` which exposes the Reflect interface
fn to_lua(self, ctx: &'lua Lua) -> mlua::Result<Value<'lua>> {
fn into_lua(self, ctx: &'lua Lua) -> mlua::Result<Value<'lua>> {
let world = self.world_ptr.clone();
let world = world.read();

Expand All @@ -143,26 +142,19 @@ impl<'lua> ToLua<'lua> for ScriptRef {
if let Some(v) = g.get_type_data::<ReflectLuaProxyable>(type_id) {
v.ref_to_lua(self, ctx)
} else {
ReflectedValue { ref_: self }.to_lua(ctx)
ReflectedValue { ref_: self }.into_lua(ctx)
}
}
}

impl TypeName for ScriptRef {
/// ReflectedValue represents the "lowest common denominator" across the possible returned types
/// people can always use 'as' to cast to the right type
/// but the static analysis will be conservative, i.e. the compiler will assume the smallest set of functionality
/// by default
fn get_type_parts() -> Cow<'static, [tealr::NamePart]> {
Cow::Borrowed(&[tealr::NamePart::Type(tealr::TealType {
name: Cow::Borrowed("ReflectedValue"),
generics: None,
type_kind: tealr::KindOfType::Builtin,
})])
impl ToTypename for ScriptRef {
fn to_typename() -> tealr::Type {
tealr::Type::new_single("ReflectedValue", tealr::KindOfType::External)
}
}

impl_tealr_type!(ReflectedValue);
impl_from_lua_with_clone!(ReflectedValue);
impl TealData for ReflectedValue {
fn add_methods<'lua, T: TealDataMethods<'lua, Self>>(methods: &mut T) {
methods.document_type("This type represents a generic reflected value.");
Expand Down Expand Up @@ -266,7 +258,7 @@ pub trait ValueLuaType {}

impl<T: Clone + UserData + Send + ValueLuaType + Reflect + 'static> LuaProxyable for T {
fn ref_to_lua(self_: ScriptRef, lua: &Lua) -> mlua::Result<Value> {
self_.get_typed(|s: &Self| s.clone().to_lua(lua))?
self_.get_typed(|s: &Self| s.clone().into_lua(lua))?
}

fn apply_lua<'lua>(
Expand All @@ -288,15 +280,17 @@ impl<T: Clone + UserData + Send + ValueLuaType + Reflect + 'static> LuaProxyable
}
}

impl<'lua, T: Clone + UserData + Send + ValueLuaType + Reflect + 'static> FromLuaProxy<'lua> for T {
impl<'lua, T: Clone + UserData + FromLua<'lua> + Send + ValueLuaType + Reflect + 'static>
FromLuaProxy<'lua> for T
{
fn from_lua_proxy(new_val: Value<'lua>, lua: &'lua Lua) -> mlua::Result<Self> {
T::from_lua(new_val, lua)
}
}

impl<'lua, T: Clone + UserData + Send + ValueLuaType + Reflect + 'static> ToLuaProxy<'lua> for T {
fn to_lua_proxy(self, lua: &'lua Lua) -> mlua::Result<Value<'lua>> {
self.to_lua(lua)
self.into_lua(lua)
}
}

Expand Down
51 changes: 20 additions & 31 deletions bevy_script_api/src/lua/std.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use ::std::borrow::Cow;

use bevy::reflect::FromReflect;
use bevy::reflect::Reflect;

use bevy::reflect::TypePath;
use bevy_mod_scripting_lua::tealr;

use bevy_mod_scripting_lua::tealr::ToTypename;
use tealr::mlu::mlua::MetaMethod;
use tealr::mlu::TypedFunction;

Check warning on line 9 in bevy_script_api/src/lua/std.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, lua51)

unused import: `tealr::mlu::TypedFunction`

Check warning on line 9 in bevy_script_api/src/lua/std.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, luajit)

unused import: `tealr::mlu::TypedFunction`
use tealr::mlu::{
mlua::{self, FromLua, Lua, ToLua, UserData, Value},
mlua::{self, FromLua, IntoLua, Lua, UserData, Value},
TealData, TealDataMethods,
};
use tealr::TypeBody;
use tealr::TypeName;

use paste::paste;

Expand All @@ -38,7 +36,7 @@ macro_rules! impl_proxyable_by_copy(
$(
impl $crate::lua::LuaProxyable for $num_ty {
fn ref_to_lua(self_: $crate::script_ref::ScriptRef,lua: & tealr::mlu::mlua::Lua) -> tealr::mlu::mlua::Result<tealr::mlu::mlua::Value< '_> > {
self_.get_typed(|self_ : &Self| self_.to_lua(lua))?
self_.get_typed(|self_ : &Self| self_.into_lua(lua))?
}

fn apply_lua< 'lua>(self_: &mut $crate::script_ref::ScriptRef,lua: & 'lua tealr::mlu::mlua::Lua,new_val:tealr::mlu::mlua::Value< 'lua>) -> tealr::mlu::mlua::Result<()> {
Expand All @@ -57,7 +55,7 @@ macro_rules! impl_proxyable_by_copy(
impl <'lua>$crate::lua::ToLuaProxy<'lua> for $num_ty {
#[inline(always)]
fn to_lua_proxy(self, lua: &'lua Lua) -> tealr::mlu::mlua::Result<Value<'lua>> {
self.to_lua(lua)
self.into_lua(lua)
}
}
)*
Expand All @@ -72,7 +70,7 @@ impl_proxyable_by_copy!(u8, u16, u32, u64, u128, usize);

impl LuaProxyable for String {
fn ref_to_lua(self_: ScriptRef, lua: &Lua) -> mlua::Result<Value> {
self_.get_typed(|self_: &String| self_.as_str().to_lua(lua))?
self_.get_typed(|self_: &String| self_.as_str().into_lua(lua))?
}

fn apply_lua<'lua>(
Expand All @@ -95,7 +93,7 @@ impl<'lua> FromLuaProxy<'lua> for String {

impl<'lua> ToLuaProxy<'lua> for String {
fn to_lua_proxy(self, lua: &'lua Lua) -> mlua::Result<Value<'lua>> {
self.to_lua(lua)
self.into_lua(lua)
}
}

Expand Down Expand Up @@ -234,7 +232,7 @@ impl<'lua, T: for<'a> ToLuaProxy<'a>> ToLuaProxy<'lua> for Option<T> {
pub type LuaVec<T> = ScriptVec<T>;

impl<
T: TypeName
T: ToTypename
+ FromReflect
+ TypePath
+ LuaProxyable
Expand All @@ -253,24 +251,15 @@ impl<
}
}

impl<T: TypeName> TypeName for LuaVec<T> {
fn get_type_parts() -> Cow<'static, [tealr::NamePart]> {
let mut parts = vec![
tealr::NamePart::Type(tealr::TealType {
name: Cow::Borrowed("LuaVec"),
type_kind: tealr::KindOfType::External,
generics: None,
}),
tealr::NamePart::Symbol("<".into()),
];
parts.extend(T::get_type_parts().iter().cloned());
parts.push(tealr::NamePart::Symbol(">".into()));
parts.into()
impl<T: ToTypename> ToTypename for LuaVec<T> {
/// Before tealr deprecated TypeName, this used to incorporate generics here, but right now I don't think they're supported anymore
fn to_typename() -> tealr::Type {
tealr::Type::new_single("LuaVec", tealr::KindOfType::External)
}
}

impl<
T: TypeName
T: ToTypename
+ FromReflect
+ TypePath
+ LuaProxyable
Expand All @@ -289,7 +278,7 @@ impl<
}

impl<
T: TypeName
T: ToTypename
+ FromReflect
+ TypePath
+ LuaProxyable
Expand Down Expand Up @@ -325,8 +314,8 @@ impl<
move |ctx, ()| {
let o = if curr_idx < len {
(
to_lua_idx(curr_idx).to_lua(ctx)?,
ref_.index(curr_idx).to_lua(ctx)?,
to_lua_idx(curr_idx).into_lua(ctx)?,
ref_.index(curr_idx).into_lua(ctx)?,
)
} else {
(Value::Nil, Value::Nil)
Expand All @@ -346,7 +335,7 @@ impl<
let len = s.len()?;

for i in 0..len {
table.raw_set(to_lua_idx(i), s.index(i).to_lua(ctx)?)?;
table.raw_set(to_lua_idx(i), s.index(i).into_lua(ctx)?)?;
}

Ok(table)
Expand Down Expand Up @@ -378,7 +367,7 @@ impl<
}

impl<
T: TypeName
T: ToTypename
+ FromReflect
+ TypePath
+ LuaProxyable
Expand All @@ -388,7 +377,7 @@ impl<
> LuaProxyable for Vec<T>
{
fn ref_to_lua(self_: ScriptRef, lua: &Lua) -> mlua::Result<Value> {
LuaVec::<T>::new_ref(self_).to_lua(lua)
LuaVec::<T>::new_ref(self_).into_lua(lua)
}

fn apply_lua<'lua>(
Expand Down Expand Up @@ -436,7 +425,7 @@ impl<

impl<
'lua,
T: TypeName
T: ToTypename
+ for<'a> FromLuaProxy<'a>
+ for<'a> ToLuaProxy<'a>
+ Clone
Expand Down Expand Up @@ -478,6 +467,6 @@ impl<'lua, T: for<'a> ToLuaProxy<'a> + Clone + FromReflect + LuaProxyable> ToLua
proxies.raw_set(idx, elem.to_lua_proxy(lua)?)?;
}

proxies.to_lua(lua)
proxies.into_lua(lua)
}
}
Loading

0 comments on commit 85e66a5

Please sign in to comment.