Skip to content

Commit be019f2

Browse files
committed
LibJS: Add a PropertyName class that represents a string or a number
Now that we have two separate storages for Object properties depending on what kind of index they have, it's nice to have an abstraction that still allows us to say "here's a property name". We use PropertyName to always choose the optimal storage path directly while interpreting the AST. :^)
1 parent 90ba014 commit be019f2

File tree

5 files changed

+91
-4
lines changed

5 files changed

+91
-4
lines changed

Libraries/LibJS/AST.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,13 +855,17 @@ void MemberExpression::dump(int indent) const
855855
m_property->dump(indent + 1);
856856
}
857857

858-
FlyString MemberExpression::computed_property_name(Interpreter& interpreter) const
858+
PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
859859
{
860860
if (!is_computed()) {
861861
ASSERT(m_property->is_identifier());
862-
return static_cast<const Identifier&>(*m_property).string();
862+
return PropertyName(static_cast<const Identifier&>(*m_property).string());
863863
}
864-
return m_property->execute(interpreter).to_string();
864+
auto index = m_property->execute(interpreter);
865+
// FIXME: What about non-integer numbers tho.
866+
if (index.is_number())
867+
return PropertyName(index.to_i32());
868+
return PropertyName(index.to_string());
865869
}
866870

867871
Value MemberExpression::execute(Interpreter& interpreter) const

Libraries/LibJS/AST.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <AK/String.h>
3434
#include <AK/Vector.h>
3535
#include <LibJS/Forward.h>
36+
#include <LibJS/Runtime/PropertyName.h>
3637
#include <LibJS/Runtime/Value.h>
3738

3839
namespace JS {
@@ -672,7 +673,7 @@ class MemberExpression final : public Expression {
672673
const Expression& object() const { return *m_object; }
673674
const Expression& property() const { return *m_property; }
674675

675-
FlyString computed_property_name(Interpreter&) const;
676+
PropertyName computed_property_name(Interpreter&) const;
676677

677678
private:
678679
virtual bool is_member_expression() const override { return true; }

Libraries/LibJS/Runtime/Object.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ Optional<Value> Object::get(const FlyString& property_name) const
150150
return {};
151151
}
152152

153+
Optional<Value> Object::get(PropertyName property_name) const
154+
{
155+
if (property_name.is_number())
156+
return get_by_index(property_name.as_number());
157+
return get(property_name.as_string());
158+
}
159+
153160
void Object::put_by_index(i32 property_index, Value value)
154161
{
155162
if (property_index < 0)
@@ -189,6 +196,13 @@ void Object::put(const FlyString& property_name, Value value)
189196
put_own_property(*this, property_name, value);
190197
}
191198

199+
void Object::put(PropertyName property_name, Value value)
200+
{
201+
if (property_name.is_number())
202+
return put_by_index(property_name.as_number(), value);
203+
return put(property_name.as_string(), value);
204+
}
205+
192206
void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
193207
{
194208
auto* function = heap().allocate<NativeFunction>(move(native_function));

Libraries/LibJS/Runtime/Object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <LibJS/Forward.h>
3232
#include <LibJS/Runtime/Cell.h>
3333
#include <LibJS/Runtime/PrimitiveString.h>
34+
#include <LibJS/Runtime/PropertyName.h>
3435
#include <LibJS/Runtime/Value.h>
3536

3637
namespace JS {
@@ -45,9 +46,11 @@ class Object : public Cell {
4546

4647
Optional<Value> get_by_index(i32 property_index) const;
4748
Optional<Value> get(const FlyString& property_name) const;
49+
Optional<Value> get(PropertyName) const;
4850

4951
void put_by_index(i32 property_index, Value);
5052
void put(const FlyString& property_name, Value);
53+
void put(PropertyName, Value);
5154

5255
virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const;
5356
virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#pragma once
28+
29+
#include <AK/FlyString.h>
30+
31+
namespace JS {
32+
33+
class PropertyName {
34+
public:
35+
enum class Type {
36+
Number,
37+
String,
38+
};
39+
40+
explicit PropertyName(i32 index)
41+
: m_type(Type::Number)
42+
, m_number(index)
43+
{
44+
ASSERT(m_number >= 0);
45+
}
46+
47+
explicit PropertyName(const FlyString& string)
48+
: m_type(Type::String)
49+
, m_string(string)
50+
{
51+
}
52+
53+
bool is_number() const { return m_type == Type::Number; }
54+
bool is_string() const { return m_type == Type::String; }
55+
56+
i32 as_number() const { return m_number; }
57+
const FlyString& as_string() const { return m_string; }
58+
59+
private:
60+
Type m_type;
61+
FlyString m_string;
62+
i32 m_number { 0 };
63+
};
64+
65+
}

0 commit comments

Comments
 (0)