Bug 837773 - Add a PropertyKey.h header. r=jorendorff

--HG--
rename : js/src/jsval.h => js/public/Value.h
extra : rebase_source : 1756e599b0718b8e765cecb972737040e0ba56e4
This commit is contained in:
Jeff Walden 2013-02-01 16:42:48 -08:00
Родитель 228f97350d
Коммит 88e94604ab
6 изменённых файлов: 142 добавлений и 1 удалений

99
js/public/PropertyKey.h Normal file
Просмотреть файл

@ -0,0 +1,99 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=4 sw=4 et tw=99 ft=cpp:
*
* 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/. */
/* JS::PropertyKey implementation. */
#ifndef js_PropertyKey_h___
#define js_PropertyKey_h___
#include "mozilla/Attributes.h"
#include "js/Value.h"
struct JSContext;
namespace JS {
class PropertyKey;
namespace detail {
extern JS_PUBLIC_API(bool)
ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key);
} // namespace detail
/*
* A PropertyKey is a key used to access some property on an object. It is a
* natural way to represent a property accessed using a JavaScript value.
*
* PropertyKey can represent indexes, named properties, and ES6 symbols. The
* latter aren't implemented in SpiderMonkey yet, but PropertyKey carves out
* space for them.
*/
class PropertyKey
{
Value v;
friend bool detail::ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key);
public:
explicit PropertyKey(uint32_t index) : v(PrivateUint32Value(index)) {}
/*
* An index is a string property name whose characters exactly spell out an
* unsigned 32-bit integer in decimal: "0", "1", "2", ...., "4294967294",
* "4294967295".
*/
bool isIndex(uint32_t *index) {
// The implementation here assumes that private uint32_t are stored
// using the int32_t representation. This is purely an implementation
// detail: embedders must not rely upon this!
if (!v.isInt32())
return false;
*index = v.toPrivateUint32();
return true;
}
/*
* A name is a string property name which is *not* an index. Note that by
* the ECMAScript language grammar, any dotted property access |obj.prop|
* will access a named property.
*/
bool isName(JSString **str) {
uint32_t dummy;
if (isIndex(&dummy))
return false;
*str = v.toString();
return true;
}
/*
* A symbol is a property name that's a Symbol, a particular kind of object
* in ES6. It is the only kind of property name that's not a string.
*
* SpiderMonkey doesn't yet implement symbols, but we're carving out API
* space for them in advance.
*/
bool isSymbol() {
return false;
}
};
inline bool
ToPropertyKey(JSContext *cx, HandleValue v, PropertyKey *key)
{
if (v.isInt32() && v.toInt32() >= 0) {
*key = PropertyKey(uint32_t(v.toInt32()));
return true;
}
return detail::ToPropertyKeySlow(cx, v, key);
}
} // namespace JS
#endif /* js_PropertyKey_h___ */

Просмотреть файл

@ -127,6 +127,7 @@ CPPSRCS = \
GlobalObject.cpp \
Object.cpp \
ObjectImpl.cpp \
PropertyKey.cpp \
ScopeObject.cpp \
Shape.cpp \
Stack.cpp \
@ -230,6 +231,7 @@ EXPORTS_js = \
HeapAPI.h \
LegacyIntTypes.h \
MemoryMetrics.h \
PropertyKey.h \
TemplateLib.h \
Utility.h \
Value.h \

Просмотреть файл

@ -23,6 +23,7 @@
#include "js/HashTable.h"
#include "js/HeapAPI.h"
#include "js/MemoryMetrics.h"
#include "js/PropertyKey.h"
#include "js/TemplateLib.h"
#include "js/Utility.h"
#include "js/Value.h"

Просмотреть файл

@ -29,6 +29,7 @@
#include "js/Anchor.h"
#include "js/CharacterEncoding.h"
#include "js/HashTable.h"
#include "js/PropertyKey.h"
#include "js/Utility.h"
#include "js/Value.h"
#include "js/Vector.h"

Просмотреть файл

@ -8,11 +8,14 @@
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "gc/Barrier-inl.h"
#include "js/TemplateLib.h"
#include "js/Value.h"
#include "vm/Debugger.h"
#include "vm/ObjectImpl.h"
#include "jsatominlines.h"
#include "gc/Barrier-inl.h"
#include "vm/ObjectImpl-inl.h"
#include "vm/Shape-inl.h"

35
js/src/vm/PropertyKey.cpp Normal file
Просмотреть файл

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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/. */
/* PropertyKey implementation details. */
#include "mozilla/Assertions.h"
#include "gc/Root.h"
#include "js/PropertyKey.h"
#include "js/Value.h"
#include "vm/String.h"
using namespace js;
bool
JS::detail::ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key)
{
MOZ_ASSERT_IF(v.isInt32(), v.toInt32() < 0);
RootedAtom atom(cx);
if (!ToAtom<CanGC>(cx, v))
return false;
uint32_t index;
if (atom->isIndex(&index)) {
*key = PropertyKey(index);
return true;
}
key->v.setString(atom->asPropertyName());
return true;
}