Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1306)

Unified Diff: test/cctest/test-api.cc

Issue 15001041: Externalization API for ArrayBuffer (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: CR feedback Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« include/v8.h ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index c8f67de0abfc857747de6893320715eab7601cd1..675ac7fd7e9a1806c79e5c7b3367a9c2c5bd8c5a 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -2493,7 +2493,7 @@ THREADED_TEST(SymbolProperties) {
}
-THREADED_TEST(ArrayBuffer) {
+THREADED_TEST(ArrayBuffer_ApiInternalToExternal) {
i::FLAG_harmony_array_buffer = true;
i::FLAG_harmony_typed_arrays = true;
@@ -2502,10 +2502,16 @@ THREADED_TEST(ArrayBuffer) {
v8::HandleScope handle_scope(isolate);
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(1024);
+ CHECK_EQ(1024, ab->ByteLength());
+ CHECK(!ab->IsExternal());
HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
- CHECK_EQ(1024, static_cast<int>(ab->ByteLength()));
- uint8_t* data = static_cast<uint8_t*>(ab->Data());
+ v8::ArrayBufferContents ab_contents;
+ ab->Externalize(&ab_contents);
+ CHECK(ab->IsExternal());
+
+ CHECK_EQ(1024, static_cast<int>(ab_contents.ByteLength()));
+ uint8_t* data = static_cast<uint8_t*>(ab_contents.Data());
ASSERT(data != NULL);
env->Global()->Set(v8_str("ab"), ab);
@@ -2523,27 +2529,73 @@ THREADED_TEST(ArrayBuffer) {
data[1] = 0x11;
result = CompileRun("u8[0] + u8[1]");
CHECK_EQ(0xDD, result->Int32Value());
+}
+
- result = CompileRun("var ab1 = new ArrayBuffer(2);"
- "var u8_a = new Uint8Array(ab1);"
- "u8_a[0] = 0xAA;"
- "u8_a[1] = 0xFF; u8_a.buffer");
+THREADED_TEST(ArrayBuffer_JSInternalToExternal) {
+ i::FLAG_harmony_array_buffer = true;
+ i::FLAG_harmony_typed_arrays = true;
+
+ LocalContext env;
+ v8::Isolate* isolate = env->GetIsolate();
+ v8::HandleScope handle_scope(isolate);
+
+
+ v8::Handle<v8::Value> result =
+ CompileRun("var ab1 = new ArrayBuffer(2);"
+ "var u8_a = new Uint8Array(ab1);"
+ "u8_a[0] = 0xAA;"
+ "u8_a[1] = 0xFF; u8_a.buffer");
Local<v8::ArrayBuffer> ab1 = v8::ArrayBuffer::Cast(*result);
CHECK_EQ(2, static_cast<int>(ab1->ByteLength()));
- uint8_t* ab1_data = static_cast<uint8_t*>(ab1->Data());
- CHECK_EQ(0xAA, ab1_data[0]);
+ CHECK(!ab1->IsExternal());
+ v8::ArrayBufferContents ab1_contents;
+ ab1->Externalize(&ab1_contents);
+ CHECK(ab1->IsExternal());
+
+ result = CompileRun("ab1.byteLength");
+ CHECK_EQ(2, result->Int32Value());
+ result = CompileRun("u8_a[0]");
+ CHECK_EQ(0xAA, result->Int32Value());
+ result = CompileRun("u8_a[1]");
+ CHECK_EQ(0xFF, result->Int32Value());
+ result = CompileRun("var u8_b = new Uint8Array(ab1);"
+ "u8_b[0] = 0xBB;"
+ "u8_a[0]");
+ CHECK_EQ(0xBB, result->Int32Value());
+ result = CompileRun("u8_b[1]");
+ CHECK_EQ(0xFF, result->Int32Value());
+
+ CHECK_EQ(2, static_cast<int>(ab1_contents.ByteLength()));
+ uint8_t* ab1_data = static_cast<uint8_t*>(ab1_contents.Data());
+ CHECK_EQ(0xBB, ab1_data[0]);
CHECK_EQ(0xFF, ab1_data[1]);
ab1_data[0] = 0xCC;
ab1_data[1] = 0x11;
result = CompileRun("u8_a[0] + u8_a[1]");
CHECK_EQ(0xDD, result->Int32Value());
+}
+
- uint8_t* my_data = new uint8_t[100];
- memset(my_data, 0, 100);
- Local<v8::ArrayBuffer> ab3 = v8::ArrayBuffer::New(my_data, 100);
+THREADED_TEST(ArrayBuffer_External) {
+ i::FLAG_harmony_array_buffer = true;
+ i::FLAG_harmony_typed_arrays = true;
+
+ LocalContext env;
+ v8::Isolate* isolate = env->GetIsolate();
+ v8::HandleScope handle_scope(isolate);
+
+ i::ScopedVector<uint8_t> my_data(100);
+ memset(my_data.start(), 0, 100);
+ Local<v8::ArrayBuffer> ab3 = v8::ArrayBuffer::New(my_data.start(), 100);
CHECK_EQ(100, static_cast<int>(ab3->ByteLength()));
- CHECK_EQ(my_data, ab3->Data());
+ CHECK(ab3->IsExternal());
+
env->Global()->Set(v8_str("ab3"), ab3);
+
+ v8::Handle<v8::Value> result = CompileRun("ab3.byteLength");
+ CHECK_EQ(100, result->Int32Value());
+
result = CompileRun("var u8_b = new Uint8Array(ab3);"
"u8_b[0] = 0xBB;"
"u8_b[1] = 0xCC;"
@@ -2555,14 +2607,9 @@ THREADED_TEST(ArrayBuffer) {
my_data[1] = 0x11;
result = CompileRun("u8_b[0] + u8_b[1]");
CHECK_EQ(0xDD, result->Int32Value());
-
- delete[] my_data;
}
-
-
-
THREADED_TEST(HiddenProperties) {
LocalContext env;
v8::HandleScope scope(env->GetIsolate());
@@ -15460,12 +15507,14 @@ void TypedArrayTestHelper(v8::ExternalArrayType array_type,
i::FLAG_harmony_array_buffer = true;
i::FLAG_harmony_typed_arrays = true;
+ i::ScopedVector<ElementType> backing_store(kElementCount+2);
+
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope handle_scope(isolate);
- Local<v8::ArrayBuffer> ab =
- v8::ArrayBuffer::New((kElementCount+2)*sizeof(ElementType));
+ Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
+ backing_store.start(), (kElementCount+2)*sizeof(ElementType));
Local<TypedArray> ta =
TypedArray::New(ab, 2*sizeof(ElementType), kElementCount);
CHECK_EQ(kElementCount, static_cast<int>(ta->Length()));
@@ -15474,7 +15523,7 @@ void TypedArrayTestHelper(v8::ExternalArrayType array_type,
static_cast<int>(ta->ByteLength()));
CHECK_EQ(ab, ta->Buffer());
- ElementType* data = static_cast<ElementType*>(ab->Data()) + 2;
+ ElementType* data = backing_store.start() + 2;
for (int i = 0; i < kElementCount; i++) {
data[i] = static_cast<ElementType>(i);
}
« include/v8.h ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698