|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /* JS::PropertyKey implementation. */ |
|
8 |
|
9 #ifndef js_PropertyKey_h |
|
10 #define js_PropertyKey_h |
|
11 |
|
12 #include "js/TypeDecls.h" |
|
13 #include "js/Value.h" |
|
14 |
|
15 namespace JS { |
|
16 |
|
17 class PropertyKey; |
|
18 |
|
19 namespace detail { |
|
20 |
|
21 extern JS_PUBLIC_API(bool) |
|
22 ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key); |
|
23 |
|
24 } // namespace detail |
|
25 |
|
26 /* |
|
27 * A PropertyKey is a key used to access some property on an object. It is a |
|
28 * natural way to represent a property accessed using a JavaScript value. |
|
29 * |
|
30 * PropertyKey can represent indexes, named properties, and ES6 symbols. The |
|
31 * latter aren't implemented in SpiderMonkey yet, but PropertyKey carves out |
|
32 * space for them. |
|
33 */ |
|
34 class PropertyKey |
|
35 { |
|
36 Value v; |
|
37 friend JS_PUBLIC_API(bool) detail::ToPropertyKeySlow(JSContext *cx, HandleValue v, PropertyKey *key); |
|
38 |
|
39 public: |
|
40 explicit PropertyKey(uint32_t index) : v(PrivateUint32Value(index)) {} |
|
41 |
|
42 /* |
|
43 * An index is a string property name whose characters exactly spell out an |
|
44 * unsigned 32-bit integer in decimal: "0", "1", "2", ...., "4294967294", |
|
45 * "4294967295". |
|
46 */ |
|
47 bool isIndex(uint32_t *index) { |
|
48 // The implementation here assumes that private uint32_t are stored |
|
49 // using the int32_t representation. This is purely an implementation |
|
50 // detail: embedders must not rely upon this! |
|
51 if (!v.isInt32()) |
|
52 return false; |
|
53 *index = v.toPrivateUint32(); |
|
54 return true; |
|
55 } |
|
56 |
|
57 /* |
|
58 * A name is a string property name which is *not* an index. Note that by |
|
59 * the ECMAScript language grammar, any dotted property access |obj.prop| |
|
60 * will access a named property. |
|
61 */ |
|
62 bool isName(JSString **str) { |
|
63 uint32_t dummy; |
|
64 if (isIndex(&dummy)) |
|
65 return false; |
|
66 *str = v.toString(); |
|
67 return true; |
|
68 } |
|
69 |
|
70 /* |
|
71 * A symbol is a property name that's a Symbol, a particular kind of object |
|
72 * in ES6. It is the only kind of property name that's not a string. |
|
73 * |
|
74 * SpiderMonkey doesn't yet implement symbols, but we're carving out API |
|
75 * space for them in advance. |
|
76 */ |
|
77 bool isSymbol() { |
|
78 return false; |
|
79 } |
|
80 }; |
|
81 |
|
82 inline bool |
|
83 ToPropertyKey(JSContext *cx, HandleValue v, PropertyKey *key) |
|
84 { |
|
85 if (v.isInt32() && v.toInt32() >= 0) { |
|
86 *key = PropertyKey(uint32_t(v.toInt32())); |
|
87 return true; |
|
88 } |
|
89 |
|
90 return detail::ToPropertyKeySlow(cx, v, key); |
|
91 } |
|
92 |
|
93 } // namespace JS |
|
94 |
|
95 #endif /* js_PropertyKey_h */ |