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 "src/v8.h" | |
31 #include "test/cctest/cctest.h" | |
32 | |
33 #include "src/api.h" | |
34 #include "src/heap/heap.h" | |
35 #include "src/objects.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->IsUndefined(); | |
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->IsUndefined(); | |
58 o = JSArrayBuffer::cast(o)->weak_next()) { | |
59 if (ab == o) return true; | |
60 } | |
61 return false; | |
62 } | |
63 | |
64 | |
65 TEST(WeakArrayBuffersFromScript) { | |
66 v8::V8::Initialize(); | |
67 LocalContext context; | |
68 Isolate* isolate = GetIsolateFrom(&context); | |
69 int start = CountArrayBuffersInWeakList(isolate->heap()); | |
70 | |
71 for (int i = 1; i <= 3; i++) { | |
72 // Create 3 array buffers, make i-th of them garbage, | |
73 // validate correct state of array buffer weak list. | |
74 CHECK_EQ(start, CountArrayBuffersInWeakList(isolate->heap())); | |
75 { | |
76 v8::HandleScope scope(context->GetIsolate()); | |
77 | |
78 { | |
79 v8::HandleScope s1(context->GetIsolate()); | |
80 CompileRun("var ab1 = new ArrayBuffer(256);" | |
81 "var ab2 = new ArrayBuffer(256);" | |
82 "var ab3 = new ArrayBuffer(256);"); | |
83 v8::Handle<v8::ArrayBuffer> ab1 = | |
84 v8::Handle<v8::ArrayBuffer>::Cast(CompileRun("ab1")); | |
85 v8::Handle<v8::ArrayBuffer> ab2 = | |
86 v8::Handle<v8::ArrayBuffer>::Cast(CompileRun("ab2")); | |
87 v8::Handle<v8::ArrayBuffer> ab3 = | |
88 v8::Handle<v8::ArrayBuffer>::Cast(CompileRun("ab3")); | |
89 | |
90 CHECK_EQ(3, CountArrayBuffersInWeakList(isolate->heap()) - start); | |
91 CHECK(HasArrayBufferInWeakList(isolate->heap(), | |
92 *v8::Utils::OpenHandle(*ab1))); | |
93 CHECK(HasArrayBufferInWeakList(isolate->heap(), | |
94 *v8::Utils::OpenHandle(*ab2))); | |
95 CHECK(HasArrayBufferInWeakList(isolate->heap(), | |
96 *v8::Utils::OpenHandle(*ab3))); | |
97 } | |
98 | |
99 i::ScopedVector<char> source(1024); | |
100 i::SNPrintF(source, "ab%d = null;", i); | |
101 CompileRun(source.start()); | |
102 isolate->heap()->CollectAllGarbage(); | |
103 | |
104 CHECK_EQ(2, CountArrayBuffersInWeakList(isolate->heap()) - start); | |
105 | |
106 { | |
107 v8::HandleScope s2(context->GetIsolate()); | |
108 for (int j = 1; j <= 3; j++) { | |
109 if (j == i) continue; | |
110 i::SNPrintF(source, "ab%d", j); | |
111 v8::Handle<v8::ArrayBuffer> ab = | |
112 v8::Handle<v8::ArrayBuffer>::Cast(CompileRun(source.start())); | |
113 CHECK(HasArrayBufferInWeakList(isolate->heap(), | |
114 *v8::Utils::OpenHandle(*ab))); | |
115 } | |
116 } | |
117 | |
118 CompileRun("ab1 = null; ab2 = null; ab3 = null;"); | |
119 } | |
120 | |
121 isolate->heap()->CollectAllGarbage(); | |
122 CHECK_EQ(start, CountArrayBuffersInWeakList(isolate->heap())); | |
123 } | |
124 } | |
OLD | NEW |