Add support for V8 ArrayBuffers (issue #244)

This commit is contained in:
Christopher Cifra
2018-04-10 15:37:33 -04:00
committed by Marshall Greenblatt
parent bb28b85bdd
commit 4315f3b724
16 changed files with 775 additions and 8 deletions

View File

@ -57,6 +57,8 @@ enum V8TestMode {
V8TEST_EMPTY_STRING_CREATE,
V8TEST_ARRAY_CREATE,
V8TEST_ARRAY_VALUE,
V8TEST_ARRAY_BUFFER,
V8TEST_ARRAY_BUFFER_VALUE,
V8TEST_OBJECT_CREATE,
V8TEST_OBJECT_USERDATA,
V8TEST_OBJECT_ACCESSOR,
@ -159,6 +161,12 @@ class V8RendererTest : public ClientAppRenderer::Delegate,
case V8TEST_ARRAY_VALUE:
RunArrayValueTest();
break;
case V8TEST_ARRAY_BUFFER:
RunArrayBufferTest();
break;
case V8TEST_ARRAY_BUFFER_VALUE:
RunArrayBufferValueTest();
break;
case V8TEST_OBJECT_CREATE:
RunObjectCreateTest();
break;
@ -543,6 +551,117 @@ class V8RendererTest : public ClientAppRenderer::Delegate,
DestroyTest();
}
void RunArrayBufferTest() {
class TestArrayBufferReleaseCallback
: public CefV8ArrayBufferReleaseCallback {
public:
TestArrayBufferReleaseCallback(bool* destructorCalled,
bool* releaseBufferCalled)
: destructorCalled_(destructorCalled),
releaseBufferCalled_(releaseBufferCalled) {}
~TestArrayBufferReleaseCallback() { *destructorCalled_ = true; }
void ReleaseBuffer(void* buffer) { *releaseBufferCalled_ = true; }
IMPLEMENT_REFCOUNTING(TestArrayBufferReleaseCallback);
private:
bool* destructorCalled_;
bool* releaseBufferCalled_;
};
CefRefPtr<CefV8Context> context = GetContext();
bool destructorCalled = false;
bool releaseBufferCalled = false;
bool neuteredDestructorCalled = false;
bool neuteredReleaseBufferCalled = false;
// Enter the V8 context.
EXPECT_TRUE(context->Enter());
{
int static_data[16];
CefRefPtr<CefV8Value> value;
CefRefPtr<TestArrayBufferReleaseCallback> release_callback =
new TestArrayBufferReleaseCallback(&destructorCalled,
&releaseBufferCalled);
CefRefPtr<CefV8Value> neuteredValue;
CefRefPtr<TestArrayBufferReleaseCallback> neuteredReleaseCallback =
new TestArrayBufferReleaseCallback(&neuteredDestructorCalled,
&neuteredReleaseBufferCalled);
value = CefV8Value::CreateArrayBuffer(static_data, sizeof(static_data),
release_callback);
neuteredValue = CefV8Value::CreateArrayBuffer(
static_data, sizeof(static_data), neuteredReleaseCallback);
EXPECT_TRUE(value.get());
EXPECT_TRUE(value->IsArrayBuffer());
EXPECT_TRUE(value->IsObject());
EXPECT_FALSE(value->HasValue(0));
EXPECT_FALSE(destructorCalled);
EXPECT_TRUE(value->GetArrayBufferReleaseCallback().get() != nullptr);
EXPECT_TRUE(((TestArrayBufferReleaseCallback*)value
->GetArrayBufferReleaseCallback()
.get()) == release_callback);
EXPECT_TRUE(neuteredValue->NeuterArrayBuffer());
}
// Exit the V8 context.
EXPECT_TRUE(destructorCalled);
EXPECT_TRUE(releaseBufferCalled);
EXPECT_TRUE(neuteredDestructorCalled);
EXPECT_FALSE(neuteredReleaseBufferCalled);
EXPECT_TRUE(context->Exit());
DestroyTest();
}
void RunArrayBufferValueTest() {
class TestArrayBufferReleaseCallback
: public CefV8ArrayBufferReleaseCallback {
public:
TestArrayBufferReleaseCallback() {}
~TestArrayBufferReleaseCallback() {}
void ReleaseBuffer(void* buffer) {}
IMPLEMENT_REFCOUNTING(TestArrayBufferReleaseCallback);
};
CefRefPtr<CefV8Context> context = GetContext();
// Enter the V8 context.
CefRefPtr<CefV8Value> value;
CefRefPtr<TestArrayBufferReleaseCallback> owner =
new TestArrayBufferReleaseCallback();
EXPECT_TRUE(context->Enter());
int static_data[16];
static_data[0] = 3;
value =
CefV8Value::CreateArrayBuffer(static_data, sizeof(static_data), owner);
CefRefPtr<CefV8Value> object = context->GetGlobal();
EXPECT_TRUE(object.get());
object->SetValue("arr", value, V8_PROPERTY_ATTRIBUTE_NONE);
std::string test =
"let data = new Int32Array(window.arr); data[0] += data.length";
CefRefPtr<CefV8Value> retval;
CefRefPtr<CefV8Exception> exception;
EXPECT_TRUE(context->Eval(test, CefString(), 0, retval, exception));
if (exception.get())
ADD_FAILURE() << exception->GetMessage().c_str();
EXPECT_TRUE(static_data[0] == 19);
EXPECT_TRUE(value->GetArrayBufferReleaseCallback().get() != nullptr);
EXPECT_TRUE(value->NeuterArrayBuffer());
// Exit the V8 context.
EXPECT_TRUE(context->Exit());
DestroyTest();
}
void RunObjectCreateTest() {
CefRefPtr<CefV8Context> context = GetContext();
@ -2972,6 +3091,8 @@ V8_TEST(StringCreate, V8TEST_STRING_CREATE);
V8_TEST(EmptyStringCreate, V8TEST_EMPTY_STRING_CREATE);
V8_TEST(ArrayCreate, V8TEST_ARRAY_CREATE);
V8_TEST(ArrayValue, V8TEST_ARRAY_VALUE);
V8_TEST(ArrayBuffer, V8TEST_ARRAY_BUFFER);
V8_TEST(ArrayBufferValue, V8TEST_ARRAY_BUFFER_VALUE);
V8_TEST(ObjectCreate, V8TEST_OBJECT_CREATE);
V8_TEST(ObjectUserData, V8TEST_OBJECT_USERDATA);
V8_TEST(ObjectAccessor, V8TEST_OBJECT_ACCESSOR);