From 1da2a378bbd5fa7d33c3c25eea5c9ec7ec0e96d9 Mon Sep 17 00:00:00 2001 From: Jan de Mooij Date: Tue, 22 May 2018 15:02:37 +0200 Subject: [PATCH] Bug 1461938 part 13 - Move dtoaCache and newProxyCache from JSCompartment to JS::Realm. r=anba --- js/src/gc/GC.cpp | 4 ++-- js/src/gc/PrivateIterators-inl.h | 3 ++- js/src/jsapi-tests/testIndexToString.cpp | 2 +- js/src/jsnum.cpp | 22 +++++++++++----------- js/src/vm/JSCompartment.cpp | 8 +++++--- js/src/vm/JSCompartment.h | 14 +++++--------- js/src/vm/ProxyObject.cpp | 7 ++++--- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/js/src/gc/GC.cpp b/js/src/gc/GC.cpp index 53bce862daca..7015ccff019e 100644 --- a/js/src/gc/GC.cpp +++ b/js/src/gc/GC.cpp @@ -4013,8 +4013,8 @@ GCRuntime::purgeRuntime() { gcstats::AutoPhase ap(stats(), gcstats::PhaseKind::PURGE); - for (GCCompartmentsIter comp(rt); !comp.done(); comp.next()) - comp->purge(); + for (GCRealmsIter realm(rt); !realm.done(); realm.next()) + realm->purge(); for (GCZonesIter zone(rt); !zone.done(); zone.next()) { zone->atomCache().clearAndShrink(); diff --git a/js/src/gc/PrivateIterators-inl.h b/js/src/gc/PrivateIterators-inl.h index 16b668b0a917..6e8d3c0b12e9 100644 --- a/js/src/gc/PrivateIterators-inl.h +++ b/js/src/gc/PrivateIterators-inl.h @@ -89,7 +89,8 @@ class GCZonesIter JS::Zone* operator->() const { return get(); } }; -typedef CompartmentsIterT GCCompartmentsIter; +using GCCompartmentsIter = CompartmentsIterT; +using GCRealmsIter = RealmsIterT; /* Iterates over all zones in the current sweep group. */ class SweepGroupZonesIter { diff --git a/js/src/jsapi-tests/testIndexToString.cpp b/js/src/jsapi-tests/testIndexToString.cpp index 78de99db3db9..5ae52bcc115d 100644 --- a/js/src/jsapi-tests/testIndexToString.cpp +++ b/js/src/jsapi-tests/testIndexToString.cpp @@ -56,7 +56,7 @@ BEGIN_TEST(testIndexToString) CHECK(str); if (!js::StaticStrings::hasUint(u)) - CHECK(cx->compartment()->dtoaCache.lookup(10, u) == str); + CHECK(cx->realm()->dtoaCache.lookup(10, u) == str); bool match = false; CHECK(JS_StringEqualsAscii(cx, str, tests[i].expected, &match)); diff --git a/js/src/jsnum.cpp b/js/src/jsnum.cpp index 8a7048c27a5d..0c4b6090f4dd 100644 --- a/js/src/jsnum.cpp +++ b/js/src/jsnum.cpp @@ -568,8 +568,8 @@ MOZ_ALWAYS_INLINE static JSFlatString* LookupDtoaCache(JSContext* cx, double d) { - if (JSCompartment* comp = cx->compartment()) { - if (JSFlatString* str = comp->dtoaCache.lookup(10, d)) + if (Realm* realm = cx->realm()) { + if (JSFlatString* str = realm->dtoaCache.lookup(10, d)) return str; } @@ -580,8 +580,8 @@ MOZ_ALWAYS_INLINE static void CacheNumber(JSContext* cx, double d, JSFlatString* str) { - if (JSCompartment* comp = cx->compartment()) - comp->dtoaCache.cache(10, d, str); + if (Realm* realm = cx->realm()) + realm->dtoaCache.cache(10, d, str); } MOZ_ALWAYS_INLINE @@ -1327,7 +1327,7 @@ NumberToStringWithBase(JSContext* cx, double d, int base) ToCStringBuf cbuf; char* numStr; - JSCompartment* comp = cx->compartment(); + Realm* realm = cx->realm(); int32_t i; bool isBase10Int = false; @@ -1343,14 +1343,14 @@ NumberToStringWithBase(JSContext* cx, double d, int base) return cx->staticStrings().getUnit(c); } - if (JSFlatString* str = comp->dtoaCache.lookup(base, d)) + if (JSFlatString* str = realm->dtoaCache.lookup(base, d)) return str; size_t len; numStr = Int32ToCString(&cbuf, i, &len, base); MOZ_ASSERT(!cbuf.dbuf && numStr >= cbuf.sbuf && numStr < cbuf.sbuf + cbuf.sbufSize); } else { - if (JSFlatString* str = comp->dtoaCache.lookup(base, d)) + if (JSFlatString* str = realm->dtoaCache.lookup(base, d)) return str; numStr = FracNumberToCString(cx, &cbuf, d, base); @@ -1371,7 +1371,7 @@ NumberToStringWithBase(JSContext* cx, double d, int base) if (isBase10Int && i >= 0) s->maybeInitializeIndex(i); - comp->dtoaCache.cache(base, d, s); + realm->dtoaCache.cache(base, d, s); return s; } @@ -1422,8 +1422,8 @@ js::IndexToString(JSContext* cx, uint32_t index) if (StaticStrings::hasUint(index)) return cx->staticStrings().getUint(index); - JSCompartment* c = cx->compartment(); - if (JSFlatString* str = c->dtoaCache.lookup(10, index)) + Realm* realm = cx->realm(); + if (JSFlatString* str = realm->dtoaCache.lookup(10, index)) return str; Latin1Char buffer[JSFatInlineString::MAX_LENGTH_LATIN1 + 1]; @@ -1437,7 +1437,7 @@ js::IndexToString(JSContext* cx, uint32_t index) if (!str) return nullptr; - c->dtoaCache.cache(10, index, str); + realm->dtoaCache.cache(10, index, str); return str; } diff --git a/js/src/vm/JSCompartment.cpp b/js/src/vm/JSCompartment.cpp index 31f339f36b50..59e74381a584 100644 --- a/js/src/vm/JSCompartment.cpp +++ b/js/src/vm/JSCompartment.cpp @@ -759,7 +759,9 @@ JSCompartment::sweepAfterMinorGC(JSTracer* trc) table.sweepAfterMinorGC(); crossCompartmentWrappers.sweepAfterMinorGC(trc); - dtoaCache.purge(); + + Realm* realm = JS::GetRealmForCompartment(this); + realm->dtoaCache.purge(); } void @@ -902,7 +904,7 @@ JSCompartment::fixupAfterMovingGC() Realm* realm = JS::GetRealmForCompartment(this); - purge(); + realm->purge(); realm->fixupGlobal(); objectGroups.fixupTablesAfterMovingGC(); realm->fixupScriptMapsAfterMovingGC(); @@ -994,7 +996,7 @@ Realm::checkScriptMapsAfterMovingGC() #endif void -JSCompartment::purge() +Realm::purge() { dtoaCache.purge(); newProxyCache.purge(); diff --git a/js/src/vm/JSCompartment.h b/js/src/vm/JSCompartment.h index 2e7206422dd7..1c36e9ad8076 100644 --- a/js/src/vm/JSCompartment.h +++ b/js/src/vm/JSCompartment.h @@ -851,8 +851,6 @@ struct JSCompartment void sweepNativeIterators(); void sweepTemplateObjects(); - void purge(); - static void fixupCrossCompartmentWrappersAfterMovingGC(JSTracer* trc); void fixupAfterMovingGC(); @@ -860,9 +858,6 @@ struct JSCompartment void findOutgoingEdges(js::gc::ZoneComponentFinder& finder); - js::DtoaCache dtoaCache; - js::NewProxyCache newProxyCache; - // Random number generator for Math.random(). mozilla::Maybe randomNumberGenerator; @@ -881,10 +876,6 @@ struct JSCompartment return offsetof(JSCompartment, regExps); } - private: - JSCompartment* thisForCtor() { return this; } - - public: // // The Debugger observes execution on a frame-by-frame basis. The // invariants of JSCompartment's debug mode bits, JSScript::isDebuggee, @@ -1071,6 +1062,9 @@ class JS::Realm : public JSCompartment // WebAssembly state for the realm. js::wasm::Realm wasm; + js::DtoaCache dtoaCache; + js::NewProxyCache newProxyCache; + js::ScriptCountsMap* scriptCountsMap = nullptr; js::ScriptNameMap* scriptNameMap = nullptr; js::DebugScriptMap* debugScriptMap = nullptr; @@ -1154,6 +1148,8 @@ class JS::Realm : public JSCompartment void clearScriptCounts(); void clearScriptNames(); + void purge(); + void fixupScriptMapsAfterMovingGC(); #ifdef JSGC_HASH_TABLE_CHECKS diff --git a/js/src/vm/ProxyObject.cpp b/js/src/vm/ProxyObject.cpp index c240270bdb6e..28c2ec1dd481 100644 --- a/js/src/vm/ProxyObject.cpp +++ b/js/src/vm/ProxyObject.cpp @@ -160,12 +160,12 @@ ProxyObject::create(JSContext* cx, const Class* clasp, Handle proto { MOZ_ASSERT(clasp->isProxy()); - JSCompartment* comp = cx->compartment(); + Realm* realm = cx->realm(); RootedObjectGroup group(cx); RootedShape shape(cx); // Try to look up the group and shape in the NewProxyCache. - if (!comp->newProxyCache.lookup(clasp, proto, group.address(), shape.address())) { + if (!realm->newProxyCache.lookup(clasp, proto, group.address(), shape.address())) { group = ObjectGroup::defaultNewGroup(cx, clasp, proto, nullptr); if (!group) return cx->alreadyReportedOOM(); @@ -174,7 +174,8 @@ ProxyObject::create(JSContext* cx, const Class* clasp, Handle proto if (!shape) return cx->alreadyReportedOOM(); - comp->newProxyCache.add(group, shape); + MOZ_ASSERT(group->realm() == realm); + realm->newProxyCache.add(group, shape); } gc::InitialHeap heap = GetInitialHeap(newKind, clasp);