예제 #1
0
static HashNumber
HashValue(const Value& v, const mozilla::HashCodeScrambler& hcs)
{
    // HashableValue::setValue normalizes values so that the SameValue relation
    // on HashableValues is the same as the == relationship on
    // value.asRawBits(). So why not just return that? Security.
    //
    // To avoid revealing GC of atoms, string-based hash codes are computed
    // from the string contents rather than any pointer; to avoid revealing
    // addresses, pointer-based hash codes are computed using the
    // HashCodeScrambler.

    if (v.isString())
        return v.toString()->asAtom().hash();
    if (v.isSymbol()) {
        Symbol* sym = v.toSymbol();
        if (sym->isWellKnownSymbol())
            return HashNumber(sym->code());
        if (sym->code() == SymbolCode::InSymbolRegistry)
            return sym->description()->hash();
        return hcs.scramble(v.asRawBits());
    }
    if (v.isObject())
        return hcs.scramble(v.asRawBits());

    MOZ_ASSERT(!v.isGCThing(), "do not reveal pointers via hash codes");
    return v.asRawBits();
}