Skip to content

Commit 29b9e6f

Browse files
committed
Finish generic implementations for insert,pop,clear etc..
1 parent d11ff2a commit 29b9e6f

File tree

8 files changed

+723
-104
lines changed

8 files changed

+723
-104
lines changed

assets/scripts/bevy_api.lua

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ function on_event()
3838
print(table_to_string(comp.vec_of_usize))
3939
comp.vec_of_usize = comp.vec_of_usize
4040
print(table_to_string(comp.vec_of_usize))
41+
comp.vec_of_usize:insert(1, 42)
42+
print(table_to_string(comp.vec_of_usize))
43+
44+
print("\nmap")
45+
-- print(comp.map_of_strings["key"])
46+
comp.map_of_strings:insert("key2", "value")
47+
-- print(comp.map_of_strings["key2"])
48+
4149

4250

4351
print("============")
@@ -75,42 +83,31 @@ function on_event()
7583
comp.vec_of_option_bools:insert(1,nil)
7684
print("comp.vec_of_option_bools after insert: ", table_to_string(comp.vec_of_option_bools))
7785

86+
87+
7888
print("comp.vec_of_option_bools before push: ", table_to_string(comp.vec_of_option_bools))
7989
comp.vec_of_option_bools:push(false)
8090
print("comp.vec_of_option_bools after push: ", table_to_string(comp.vec_of_option_bools))
8191

