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 "jsapi-tests/tests.h"
8 static const char16_t arr
[] = u
"hi, don't delete me";
9 static const size_t arrlen
= js_strlen(arr
);
11 static int finalized1
= 0;
12 static int finalized2
= 0;
14 struct ExternalStringCallbacks
: public JSExternalStringCallbacks
{
15 int* finalizedCount
= nullptr;
17 explicit ExternalStringCallbacks(int* finalizedCount
)
18 : finalizedCount(finalizedCount
) {}
20 void finalize(char16_t
* chars
) const override
{
21 MOZ_ASSERT(chars
== arr
);
25 size_t sizeOfBuffer(const char16_t
* chars
,
26 mozilla::MallocSizeOf mallocSizeOf
) const override
{
27 MOZ_CRASH("Unexpected call");
31 static const ExternalStringCallbacks
callbacks1(&finalized1
);
32 static const ExternalStringCallbacks
callbacks2(&finalized2
);
34 BEGIN_TEST(testExternalStrings
) {
35 const unsigned N
= 1000;
37 for (unsigned i
= 0; i
< N
; ++i
) {
38 CHECK(JS_NewExternalString(cx
, arr
, arrlen
, &callbacks1
));
39 CHECK(JS_NewExternalString(cx
, arr
, arrlen
, &callbacks2
));
44 // a generous fudge factor to account for strings rooted by conservative gc
45 const unsigned epsilon
= 10;
47 CHECK((N
- finalized1
) < epsilon
);
48 CHECK((N
- finalized2
) < epsilon
);
52 END_TEST(testExternalStrings
)