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

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

Issue 1041403003: Expose an API on ArrayBufferView to copy out content w/o changing the buffer (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updates Created 5 years, 9 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
« no previous file with comments | « test/cctest/cctest.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-typedarrays.cc
diff --git a/test/cctest/test-typedarrays.cc b/test/cctest/test-typedarrays.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dc1bca2e0a241b4e4d02e92f2a783e4793b92591
--- /dev/null
+++ b/test/cctest/test-typedarrays.cc
@@ -0,0 +1,79 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdlib.h>
+
+#include "src/v8.h"
+#include "test/cctest/cctest.h"
+
+#include "src/api.h"
+#include "src/heap/heap.h"
+#include "src/objects.h"
+
+using namespace v8::internal;
+
+void TestArrayBufferViewContents(LocalContext& env, bool should_use_buffer) {
+ v8::Local<v8::Object> obj_a =
+ v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
+ CHECK(obj_a->IsArrayBufferView());
+ v8::Local<v8::ArrayBufferView> array_buffer_view =
+ v8::Local<v8::ArrayBufferView>::Cast(obj_a);
+ Handle<JSArrayBufferView> internal_view(
+ v8::Utils::OpenHandle(*array_buffer_view));
+ bool has_buffer = true;
+ if (internal_view->IsJSTypedArray()) {
+ Handle<JSTypedArray> typed_array(JSTypedArray::cast(*internal_view));
+ has_buffer = !typed_array->buffer()->IsSmi();
+ }
+ CHECK_EQ(has_buffer, should_use_buffer);
+ unsigned char contents[4] = {23, 23, 23, 23};
+ CHECK_EQ(sizeof(contents),
+ array_buffer_view->CopyContents(contents, sizeof(contents)));
+ if (!has_buffer) {
+ CHECK(internal_view->IsJSTypedArray());
+ Handle<JSTypedArray> typed_array(JSTypedArray::cast(*internal_view));
+ CHECK(typed_array->buffer()->IsSmi());
+ }
+ for (size_t i = 0; i < sizeof(contents); ++i) {
+ CHECK_EQ(i, contents[i]);
+ }
+}
+
+
+TEST(CopyContentsTypedArray) {
+ LocalContext env;
+ v8::HandleScope scope(env->GetIsolate());
+ CompileRun(
+ "var a = new Uint8Array(4);"
+ "a[0] = 0;"
+ "a[1] = 1;"
+ "a[2] = 2;"
+ "a[3] = 3;");
+ TestArrayBufferViewContents(env, false);
+}
+
+
+TEST(CopyContentsArray) {
+ LocalContext env;
+ v8::HandleScope scope(env->GetIsolate());
+ CompileRun("var a = new Uint8Array([0, 1, 2, 3]);");
+ TestArrayBufferViewContents(env, true);
+}
+
+
+TEST(CopyContentsView) {
+ LocalContext env;
+ v8::HandleScope scope(env->GetIsolate());
+ CompileRun(
+ "var b = new ArrayBuffer(6);"
+ "var c = new Uint8Array(b);"
+ "c[0] = -1;"
+ "c[1] = -1;"
+ "c[2] = 0;"
+ "c[3] = 1;"
+ "c[4] = 2;"
+ "c[5] = 3;"
+ "var a = new DataView(b, 2);");
+ TestArrayBufferViewContents(env, true);
+}
« no previous file with comments | « test/cctest/cctest.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698