1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
5 #include <stdint.h> // uint32_t
7 #include "js/ArrayBuffer.h" // JS::{DetachArrayBuffer,GetArrayBuffer{ByteLength,Data},IsArrayBufferObject,NewArrayBufferWithUserOwnedContents}
8 #include "js/GCAPI.h" // JS::AutoCheckCannotGC, JS_GC
9 #include "js/RootingAPI.h" // JS::Rooted
10 #include "jsapi-tests/tests.h"
11 #include "vm/ArrayBufferObject.h"
14 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
16 constexpr size_t testDataLength
= sizeof(testData
);
18 static void GC(JSContext
* cx
) {
20 // Trigger another to wait for background finalization to end.
24 BEGIN_TEST(testArrayBufferWithUserOwnedContents
) {
25 JS::Rooted
<JSObject
*> obj(cx
, JS::NewArrayBufferWithUserOwnedContents(
26 cx
, testDataLength
, testData
));
28 CHECK(VerifyObject(obj
, testDataLength
));
30 JS::DetachArrayBuffer(cx
, obj
);
32 CHECK(VerifyObject(obj
, 0));
37 bool VerifyObject(JS::HandleObject obj
, uint32_t length
) {
38 JS::AutoCheckCannotGC nogc
;
41 CHECK(JS::IsArrayBufferObject(obj
));
42 CHECK_EQUAL(JS::GetArrayBufferByteLength(obj
), length
);
44 const char* data
= reinterpret_cast<const char*>(
45 JS::GetArrayBufferData(obj
, &sharedDummy
, nogc
));
46 if (length
== testDataLength
) {
48 CHECK(testData
== data
);
54 END_TEST(testArrayBufferWithUserOwnedContents
)