Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Add Instance:IsA() #52

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased Changes

* Added Instance:IsA() ([#52](https://github.com/rojo-rbx/remodel/pull/52))
## 0.9.1 (2021-10-11)
* Updated to latest rbx-dom libraries.
* Increased Roblox API request timeout from 30 seconds to 3 minutes. ([#63])
Expand Down
37 changes: 37 additions & 0 deletions src/roblox_api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,40 @@ impl LuaInstance {
}
}

fn is_a(&self, class_name: &str) -> rlua::Result<bool> {
let tree = self.tree.lock().unwrap();

let instance = tree
.get_by_ref(self.id)
.ok_or_else(|| rlua::Error::external("Cannot call IsA() on a destroyed instance"))?;

if class_name == "Instance" || instance.class == class_name {
return Ok(true);
}

let database = rbx_reflection_database::get();
let mut superclass_name = instance.class.as_str();

loop {
if class_name == superclass_name {
break Ok(true);
}

let descriptor = database.classes.get(superclass_name).ok_or_else(|| {
rlua::Error::external(format!(
"Unable to obtain class {} from reflection database",
&superclass_name
))
})?;

if let Some(super_class) = &descriptor.superclass {
superclass_name = super_class;
} else {
break Ok(false);
}
}
}

fn get_class_name<'lua>(
&self,
context: rlua::Context<'lua>,
Expand Down Expand Up @@ -395,6 +429,9 @@ impl UserData for LuaInstance {
this.find_first_child_of_class(&class)
});

methods.add_method("IsA", |_context, this, class_name: String| {
this.is_a(&class_name)
});
methods.add_method("GetFullName", |_context, this, _args: ()| {
this.get_full_name()
});
Expand Down
15 changes: 15 additions & 0 deletions test-scripts/is-a.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local folder = Instance.new("Folder")

assert(folder:IsA("Folder"))
assert(folder:IsA("Instance"))
assert(not folder:IsA("BasePart"))

local spawnLocation = Instance.new("SpawnLocation")

assert(spawnLocation:IsA("SpawnLocation"))
assert(spawnLocation:IsA("Part"))
assert(spawnLocation:IsA("FormFactorPart"))
assert(spawnLocation:IsA("BasePart"))
assert(spawnLocation:IsA("PVInstance"))
assert(spawnLocation:IsA("Instance"))
assert(not spawnLocation:IsA("Folder"))