1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/public/PropertyKey.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* JS::PropertyKey implementation. */ 1.11 + 1.12 +#ifndef js_PropertyKey_h 1.13 +#define js_PropertyKey_h 1.14 + 1.15 +#include "js/TypeDecls.h" 1.16 +#include "js/Value.h" 1.17 + 1.18 +namespace JS { 1.19 + 1.20 +class PropertyKey; 1.21 + 1.22 +namespace detail { 1.23 + 1.24 +extern JS_PUBLIC_API(bool) 1.25 +ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key); 1.26 + 1.27 +} // namespace detail 1.28 + 1.29 +/* 1.30 + * A PropertyKey is a key used to access some property on an object. It is a 1.31 + * natural way to represent a property accessed using a JavaScript value. 1.32 + * 1.33 + * PropertyKey can represent indexes, named properties, and ES6 symbols. The 1.34 + * latter aren't implemented in SpiderMonkey yet, but PropertyKey carves out 1.35 + * space for them. 1.36 + */ 1.37 +class PropertyKey 1.38 +{ 1.39 + Value v; 1.40 + friend JS_PUBLIC_API(bool) detail::ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key); 1.41 + 1.42 + public: 1.43 + explicit PropertyKey(uint32_t index) : v(PrivateUint32Value(index)) {} 1.44 + 1.45 + /* 1.46 + * An index is a string property name whose characters exactly spell out an 1.47 + * unsigned 32-bit integer in decimal: "0", "1", "2", ...., "4294967294", 1.48 + * "4294967295". 1.49 + */ 1.50 + bool isIndex(uint32_t *index) { 1.51 + // The implementation here assumes that private uint32_t are stored 1.52 + // using the int32_t representation. This is purely an implementation 1.53 + // detail: embedders must not rely upon this! 1.54 + if (!v.isInt32()) 1.55 + return false; 1.56 + *index = v.toPrivateUint32(); 1.57 + return true; 1.58 + } 1.59 + 1.60 + /* 1.61 + * A name is a string property name which is *not* an index. Note that by 1.62 + * the ECMAScript language grammar, any dotted property access |obj.prop| 1.63 + * will access a named property. 1.64 + */ 1.65 + bool isName(JSString **str) { 1.66 + uint32_t dummy; 1.67 + if (isIndex(&dummy)) 1.68 + return false; 1.69 + *str = v.toString(); 1.70 + return true; 1.71 + } 1.72 + 1.73 + /* 1.74 + * A symbol is a property name that's a Symbol, a particular kind of object 1.75 + * in ES6. It is the only kind of property name that's not a string. 1.76 + * 1.77 + * SpiderMonkey doesn't yet implement symbols, but we're carving out API 1.78 + * space for them in advance. 1.79 + */ 1.80 + bool isSymbol() { 1.81 + return false; 1.82 + } 1.83 +}; 1.84 + 1.85 +inline bool 1.86 +ToPropertyKey(JSContext *cx, HandleValue v, PropertyKey *key) 1.87 +{ 1.88 + if (v.isInt32() && v.toInt32() >= 0) { 1.89 + *key = PropertyKey(uint32_t(v.toInt32())); 1.90 + return true; 1.91 + } 1.92 + 1.93 + return detail::ToPropertyKeySlow(cx, v, key); 1.94 +} 1.95 + 1.96 +} // namespace JS 1.97 + 1.98 +#endif /* js_PropertyKey_h */