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

IsA, ClassName & Parent should work if an instance is already destroyed #271

Merged
merged 2 commits into from
Nov 5, 2024
Merged
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
12 changes: 6 additions & 6 deletions crates/lune-roblox/src/instance/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ pub fn add_methods<'lua, M: LuaUserDataMethods<'lua, Instance>>(m: &mut M) {
},
);
m.add_method("IsA", |_, this, class_name: String| {
ensure_not_destroyed(this)?;
Ok(class_is_a(&this.class_name, class_name).unwrap_or(false))
});
m.add_method(
Expand Down Expand Up @@ -217,19 +216,20 @@ fn instance_property_get<'lua>(
this: &Instance,
prop_name: String,
) -> LuaResult<LuaValue<'lua>> {
ensure_not_destroyed(this)?;

match prop_name.as_str() {
"ClassName" => return this.get_class_name().into_lua(lua),
"Name" => {
return this.get_name().into_lua(lua);
}
"Parent" => {
return this.get_parent().into_lua(lua);
}
_ => {}
}

ensure_not_destroyed(this)?;

if prop_name.as_str() == "Name" {
return this.get_name().into_lua(lua);
}

if let Some(info) = find_property_info(&this.class_name, &prop_name) {
if let Some(prop) = this.get_property(&prop_name) {
if let DomValue::Enum(enum_value) = prop {
Expand Down
5 changes: 1 addition & 4 deletions crates/lune-roblox/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,7 @@ impl Instance {
pub fn get_parent(&self) -> Option<Instance> {
let dom = INTERNAL_DOM.lock().expect("Failed to lock document");

let parent_ref = dom
.get_by_ref(self.dom_ref)
.expect("Failed to find instance in document")
.parent();
let parent_ref = dom.get_by_ref(self.dom_ref)?.parent();

if parent_ref == dom.root_ref() {
None
Expand Down
8 changes: 2 additions & 6 deletions tests/roblox/instance/methods/ClearAllChildren.luau
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ assert(not pcall(function()
return child1.Name
end))

assert(not pcall(function()
return child1.Parent
end))
assert(not child1.Parent)

assert(not pcall(function()
return child2.Name
end))

assert(not pcall(function()
return child2.Parent
end))
assert(not child2.Parent)
12 changes: 3 additions & 9 deletions tests/roblox/instance/methods/Destroy.luau
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@ assert(not pcall(function()
return root.Name
end))

assert(not pcall(function()
return root.Parent
end))
assert(not root.Parent)

assert(not pcall(function()
return child.Name
end))

assert(not pcall(function()
return child.Parent
end))
assert(not child.Parent)

assert(not pcall(function()
return descendant.Name
end))

assert(not pcall(function()
return descendant.Parent
end))
assert(not descendant.Parent)