Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve trait system for bindings/wrappers #112

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 17 additions & 45 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,33 @@
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug specific unit test",
"preLaunchTask": "Build specific package's unit tests",
"program": "${workspaceFolder}/target/debug/test_binary",
"env": {
"CARGO_MANIFEST_DIR": "${workspaceFolder}/bevy_mod_scripting",
"LD_LIBRARY_PATH": "${workspaceFolder}/target/debug/deps:${env:HOME}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib"
},
"cwd": "${workspaceFolder}",
},
{
"name": "Debug example 'game_of_life_lua'",
"name": "Debug a test in bevy_mod_scripting_lua",
"type": "lldb",
"request": "launch",
"cargo": {
"args": [
"build",
"--example=game_of_life_lua",
"--package=bevy_mod_scripting",
"--features=lua54,teal,lua_script_api",
"test",
"--no-run",
"--features",
"lua54",
"--package",
"bevy_mod_scripting_lua",
"--lib",
"--",
"${input:test_name}",
"--show-output"
],
"filter": {
"name": "game_of_life_lua",
"kind": "example"
"kind": "lib",
"name": "bevy_mod_scripting_lua"
}
},
"args": [],
"cwd": "${workspaceFolder}",
"cwd": "${workspaceFolder}/crates/languages/bevy_mod_scripting_lua",
"env": {
"CARGO_MANIFEST_DIR": "${workspaceFolder}",
"CARGO_MANIFEST_DIR": "${workspaceFolder}/crates/languages/bevy_mod_scripting_lua",
"LD_LIBRARY_PATH": "${workspaceFolder}/target/debug/deps:${env:HOME}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib"
}
},
{
"name": "Debug example 'game_of_life_rhai'",
"type": "lldb",
"request": "launch",
"cargo": {
"args": [
"build",
"--example=game_of_life_rhai",
"--package=bevy_mod_scripting",
"--features=rhai,rhai_script_api",
],
"filter": {
"name": "game_of_life_rhai",
"kind": "example"
}
},
"args": [],
"cwd": "${workspaceFolder}",
"env": {
"CARGO_MANIFEST_DIR": "${workspaceFolder}"
}
}
]
],
"inputs": []
}
28 changes: 14 additions & 14 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"version": "2.0.0",
"inputs": [
// used for launch.json as well due to bug https://github.com/microsoft/vscode/issues/212384
{
"id": "test_name",
"type": "promptString",
"description": "Run only tests including this string in their name",
"default": ""
"description": "Run tests with given path",
"default": "bindings::reference::test::test_index_lua_value_field"
},
{
"id": "package",
"type": "promptString",
"description": "The crate location of this unit test",
"default": "bevy_mod_scripting"
}
],
"tasks": [
},
{
"id": "features",
"type": "promptString",
"description": "Features to activate for the build",
},
{
"label": "Build specific package's unit tests",
"type": "process",
"command": "make",
"args": [
"build_test_in_package",
"PACKAGE=${input:package}",
"TEST_NAME=${input:test_name}"
]
}
"id": "pwd",
"type": "promptString",
"description": "The working directory for the test",
"default": "${workspaceFolder}"
},
]
}
13 changes: 4 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ luajit = ["bevy_mod_scripting_lua/luajit", "lua"]
luajit52 = ["bevy_mod_scripting_lua/luajit52", "lua"]

# optional
lua_script_api = ["bevy_script_api/lua"]
# lua_script_api = ["bevy_script_api/lua"]
unsafe_lua_modules = ["bevy_mod_scripting_lua/unsafe_lua_modules"]
teal = ["bevy_mod_scripting_lua/teal"]
mlua_serialize = ["bevy_mod_scripting_lua/mlua_serialize"]
Expand All @@ -55,7 +55,7 @@ mlua_async = ["bevy_mod_scripting_lua/mlua_async"]

## rhai
rhai = ["bevy_mod_scripting_rhai"]
rhai_script_api = ["bevy_script_api/rhai"]
# rhai_script_api = ["bevy_script_api/rhai"]

## rune
rune = ["bevy_mod_scripting_rune"]
Expand All @@ -66,13 +66,12 @@ bevy_mod_scripting_core = { workspace = true }
bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", version = "0.6.0", optional = true }
bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.6.0", optional = true }
bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.6.0", optional = true }
bevy_script_api = { path = "crates/bevy_script_api", version = "0.6.0", optional = true }
# bevy_script_api = { path = "crates/bevy_script_api", version = "0.6.0", optional = true }


[workspace.dependencies]
bevy = { version = "=0.13.1", default-features = false }
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.6.0" }
bevy_mod_scripting_common = { path = "crates/bevy_mod_scripting_common", version = "0.6.0" }

[dev-dependencies]
bevy = { workspace = true, default-features = true }
Expand All @@ -84,14 +83,10 @@ rhai-rand = "0.1"
[workspace]
members = [
"crates/bevy_mod_scripting_core",
"crates/bevy_event_priority",
"crates/bevy_script_api",
"crates/bevy_mod_scripting_derive",
"crates/languages/bevy_mod_scripting_lua",
"crates/languages/bevy_mod_scripting_lua_derive",
"crates/languages/bevy_mod_scripting_rhai",
"crates/languages/bevy_mod_scripting_rhai_derive",
"crates/languages/bevy_mod_scripting_rune",
"crates/bevy_mod_scripting_common",
]
resolver = "2"
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]
Expand Down
3 changes: 1 addition & 2 deletions assets/scripts/event_recipients.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
function on_event(id)
print(string.format("on_event, script_id: %d, Handling:", script_id))
print(string.format("\t-> id: %d", id))
print(string.format("LUA: event_recipients::on_event(%d) called on entity: %s", id, entity))
end
5 changes: 3 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
unset RUSTUP_TOOLCHAIN
CURRENT_DIR=$(basename "$PWD")


if [[ "$CURRENT_DIR" == "bevy_api_gen" ]]; then
cargo clippy --all-targets --message-format=json
elif [[ "$CURRENT_DIR" == "macro_tests" ]]; then
cargo clippy --all-targets --message-format=json
else
cargo clippy --workspace --all-targets --message-format=json --features="lua54 lua_script_api rhai rhai_script_api teal rune bevy/file_watcher bevy/multi-threaded"
cargo clippy --all-targets --workspace --message-format=json --features="lua54 rhai teal rune bevy/file_watcher bevy/multi-threaded"
fi
7 changes: 6 additions & 1 deletion crates/bevy_api_gen/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ pub enum Command {

/// The name of the API, this will be passed to the `collect.rs` template, which by default will be used as the APIProvider name and the
/// title of the documentation.
#[arg(short, long, value_name = "NAME", default_value = "LuaBevyAPIProvider")]
#[arg(
short,
long,
value_name = "NAME",
default_value = "LuaBevyScriptingPlugin"
)]
api_name: String,
},
}
Expand Down
20 changes: 15 additions & 5 deletions crates/bevy_api_gen/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ fn main() {
(Some(root), true) => {
let feature_graph = FeatureGraph::from_metadata(&metadata, root);
let dependencies = feature_graph
.dependencies_for_features(args.features.as_ref(), !args.no_default_features)
.workspace_dependencies_for_features(
args.features.as_ref(),
!args.no_default_features,
)
.into_iter()
.map(|s| s.to_owned())
.collect::<Vec<String>>();
Expand Down Expand Up @@ -134,12 +137,13 @@ fn main() {

let temp_dir = tempdir::TempDir::new("bevy_api_gen_bootstrap")
.expect("Error occured when trying to acquire temp file");
let path = temp_dir.path().to_owned();

debug!("Temporary directory: {}", &temp_dir.path().display());
debug!("Temporary directory: {}", &path.display());

write_bootstrap_files(temp_dir.path());
write_bootstrap_files(&path);

let bootstrap_rlibs = build_bootstrap(temp_dir.path(), &plugin_target_dir.join("bootstrap"));
let bootstrap_rlibs = build_bootstrap(&path, &plugin_target_dir.join("bootstrap"));

if bootstrap_rlibs.len() == BOOTSTRAP_DEPS.len() {
let extern_args = bootstrap_rlibs
Expand Down Expand Up @@ -169,7 +173,13 @@ fn main() {
rustc_plugin::cli_main(BevyAnalyzer);

// just making sure the temp dir lives until everything is done
drop(temp_dir);
if let Err(err) = std::fs::remove_dir_all(&path) {
log::error!(
"Error occured when trying to delete temporary directory: `{}`. {}",
path.to_string_lossy(),
err
)
}
}

/// Build bootstrap files if they don't exist
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_api_gen/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl rustc_driver::Callbacks for BevyAnalyzerCallbacks {
if !continue_ {
break;
}
trace!("Finished pass, continuing");
}
});
rustc_driver::Compilation::Continue
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_api_gen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub(crate) struct FunctionContext {
pub(crate) def_id: DefId,
pub(crate) has_self: bool,
pub(crate) is_unsafe: bool,
pub(crate) trait_did: Option<DefId>,
pub(crate) trait_and_impl_did: Option<(DefId, DefId)>,
/// strategies for input and output (last element is the output)
pub(crate) reflection_strategies: Vec<ReflectionStrategy>,
}
Expand Down
43 changes: 31 additions & 12 deletions crates/bevy_api_gen/src/feature_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub struct FeatureGraph {
}

impl FeatureGraph {
/// Works out which dependencies are enabled by the given feature, including transitive relations between crates
/// Works out which dependencies are enabled (in the workspace) are enabled by the given feature, including transitive relations between crates
/// includes normal dependencies as well
pub fn dependencies_for_features(
pub fn workspace_dependencies_for_features(
&self,
features: &[String],
include_default: bool,
Expand All @@ -70,14 +70,24 @@ impl FeatureGraph {
.crates
.iter()
.find(|c| c.name == self.workspace_root)
.unwrap();
.unwrap_or_else(|| {
panic!(
"Failed to find workspace root `{}`, in crates in this workspace.",
self.workspace_root
)
});
let mut buffer = Default::default();
self.dependencies_for_features_on_crate(root, features, include_default, &mut buffer);
self.workspace_dependencies_for_features_on_crate(
root,
features,
include_default,
&mut buffer,
);

buffer.iter().map(|c| c.name.as_str()).collect()
}

fn dependencies_for_features_on_crate<'a>(
fn workspace_dependencies_for_features_on_crate<'a>(
&'a self,
crate_: &'a Crate,
features: &[String],
Expand Down Expand Up @@ -107,15 +117,19 @@ impl FeatureGraph {
enable_optional,
} => {
if *enable_optional {
deps.entry(self.crates.iter().find(|c| c.name == *dependency).unwrap())
.or_default()
.push(feature.to_owned());
if let Some(workspace_dep) =
self.crates.iter().find(|c| c.name == *dependency)
{
deps.entry(workspace_dep)
.or_default()
.push(feature.to_owned());
}
}
}
FeatureEffect::EnableOptionalDep(d) => {
_ = deps
.entry(self.crates.iter().find(|c| c.name == *d).unwrap())
.or_default()
if let Some(workspace_dep) = self.crates.iter().find(|c| c.name == *d) {
deps.entry(workspace_dep).or_default();
}
}
_ => unreachable!(),
};
Expand All @@ -131,7 +145,12 @@ impl FeatureGraph {
// repeat for all dependencies recursively
for (dep, features) in deps.iter() {
buffer.insert(dep);
self.dependencies_for_features_on_crate(dep, features, include_default, buffer);
self.workspace_dependencies_for_features_on_crate(
dep,
features,
include_default,
buffer,
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_api_gen/src/import_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl std::fmt::Debug for ImportPathElement {
/// Because we do not need ALL the items in the crate, we start searching from the item itself and traverse up the tree.
/// Caches results for already found items.
pub(crate) struct ImportPathFinder<'tcx> {
tcx: TyCtxt<'tcx>,
pub(crate) tcx: TyCtxt<'tcx>,
pub(crate) cache: IndexMap<DefId, Vec<Vec<ImportPathElement>>>,
pub(crate) include_private_paths: bool,
pub(crate) import_path_processor: Option<Box<dyn Fn(&str) -> String>>,
Expand Down
22 changes: 22 additions & 0 deletions crates/bevy_api_gen/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ impl MetaLoader {
self.meta_for_retry(crate_name, 3)
}

/// Searches the given meta sources in order for the provided DefPathHash, once a meta file containing this hash is found
/// the search stops and returns true, if no meta file is found containing the hash, false is returned
///
/// if a curr_source argument is provided, the search will skip this source as it is assumed that the current crate is still being compiled and not meta file for it exists yet
pub fn one_of_meta_files_contains(
&self,
meta_sources: &[&str],
curr_source: Option<&str>,
target_def_path_hash: DefPathHash,
) -> bool {
let meta = match meta_sources
.iter()
.filter(|s| curr_source.is_none() || curr_source.is_some_and(|cs| cs == **s))
.find_map(|s| self.meta_for(s))
{
Some(meta) => meta,
None => return false, // TODO: is it possible we get false negatives here ? perhaps due to parallel compilation ? or possibly because of dependency order
};

meta.contains_def_path_hash(target_def_path_hash)
}

fn meta_for_retry(&self, crate_name: &str, _try_attempts: usize) -> Option<Meta> {
let meta = self
.meta_dirs
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_api_gen/src/passes/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(crate) fn codegen(ctxt: &mut BevyCtxt<'_>, args: &Args) -> bool {
.expect("Failed to render crate artifact");

file.flush().unwrap();
log::trace!("Written files");

true
}
Expand Down
Loading