michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/PodOperations.h" michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: using mozilla::ArrayLength; michael@0: using mozilla::PodEqual; michael@0: michael@0: static const jschar arr[] = { michael@0: 'h', 'i', ',', 'd', 'o', 'n', '\'', 't', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', 'm', 'e', '\0' michael@0: }; michael@0: static const size_t arrlen = ArrayLength(arr) - 1; michael@0: michael@0: static int finalized1 = 0; michael@0: static int finalized2 = 0; michael@0: michael@0: static void michael@0: finalize_str(const JSStringFinalizer *fin, jschar *chars); michael@0: michael@0: static const JSStringFinalizer finalizer1 = { finalize_str }; michael@0: static const JSStringFinalizer finalizer2 = { finalize_str }; michael@0: michael@0: static void michael@0: finalize_str(const JSStringFinalizer *fin, jschar *chars) michael@0: { michael@0: if (chars && PodEqual(const_cast(chars), arr, arrlen)) { michael@0: if (fin == &finalizer1) { michael@0: ++finalized1; michael@0: } else if (fin == &finalizer2) { michael@0: ++finalized2; michael@0: } michael@0: } michael@0: } michael@0: michael@0: BEGIN_TEST(testExternalStrings) michael@0: { michael@0: const unsigned N = 1000; michael@0: michael@0: for (unsigned i = 0; i < N; ++i) { michael@0: CHECK(JS_NewExternalString(cx, arr, arrlen, &finalizer1)); michael@0: CHECK(JS_NewExternalString(cx, arr, arrlen, &finalizer2)); michael@0: } michael@0: michael@0: // clear that newborn root michael@0: JS_NewUCStringCopyN(cx, arr, arrlen); michael@0: michael@0: JS_GC(rt); michael@0: michael@0: // a generous fudge factor to account for strings rooted by conservative gc michael@0: const unsigned epsilon = 10; michael@0: michael@0: CHECK((N - finalized1) < epsilon); michael@0: CHECK((N - finalized2) < epsilon); michael@0: michael@0: return true; michael@0: } michael@0: END_TEST(testExternalStrings)