Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mozilla/ArrayUtils.h" |
michael@0 | 6 | #include "mozilla/PodOperations.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "jsapi-tests/tests.h" |
michael@0 | 9 | |
michael@0 | 10 | using mozilla::ArrayLength; |
michael@0 | 11 | using mozilla::PodEqual; |
michael@0 | 12 | |
michael@0 | 13 | static const jschar arr[] = { |
michael@0 | 14 | 'h', 'i', ',', 'd', 'o', 'n', '\'', 't', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', 'm', 'e', '\0' |
michael@0 | 15 | }; |
michael@0 | 16 | static const size_t arrlen = ArrayLength(arr) - 1; |
michael@0 | 17 | |
michael@0 | 18 | static int finalized1 = 0; |
michael@0 | 19 | static int finalized2 = 0; |
michael@0 | 20 | |
michael@0 | 21 | static void |
michael@0 | 22 | finalize_str(const JSStringFinalizer *fin, jschar *chars); |
michael@0 | 23 | |
michael@0 | 24 | static const JSStringFinalizer finalizer1 = { finalize_str }; |
michael@0 | 25 | static const JSStringFinalizer finalizer2 = { finalize_str }; |
michael@0 | 26 | |
michael@0 | 27 | static void |
michael@0 | 28 | finalize_str(const JSStringFinalizer *fin, jschar *chars) |
michael@0 | 29 | { |
michael@0 | 30 | if (chars && PodEqual(const_cast<const jschar *>(chars), arr, arrlen)) { |
michael@0 | 31 | if (fin == &finalizer1) { |
michael@0 | 32 | ++finalized1; |
michael@0 | 33 | } else if (fin == &finalizer2) { |
michael@0 | 34 | ++finalized2; |
michael@0 | 35 | } |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | BEGIN_TEST(testExternalStrings) |
michael@0 | 40 | { |
michael@0 | 41 | const unsigned N = 1000; |
michael@0 | 42 | |
michael@0 | 43 | for (unsigned i = 0; i < N; ++i) { |
michael@0 | 44 | CHECK(JS_NewExternalString(cx, arr, arrlen, &finalizer1)); |
michael@0 | 45 | CHECK(JS_NewExternalString(cx, arr, arrlen, &finalizer2)); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // clear that newborn root |
michael@0 | 49 | JS_NewUCStringCopyN(cx, arr, arrlen); |
michael@0 | 50 | |
michael@0 | 51 | JS_GC(rt); |
michael@0 | 52 | |
michael@0 | 53 | // a generous fudge factor to account for strings rooted by conservative gc |
michael@0 | 54 | const unsigned epsilon = 10; |
michael@0 | 55 | |
michael@0 | 56 | CHECK((N - finalized1) < epsilon); |
michael@0 | 57 | CHECK((N - finalized2) < epsilon); |
michael@0 | 58 | |
michael@0 | 59 | return true; |
michael@0 | 60 | } |
michael@0 | 61 | END_TEST(testExternalStrings) |