1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is SpiderMonkey JavaScript engine.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * the Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2011-2012
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Jason Orendorff <jorendorff@mozilla.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef MapObject_h__
42 : #define MapObject_h__
43 :
44 : #include "jsapi.h"
45 : #include "jscntxt.h"
46 : #include "jsobj.h"
47 :
48 : #include "js/HashTable.h"
49 :
50 : namespace js {
51 :
52 : /*
53 : * Comparing two ropes for equality can fail. The js::HashTable template
54 : * requires infallible hash() and match() operations. Therefore we require
55 : * all values to be converted to hashable form before being used as a key
56 : * in a Map or Set object.
57 : *
58 : * All values except ropes are hashable as-is.
59 : */
60 10107819 : class HashableValue {
61 : HeapValue value;
62 :
63 : public:
64 : struct Hasher {
65 : typedef HashableValue Lookup;
66 2369457 : static HashNumber hash(const Lookup &v) { return v.hash(); }
67 1183797 : static bool match(const HashableValue &k, const Lookup &l) { return k.equals(l); }
68 : };
69 :
70 7148457 : HashableValue() : value(UndefinedValue()) {}
71 :
72 6750 : operator const HeapValue &() const { return value; }
73 : bool setValue(JSContext *cx, const Value &v);
74 : HashNumber hash() const;
75 : bool equals(const HashableValue &other) const;
76 : };
77 :
78 : typedef HashMap<HashableValue, HeapValue, HashableValue::Hasher, RuntimeAllocPolicy> ValueMap;
79 : typedef HashSet<HashableValue, HashableValue::Hasher, RuntimeAllocPolicy> ValueSet;
80 :
81 : class MapObject : public JSObject {
82 : public:
83 : static JSObject *initClass(JSContext *cx, JSObject *obj);
84 : static Class class_;
85 : private:
86 : typedef ValueMap Data;
87 : static JSFunctionSpec methods[];
88 1194129 : ValueMap *getData() { return static_cast<ValueMap *>(getPrivate()); }
89 : static void mark(JSTracer *trc, JSObject *obj);
90 : static void finalize(JSContext *cx, JSObject *obj);
91 : static JSBool construct(JSContext *cx, unsigned argc, Value *vp);
92 : static JSBool size(JSContext *cx, unsigned argc, Value *vp);
93 : static JSBool get(JSContext *cx, unsigned argc, Value *vp);
94 : static JSBool has(JSContext *cx, unsigned argc, Value *vp);
95 : static JSBool set(JSContext *cx, unsigned argc, Value *vp);
96 : static JSBool delete_(JSContext *cx, unsigned argc, Value *vp);
97 : };
98 :
99 : class SetObject : public JSObject {
100 : public:
101 : static JSObject *initClass(JSContext *cx, JSObject *obj);
102 : static Class class_;
103 : private:
104 : typedef ValueSet Data;
105 : static JSFunctionSpec methods[];
106 1188558 : ValueSet *getData() { return static_cast<ValueSet *>(getPrivate()); }
107 : static void mark(JSTracer *trc, JSObject *obj);
108 : static void finalize(JSContext *cx, JSObject *obj);
109 : static JSBool construct(JSContext *cx, unsigned argc, Value *vp);
110 : static JSBool size(JSContext *cx, unsigned argc, Value *vp);
111 : static JSBool has(JSContext *cx, unsigned argc, Value *vp);
112 : static JSBool add(JSContext *cx, unsigned argc, Value *vp);
113 : static JSBool delete_(JSContext *cx, unsigned argc, Value *vp);
114 : };
115 :
116 : } /* namespace js */
117 :
118 : extern JSObject *
119 : js_InitMapClass(JSContext *cx, JSObject *obj);
120 :
121 : extern JSObject *
122 : js_InitSetClass(JSContext *cx, JSObject *obj);
123 :
124 : #endif /* MapObject_h__ */
|