Skip to content

Commit 0efa98a

Browse files
committed
LibJS+LibWeb+WebContent: Port JS::PropertyKey to UTF-16
This has quite a lot of fall out. But the majority of it is just type or UDL substitution, where the changes just fall through to other function calls. By changing property key storage to UTF-16, the main affected areas are: * NativeFunction names must now be UTF-16 * Bytecode identifiers must now be UTF-16 * Module/binding names must now be UTF-16
1 parent cd27623 commit 0efa98a

File tree

131 files changed

+767
-727
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+767
-727
lines changed

Libraries/LibJS/AST.cpp

Lines changed: 53 additions & 45 deletions
Large diffs are not rendered by default.

Libraries/LibJS/AST.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ class FunctionNode {
797797
bool uses_this_from_environment() const { return m_parsing_insights.uses_this_from_environment; }
798798

799799
virtual bool has_name() const = 0;
800-
virtual Value instantiate_ordinary_function_expression(VM&, FlyString given_name) const = 0;
800+
virtual Value instantiate_ordinary_function_expression(VM&, Utf16FlyString given_name) const = 0;
801801

802802
RefPtr<SharedFunctionInstanceData> shared_data() const;
803803
void set_shared_data(RefPtr<SharedFunctionInstanceData>) const;
@@ -847,7 +847,7 @@ class FunctionDeclaration final
847847
void set_should_do_additional_annexB_steps() { m_is_hoisted = true; }
848848

849849
bool has_name() const override { return true; }
850-
Value instantiate_ordinary_function_expression(VM&, FlyString) const override { VERIFY_NOT_REACHED(); }
850+
Value instantiate_ordinary_function_expression(VM&, Utf16FlyString) const override { VERIFY_NOT_REACHED(); }
851851

852852
virtual ~FunctionDeclaration() { }
853853

@@ -874,7 +874,7 @@ class FunctionExpression final
874874

875875
bool has_name() const override { return !name().is_empty(); }
876876

877-
Value instantiate_ordinary_function_expression(VM&, FlyString given_name) const override;
877+
Value instantiate_ordinary_function_expression(VM&, Utf16FlyString given_name) const override;
878878

879879
virtual ~FunctionExpression() { }
880880

@@ -1518,7 +1518,7 @@ class ClassExpression final : public Expression {
15181518

15191519
bool has_name() const { return m_name; }
15201520

1521-
ThrowCompletionOr<ECMAScriptFunctionObject*> create_class_constructor(VM&, Environment* class_environment, Environment* environment, Value super_class, ReadonlySpan<Value> element_keys, Optional<FlyString> const& binding_name = {}, FlyString const& class_name = {}) const;
1521+
ThrowCompletionOr<ECMAScriptFunctionObject*> create_class_constructor(VM&, Environment* class_environment, Environment* environment, Value super_class, ReadonlySpan<Value> element_keys, Optional<Utf16FlyString> const& binding_name = {}, Utf16FlyString const& class_name = {}) const;
15221522

15231523
private:
15241524
virtual bool is_class_expression() const override { return true; }

Libraries/LibJS/Bytecode/Executable.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#pragma once
88

9-
#include <AK/FlyString.h>
109
#include <AK/HashMap.h>
1110
#include <AK/NonnullOwnPtr.h>
1211
#include <AK/OwnPtr.h>
12+
#include <AK/Utf16FlyString.h>
1313
#include <AK/WeakPtr.h>
1414
#include <LibGC/CellAllocator.h>
1515
#include <LibJS/Bytecode/IdentifierTable.h>
@@ -67,7 +67,7 @@ class JS_API Executable final : public Cell {
6767

6868
virtual ~Executable() override;
6969

70-
FlyString name;
70+
Utf16FlyString name;
7171
Vector<u8> bytecode;
7272
Vector<PropertyLookupCache> property_lookup_caches;
7373
Vector<GlobalVariableCache> global_variable_caches;
@@ -99,9 +99,9 @@ class JS_API Executable final : public Cell {
9999
Optional<IdentifierTableIndex> length_identifier;
100100

101101
String const& get_string(StringTableIndex index) const { return string_table->get(index); }
102-
FlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
102+
Utf16FlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
103103

104-
Optional<FlyString const&> get_identifier(Optional<IdentifierTableIndex> const& index) const
104+
Optional<Utf16FlyString const&> get_identifier(Optional<IdentifierTableIndex> const& index) const
105105
{
106106
if (!index.has_value())
107107
return {};

Libraries/LibJS/Bytecode/Generator.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ class Generator {
212212
return m_regex_table->insert(move(regex));
213213
}
214214

215-
IdentifierTableIndex intern_identifier(FlyString string)
215+
IdentifierTableIndex intern_identifier(FlyString const& string)
216+
{
217+
return intern_identifier(Utf16FlyString::from_utf8(string));
218+
}
219+
220+
IdentifierTableIndex intern_identifier(Utf16FlyString string)
216221
{
217222
return m_identifier_table->insert(move(string));
218223
}

Libraries/LibJS/Bytecode/IdentifierTable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
namespace JS::Bytecode {
1010

11-
IdentifierTableIndex IdentifierTable::insert(FlyString string)
11+
IdentifierTableIndex IdentifierTable::insert(Utf16FlyString string)
1212
{
1313
m_identifiers.append(move(string));
1414
VERIFY(m_identifiers.size() <= NumericLimits<u32>::max());
1515
return { static_cast<u32>(m_identifiers.size() - 1) };
1616
}
1717

18-
FlyString const& IdentifierTable::get(IdentifierTableIndex index) const
18+
Utf16FlyString const& IdentifierTable::get(IdentifierTableIndex index) const
1919
{
2020
return m_identifiers[index.value];
2121
}

Libraries/LibJS/Bytecode/IdentifierTable.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#pragma once
88

99
#include <AK/DistinctNumeric.h>
10-
#include <AK/FlyString.h>
10+
#include <AK/Utf16FlyString.h>
1111
#include <AK/Vector.h>
1212

1313
namespace JS::Bytecode {
@@ -25,13 +25,13 @@ class IdentifierTable {
2525
public:
2626
IdentifierTable() = default;
2727

28-
IdentifierTableIndex insert(FlyString);
29-
FlyString const& get(IdentifierTableIndex) const;
28+
IdentifierTableIndex insert(Utf16FlyString);
29+
Utf16FlyString const& get(IdentifierTableIndex) const;
3030
void dump() const;
3131
bool is_empty() const { return m_identifiers.is_empty(); }
3232

3333
private:
34-
Vector<FlyString> m_identifiers;
34+
Vector<Utf16FlyString> m_identifiers;
3535
};
3636

3737
}

Libraries/LibJS/Bytecode/Interpreter.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ void Interpreter::enter_object_environment(Object& object)
821821
running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
822822
}
823823

824-
ThrowCompletionOr<GC::Ref<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, FlyString const& name)
824+
ThrowCompletionOr<GC::Ref<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, Utf16FlyString const& name)
825825
{
826826
auto executable_result = Bytecode::Generator::generate_from_ast_node(vm, node, kind);
827827
if (executable_result.is_error())
@@ -1201,7 +1201,7 @@ inline ThrowCompletionOr<Value> get_global(Interpreter& interpreter, IdentifierT
12011201
return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, identifier);
12021202
}
12031203

1204-
inline ThrowCompletionOr<void> put_by_property_key(VM& vm, Value base, Value this_value, Value value, Optional<FlyString const&> const& base_identifier, PropertyKey name, Op::PropertyKind kind, PropertyLookupCache* caches = nullptr)
1204+
inline ThrowCompletionOr<void> put_by_property_key(VM& vm, Value base, Value this_value, Value value, Optional<Utf16FlyString const&> const& base_identifier, PropertyKey name, Op::PropertyKind kind, PropertyLookupCache* caches = nullptr)
12051205
{
12061206
// Better error message than to_object would give
12071207
if (vm.in_strict_mode() && base.is_nullish())
@@ -1221,14 +1221,14 @@ inline ThrowCompletionOr<void> put_by_property_key(VM& vm, Value base, Value thi
12211221
case Op::PropertyKind::Getter: {
12221222
auto& function = value.as_function();
12231223
if (is<ECMAScriptFunctionObject>(function) && static_cast<ECMAScriptFunctionObject const&>(function).name().is_empty())
1224-
static_cast<ECMAScriptFunctionObject*>(&function)->set_name(MUST(String::formatted("get {}", name)));
1224+
static_cast<ECMAScriptFunctionObject*>(&function)->set_name(Utf16String::formatted("get {}", name));
12251225
object->define_direct_accessor(name, &function, nullptr, Attribute::Configurable | Attribute::Enumerable);
12261226
break;
12271227
}
12281228
case Op::PropertyKind::Setter: {
12291229
auto& function = value.as_function();
12301230
if (is<ECMAScriptFunctionObject>(function) && static_cast<ECMAScriptFunctionObject const&>(function).name().is_empty())
1231-
static_cast<ECMAScriptFunctionObject*>(&function)->set_name(MUST(String::formatted("set {}", name)));
1231+
static_cast<ECMAScriptFunctionObject*>(&function)->set_name(Utf16String::formatted("set {}", name));
12321232
object->define_direct_accessor(name, nullptr, &function, Attribute::Configurable | Attribute::Enumerable);
12331233
break;
12341234
}
@@ -1354,14 +1354,14 @@ inline Value new_function(VM& vm, FunctionNode const& function_node, Optional<Id
13541354
Value value;
13551355

13561356
if (!function_node.has_name()) {
1357-
FlyString name;
1357+
Utf16FlyString name;
13581358
if (lhs_name.has_value())
13591359
name = vm.bytecode_interpreter().current_executable().get_identifier(lhs_name.value());
13601360
value = function_node.instantiate_ordinary_function_expression(vm, name);
13611361
} else {
13621362
value = ECMAScriptFunctionObject::create_from_function_node(
13631363
function_node,
1364-
function_node.name(),
1364+
Utf16FlyString::from_utf8(function_node.name()),
13651365
*vm.current_realm(),
13661366
vm.lexical_environment(),
13671367
vm.running_execution_context().private_environment);
@@ -1375,7 +1375,7 @@ inline Value new_function(VM& vm, FunctionNode const& function_node, Optional<Id
13751375
return value;
13761376
}
13771377

1378-
inline ThrowCompletionOr<void> put_by_value(VM& vm, Value base, Optional<FlyString const&> const& base_identifier, Value property_key_value, Value value, Op::PropertyKind kind)
1378+
inline ThrowCompletionOr<void> put_by_value(VM& vm, Value base, Optional<Utf16FlyString const&> const& base_identifier, Value property_key_value, Value value, Op::PropertyKind kind)
13791379
{
13801380
// OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects.
13811381
if ((kind == Op::PropertyKind::KeyValue || kind == Op::PropertyKind::DirectKeyValue)
@@ -1496,7 +1496,7 @@ struct CalleeAndThis {
14961496
Value this_value;
14971497
};
14981498

1499-
inline ThrowCompletionOr<CalleeAndThis> get_callee_and_this_from_environment(Bytecode::Interpreter& interpreter, FlyString const& name, EnvironmentCoordinate& cache)
1499+
inline ThrowCompletionOr<CalleeAndThis> get_callee_and_this_from_environment(Bytecode::Interpreter& interpreter, Utf16FlyString const& name, EnvironmentCoordinate& cache)
15001500
{
15011501
auto& vm = interpreter.vm();
15021502

@@ -1583,7 +1583,7 @@ inline Span<Value> argument_list_evaluation(Interpreter& interpreter, Value argu
15831583
return argument_values;
15841584
}
15851585

1586-
inline ThrowCompletionOr<void> create_variable(VM& vm, FlyString const& name, Op::EnvironmentMode mode, bool is_global, bool is_immutable, bool is_strict)
1586+
inline ThrowCompletionOr<void> create_variable(VM& vm, Utf16FlyString const& name, Op::EnvironmentMode mode, bool is_global, bool is_immutable, bool is_strict)
15871587
{
15881588
if (mode == Op::EnvironmentMode::Lexical) {
15891589
VERIFY(!is_global);
@@ -1612,17 +1612,17 @@ inline ThrowCompletionOr<void> create_variable(VM& vm, FlyString const& name, Op
16121612
inline ThrowCompletionOr<ECMAScriptFunctionObject*> new_class(VM& vm, Value super_class, ClassExpression const& class_expression, Optional<IdentifierTableIndex> const& lhs_name, ReadonlySpan<Value> element_keys)
16131613
{
16141614
auto& interpreter = vm.bytecode_interpreter();
1615-
auto name = class_expression.name();
16161615

16171616
// NOTE: NewClass expects classEnv to be active lexical environment
16181617
auto* class_environment = vm.lexical_environment();
16191618
vm.running_execution_context().lexical_environment = vm.running_execution_context().saved_lexical_environments.take_last();
16201619

1621-
Optional<FlyString> binding_name;
1622-
FlyString class_name;
1620+
Optional<Utf16FlyString> binding_name;
1621+
Utf16FlyString class_name;
16231622
if (!class_expression.has_name() && lhs_name.has_value()) {
16241623
class_name = interpreter.current_executable().get_identifier(lhs_name.value());
16251624
} else {
1625+
auto name = Utf16FlyString::from_utf8(class_expression.name());
16261626
binding_name = name;
16271627
class_name = name;
16281628
}

Libraries/LibJS/Bytecode/Interpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class JS_API Interpreter {
107107

108108
JS_API extern bool g_dump_bytecode;
109109

110-
ThrowCompletionOr<GC::Ref<Bytecode::Executable>> compile(VM&, ASTNode const&, JS::FunctionKind kind, FlyString const& name);
110+
ThrowCompletionOr<GC::Ref<Bytecode::Executable>> compile(VM&, ASTNode const&, JS::FunctionKind kind, Utf16FlyString const& name);
111111
ThrowCompletionOr<GC::Ref<Bytecode::Executable>> compile(VM&, ECMAScriptFunctionObject const&);
112112

113113
}

Libraries/LibJS/Console.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static ThrowCompletionOr<GC::Ref<Object>> create_table_row(Realm& realm, Value r
172172

173173
// 2. Set `row["(index)"]` to `rowIndex`
174174
{
175-
auto key = PropertyKey { "(index)"_fly_string, PropertyKey::StringMayBeNumber::No };
175+
auto key = PropertyKey { "(index)"_utf16_fly_string, PropertyKey::StringMayBeNumber::No };
176176
TRY(row->set(key, row_index, Object::ShouldThrowExceptions::No));
177177

178178
add_column(key);

Libraries/LibJS/Contrib/Test262/262Object.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ void $262Object::initialize(Realm& realm)
3636
m_is_htmldda = realm.create<IsHTMLDDA>(realm);
3737

3838
u8 attr = Attribute::Writable | Attribute::Configurable;
39-
define_native_function(realm, "clearKeptObjects"_fly_string, clear_kept_objects, 0, attr);
40-
define_native_function(realm, "createRealm"_fly_string, create_realm, 0, attr);
41-
define_native_function(realm, "detachArrayBuffer"_fly_string, detach_array_buffer, 1, attr);
42-
define_native_function(realm, "evalScript"_fly_string, eval_script, 1, attr);
43-
44-
define_direct_property("agent"_fly_string, m_agent, attr);
45-
define_direct_property("gc"_fly_string, realm.global_object().get_without_side_effects("gc"_fly_string), attr);
46-
define_direct_property("global"_fly_string, &realm.global_object(), attr);
47-
define_direct_property("IsHTMLDDA"_fly_string, m_is_htmldda, attr);
39+
define_native_function(realm, "clearKeptObjects"_utf16_fly_string, clear_kept_objects, 0, attr);
40+
define_native_function(realm, "createRealm"_utf16_fly_string, create_realm, 0, attr);
41+
define_native_function(realm, "detachArrayBuffer"_utf16_fly_string, detach_array_buffer, 1, attr);
42+
define_native_function(realm, "evalScript"_utf16_fly_string, eval_script, 1, attr);
43+
44+
define_direct_property("agent"_utf16_fly_string, m_agent, attr);
45+
define_direct_property("gc"_utf16_fly_string, realm.global_object().get_without_side_effects("gc"_utf16_fly_string), attr);
46+
define_direct_property("global"_utf16_fly_string, &realm.global_object(), attr);
47+
define_direct_property("IsHTMLDDA"_utf16_fly_string, m_is_htmldda, attr);
4848
}
4949

5050
void $262Object::visit_edges(Cell::Visitor& visitor)

0 commit comments

Comments
 (0)