LCOV - code coverage report
Current view: top level - js/src/builtin - MapObject.h (source / functions) Hit Total Coverage
Test: output.info Lines: 32 41 78.0 %
Date: 2017-07-14 16:53:18 Functions: 17 20 85.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #ifndef builtin_MapObject_h
       8             : #define builtin_MapObject_h
       9             : 
      10             : #include "jsobj.h"
      11             : 
      12             : #include "builtin/SelfHostingDefines.h"
      13             : #include "vm/GlobalObject.h"
      14             : #include "vm/NativeObject.h"
      15             : #include "vm/PIC.h"
      16             : #include "vm/Runtime.h"
      17             : 
      18             : namespace js {
      19             : 
      20             : /*
      21             :  * Comparing two ropes for equality can fail. The js::HashTable template
      22             :  * requires infallible hash() and match() operations. Therefore we require
      23             :  * all values to be converted to hashable form before being used as a key
      24             :  * in a Map or Set object.
      25             :  *
      26             :  * All values except ropes are hashable as-is.
      27             :  */
      28        9731 : class HashableValue
      29             : {
      30             :     PreBarrieredValue value;
      31             : 
      32             :   public:
      33             :     struct Hasher {
      34             :         typedef HashableValue Lookup;
      35        3080 :         static HashNumber hash(const Lookup& v, const mozilla::HashCodeScrambler& hcs) {
      36        3080 :             return v.hash(hcs);
      37             :         }
      38        3017 :         static bool match(const HashableValue& k, const Lookup& l) { return k == l; }
      39        2782 :         static bool isEmpty(const HashableValue& v) { return v.value.isMagic(JS_HASH_KEY_EMPTY); }
      40          31 :         static void makeEmpty(HashableValue* vp) { vp->value = MagicValue(JS_HASH_KEY_EMPTY); }
      41             :     };
      42             : 
      43        2064 :     HashableValue() : value(UndefinedValue()) {}
      44             : 
      45             :     MOZ_MUST_USE bool setValue(JSContext* cx, HandleValue v);
      46             :     HashNumber hash(const mozilla::HashCodeScrambler& hcs) const;
      47             :     bool operator==(const HashableValue& other) const;
      48             :     HashableValue trace(JSTracer* trc) const;
      49        1444 :     Value get() const { return value.get(); }
      50             : 
      51           0 :     void trace(JSTracer* trc) {
      52           0 :         TraceEdge(trc, &value, "HashableValue");
      53           0 :     }
      54             : };
      55             : 
      56             : template <typename Wrapper>
      57        2064 : class WrappedPtrOperations<HashableValue, Wrapper>
      58             : {
      59             :   public:
      60         812 :     Value value() const {
      61         812 :         return static_cast<const Wrapper*>(this)->get().get();
      62             :     }
      63             : };
      64             : 
      65             : template <typename Wrapper>
      66        2064 : class MutableWrappedPtrOperations<HashableValue, Wrapper>
      67             :   : public WrappedPtrOperations<HashableValue, Wrapper>
      68             : {
      69             :   public:
      70        2327 :     MOZ_MUST_USE bool setValue(JSContext* cx, HandleValue v) {
      71        2327 :         return static_cast<Wrapper*>(this)->get().setValue(cx, v);
      72             :     }
      73             : };
      74             : 
      75             : template <class Key, class Value, class OrderedHashPolicy, class AllocPolicy>
      76             : class OrderedHashMap;
      77             : 
      78             : template <class T, class OrderedHashPolicy, class AllocPolicy>
      79             : class OrderedHashSet;
      80             : 
      81             : typedef OrderedHashMap<HashableValue,
      82             :                        HeapPtr<Value>,
      83             :                        HashableValue::Hasher,
      84             :                        RuntimeAllocPolicy> ValueMap;
      85             : 
      86             : typedef OrderedHashSet<HashableValue,
      87             :                        HashableValue::Hasher,
      88             :                        RuntimeAllocPolicy> ValueSet;
      89             : 
      90             : template <typename ObjectT>
      91             : class OrderedHashTableRef;
      92             : 
      93             : struct UnbarrieredHashPolicy;
      94             : 
      95             : class MapObject : public NativeObject {
      96             :   public:
      97             :     enum IteratorKind { Keys, Values, Entries };
      98             :     static_assert(Keys == ITEM_KIND_KEY,
      99             :                   "IteratorKind Keys must match self-hosting define for item kind key.");
     100             :     static_assert(Values == ITEM_KIND_VALUE,
     101             :                   "IteratorKind Values must match self-hosting define for item kind value.");
     102             :     static_assert(Entries == ITEM_KIND_KEY_AND_VALUE,
     103             :                   "IteratorKind Entries must match self-hosting define for item kind "
     104             :                   "key-and-value.");
     105             : 
     106             :     static const Class class_;
     107             :     static const Class protoClass_;
     108             : 
     109             :     enum { NurseryKeysSlot, SlotCount };
     110             : 
     111             :     static MOZ_MUST_USE bool getKeysAndValuesInterleaved(JSContext* cx, HandleObject obj,
     112             :                                             JS::MutableHandle<GCVector<JS::Value>> entries);
     113             :     static MOZ_MUST_USE bool entries(JSContext* cx, unsigned argc, Value* vp);
     114             :     static MOZ_MUST_USE bool has(JSContext* cx, unsigned argc, Value* vp);
     115             :     static MapObject* create(JSContext* cx, HandleObject proto = nullptr);
     116             : 
     117             :     // Publicly exposed Map calls for JSAPI access (webidl maplike/setlike
     118             :     // interfaces, etc.)
     119             :     static uint32_t size(JSContext *cx, HandleObject obj);
     120             :     static MOZ_MUST_USE bool get(JSContext *cx, HandleObject obj, HandleValue key,
     121             :                                  MutableHandleValue rval);
     122             :     static MOZ_MUST_USE bool has(JSContext *cx, HandleObject obj, HandleValue key, bool* rval);
     123             :     static MOZ_MUST_USE bool delete_(JSContext *cx, HandleObject obj, HandleValue key, bool* rval);
     124             : 
     125             :     // Set call for public JSAPI exposure. Does not actually return map object
     126             :     // as stated in spec, expects caller to return a value. for instance, with
     127             :     // webidl maplike/setlike, should return interface object.
     128             :     static MOZ_MUST_USE bool set(JSContext *cx, HandleObject obj, HandleValue key, HandleValue val);
     129             :     static MOZ_MUST_USE bool clear(JSContext *cx, HandleObject obj);
     130             :     static MOZ_MUST_USE bool iterator(JSContext *cx, IteratorKind kind, HandleObject obj,
     131             :                                       MutableHandleValue iter);
     132             : 
     133             :     using UnbarrieredTable = OrderedHashMap<Value, Value, UnbarrieredHashPolicy, RuntimeAllocPolicy>;
     134             :     friend class OrderedHashTableRef<MapObject>;
     135             : 
     136             :   private:
     137             :     static const ClassSpec classSpec_;
     138             :     static const ClassOps classOps_;
     139             : 
     140             :     static const JSPropertySpec properties[];
     141             :     static const JSFunctionSpec methods[];
     142             :     static const JSPropertySpec staticProperties[];
     143        1858 :     ValueMap* getData() { return static_cast<ValueMap*>(getPrivate()); }
     144             :     static ValueMap& extract(HandleObject o);
     145             :     static ValueMap& extract(const CallArgs& args);
     146             :     static void trace(JSTracer* trc, JSObject* obj);
     147             :     static void finalize(FreeOp* fop, JSObject* obj);
     148             :     static MOZ_MUST_USE bool construct(JSContext* cx, unsigned argc, Value* vp);
     149             : 
     150             :     static bool is(HandleValue v);
     151             :     static bool is(HandleObject o);
     152             : 
     153             :     static MOZ_MUST_USE bool iterator_impl(JSContext* cx, const CallArgs& args, IteratorKind kind);
     154             : 
     155             :     static MOZ_MUST_USE bool size_impl(JSContext* cx, const CallArgs& args);
     156             :     static MOZ_MUST_USE bool size(JSContext* cx, unsigned argc, Value* vp);
     157             :     static MOZ_MUST_USE bool get_impl(JSContext* cx, const CallArgs& args);
     158             :     static MOZ_MUST_USE bool get(JSContext* cx, unsigned argc, Value* vp);
     159             :     static MOZ_MUST_USE bool has_impl(JSContext* cx, const CallArgs& args);
     160             :     static MOZ_MUST_USE bool set_impl(JSContext* cx, const CallArgs& args);
     161             :     static MOZ_MUST_USE bool set(JSContext* cx, unsigned argc, Value* vp);
     162             :     static MOZ_MUST_USE bool delete_impl(JSContext* cx, const CallArgs& args);
     163             :     static MOZ_MUST_USE bool delete_(JSContext* cx, unsigned argc, Value* vp);
     164             :     static MOZ_MUST_USE bool keys_impl(JSContext* cx, const CallArgs& args);
     165             :     static MOZ_MUST_USE bool keys(JSContext* cx, unsigned argc, Value* vp);
     166             :     static MOZ_MUST_USE bool values_impl(JSContext* cx, const CallArgs& args);
     167             :     static MOZ_MUST_USE bool values(JSContext* cx, unsigned argc, Value* vp);
     168             :     static MOZ_MUST_USE bool entries_impl(JSContext* cx, const CallArgs& args);
     169             :     static MOZ_MUST_USE bool clear_impl(JSContext* cx, const CallArgs& args);
     170             :     static MOZ_MUST_USE bool clear(JSContext* cx, unsigned argc, Value* vp);
     171             : };
     172             : 
     173             : class MapIteratorObject : public NativeObject
     174             : {
     175             :   public:
     176             :     static const Class class_;
     177             : 
     178             :     enum { TargetSlot, RangeSlot, KindSlot, SlotCount };
     179             : 
     180             :     static_assert(TargetSlot == ITERATOR_SLOT_TARGET,
     181             :                   "TargetSlot must match self-hosting define for iterated object slot.");
     182             :     static_assert(RangeSlot == ITERATOR_SLOT_RANGE,
     183             :                   "RangeSlot must match self-hosting define for range or index slot.");
     184             :     static_assert(KindSlot == ITERATOR_SLOT_ITEM_KIND,
     185             :                   "KindSlot must match self-hosting define for item kind slot.");
     186             : 
     187             :     static const JSFunctionSpec methods[];
     188             :     static MapIteratorObject* create(JSContext* cx, HandleObject mapobj, ValueMap* data,
     189             :                                      MapObject::IteratorKind kind);
     190             :     static void finalize(FreeOp* fop, JSObject* obj);
     191             : 
     192             :     static MOZ_MUST_USE bool next(Handle<MapIteratorObject*> mapIterator,
     193             :                                   HandleArrayObject resultPairObj, JSContext* cx);
     194             : 
     195             :     static JSObject* createResultPair(JSContext* cx);
     196             : 
     197             :   private:
     198             :     inline MapObject::IteratorKind kind() const;
     199             : };
     200             : 
     201             : class SetObject : public NativeObject {
     202             :   public:
     203             :     enum IteratorKind { Keys, Values, Entries };
     204             : 
     205             :     static_assert(Keys == ITEM_KIND_KEY,
     206             :                   "IteratorKind Keys must match self-hosting define for item kind key.");
     207             :     static_assert(Values == ITEM_KIND_VALUE,
     208             :                   "IteratorKind Values must match self-hosting define for item kind value.");
     209             :     static_assert(Entries == ITEM_KIND_KEY_AND_VALUE,
     210             :                   "IteratorKind Entries must match self-hosting define for item kind "
     211             :                   "key-and-value.");
     212             : 
     213             :     static const Class class_;
     214             :     static const Class protoClass_;
     215             : 
     216             :     enum { NurseryKeysSlot, SlotCount };
     217             : 
     218             :     static MOZ_MUST_USE bool keys(JSContext *cx, HandleObject obj,
     219             :                                   JS::MutableHandle<GCVector<JS::Value>> keys);
     220             :     static MOZ_MUST_USE bool values(JSContext *cx, unsigned argc, Value *vp);
     221             :     static MOZ_MUST_USE bool add(JSContext *cx, HandleObject obj, HandleValue key);
     222             :     static MOZ_MUST_USE bool has(JSContext *cx, unsigned argc, Value *vp);
     223             : 
     224             :     // Publicly exposed Set calls for JSAPI access (webidl maplike/setlike
     225             :     // interfaces, etc.)
     226             :     static SetObject* create(JSContext *cx, HandleObject proto = nullptr);
     227             :     static uint32_t size(JSContext *cx, HandleObject obj);
     228             :     static MOZ_MUST_USE bool has(JSContext *cx, HandleObject obj, HandleValue key, bool* rval);
     229             :     static MOZ_MUST_USE bool clear(JSContext *cx, HandleObject obj);
     230             :     static MOZ_MUST_USE bool iterator(JSContext *cx, IteratorKind kind, HandleObject obj,
     231             :                                       MutableHandleValue iter);
     232             :     static MOZ_MUST_USE bool delete_(JSContext *cx, HandleObject obj, HandleValue key, bool *rval);
     233             : 
     234             :     using UnbarrieredTable = OrderedHashSet<Value, UnbarrieredHashPolicy, RuntimeAllocPolicy>;
     235             :     friend class OrderedHashTableRef<SetObject>;
     236             : 
     237             :   private:
     238             :     static const ClassSpec classSpec_;
     239             :     static const ClassOps classOps_;
     240             : 
     241             :     static const JSPropertySpec properties[];
     242             :     static const JSFunctionSpec methods[];
     243             :     static const JSPropertySpec staticProperties[];
     244             : 
     245         581 :     ValueSet* getData() { return static_cast<ValueSet*>(getPrivate()); }
     246             :     static ValueSet& extract(HandleObject o);
     247             :     static ValueSet& extract(const CallArgs& args);
     248             :     static void trace(JSTracer* trc, JSObject* obj);
     249             :     static void finalize(FreeOp* fop, JSObject* obj);
     250             :     static bool construct(JSContext* cx, unsigned argc, Value* vp);
     251             : 
     252             :     static bool is(HandleValue v);
     253             :     static bool is(HandleObject o);
     254             : 
     255             :     static bool isBuiltinAdd(HandleValue add, JSContext* cx);
     256             : 
     257             :     static MOZ_MUST_USE bool iterator_impl(JSContext* cx, const CallArgs& args, IteratorKind kind);
     258             : 
     259             :     static MOZ_MUST_USE bool size_impl(JSContext* cx, const CallArgs& args);
     260             :     static MOZ_MUST_USE bool size(JSContext* cx, unsigned argc, Value* vp);
     261             :     static MOZ_MUST_USE bool has_impl(JSContext* cx, const CallArgs& args);
     262             :     static MOZ_MUST_USE bool add_impl(JSContext* cx, const CallArgs& args);
     263             :     static MOZ_MUST_USE bool add(JSContext* cx, unsigned argc, Value* vp);
     264             :     static MOZ_MUST_USE bool delete_impl(JSContext* cx, const CallArgs& args);
     265             :     static MOZ_MUST_USE bool delete_(JSContext* cx, unsigned argc, Value* vp);
     266             :     static MOZ_MUST_USE bool values_impl(JSContext* cx, const CallArgs& args);
     267             :     static MOZ_MUST_USE bool entries_impl(JSContext* cx, const CallArgs& args);
     268             :     static MOZ_MUST_USE bool entries(JSContext* cx, unsigned argc, Value* vp);
     269             :     static MOZ_MUST_USE bool clear_impl(JSContext* cx, const CallArgs& args);
     270             :     static MOZ_MUST_USE bool clear(JSContext* cx, unsigned argc, Value* vp);
     271             : };
     272             : 
     273             : class SetIteratorObject : public NativeObject
     274             : {
     275             :   public:
     276             :     static const Class class_;
     277             : 
     278             :     enum { TargetSlot, RangeSlot, KindSlot, SlotCount };
     279             : 
     280             :     static_assert(TargetSlot == ITERATOR_SLOT_TARGET,
     281             :                   "TargetSlot must match self-hosting define for iterated object slot.");
     282             :     static_assert(RangeSlot == ITERATOR_SLOT_RANGE,
     283             :                   "RangeSlot must match self-hosting define for range or index slot.");
     284             :     static_assert(KindSlot == ITERATOR_SLOT_ITEM_KIND,
     285             :                   "KindSlot must match self-hosting define for item kind slot.");
     286             : 
     287             :     static const JSFunctionSpec methods[];
     288             :     static SetIteratorObject* create(JSContext* cx, HandleObject setobj, ValueSet* data,
     289             :                                      SetObject::IteratorKind kind);
     290             :     static void finalize(FreeOp* fop, JSObject* obj);
     291             : 
     292             :     static MOZ_MUST_USE bool next(Handle<SetIteratorObject*> setIterator,
     293             :                                   HandleArrayObject resultObj, JSContext* cx);
     294             : 
     295             :     static JSObject* createResult(JSContext* cx);
     296             : 
     297             :   private:
     298             :     inline SetObject::IteratorKind kind() const;
     299             : };
     300             : 
     301             : using SetInitGetPrototypeOp = NativeObject* (*)(JSContext*, Handle<GlobalObject*>);
     302             : using SetInitIsBuiltinOp = bool (*)(HandleValue, JSContext*);
     303             : 
     304             : template <SetInitGetPrototypeOp getPrototypeOp, SetInitIsBuiltinOp isBuiltinOp>
     305             : static MOZ_MUST_USE bool
     306          61 : IsOptimizableInitForSet(JSContext* cx, HandleObject setObject, HandleValue iterable, bool* optimized)
     307             : {
     308          61 :     MOZ_ASSERT(!*optimized);
     309             : 
     310          61 :     if (!iterable.isObject())
     311           0 :         return true;
     312             : 
     313         122 :     RootedObject array(cx, &iterable.toObject());
     314          61 :     if (!IsPackedArray(array))
     315           9 :         return true;
     316             : 
     317             :     // Get the canonical prototype object.
     318         104 :     RootedNativeObject setProto(cx, getPrototypeOp(cx, cx->global()));
     319          52 :     if (!setProto)
     320           0 :         return false;
     321             : 
     322             :     // Ensures setObject's prototype is the canonical prototype.
     323          52 :     if (setObject->staticPrototype() != setProto)
     324           0 :         return true;
     325             : 
     326             :     // Look up the 'add' value on the prototype object.
     327          52 :     Shape* addShape = setProto->lookup(cx, cx->names().add);
     328          52 :     if (!addShape || !addShape->hasSlot())
     329           0 :         return true;
     330             : 
     331             :     // Get the referred value, ensure it holds the canonical add function.
     332         104 :     RootedValue add(cx, setProto->getSlot(addShape->slot()));
     333          52 :     if (!isBuiltinOp(add, cx))
     334           0 :         return true;
     335             : 
     336          52 :     ForOfPIC::Chain* stubChain = ForOfPIC::getOrCreate(cx);
     337          52 :     if (!stubChain)
     338           0 :         return false;
     339             : 
     340          52 :     return stubChain->tryOptimizeArray(cx, array.as<ArrayObject>(), optimized);
     341             : }
     342             : 
     343             : } /* namespace js */
     344             : 
     345             : #endif /* builtin_MapObject_h */

Generated by: LCOV version 1.13