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