• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 #include "cctest.h"
33 
34 using namespace v8::internal;
35 
36 
37 class HandleArray : public Malloced {
38  public:
39   static const unsigned kArraySize = 200;
HandleArray()40   explicit HandleArray() {}
~HandleArray()41   ~HandleArray() { Reset(); }
Reset()42   void Reset() {
43     for (unsigned i = 0; i < kArraySize; i++) {
44       if (handles_[i].IsEmpty()) continue;
45       handles_[i].Reset();
46     }
47   }
48   v8::Persistent<v8::Value> handles_[kArraySize];
49  private:
50   DISALLOW_COPY_AND_ASSIGN(HandleArray);
51 };
52 
53 
54 // An aligned character array of size 1024.
55 class AlignedArray : public Malloced {
56  public:
57   static const unsigned kArraySize = 1024/sizeof(uint64_t);
AlignedArray()58   AlignedArray() { Reset(); }
59 
Reset()60   void Reset() {
61     for (unsigned i = 0; i < kArraySize; i++) {
62       data_[i] = 0;
63     }
64   }
65 
66   template<typename T>
As()67   T As() { return reinterpret_cast<T>(data_); }
68 
69  private:
70   uint64_t data_[kArraySize];
71   DISALLOW_COPY_AND_ASSIGN(AlignedArray);
72 };
73 
74 
75 class DescriptorTestHelper {
76  public:
DescriptorTestHelper()77   DescriptorTestHelper() :
78       isolate_(NULL), array_(new AlignedArray), handle_array_(new HandleArray) {
79     v8::V8::Initialize();
80     isolate_ = CcTest::isolate();
81   }
82   v8::Isolate* isolate_;
83   // Data objects.
84   SmartPointer<AlignedArray> array_;
85   SmartPointer<HandleArray> handle_array_;
86  private:
87   DISALLOW_COPY_AND_ASSIGN(DescriptorTestHelper);
88 };
89 
90 
CreateConstructor(v8::Handle<v8::Context> context,const char * class_name,int internal_field,const char * descriptor_name=NULL,v8::Handle<v8::DeclaredAccessorDescriptor> descriptor=v8::Handle<v8::DeclaredAccessorDescriptor> ())91 static v8::Local<v8::ObjectTemplate> CreateConstructor(
92     v8::Handle<v8::Context> context,
93     const char* class_name,
94     int internal_field,
95     const char* descriptor_name = NULL,
96     v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
97         v8::Handle<v8::DeclaredAccessorDescriptor>()) {
98   v8::Local<v8::FunctionTemplate> constructor = v8::FunctionTemplate::New();
99   v8::Local<v8::ObjectTemplate> obj_template = constructor->InstanceTemplate();
100   // Setup object template.
101   if (descriptor_name != NULL && !descriptor.IsEmpty()) {
102     bool added_accessor =
103         obj_template->SetDeclaredAccessor(v8_str(descriptor_name), descriptor);
104     CHECK(added_accessor);
105   }
106   obj_template->SetInternalFieldCount((internal_field+1)*2 + 7);
107   context->Global()->Set(v8_str(class_name), constructor->GetFunction());
108   return obj_template;
109 }
110 
111 
VerifyRead(v8::Handle<v8::DeclaredAccessorDescriptor> descriptor,int internal_field,void * internal_object,v8::Handle<v8::Value> expected_value)112 static void VerifyRead(v8::Handle<v8::DeclaredAccessorDescriptor> descriptor,
113                        int internal_field,
114                        void* internal_object,
115                        v8::Handle<v8::Value> expected_value) {
116   LocalContext local_context;
117   v8::HandleScope scope(local_context->GetIsolate());
118   v8::Handle<v8::Context> context = local_context.local();
119   CreateConstructor(context, "Accessible", internal_field, "x", descriptor);
120   // Setup object.
121   CompileRun("var accessible = new Accessible();");
122   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(
123       context->Global()->Get(v8_str("accessible")));
124   obj->SetAlignedPointerInInternalField(internal_field, internal_object);
125   bool added_accessor;
126   added_accessor = obj->SetDeclaredAccessor(v8_str("y"), descriptor);
127   CHECK(added_accessor);
128   added_accessor = obj->SetDeclaredAccessor(v8_str("13"), descriptor);
129   CHECK(added_accessor);
130   // Test access from template getter.
131   v8::Local<v8::Value> value;
132   value = CompileRun("accessible.x;");
133   CHECK_EQ(expected_value, value);
134   value = CompileRun("accessible['x'];");
135   CHECK_EQ(expected_value, value);
136   // Test access from object getter.
137   value = CompileRun("accessible.y;");
138   CHECK_EQ(expected_value, value);
139   value = CompileRun("accessible['y'];");
140   CHECK_EQ(expected_value, value);
141   value = CompileRun("accessible[13];");
142   CHECK_EQ(expected_value, value);
143   value = CompileRun("accessible['13'];");
144   CHECK_EQ(expected_value, value);
145 }
146 
147 
Convert(int32_t value,v8::Isolate * isolate)148 static v8::Handle<v8::Value> Convert(int32_t value, v8::Isolate* isolate) {
149   return v8::Integer::New(value, isolate);
150 }
151 
152 
Convert(float value,v8::Isolate *)153 static v8::Handle<v8::Value> Convert(float value, v8::Isolate*) {
154   return v8::Number::New(value);
155 }
156 
157 
Convert(double value,v8::Isolate *)158 static v8::Handle<v8::Value> Convert(double value, v8::Isolate*) {
159   return v8::Number::New(value);
160 }
161 
162 
163 typedef v8::ObjectOperationDescriptor OOD;
164 
165 template<typename T>
TestPrimitiveValue(T value,v8::DeclaredAccessorDescriptorDataType data_type,DescriptorTestHelper * helper)166 static void TestPrimitiveValue(
167     T value,
168     v8::DeclaredAccessorDescriptorDataType data_type,
169     DescriptorTestHelper* helper) {
170   v8::HandleScope handle_scope(helper->isolate_);
171   int index = 17;
172   int internal_field = 6;
173   v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
174       OOD::NewInternalFieldDereference(helper->isolate_, internal_field)
175       ->NewRawShift(helper->isolate_, static_cast<uint16_t>(index*sizeof(T)))
176       ->NewPrimitiveValue(helper->isolate_, data_type, 0);
177   v8::Handle<v8::Value> expected = Convert(value, helper->isolate_);
178   helper->array_->Reset();
179   helper->array_->As<T*>()[index] = value;
180   VerifyRead(descriptor, internal_field, *helper->array_, expected);
181 }
182 
183 
TEST(PrimitiveValueRead)184 TEST(PrimitiveValueRead) {
185   DescriptorTestHelper helper;
186   TestPrimitiveValue<int32_t>(203, v8::kDescriptorInt32Type, &helper);
187   TestPrimitiveValue<float>(23.7f, v8::kDescriptorFloatType, &helper);
188   TestPrimitiveValue<double>(23.7, v8::kDescriptorDoubleType, &helper);
189 }
190 
191 
192 template<typename T>
TestBitmaskCompare(T bitmask,T compare_value,DescriptorTestHelper * helper)193 static void TestBitmaskCompare(T bitmask,
194                                T compare_value,
195                                DescriptorTestHelper* helper) {
196   v8::HandleScope handle_scope(helper->isolate_);
197   int index = 13;
198   int internal_field = 4;
199   v8::Handle<v8::RawOperationDescriptor> raw_descriptor =
200       OOD::NewInternalFieldDereference(helper->isolate_, internal_field)
201       ->NewRawShift(helper->isolate_, static_cast<uint16_t>(index*sizeof(T)));
202   v8::Handle<v8::DeclaredAccessorDescriptor> descriptor;
203   switch (sizeof(T)) {
204     case 1:
205       descriptor = raw_descriptor->NewBitmaskCompare8(
206             helper->isolate_,
207             static_cast<uint8_t>(bitmask),
208             static_cast<uint8_t>(compare_value));
209       break;
210     case 2:
211       descriptor = raw_descriptor->NewBitmaskCompare16(
212           helper->isolate_,
213           static_cast<uint16_t>(bitmask),
214           static_cast<uint16_t>(compare_value));
215       break;
216     case 4:
217       descriptor = raw_descriptor->NewBitmaskCompare32(
218           helper->isolate_,
219           static_cast<uint32_t>(bitmask),
220           static_cast<uint32_t>(compare_value));
221       break;
222     default:
223       CHECK(false);
224       break;
225   }
226   AlignedArray* array = *helper->array_;
227   array->Reset();
228   VerifyRead(descriptor, internal_field, array, v8::False(helper->isolate_));
229   array->As<T*>()[index] = compare_value;
230   VerifyRead(descriptor, internal_field, array, v8::True(helper->isolate_));
231   helper->array_->As<T*>()[index] = compare_value & bitmask;
232   VerifyRead(descriptor, internal_field, array, v8::True(helper->isolate_));
233 }
234 
235 
TEST(BitmaskCompareRead)236 TEST(BitmaskCompareRead) {
237   DescriptorTestHelper helper;
238   TestBitmaskCompare<uint8_t>(0xf3, 0xa8, &helper);
239   TestBitmaskCompare<uint16_t>(0xfefe, 0x7d42, &helper);
240   TestBitmaskCompare<uint32_t>(0xfefeab18, 0x1234fdec, &helper);
241 }
242 
243 
TEST(PointerCompareRead)244 TEST(PointerCompareRead) {
245   DescriptorTestHelper helper;
246   v8::HandleScope handle_scope(helper.isolate_);
247   int index = 35;
248   int internal_field = 3;
249   void* ptr = helper.isolate_;
250   v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
251       OOD::NewInternalFieldDereference(helper.isolate_, internal_field)
252       ->NewRawShift(helper.isolate_, static_cast<uint16_t>(index*sizeof(ptr)))
253       ->NewPointerCompare(helper.isolate_, ptr);
254   AlignedArray* array = *helper.array_;
255   VerifyRead(descriptor, internal_field, array, v8::False(helper.isolate_));
256   array->As<uintptr_t*>()[index] = reinterpret_cast<uintptr_t>(ptr);
257   VerifyRead(descriptor, internal_field, array, v8::True(helper.isolate_));
258 }
259 
260 
TEST(PointerDereferenceRead)261 TEST(PointerDereferenceRead) {
262   DescriptorTestHelper helper;
263   v8::HandleScope handle_scope(helper.isolate_);
264   int first_index = 13;
265   int internal_field = 7;
266   int second_index = 11;
267   int pointed_to_index = 75;
268   uint16_t expected = 0x1425;
269   v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
270       OOD::NewInternalFieldDereference(helper.isolate_, internal_field)
271       ->NewRawShift(helper.isolate_, first_index*kPointerSize)
272       ->NewRawDereference(helper.isolate_)
273       ->NewRawShift(helper.isolate_,
274                     static_cast<uint16_t>(second_index*sizeof(int16_t)))
275       ->NewPrimitiveValue(helper.isolate_, v8::kDescriptorInt16Type, 0);
276   AlignedArray* array = *helper.array_;
277   array->As<uintptr_t**>()[first_index] =
278       &array->As<uintptr_t*>()[pointed_to_index];
279   VerifyRead(descriptor, internal_field, array, v8::Integer::New(0));
280   second_index += pointed_to_index*sizeof(uintptr_t)/sizeof(uint16_t);
281   array->As<uint16_t*>()[second_index] = expected;
282   VerifyRead(descriptor, internal_field, array, v8::Integer::New(expected));
283 }
284 
285 
TEST(HandleDereferenceRead)286 TEST(HandleDereferenceRead) {
287   DescriptorTestHelper helper;
288   v8::HandleScope handle_scope(helper.isolate_);
289   int index = 13;
290   int internal_field = 0;
291   v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
292       OOD::NewInternalFieldDereference(helper.isolate_, internal_field)
293       ->NewRawShift(helper.isolate_, index*kPointerSize)
294       ->NewHandleDereference(helper.isolate_);
295   HandleArray* array = *helper.handle_array_;
296   v8::Handle<v8::String> expected = v8_str("whatever");
297   array->handles_[index].Reset(helper.isolate_, expected);
298   VerifyRead(descriptor, internal_field, array, expected);
299 }
300