Bug 1812540 - Part 1: Add JS::ToGetterId and JS::ToSetterId. r=sfink

Differential Revision: https://phabricator.services.mozilla.com/D168512
This commit is contained in:
Tooru Fujisawa 2023-02-07 07:36:17 +00:00
parent 2e02e360ca
commit bbd6a64a49
4 changed files with 103 additions and 0 deletions

View File

@ -255,6 +255,16 @@ MOZ_ALWAYS_INLINE void AssertIdIsNotGray(jsid id) {
extern JS_PUBLIC_API PropertyKey GetWellKnownSymbolKey(JSContext* cx,
SymbolCode which);
/**
* Generate getter/setter id for given id, by adding "get " or "set " prefix.
*/
extern JS_PUBLIC_API bool ToGetterId(
JSContext* cx, JS::Handle<JS::PropertyKey> id,
JS::MutableHandle<JS::PropertyKey> getterId);
extern JS_PUBLIC_API bool ToSetterId(
JSContext* cx, JS::Handle<JS::PropertyKey> id,
JS::MutableHandle<JS::PropertyKey> setterId);
} // namespace JS
namespace js {

View File

@ -96,6 +96,7 @@ UNIFIED_SOURCES += [
"testProfileStrings.cpp",
"testPromise.cpp",
"testPropCache.cpp",
"testPropertyKey.cpp",
"testRecordTupleToSource.cpp",
"testRegExp.cpp",
"testResolveRecursion.cpp",

View File

@ -0,0 +1,69 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/Id.h" // JS::PropertyKey, JS::GetWellKnownSymbolKey, JS::ToGetterId, JS::ToSetterId
#include "js/String.h" // JSString, JS_AtomizeString, JS_StringEqualsAscii
#include "js/Symbol.h" // JS::Symbol, JS::SymbolCode
#include "jsapi-tests/tests.h"
BEGIN_TEST(testPropertyKeyGetterAndSetter) {
JS::Rooted<JSString*> str(cx, JS_AtomizeString(cx, "prop"));
CHECK(str);
JS::Rooted<JS::PropertyKey> strId(cx, JS::PropertyKey::NonIntAtom(str));
MOZ_ASSERT(strId.isString());
JS::Rooted<JS::PropertyKey> symId(
cx, JS::GetWellKnownSymbolKey(cx, JS::SymbolCode::iterator));
MOZ_ASSERT(symId.isSymbol());
JS::Rooted<JS::PropertyKey> numId(cx, JS::PropertyKey::Int(42));
MOZ_ASSERT(numId.isInt());
bool match;
JS::Rooted<JS::PropertyKey> strGetterId(cx);
CHECK(JS::ToGetterId(cx, strId, &strGetterId));
CHECK(strGetterId.isString());
CHECK(JS_StringEqualsAscii(cx, strGetterId.toString(), "get prop", &match));
CHECK(match);
JS::Rooted<JS::PropertyKey> strSetterId(cx);
CHECK(JS::ToSetterId(cx, strId, &strSetterId));
CHECK(strSetterId.isString());
CHECK(JS_StringEqualsAscii(cx, strSetterId.toString(), "set prop", &match));
CHECK(match);
JS::Rooted<JS::PropertyKey> symGetterId(cx);
CHECK(JS::ToGetterId(cx, symId, &symGetterId));
CHECK(symGetterId.isString());
CHECK(JS_StringEqualsAscii(cx, symGetterId.toString(),
"get [Symbol.iterator]", &match));
CHECK(match);
JS::Rooted<JS::PropertyKey> symSetterId(cx);
CHECK(JS::ToSetterId(cx, symId, &symSetterId));
CHECK(symSetterId.isString());
CHECK(JS_StringEqualsAscii(cx, symSetterId.toString(),
"set [Symbol.iterator]", &match));
CHECK(match);
JS::Rooted<JS::PropertyKey> numGetterId(cx);
CHECK(JS::ToGetterId(cx, numId, &numGetterId));
CHECK(numGetterId.isString());
CHECK(JS_StringEqualsAscii(cx, numGetterId.toString(), "get 42", &match));
CHECK(match);
JS::Rooted<JS::PropertyKey> numSetterId(cx);
CHECK(JS::ToSetterId(cx, numId, &numSetterId));
CHECK(numSetterId.isString());
CHECK(JS_StringEqualsAscii(cx, numSetterId.toString(), "set 42", &match));
CHECK(match);
return true;
}
END_TEST(testPropertyKeyGetterAndSetter)

View File

@ -76,6 +76,7 @@
#include "vm/EnvironmentObject.h"
#include "vm/ErrorObject.h"
#include "vm/ErrorReporting.h"
#include "vm/FunctionPrefixKind.h"
#include "vm/Interpreter.h"
#include "vm/JSAtom.h"
#include "vm/JSAtomState.h"
@ -3389,6 +3390,28 @@ JS_PUBLIC_API JS::PropertyKey JS::GetWellKnownSymbolKey(JSContext* cx,
return PropertyKey::Symbol(cx->wellKnownSymbols().get(which));
}
static bool AddPrefix(JSContext* cx, JS::Handle<JS::PropertyKey> id,
FunctionPrefixKind prefixKind,
JS::MutableHandle<JS::PropertyKey> out) {
JS::Rooted<JSAtom*> atom(cx, js::IdToFunctionName(cx, id, prefixKind));
if (!atom) {
return false;
}
out.set(JS::PropertyKey::NonIntAtom(atom));
return true;
}
JS_PUBLIC_API bool JS::ToGetterId(JSContext* cx, JS::Handle<JS::PropertyKey> id,
JS::MutableHandle<JS::PropertyKey> getterId) {
return AddPrefix(cx, id, FunctionPrefixKind::Get, getterId);
}
JS_PUBLIC_API bool JS::ToSetterId(JSContext* cx, JS::Handle<JS::PropertyKey> id,
JS::MutableHandle<JS::PropertyKey> setterId) {
return AddPrefix(cx, id, FunctionPrefixKind::Set, setterId);
}
#ifdef DEBUG
static bool PropertySpecNameIsDigits(JSPropertySpec::Name name) {
if (name.isSymbol()) {