1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testExternalStrings.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,61 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "mozilla/ArrayUtils.h" 1.9 +#include "mozilla/PodOperations.h" 1.10 + 1.11 +#include "jsapi-tests/tests.h" 1.12 + 1.13 +using mozilla::ArrayLength; 1.14 +using mozilla::PodEqual; 1.15 + 1.16 +static const jschar arr[] = { 1.17 + 'h', 'i', ',', 'd', 'o', 'n', '\'', 't', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', 'm', 'e', '\0' 1.18 +}; 1.19 +static const size_t arrlen = ArrayLength(arr) - 1; 1.20 + 1.21 +static int finalized1 = 0; 1.22 +static int finalized2 = 0; 1.23 + 1.24 +static void 1.25 +finalize_str(const JSStringFinalizer *fin, jschar *chars); 1.26 + 1.27 +static const JSStringFinalizer finalizer1 = { finalize_str }; 1.28 +static const JSStringFinalizer finalizer2 = { finalize_str }; 1.29 + 1.30 +static void 1.31 +finalize_str(const JSStringFinalizer *fin, jschar *chars) 1.32 +{ 1.33 + if (chars && PodEqual(const_cast<const jschar *>(chars), arr, arrlen)) { 1.34 + if (fin == &finalizer1) { 1.35 + ++finalized1; 1.36 + } else if (fin == &finalizer2) { 1.37 + ++finalized2; 1.38 + } 1.39 + } 1.40 +} 1.41 + 1.42 +BEGIN_TEST(testExternalStrings) 1.43 +{ 1.44 + const unsigned N = 1000; 1.45 + 1.46 + for (unsigned i = 0; i < N; ++i) { 1.47 + CHECK(JS_NewExternalString(cx, arr, arrlen, &finalizer1)); 1.48 + CHECK(JS_NewExternalString(cx, arr, arrlen, &finalizer2)); 1.49 + } 1.50 + 1.51 + // clear that newborn root 1.52 + JS_NewUCStringCopyN(cx, arr, arrlen); 1.53 + 1.54 + JS_GC(rt); 1.55 + 1.56 + // a generous fudge factor to account for strings rooted by conservative gc 1.57 + const unsigned epsilon = 10; 1.58 + 1.59 + CHECK((N - finalized1) < epsilon); 1.60 + CHECK((N - finalized2) < epsilon); 1.61 + 1.62 + return true; 1.63 +} 1.64 +END_TEST(testExternalStrings)