Bug 1461938 part 24 - Move randomNumberGenerator from JSCompartment to JS::Realm. r=evilpie

This commit is contained in:
Jan de Mooij 2018-05-23 15:26:33 +02:00
parent 4975a53b6d
commit 1c6e8b72c0
7 changed files with 25 additions and 23 deletions

View File

@ -4428,8 +4428,7 @@ SetRNGState(JSContext* cx, unsigned argc, Value* vp)
return false;
}
cx->compartment()->ensureRandomNumberGenerator();
cx->compartment()->randomNumberGenerator.ref().setState(seed0, seed1);
cx->realm()->getOrCreateRandomNumberGenerator().setState(seed0, seed1);
args.rval().setUndefined();
return true;

View File

@ -257,7 +257,7 @@ CompileCompartment::runtime()
const void*
CompileCompartment::addressOfRandomNumberGenerator()
{
return compartment()->randomNumberGenerator.ptr();
return JS::GetRealmForCompartment(compartment())->addressOfRandomNumberGenerator();
}
const JitCompartment*

View File

@ -1394,7 +1394,7 @@ IonBuilder::inlineMathRandom(CallInfo& callInfo)
// MRandom JIT code directly accesses the RNG. It's (barely) possible to
// inline Math.random without it having been called yet, so ensure RNG
// state that isn't guaranteed to be initialized already.
script()->compartment()->ensureRandomNumberGenerator();
script()->realm()->getOrCreateRandomNumberGenerator();
callInfo.setImplicitlyUsedUnchecked();

View File

@ -716,22 +716,22 @@ js::GenerateXorShift128PlusSeed(mozilla::Array<uint64_t, 2>& seed)
} while (seed[0] == 0 && seed[1] == 0);
}
void
JSCompartment::ensureRandomNumberGenerator()
mozilla::non_crypto::XorShift128PlusRNG&
Realm::getOrCreateRandomNumberGenerator()
{
if (randomNumberGenerator.isNothing()) {
if (randomNumberGenerator_.isNothing()) {
mozilla::Array<uint64_t, 2> seed;
GenerateXorShift128PlusSeed(seed);
randomNumberGenerator.emplace(seed[0], seed[1]);
randomNumberGenerator_.emplace(seed[0], seed[1]);
}
return randomNumberGenerator_.ref();
}
double
js::math_random_impl(JSContext* cx)
{
JSCompartment* comp = cx->compartment();
comp->ensureRandomNumberGenerator();
return comp->randomNumberGenerator.ref().nextDouble();
return cx->realm()->getOrCreateRandomNumberGenerator().nextDouble();
}
bool

View File

@ -1330,10 +1330,9 @@ Realm::addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf,
}
HashNumber
JSCompartment::randomHashCode()
Realm::randomHashCode()
{
ensureRandomNumberGenerator();
return HashNumber(randomNumberGenerator.ref().next());
return HashNumber(getOrCreateRandomNumberGenerator().next());
}
mozilla::HashCodeScrambler

View File

@ -751,18 +751,10 @@ struct JSCompartment
void findOutgoingEdges(js::gc::ZoneComponentFinder& finder);
// Random number generator for Math.random().
mozilla::Maybe<mozilla::non_crypto::XorShift128PlusRNG> randomNumberGenerator;
// Initialize randomNumberGenerator if needed.
void ensureRandomNumberGenerator();
private:
mozilla::non_crypto::XorShift128PlusRNG randomKeyGenerator_;
public:
js::HashNumber randomHashCode();
mozilla::HashCodeScrambler randomHashCodeScrambler();
static size_t offsetOfRegExps() {
@ -822,6 +814,9 @@ class JS::Realm : public JSCompartment
friend class js::AutoSetNewObjectMetadata;
js::NewObjectMetadataState objectMetadataState_ { js::ImmediateMetadata() };
// Random number generator for Math.random().
mozilla::Maybe<mozilla::non_crypto::XorShift128PlusRNG> randomNumberGenerator_;
JSPrincipals* principals_ = nullptr;
// Used by memory reporters and invalid otherwise.
@ -1224,6 +1219,15 @@ class JS::Realm : public JSCompartment
bool ensureDelazifyScriptsForDebugger(JSContext* cx);
void clearBreakpointsIn(js::FreeOp* fop, js::Debugger* dbg, JS::HandleObject handler);
// Initializes randomNumberGenerator if needed.
mozilla::non_crypto::XorShift128PlusRNG& getOrCreateRandomNumberGenerator();
const void* addressOfRandomNumberGenerator() const {
return randomNumberGenerator_.ptr();
}
js::HashNumber randomHashCode();
};
namespace js {

View File

@ -49,7 +49,7 @@ Symbol::new_(JSContext* cx, JS::SymbolCode code, JSString* description)
Symbol* sym;
{
AutoAtomsRealm ar(cx, lock);
sym = newInternal(cx, code, cx->compartment()->randomHashCode(), atom, lock);
sym = newInternal(cx, code, cx->realm()->randomHashCode(), atom, lock);
}
if (sym)
cx->markAtom(sym);