mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:18:11 +00:00
LibJS: Implement the "instanceof" operator
This operator walks the prototype chain of the RHS value and looks for a "prototype" property with the same value as the prototype of the LHS. This is pretty cool. :^)
This commit is contained in:
parent
37fe16a99c
commit
a3d92b1210
6 changed files with 41 additions and 1 deletions
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
@ -253,6 +254,27 @@ Value eq(Value lhs, Value rhs)
|
|||
return Value(false);
|
||||
}
|
||||
|
||||
Value instance_of(Value lhs, Value rhs)
|
||||
{
|
||||
if (!lhs.is_object() || !rhs.is_object())
|
||||
return Value(false);
|
||||
|
||||
auto* instance_prototype = lhs.as_object()->prototype();
|
||||
|
||||
if (!instance_prototype)
|
||||
return Value(false);
|
||||
|
||||
for (auto* constructor_object = rhs.as_object(); constructor_object; constructor_object = constructor_object->prototype()) {
|
||||
auto prototype_property = constructor_object->get_own_property(*constructor_object, "prototype");
|
||||
if (!prototype_property.has_value())
|
||||
continue;
|
||||
if (prototype_property.value().is_object() && prototype_property.value().as_object() == instance_prototype)
|
||||
return Value(true);
|
||||
}
|
||||
|
||||
return Value(false);
|
||||
}
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, const Value& value)
|
||||
{
|
||||
return stream << value.to_string();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue