Commit 2a5eb829 authored by jochen's avatar jochen Committed by Commit bot

Expose an API on ArrayBufferView to copy out content w/o changing the buffer

BUG=v8:3996
LOG=y
R=dslomov@chromium.org,kbr@chromium.org,hpayer@chromium.org

Review URL: https://codereview.chromium.org/1041403003

Cr-Commit-Position: refs/heads/master@{#27580}
parent 83f827a0
......@@ -3482,6 +3482,17 @@ class V8_EXPORT ArrayBufferView : public Object {
*/
size_t ByteLength();
/**
* Copy the contents of the ArrayBufferView's buffer to an embedder defined
* memory without additional overhead that calling ArrayBufferView::Buffer
* might incur.
*
* Will write at most min(|byte_length|, ByteLength) bytes starting at
* ByteOffset of the underling buffer to the memory starting at |dest|.
* Returns the number of bytes actually written.
*/
size_t CopyContents(void* dest, size_t byte_length);
V8_INLINE static ArrayBufferView* Cast(Value* obj);
static const int kInternalFieldCount =
......
......@@ -6425,6 +6425,39 @@ Local<ArrayBuffer> v8::ArrayBufferView::Buffer() {
}
size_t v8::ArrayBufferView::CopyContents(void* dest, size_t byte_length) {
i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
i::Isolate* isolate = obj->GetIsolate();
size_t byte_offset = i::NumberToSize(isolate, obj->byte_offset());
size_t bytes_to_copy =
i::Min(byte_length, i::NumberToSize(isolate, obj->byte_length()));
if (bytes_to_copy) {
i::DisallowHeapAllocation no_gc;
const char* source = nullptr;
if (obj->IsJSDataView()) {
i::Handle<i::JSDataView> data_view(i::JSDataView::cast(*obj));
i::Handle<i::JSArrayBuffer> buffer(
i::JSArrayBuffer::cast(data_view->buffer()));
source = reinterpret_cast<char*>(buffer->backing_store());
} else {
DCHECK(obj->IsJSTypedArray());
i::Handle<i::JSTypedArray> typed_array(i::JSTypedArray::cast(*obj));
if (typed_array->buffer()->IsSmi()) {
i::Handle<i::FixedTypedArrayBase> fixed_array(
i::FixedTypedArrayBase::cast(typed_array->elements()));
source = reinterpret_cast<char*>(fixed_array->DataPtr());
} else {
i::Handle<i::JSArrayBuffer> buffer(
i::JSArrayBuffer::cast(typed_array->buffer()));
source = reinterpret_cast<char*>(buffer->backing_store());
}
}
memcpy(dest, source + byte_offset, bytes_to_copy);
}
return bytes_to_copy;
}
size_t v8::ArrayBufferView::ByteOffset() {
i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
return static_cast<size_t>(obj->byte_offset()->Number());
......
......@@ -158,6 +158,7 @@
'test-thread-termination.cc',
'test-threads.cc',
'test-transitions.cc',
'test-typedarrays.cc',
'test-types.cc',
'test-unbound-queue.cc',
'test-unboxed-doubles.cc',
......
// 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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment