Skip to content

Commit e347a8b

Browse files
committed
improve type data, and add iterator to reflect ref
1 parent 60857bb commit e347a8b

File tree

17 files changed

+1306
-881
lines changed

17 files changed

+1306
-881
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
],
2525
"rust-analyzer.showUnlinkedFileNotification": false,
2626
// "rust-analyzer.semanticHighlighting.operator.enable": false
27-
}
27+
}

assets/scripts/bevy_api.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ function on_event()
1919
local comp = world:get_component(entity, my_component_type)
2020
print("Before script: ", comp)
2121

22-
22+
print("\noption")
2323
print(comp.option_usize)
2424
comp.option_usize = 69
2525
print(comp.option_usize)
2626
comp.option_usize = nil
2727
print(comp.option_usize)
2828

29-
print("vec")
30-
print(comp.vec_of_usize)
31-
print(comp.vec_of_usize[2])
32-
comp.vec_of_usize[2] = 69
29+
print("\nvec")
30+
print(table_to_string(comp.vec_of_usize))
31+
comp.vec_of_usize = {42,69,72}
32+
comp.vec_of_usize[1] = 0
3333
print(comp.vec_of_usize[2])
34-
world:exit()
34+
print(table_to_string(comp.vec_of_usize))
35+
comp.vec_of_usize = {}
36+
print(table_to_string(comp.vec_of_usize))
37+
comp.vec_of_usize = comp.vec_of_usize2
38+
print(table_to_string(comp.vec_of_usize))
39+
comp.vec_of_usize = comp.vec_of_usize
40+
print(table_to_string(comp.vec_of_usize))
41+
3542

3643
print("============")
3744

crates/bevy_mod_scripting_core/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ path = "src/lib.rs"
1919
# if enabled enables documentation updating in optimized builds
2020
doc_always = []
2121

22+
# if enabled enables some common mlua trait implementations
23+
mlua_impls = ["mlua"]
2224

2325
[dependencies]
26+
mlua = { version = "0.9", optional = true }
27+
2428
bevy = { workspace = true, default-features = false, features = ["bevy_asset"] }
2529
thiserror = "1.0.31"
2630
paste = "1.0.7"
2731
parking_lot = "0.12.1"
2832
lockable = "0.0.8"
2933
smallvec = "1.11"
30-
34+
itertools = "0.13"
3135
[dev-dependencies]
3236
test_utils = { workspace = true }

crates/bevy_mod_scripting_core/src/bindings/allocator.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,28 @@ impl ReflectAllocator {
6262
(id, value)
6363
}
6464

65-
/// Moves the given boxed [`PartialReflect`] value into the allocator, returning an [`AllocationId`] which can be used to access it later
66-
pub fn allocate_boxed(
67-
&mut self,
68-
existing: Box<dyn PartialReflect>,
69-
) -> (ReflectAllocationId, ReflectAllocation) {
70-
let type_id = existing.get_represented_type_info().map(|i| i.type_id());
71-
let id = ReflectAllocationId(self.allocations.len());
72-
73-
let raw_ptr = Box::into_raw(existing);
74-
// Safety:
75-
// - we are the only ones to have access to this value since we have the Box
76-
// - UnsafeCell is repr(transparent), meaning we can safely transmute between it and the trait object
77-
// TODO: I don't think we can use this, because from_raw has a pre-condition that requires the pointer to have been an arc before
78-
let arc: Arc<UnsafeCell<dyn PartialReflect>> =
79-
unsafe { Arc::from_raw(raw_ptr as *const _) };
80-
let allocation = ReflectAllocation::new(arc);
81-
self.allocations.insert(id, allocation.clone());
82-
if let Some(type_id) = type_id {
83-
self.types.insert(id, type_id);
84-
}
85-
(id, allocation)
86-
}
65+
// /// Moves the given boxed [`PartialReflect`] value into the allocator, returning an [`AllocationId`] which can be used to access it later
66+
// pub fn allocate_boxed(
67+
// &mut self,
68+
// existing: Box<dyn PartialReflect>,
69+
// ) -> (ReflectAllocationId, ReflectAllocation) {
70+
// let type_id = existing.get_represented_type_info().map(|i| i.type_id());
71+
// let id = ReflectAllocationId(self.allocations.len());
72+
73+
// let raw_ptr = Box::into_raw(existing);
74+
// // Safety:
75+
// // - we are the only ones to have access to this value since we have the Box
76+
// // - UnsafeCell is repr(transparent), meaning we can safely transmute between it and the trait object
77+
// // TODO: I don't think we can use this, because from_raw has a pre-condition that requires the pointer to have been an arc before
78+
// let arc: Arc<UnsafeCell<dyn PartialReflect>> =
79+
// unsafe { Arc::from_raw(raw_ptr as *const _) };
80+
// let allocation = ReflectAllocation::new(arc);
81+
// self.allocations.insert(id, allocation.clone());
82+
// if let Some(type_id) = type_id {
83+
// self.types.insert(id, type_id);
84+
// }
85+
// (id, allocation)
86+
// }
8787

8888
pub fn get(&self, id: ReflectAllocationId) -> Option<ReflectAllocation> {
8989
self.allocations.get(&id).cloned()

crates/bevy_mod_scripting_core/src/bindings/proxy.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,7 @@ where
186186
) -> ScriptResult<Self::Output<'o>> {
187187
let reflect_ref: &ReflectReference = self.0.as_ref();
188188
let access = reflect_ref.base.base_id.get_reflect_access_id();
189-
let access = guard
190-
.get_access_timeout(access, DEFAULT_TIMEOUT, DEFAULT_INTERVAL)
191-
.ok_or_else(|| {
192-
ScriptError::new_reflection_error(format!(
193-
"Could not unproxy type: `{}`. Aliasing access.",
194-
std::any::type_name::<T>()
195-
))
196-
})?;
189+
let access = guard.get_access_timeout(access, DEFAULT_TIMEOUT, DEFAULT_INTERVAL);
197190
let out = reflect_ref.reflect(
198191
guard.as_unsafe_world_cell(),
199192
&access,
@@ -248,14 +241,7 @@ where
248241
) -> ScriptResult<()> {
249242
let reflect_ref: &ReflectReference = self.0.as_ref();
250243
let access = reflect_ref.base.base_id.get_reflect_access_id();
251-
let access = guard
252-
.get_access_timeout(access, DEFAULT_TIMEOUT, DEFAULT_INTERVAL)
253-
.ok_or_else(|| {
254-
ScriptError::new_reflection_error(format!(
255-
"Could not unproxy type: `{}`. Aliasing access.",
256-
std::any::type_name::<T>()
257-
))
258-
})?;
244+
let access = guard.get_access_timeout(access, DEFAULT_TIMEOUT, DEFAULT_INTERVAL);
259245
accesses.push(access);
260246
Ok(())
261247
}
@@ -309,14 +295,7 @@ where
309295
) -> ScriptResult<()> {
310296
let reflect_ref: &ReflectReference = self.0.as_ref();
311297
let access = reflect_ref.base.base_id.get_reflect_access_id();
312-
let access = guard
313-
.get_access_timeout(access, DEFAULT_TIMEOUT, DEFAULT_INTERVAL)
314-
.ok_or_else(|| {
315-
ScriptError::new_reflection_error(format!(
316-
"Could not unproxy type: `{}`. Aliasing access.",
317-
std::any::type_name::<T>()
318-
))
319-
})?;
298+
let access = guard.get_access_timeout(access, DEFAULT_TIMEOUT, DEFAULT_INTERVAL);
320299
accesses.push(access);
321300
Ok(())
322301
}

0 commit comments

Comments
 (0)