OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdlib.h> |
| 6 |
| 7 #include "src/v8.h" |
| 8 #include "test/cctest/cctest.h" |
| 9 |
| 10 #include "src/api.h" |
| 11 #include "src/heap/heap.h" |
| 12 #include "src/objects.h" |
| 13 |
| 14 using namespace v8::internal; |
| 15 |
| 16 void TestArrayBufferViewContents(LocalContext& env, bool should_use_buffer) { |
| 17 v8::Local<v8::Object> obj_a = |
| 18 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a"))); |
| 19 CHECK(obj_a->IsArrayBufferView()); |
| 20 v8::Local<v8::ArrayBufferView> array_buffer_view = |
| 21 v8::Local<v8::ArrayBufferView>::Cast(obj_a); |
| 22 Handle<JSArrayBufferView> internal_view( |
| 23 v8::Utils::OpenHandle(*array_buffer_view)); |
| 24 bool has_buffer = true; |
| 25 if (internal_view->IsJSTypedArray()) { |
| 26 Handle<JSTypedArray> typed_array(JSTypedArray::cast(*internal_view)); |
| 27 has_buffer = !typed_array->buffer()->IsSmi(); |
| 28 } |
| 29 CHECK_EQ(has_buffer, should_use_buffer); |
| 30 unsigned char contents[4] = {23, 23, 23, 23}; |
| 31 CHECK_EQ(sizeof(contents), |
| 32 array_buffer_view->CopyContents(contents, sizeof(contents))); |
| 33 if (!has_buffer) { |
| 34 CHECK(internal_view->IsJSTypedArray()); |
| 35 Handle<JSTypedArray> typed_array(JSTypedArray::cast(*internal_view)); |
| 36 CHECK(typed_array->buffer()->IsSmi()); |
| 37 } |
| 38 for (size_t i = 0; i < sizeof(contents); ++i) { |
| 39 CHECK_EQ(i, contents[i]); |
| 40 } |
| 41 } |
| 42 |
| 43 |
| 44 TEST(CopyContentsTypedArray) { |
| 45 LocalContext env; |
| 46 v8::HandleScope scope(env->GetIsolate()); |
| 47 CompileRun( |
| 48 "var a = new Uint8Array(4);" |
| 49 "a[0] = 0;" |
| 50 "a[1] = 1;" |
| 51 "a[2] = 2;" |
| 52 "a[3] = 3;"); |
| 53 TestArrayBufferViewContents(env, false); |
| 54 } |
| 55 |
| 56 |
| 57 TEST(CopyContentsArray) { |
| 58 LocalContext env; |
| 59 v8::HandleScope scope(env->GetIsolate()); |
| 60 CompileRun("var a = new Uint8Array([0, 1, 2, 3]);"); |
| 61 TestArrayBufferViewContents(env, true); |
| 62 } |
| 63 |
| 64 |
| 65 TEST(CopyContentsView) { |
| 66 LocalContext env; |
| 67 v8::HandleScope scope(env->GetIsolate()); |
| 68 CompileRun( |
| 69 "var b = new ArrayBuffer(6);" |
| 70 "var c = new Uint8Array(b);" |
| 71 "c[0] = -1;" |
| 72 "c[1] = -1;" |
| 73 "c[2] = 0;" |
| 74 "c[3] = 1;" |
| 75 "c[4] = 2;" |
| 76 "c[5] = 3;" |
| 77 "var a = new DataView(b, 2);"); |
| 78 TestArrayBufferViewContents(env, true); |
| 79 } |
OLD | NEW |