8292
print("comp.vec_of_option_bools len after push: ", #comp.vec_of_option_bools)
8393

8494
print("comp.vec_of_option_bools before pop: ", table_to_string(comp.vec_of_option_bools))
85-
print(comp.vec_of_option_bools:pop())
95+
print(comp.vec_of_option_bools:pop():print_value())
8696
print("comp.vec_of_option_bools after pop: ", table_to_string(comp.vec_of_option_bools))
8797

8898
print("the pairs inside comp.vec_of_option_bools: ")
8999
for k,v in pairs(comp.vec_of_option_bools) do
90100
print(string.format(" - %s:%s",k,v))
91101
end
92102

103+
93104
comp.vec_of_option_bools:clear()
94105
print("comp.vec_of_option_bools after clear: ", table_to_string(comp.vec_of_option_bools))
95-
96106
print("comp.vec_of_option_bools len after clear: ", #comp.vec_of_option_bools)
97-
print("============")
98-
99-
print("comp.option_vec_of_bools before: ", table_to_string(comp.option_vec_of_bools))
100-
print(comp.option_vec_of_bools:pop())
101-
print("comp.option_vec_of_bools after pop: ", table_to_string(comp.option_vec_of_bools))
102-
103-
104-
print("comp.option_vec_of_bools len after pop: ", #comp.option_vec_of_bools)
105-
106-
print("the pairs inside comp.option_vec_of_bools: ")
107-
for k,v in pairs(comp.option_vec_of_bools) do
108-
print(string.format(" - %s:%s",k,v))
109-
end
110107

111108
print("============")
112109

113-
local complex_vec_op = Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3.x_axis
110+
local complex_vec_op = Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3[1]
114111
print("(0,1,0).any_orthonormal_vector() + mat3.x_axis is: ", complex_vec_op)
115112

116113
local new_mat3 = Mat3.from_cols(Vec3.new(1,0,0),Vec3.new(0,1,0),Vec3.new(0,0,-1))
@@ -121,8 +118,11 @@ function on_event()
121118
comp.f32 = comp.f32 + comp.f32 + comp.vec2:min_element()
122119
comp.vec2 = Vec2.new(2,1)
123120
comp.quat = Quat.from_xyzw(3,2,1,4)
124-
comp.mat3.x_axis = Vec3.new(69,69,69)
121+
comp.mat3[1] = Vec3.new(69,69,69)
125122

123+
124+
world:exit()
125+
do return end
126126
print("============")
127127

128128
-- this is an example of something impossible to achieve with plain bevy reflection under the hood

crates/bevy_mod_scripting_core/src/bindings/allocator.rs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,54 @@ impl ReflectAllocationId {
1414
}
1515
}
1616

17+
/// Pointer which owns the value it points to, and will deallocate it when dropped
18+
#[derive(Debug)]
19+
pub struct OwningPtr<T: ?Sized> {
20+
ptr: *mut T,
21+
}
22+
23+
impl<T: ?Sized> OwningPtr<T> {
24+
/// Creates a new OwningPtr from a raw pointer
25+
/// # Safety
26+
/// The pointer must come from a Box::leak call, and no more than one OwningPtr can exist for a given pointer
27+
pub unsafe fn new(ptr: *mut T) -> Self {
28+
Self { ptr }
29+
}
30+
}
31+
32+
impl<T: ?Sized> Drop for OwningPtr<T> {
33+
fn drop(&mut self) {
34+
unsafe {
35+
// Safety: we own the pointer, only one OwningPtr can exist for a given pointer
36+
let _ = Box::from_raw(self.ptr);
37+
}
38+
}
39+
}
40+
41+
// yikes, the indirection. I need this to store boxed values too though
1742
#[derive(Clone, Debug)]
18-
pub struct ReflectAllocation(pub(self) Arc<UnsafeCell<dyn PartialReflect>>);
43+
pub enum ReflectAllocation {
44+
Double(Arc<OwningPtr<dyn PartialReflect>>),
45+
Single(Arc<UnsafeCell<dyn PartialReflect>>),
46+
}
1947

2048
unsafe impl Send for ReflectAllocation {}
2149
unsafe impl Sync for ReflectAllocation {}
2250

2351
impl ReflectAllocation {
2452
pub fn get_ptr(&self) -> *mut dyn PartialReflect {
25-
self.0.get()
53+
match self {
54+
ReflectAllocation::Double(v) => v.ptr,
55+
ReflectAllocation::Single(v) => v.get(),
56+
}
2657
}
2758
pub fn new(value: Arc<UnsafeCell<dyn PartialReflect>>) -> Self {
28-
Self(value)
59+
Self::Single(value)
60+
}
61+
62+
pub fn new_boxed(value: Box<dyn PartialReflect>) -> Self {
63+
let ptr = Box::leak::<'static>(value);
64+
Self::Double(Arc::new(unsafe { OwningPtr::new(ptr) }))
2965
}
3066
}
3167

@@ -62,6 +98,21 @@ impl ReflectAllocator {
6298
(id, value)
6399
}
64100

101+
pub fn allocate_boxed(
102+
&mut self,
103+
value: Box<dyn PartialReflect>,
104+
) -> (ReflectAllocationId, ReflectAllocation) {
105+
let type_id = value.get_represented_type_info().map(|i| i.type_id());
106+
107+
let id = ReflectAllocationId(self.allocations.len());
108+
let value = ReflectAllocation::new_boxed(value);
109+
self.allocations.insert(id, value.clone());
110+
if let Some(type_id) = type_id {
111+
self.types.insert(id, type_id);
112+
}
113+
(id, value)
114+
}
115+
65116
// /// Moves the given boxed [`PartialReflect`] value into the allocator, returning an [`AllocationId`] which can be used to access it later
66117
// pub fn allocate_boxed(
67118
// &mut self,
@@ -105,7 +156,10 @@ impl ReflectAllocator {
105156
/// Runs a garbage collection pass on the allocations, removing any allocations which have no more strong references
106157
/// Needs to be run periodically to prevent memory leaks
107158
pub fn clean_garbage_allocations(&mut self) {
108-
self.allocations.retain(|_, v| Arc::strong_count(&v.0) > 1);
159+
self.allocations.retain(|_, v| match v {
160+
ReflectAllocation::Single(v) => Arc::strong_count(v) > 1,
161+
ReflectAllocation::Double(v) => Arc::strong_count(v) > 1,
162+
});
109163
}
110164
}
111165

0 commit comments

Comments
 (0)