diff options
author | Linus Groh <mail@linusgroh.de> | 2022-12-10 00:00:33 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-10 11:23:23 +0000 |
commit | c2d33ec48aed39ca876f724536326b791bc1d5e5 (patch) | |
tree | 3c4e91ba022fe137f3d412aca766751fe4799142 /Userland/Libraries/LibJS/Runtime | |
parent | 7abd9efe339575a3cf809a409a30b8855df36194 (diff) | |
download | serenity-c2d33ec48aed39ca876f724536326b791bc1d5e5.zip |
LibJS: Add spec comments to Value::to_property_key()
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Value.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 2c61da87e3..7f9673862e 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -853,12 +853,21 @@ ThrowCompletionOr<double> Value::to_double(VM& vm) const // 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey ThrowCompletionOr<PropertyKey> Value::to_property_key(VM& vm) const { + // OPTIMIZATION: Return the value as a numeric PropertyKey, if possible. if (is_int32() && as_i32() >= 0) return PropertyKey { as_i32() }; + + // 1. Let key be ? ToPrimitive(argument, string). auto key = TRY(to_primitive(vm, PreferredType::String)); - if (key.is_symbol()) + + // 2. If key is a Symbol, then + if (key.is_symbol()) { + // a. Return key. return &key.as_symbol(); - return TRY(key.to_string(vm)); + } + + // 3. Return ! ToString(key). + return MUST(key.to_string(vm)); } ThrowCompletionOr<i32> Value::to_i32_slow_case(VM& vm) const |