OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include <stdlib.h> |
| 29 |
| 30 #include "v8.h" |
| 31 #include "api.h" |
| 32 #include "heap.h" |
| 33 #include "objects.h" |
| 34 |
| 35 #include "cctest.h" |
| 36 |
| 37 using namespace v8::internal; |
| 38 |
| 39 static Isolate* GetIsolateFrom(LocalContext* context) { |
| 40 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); |
| 41 } |
| 42 |
| 43 |
| 44 static int CountArrayBuffersInWeakList(Heap* heap) { |
| 45 int count = 0; |
| 46 for (Object* o = heap->array_buffers_list(); |
| 47 o != Smi::FromInt(0); |
| 48 o = JSArrayBuffer::cast(o)->weak_next()) { |
| 49 count++; |
| 50 } |
| 51 return count; |
| 52 } |
| 53 |
| 54 |
| 55 static bool HasArrayBufferInWeakList(Heap* heap, JSArrayBuffer* ab) { |
| 56 for (Object* o = heap->array_buffers_list(); |
| 57 o != Smi::FromInt(0); |
| 58 o = JSArrayBuffer::cast(o)->weak_next()) { |
| 59 if (ab == o) return true; |
| 60 } |
| 61 return false; |
| 62 } |
| 63 |
| 64 |
| 65 static int CountTypedArrays(JSArrayBuffer* array_buffer) { |
| 66 int count = 0; |
| 67 for (Object* o = array_buffer->weak_first_array(); |
| 68 o != Smi::FromInt(0); |
| 69 o = JSTypedArray::cast(o)->weak_next()) { |
| 70 count++; |
| 71 } |
| 72 |
| 73 return count; |
| 74 } |
| 75 |
| 76 static bool HasTypedArrayInWeakList(JSArrayBuffer* array_buffer, |
| 77 JSTypedArray* ta) { |
| 78 for (Object* o = array_buffer->weak_first_array(); |
| 79 o != Smi::FromInt(0); |
| 80 o = JSTypedArray::cast(o)->weak_next()) { |
| 81 if (ta == o) return true; |
| 82 } |
| 83 return false; |
| 84 } |
| 85 |
| 86 |
| 87 TEST(WeakArrayBuffersFromApi) { |
| 88 v8::V8::Initialize(); |
| 89 LocalContext context; |
| 90 Isolate* isolate = GetIsolateFrom(&context); |
| 91 |
| 92 v8::HandleScope s1(context->GetIsolate()); |
| 93 v8::Handle<v8::ArrayBuffer> ab1 = v8::ArrayBuffer::New(256); |
| 94 { |
| 95 v8::HandleScope s2(context->GetIsolate()); |
| 96 v8::Handle<v8::ArrayBuffer> ab2 = v8::ArrayBuffer::New(128); |
| 97 |
| 98 Handle<JSArrayBuffer> iab1 = v8::Utils::OpenHandle(*ab1); |
| 99 Handle<JSArrayBuffer> iab2 = v8::Utils::OpenHandle(*ab2); |
| 100 CHECK_EQ(2, CountArrayBuffersInWeakList(isolate->heap())); |
| 101 CHECK(HasArrayBufferInWeakList(isolate->heap(), *iab1)); |
| 102 CHECK(HasArrayBufferInWeakList(isolate->heap(), *iab2)); |
| 103 } |
| 104 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 105 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 106 CHECK_EQ(1, CountArrayBuffersInWeakList(isolate->heap())); |
| 107 { |
| 108 HandleScope scope2(isolate); |
| 109 Handle<JSArrayBuffer> iab1 = v8::Utils::OpenHandle(*ab1); |
| 110 |
| 111 CHECK(HasArrayBufferInWeakList(isolate->heap(), *iab1)); |
| 112 } |
| 113 } |
| 114 |
| 115 |
| 116 TEST(WeakArrayBuffersFromScript) { |
| 117 v8::V8::Initialize(); |
| 118 LocalContext context; |
| 119 Isolate* isolate = GetIsolateFrom(&context); |
| 120 v8::HandleScope scope(context->GetIsolate()); |
| 121 |
| 122 { |
| 123 v8::HandleScope s1(context->GetIsolate()); |
| 124 CompileRun("var ab1 = new ArrayBuffer(256);" |
| 125 "var ab2 = new ArrayBuffer(256);"); |
| 126 v8::Handle<v8::ArrayBuffer> ab1(v8::ArrayBuffer::Cast(*CompileRun("ab1"))); |
| 127 v8::Handle<v8::ArrayBuffer> ab2(v8::ArrayBuffer::Cast(*CompileRun("ab2"))); |
| 128 CHECK_EQ(2, CountArrayBuffersInWeakList(isolate->heap())); |
| 129 CHECK(HasArrayBufferInWeakList(isolate->heap(), |
| 130 *v8::Utils::OpenHandle(*ab1))); |
| 131 CHECK(HasArrayBufferInWeakList(isolate->heap(), |
| 132 *v8::Utils::OpenHandle(*ab2))); |
| 133 } |
| 134 |
| 135 CompileRun("ab2 = null;"); |
| 136 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 137 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 138 |
| 139 CHECK_EQ(1, CountArrayBuffersInWeakList(isolate->heap())); |
| 140 |
| 141 { |
| 142 v8::HandleScope s2(context->GetIsolate()); |
| 143 v8::Handle<v8::ArrayBuffer> ab1(v8::ArrayBuffer::Cast(*CompileRun("ab1"))); |
| 144 CHECK(HasArrayBufferInWeakList(isolate->heap(), |
| 145 *v8::Utils::OpenHandle(*ab1))); |
| 146 } |
| 147 } |
| 148 |
| 149 template <typename TypedArray> |
| 150 void TestTypedArrayFromApi() { |
| 151 v8::V8::Initialize(); |
| 152 LocalContext context; |
| 153 Isolate* isolate = GetIsolateFrom(&context); |
| 154 |
| 155 v8::HandleScope s1(context->GetIsolate()); |
| 156 v8::Handle<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(2048); |
| 157 Handle<JSArrayBuffer> iab = v8::Utils::OpenHandle(*ab); |
| 158 v8::Handle<TypedArray> ta1 = TypedArray::New(ab, 0, 256); |
| 159 { |
| 160 v8::HandleScope s2(context->GetIsolate()); |
| 161 v8::Handle<TypedArray> ta2 = TypedArray::New(ab, 0, 128); |
| 162 |
| 163 Handle<JSTypedArray> ita1 = v8::Utils::OpenHandle(*ta1); |
| 164 Handle<JSTypedArray> ita2 = v8::Utils::OpenHandle(*ta2); |
| 165 CHECK_EQ(2, CountTypedArrays(*iab)); |
| 166 CHECK(HasTypedArrayInWeakList(*iab, *ita1)); |
| 167 CHECK(HasTypedArrayInWeakList(*iab, *ita2)); |
| 168 } |
| 169 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 170 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 171 CHECK_EQ(1, CountTypedArrays(*iab)); |
| 172 { |
| 173 HandleScope scope2(isolate); |
| 174 Handle<JSTypedArray> ita1 = v8::Utils::OpenHandle(*ta1); |
| 175 |
| 176 CHECK(HasTypedArrayInWeakList(*iab, *ita1)); |
| 177 } |
| 178 } |
| 179 |
| 180 TEST(Uint8ArrayFromApi) { |
| 181 TestTypedArrayFromApi<v8::Uint8Array>(); |
| 182 } |
| 183 |
| 184 TEST(Int8ArrayFromApi) { |
| 185 TestTypedArrayFromApi<v8::Int8Array>(); |
| 186 } |
| 187 |
| 188 TEST(Uint16ArrayFromApi) { |
| 189 TestTypedArrayFromApi<v8::Uint16Array>(); |
| 190 } |
| 191 |
| 192 TEST(Int16ArrayFromApi) { |
| 193 TestTypedArrayFromApi<v8::Int16Array>(); |
| 194 } |
| 195 |
| 196 TEST(Uint32ArrayFromApi) { |
| 197 TestTypedArrayFromApi<v8::Uint32Array>(); |
| 198 } |
| 199 |
| 200 TEST(Float32ArrayFromApi) { |
| 201 TestTypedArrayFromApi<v8::Float32Array>(); |
| 202 } |
| 203 |
| 204 TEST(Float64ArrayFromApi) { |
| 205 TestTypedArrayFromApi<v8::Float64Array>(); |
| 206 } |
| 207 |
| 208 TEST(Uint8ClampedArrayFromApi) { |
| 209 TestTypedArrayFromApi<v8::Uint8ClampedArray>(); |
| 210 } |
| 211 |
| 212 template <typename TypedArray> |
| 213 static void TestTypedArrayFromScript(const char* constructor) { |
| 214 v8::V8::Initialize(); |
| 215 LocalContext context; |
| 216 Isolate* isolate = GetIsolateFrom(&context); |
| 217 v8::HandleScope scope(context->GetIsolate()); |
| 218 |
| 219 { |
| 220 v8::HandleScope s1(context->GetIsolate()); |
| 221 i::ScopedVector<char> source(2048); |
| 222 i::OS::SNPrintF(source, |
| 223 "var ab = new ArrayBuffer(2048);" |
| 224 "var ta1 = new %s(ab);" |
| 225 "var ta2 = new %s(ab);", |
| 226 constructor, constructor); |
| 227 |
| 228 CompileRun(source.start()); |
| 229 v8::Handle<v8::ArrayBuffer> ab(v8::ArrayBuffer::Cast(*CompileRun("ab"))); |
| 230 v8::Handle<TypedArray> ta1(TypedArray::Cast(*CompileRun("ta1"))); |
| 231 v8::Handle<TypedArray> ta2(TypedArray::Cast(*CompileRun("ta2"))); |
| 232 CHECK_EQ(1, CountArrayBuffersInWeakList(isolate->heap())); |
| 233 Handle<JSArrayBuffer> iab = v8::Utils::OpenHandle(*ab); |
| 234 CHECK_EQ(2, CountTypedArrays(*iab)); |
| 235 CHECK(HasTypedArrayInWeakList(*iab, *v8::Utils::OpenHandle(*ta1))); |
| 236 CHECK(HasTypedArrayInWeakList(*iab, *v8::Utils::OpenHandle(*ta2))); |
| 237 } |
| 238 |
| 239 CompileRun("ta2 = null;"); |
| 240 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 241 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
| 242 |
| 243 CHECK_EQ(1, CountArrayBuffersInWeakList(isolate->heap())); |
| 244 |
| 245 { |
| 246 v8::HandleScope s2(context->GetIsolate()); |
| 247 v8::Handle<v8::ArrayBuffer> ab(v8::ArrayBuffer::Cast(*CompileRun("ab"))); |
| 248 Handle<JSArrayBuffer> iab = v8::Utils::OpenHandle(*ab); |
| 249 v8::Handle<TypedArray> ta1(TypedArray::Cast(*CompileRun("ta1"))); |
| 250 CHECK(HasTypedArrayInWeakList(*iab, *v8::Utils::OpenHandle(*ta1))); |
| 251 } |
| 252 } |
| 253 |
| 254 |
| 255 TEST(Uint8ArrayFromScript) { |
| 256 TestTypedArrayFromScript<v8::Uint8Array>("Uint8Array"); |
| 257 } |
| 258 |
| 259 TEST(Int8ArrayFromScript) { |
| 260 TestTypedArrayFromScript<v8::Int8Array>("Int8Array"); |
| 261 } |
| 262 |
| 263 TEST(Uint16ArrayFromScript) { |
| 264 TestTypedArrayFromScript<v8::Uint16Array>("Uint16Array"); |
| 265 } |
| 266 |
| 267 TEST(Int16ArrayFromScript) { |
| 268 TestTypedArrayFromScript<v8::Int16Array>("Int16Array"); |
| 269 } |
| 270 |
| 271 TEST(Uint32ArrayFromScript) { |
| 272 TestTypedArrayFromScript<v8::Uint32Array>("Uint32Array"); |
| 273 } |
| 274 |
| 275 TEST(Float32ArrayFromScript) { |
| 276 TestTypedArrayFromScript<v8::Float32Array>("Float32Array"); |
| 277 } |
| 278 |
| 279 TEST(Float64ArrayFromScript) { |
| 280 TestTypedArrayFromScript<v8::Float64Array>("Float64Array"); |
| 281 } |
| 282 |
| 283 TEST(Uint8ClampedArrayFromScript) { |
| 284 TestTypedArrayFromScript<v8::Uint8ClampedArray>("Uint8ClampedArray"); |
| 285 } |
| 286 |
OLD | NEW |