summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testExternalArrayBuffer.cpp
blob: cb864d37c6e436a9000c6f57e4d1859a0564939f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 */

#include "jsfriendapi.h"
#include "jsapi-tests/tests.h"
#include "vm/ArrayBufferObject.h"

char test_data[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

static void GC(JSContext* cx)
{
    JS_GC(cx);
    // Trigger another to wait for background finalization to end.
    JS_GC(cx);
}

BEGIN_TEST(testExternalArrayBuffer)
{
    size_t length = sizeof(test_data);
    JS::RootedObject obj(cx, JS_NewArrayBufferWithExternalContents(cx, length, test_data));
    GC(cx);
    CHECK(VerifyObject(obj, length));
    GC(cx);
    JS_DetachArrayBuffer(cx, obj);
    GC(cx);
    CHECK(VerifyObject(obj, 0));

    return true;
}

bool VerifyObject(JS::HandleObject obj, uint32_t length)
{
    JS::AutoCheckCannotGC nogc;

    CHECK(obj);
    CHECK(JS_IsArrayBufferObject(obj));
    CHECK_EQUAL(JS_GetArrayBufferByteLength(obj), length);
    bool sharedDummy;
    const char* data =
        reinterpret_cast<const char*>(JS_GetArrayBufferData(obj, &sharedDummy, nogc));
    CHECK(data);
    CHECK(test_data == data);

    return true;
}

END_TEST(testExternalArrayBuffer)