From 3e53164b38fbbe73561f744e83392a8774f4c459 Mon Sep 17 00:00:00 2001 From: Jason Orendorff Date: Mon, 23 Jun 2014 10:57:03 -0500 Subject: [PATCH] Bug 645417, part 26 - Update jsid sorting for JS_MORE_DETERMINISTIC. r=luke. Unfortunately, with symbols, the original goal of this code is apparently impossible. For two unique symbols with the same description, short of doing extra bookkeeping at run time, there's no ordering that's sure to be consistent across runs. The new code orders all other symbols. --HG-- extra : rebase_source : 0734906d046e577d4c7d65149ec688dd0b80e885 --- js/src/jsiter.cpp | 54 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/js/src/jsiter.cpp b/js/src/jsiter.cpp index 434f5345325..4763b493767 100644 --- a/js/src/jsiter.cpp +++ b/js/src/jsiter.cpp @@ -201,13 +201,53 @@ struct SortComparatorIds bool operator()(jsid a, jsid b, bool *lessOrEqualp) { - /* Pick an arbitrary total order on jsids that is stable across executions. */ - RootedString astr(cx, IdToString(cx, a)); - if (!astr) - return false; - RootedString bstr(cx, IdToString(cx, b)); - if (!bstr) - return false; + // Pick an arbitrary order on jsids that is as stable as possible + // across executions. + if (a == b) { + *lessOrEqualp = true; + return true; + } + + size_t ta = JSID_BITS(a) & JSID_TYPE_MASK; + size_t tb = JSID_BITS(b) & JSID_TYPE_MASK; + if (ta != tb) { + *lessOrEqualp = (ta <= tb); + return true; + } + + if (JSID_IS_INT(a)) { + *lessOrEqualp = (JSID_TO_INT(a) <= JSID_TO_INT(b)); + return true; + } + + RootedString astr(cx), bstr(cx); + if (JSID_IS_SYMBOL(a)) { + MOZ_ASSERT(JSID_IS_SYMBOL(b)); + JS::SymbolCode ca = JSID_TO_SYMBOL(a)->code(); + JS::SymbolCode cb = JSID_TO_SYMBOL(b)->code(); + if (ca != cb) { + *lessOrEqualp = uint32_t(ca) <= uint32_t(cb); + return true; + } + JS_ASSERT(ca == JS::SymbolCode::InSymbolRegistry || ca == JS::SymbolCode::Unique); + astr = JSID_TO_SYMBOL(a)->description(); + bstr = JSID_TO_SYMBOL(b)->description(); + if (!astr || !bstr) { + *lessOrEqualp = !astr; + return true; + } + + // Fall through to string comparison on the descriptions. The sort + // order is nondeterministic if two different unique symbols have + // the same description. + } else { + astr = IdToString(cx, a); + if (!astr) + return false; + bstr = IdToString(cx, b); + if (!bstr) + return false; + } int32_t result; if (!CompareStrings(cx, astr, bstr, &result))