mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:37:45 +00:00
LibJS: Use FlyString for identifiers
This makes variable and property lookups a lot faster since comparing two FlyStrings is O(1).
This commit is contained in:
parent
4f72f6b886
commit
cccbe43056
19 changed files with 67 additions and 57 deletions
|
@ -59,7 +59,7 @@ void Array::visit_children(Cell::Visitor& visitor)
|
|||
visitor.visit(element);
|
||||
}
|
||||
|
||||
Optional<Value> Array::get_own_property(const String& property_name) const
|
||||
Optional<Value> Array::get_own_property(const FlyString& property_name) const
|
||||
{
|
||||
bool ok;
|
||||
i32 index = property_name.to_int(ok);
|
||||
|
@ -70,7 +70,7 @@ Optional<Value> Array::get_own_property(const String& property_name) const
|
|||
return Object::get_own_property(property_name);
|
||||
}
|
||||
|
||||
bool Array::put_own_property(const String& property_name, Value value)
|
||||
bool Array::put_own_property(const FlyString& property_name, Value value)
|
||||
{
|
||||
bool ok;
|
||||
i32 index = property_name.to_int(ok);
|
||||
|
|
|
@ -45,8 +45,8 @@ private:
|
|||
virtual const char* class_name() const override { return "Array"; }
|
||||
virtual void visit_children(Cell::Visitor&) override;
|
||||
virtual bool is_array() const override { return true; }
|
||||
virtual Optional<Value> get_own_property(const String& property_name) const override;
|
||||
virtual bool put_own_property(const String& property_name, Value) override;
|
||||
virtual Optional<Value> get_own_property(const FlyString& property_name) const override;
|
||||
virtual bool put_own_property(const FlyString& property_name, Value) override;
|
||||
|
||||
Vector<Value> m_elements;
|
||||
};
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <LibJS/Runtime/ConsoleObject.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <LibJS/Runtime/MathObject.h>
|
||||
|
||||
namespace JS {
|
||||
|
|
|
@ -44,7 +44,7 @@ Object::~Object()
|
|||
{
|
||||
}
|
||||
|
||||
Optional<Value> Object::get_own_property(const String& property_name) const
|
||||
Optional<Value> Object::get_own_property(const FlyString& property_name) const
|
||||
{
|
||||
auto value_here = m_properties.get(property_name);
|
||||
if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property())
|
||||
|
@ -52,7 +52,7 @@ Optional<Value> Object::get_own_property(const String& property_name) const
|
|||
return value_here;
|
||||
}
|
||||
|
||||
bool Object::put_own_property(const String& property_name, Value value)
|
||||
bool Object::put_own_property(const FlyString& property_name, Value value)
|
||||
{
|
||||
auto value_here = m_properties.get(property_name);
|
||||
if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) {
|
||||
|
@ -63,7 +63,7 @@ bool Object::put_own_property(const String& property_name, Value value)
|
|||
return true;
|
||||
}
|
||||
|
||||
Value Object::get(const String& property_name) const
|
||||
Value Object::get(const FlyString& property_name) const
|
||||
{
|
||||
const Object* object = this;
|
||||
while (object) {
|
||||
|
@ -75,7 +75,7 @@ Value Object::get(const String& property_name) const
|
|||
return js_undefined();
|
||||
}
|
||||
|
||||
void Object::put(const String& property_name, Value value)
|
||||
void Object::put(const FlyString& property_name, Value value)
|
||||
{
|
||||
Object* object = this;
|
||||
while (object) {
|
||||
|
@ -93,12 +93,12 @@ void Object::put(const String& property_name, Value value)
|
|||
put_own_property(property_name, value);
|
||||
}
|
||||
|
||||
void Object::put_native_function(String property_name, AK::Function<Value(Object*, Vector<Value>)> native_function)
|
||||
void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)> native_function)
|
||||
{
|
||||
put(property_name, heap().allocate<NativeFunction>(move(native_function)));
|
||||
}
|
||||
|
||||
void Object::put_native_property(String property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter)
|
||||
void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter)
|
||||
{
|
||||
put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ void Object::visit_children(Cell::Visitor& visitor)
|
|||
visitor.visit(it.value);
|
||||
}
|
||||
|
||||
bool Object::has_own_property(const String& property_name) const
|
||||
bool Object::has_own_property(const FlyString& property_name) const
|
||||
{
|
||||
return m_properties.get(property_name).has_value();
|
||||
}
|
||||
|
|
|
@ -40,14 +40,14 @@ public:
|
|||
Object();
|
||||
virtual ~Object();
|
||||
|
||||
Value get(const String& property_name) const;
|
||||
void put(const String& property_name, Value);
|
||||
Value get(const FlyString& property_name) const;
|
||||
void put(const FlyString& property_name, Value);
|
||||
|
||||
virtual Optional<Value> get_own_property(const String& property_name) const;
|
||||
virtual bool put_own_property(const String& property_name, Value);
|
||||
virtual Optional<Value> get_own_property(const FlyString& property_name) const;
|
||||
virtual bool put_own_property(const FlyString& property_name, Value);
|
||||
|
||||
void put_native_function(String property_name, AK::Function<Value(Object*, Vector<Value>)>);
|
||||
void put_native_property(String property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter);
|
||||
void put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)>);
|
||||
void put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter);
|
||||
|
||||
virtual bool is_array() const { return false; }
|
||||
virtual bool is_function() const { return false; }
|
||||
|
@ -62,7 +62,7 @@ public:
|
|||
const Object* prototype() const { return m_prototype; }
|
||||
void set_prototype(Object* prototype) { m_prototype = prototype; }
|
||||
|
||||
bool has_own_property(const String& property_name) const;
|
||||
bool has_own_property(const FlyString& property_name) const;
|
||||
enum class PreferredType {
|
||||
Default,
|
||||
String,
|
||||
|
@ -74,7 +74,7 @@ public:
|
|||
virtual Value to_string() const;
|
||||
|
||||
private:
|
||||
HashMap<String, Value> m_properties;
|
||||
HashMap<FlyString, Value> m_properties;
|
||||
Object* m_prototype { nullptr };
|
||||
};
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
ScriptFunction::ScriptFunction(const ScopeNode& body, Vector<String> parameters)
|
||||
ScriptFunction::ScriptFunction(const ScopeNode& body, Vector<FlyString> parameters)
|
||||
: m_body(body)
|
||||
, m_parameters(move(parameters))
|
||||
{
|
||||
|
|
|
@ -32,11 +32,11 @@ namespace JS {
|
|||
|
||||
class ScriptFunction final : public Function {
|
||||
public:
|
||||
ScriptFunction(const ScopeNode& body, Vector<String> parameters = {});
|
||||
ScriptFunction(const ScopeNode& body, Vector<FlyString> parameters = {});
|
||||
virtual ~ScriptFunction();
|
||||
|
||||
const ScopeNode& body() const { return m_body; }
|
||||
const Vector<String>& parameters() const { return m_parameters; };
|
||||
const Vector<FlyString>& parameters() const { return m_parameters; };
|
||||
|
||||
virtual Value call(Interpreter&, const Vector<Value>&) override;
|
||||
|
||||
|
@ -45,7 +45,7 @@ private:
|
|||
virtual const char* class_name() const override { return "ScriptFunction"; }
|
||||
|
||||
NonnullRefPtr<ScopeNode> m_body;
|
||||
const Vector<String> m_parameters;
|
||||
const Vector<FlyString> m_parameters;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue