|
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/. */ |
|
4 |
|
5 #include "mozilla/ArrayUtils.h" |
|
6 #include "mozilla/PodOperations.h" |
|
7 |
|
8 #include "jsapi-tests/tests.h" |
|
9 |
|
10 using mozilla::ArrayLength; |
|
11 using mozilla::PodEqual; |
|
12 |
|
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; |
|
17 |
|
18 static int finalized1 = 0; |
|
19 static int finalized2 = 0; |
|
20 |
|
21 static void |
|
22 finalize_str(const JSStringFinalizer *fin, jschar *chars); |
|
23 |
|
24 static const JSStringFinalizer finalizer1 = { finalize_str }; |
|
25 static const JSStringFinalizer finalizer2 = { finalize_str }; |
|
26 |
|
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 } |
|
38 |
|
39 BEGIN_TEST(testExternalStrings) |
|
40 { |
|
41 const unsigned N = 1000; |
|
42 |
|
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 } |
|
47 |
|
48 // clear that newborn root |
|
49 JS_NewUCStringCopyN(cx, arr, arrlen); |
|
50 |
|
51 JS_GC(rt); |
|
52 |
|
53 // a generous fudge factor to account for strings rooted by conservative gc |
|
54 const unsigned epsilon = 10; |
|
55 |
|
56 CHECK((N - finalized1) < epsilon); |
|
57 CHECK((N - finalized2) < epsilon); |
|
58 |
|
59 return true; |
|
60 } |
|
61 END_TEST(testExternalStrings) |