summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/test-declarative-accessors.cc
blob: 2d2f5cd23b2c318ea62a7f83a18e940dfb2a934e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdlib.h>

#include "v8.h"

#include "cctest.h"

using namespace v8::internal;


class HandleArray : public Malloced {
 public:
  static const unsigned kArraySize = 200;
  explicit HandleArray() {}
  ~HandleArray() { Reset(v8::Isolate::GetCurrent()); }
  void Reset(v8::Isolate* isolate) {
    for (unsigned i = 0; i < kArraySize; i++) {
      if (handles_[i].IsEmpty()) continue;
      handles_[i].Dispose(isolate);
      handles_[i].Clear();
    }
  }
  v8::Persistent<v8::Value> handles_[kArraySize];
 private:
  DISALLOW_COPY_AND_ASSIGN(HandleArray);
};


// An aligned character array of size 1024.
class AlignedArray : public Malloced {
 public:
  static const unsigned kArraySize = 1024/sizeof(uint64_t);
  AlignedArray() { Reset(); }

  void Reset() {
    for (unsigned i = 0; i < kArraySize; i++) {
      data_[i] = 0;
    }
  }

  template<typename T>
  T As() { return reinterpret_cast<T>(data_); }

 private:
  uint64_t data_[kArraySize];
  DISALLOW_COPY_AND_ASSIGN(AlignedArray);
};


class DescriptorTestHelper {
 public:
  DescriptorTestHelper() :
      isolate_(NULL), array_(new AlignedArray), handle_array_(new HandleArray) {
    v8::V8::Initialize();
    isolate_ = v8::Isolate::GetCurrent();
  }
  v8::Isolate* isolate_;
  // Data objects.
  SmartPointer<AlignedArray> array_;
  SmartPointer<HandleArray> handle_array_;
 private:
  DISALLOW_COPY_AND_ASSIGN(DescriptorTestHelper);
};


static v8::Local<v8::ObjectTemplate> 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>()) {
  v8::Local<v8::FunctionTemplate> constructor = v8::FunctionTemplate::New();
  v8::Local<v8::ObjectTemplate> obj_template = constructor->InstanceTemplate();
  // Setup object template.
  if (descriptor_name != NULL && !descriptor.IsEmpty()) {
    bool added_accessor =
        obj_template->SetAccessor(v8_str(descriptor_name), descriptor);
    CHECK(added_accessor);
  }
  obj_template->SetInternalFieldCount((internal_field+1)*2 + 7);
  context->Global()->Set(v8_str(class_name), constructor->GetFunction());
  return obj_template;
}


static void VerifyRead(v8::Handle<v8::DeclaredAccessorDescriptor> descriptor,
                       int internal_field,
                       void* internal_object,
                       v8::Handle<v8::Value> expected_value) {
  LocalContext local_context;
  v8::HandleScope scope(local_context->GetIsolate());
  v8::Handle<v8::Context> context = local_context.local();
  CreateConstructor(context, "Accessible", internal_field, "x", descriptor);
  // Setup object.
  CompileRun("var accessible = new Accessible();");
  v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(
      context->Global()->Get(v8_str("accessible")));
  obj->SetAlignedPointerInInternalField(internal_field, internal_object);
  bool added_accessor;
  added_accessor = obj->SetAccessor(v8_str("y"), descriptor);
  CHECK(added_accessor);
  added_accessor = obj->SetAccessor(v8_str("13"), descriptor);
  CHECK(added_accessor);
  // Test access from template getter.
  v8::Local<v8::Value> value;
  value = CompileRun("accessible.x;");
  CHECK_EQ(expected_value, value);
  value = CompileRun("accessible['x'];");
  CHECK_EQ(expected_value, value);
  // Test access from object getter.
  value = CompileRun("accessible.y;");
  CHECK_EQ(expected_value, value);
  value = CompileRun("accessible['y'];");
  CHECK_EQ(expected_value, value);
  value = CompileRun("accessible[13];");
  CHECK_EQ(expected_value, value);
  value = CompileRun("accessible['13'];");
  CHECK_EQ(expected_value, value);
}


static v8::Handle<v8::Value> Convert(int32_t value, v8::Isolate* isolate) {
  return v8::Integer::New(value, isolate);
}


static v8::Handle<v8::Value> Convert(float value, v8::Isolate*) {
  return v8::Number::New(value);
}


static v8::Handle<v8::Value> Convert(double value, v8::Isolate*) {
  return v8::Number::New(value);
}


typedef v8::ObjectOperationDescriptor OOD;

template<typename T>
static void TestPrimitiveValue(
    T value,
    v8::DeclaredAccessorDescriptorDataType data_type,
    DescriptorTestHelper* helper) {
  v8::HandleScope handle_scope(helper->isolate_);
  int index = 17;
  int internal_field = 6;
  v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
      OOD::NewInternalFieldDereference(helper->isolate_, internal_field)
      ->NewRawShift(helper->isolate_, static_cast<uint16_t>(index*sizeof(T)))
      ->NewPrimitiveValue(helper->isolate_, data_type, 0);
  v8::Handle<v8::Value> expected = Convert(value, helper->isolate_);
  helper->array_->Reset();
  helper->array_->As<T*>()[index] = value;
  VerifyRead(descriptor, internal_field, *helper->array_, expected);
}


TEST(PrimitiveValueRead) {
  DescriptorTestHelper helper;
  TestPrimitiveValue<int32_t>(203, v8::kDescriptorInt32Type, &helper);
  TestPrimitiveValue<float>(23.7f, v8::kDescriptorFloatType, &helper);
  TestPrimitiveValue<double>(23.7, v8::kDescriptorDoubleType, &helper);
}


template<typename T>
static void TestBitmaskCompare(T bitmask,
                               T compare_value,
                               DescriptorTestHelper* helper) {
  v8::HandleScope handle_scope(helper->isolate_);
  int index = 13;
  int internal_field = 4;
  v8::Handle<v8::RawOperationDescriptor> raw_descriptor =
      OOD::NewInternalFieldDereference(helper->isolate_, internal_field)
      ->NewRawShift(helper->isolate_, static_cast<uint16_t>(index*sizeof(T)));
  v8::Handle<v8::DeclaredAccessorDescriptor> descriptor;
  switch (sizeof(T)) {
    case 1:
      descriptor = raw_descriptor->NewBitmaskCompare8(
            helper->isolate_,
            static_cast<uint8_t>(bitmask),
            static_cast<uint8_t>(compare_value));
      break;
    case 2:
      descriptor = raw_descriptor->NewBitmaskCompare16(
          helper->isolate_,
          static_cast<uint16_t>(bitmask),
          static_cast<uint16_t>(compare_value));
      break;
    case 4:
      descriptor = raw_descriptor->NewBitmaskCompare32(
          helper->isolate_,
          static_cast<uint32_t>(bitmask),
          static_cast<uint32_t>(compare_value));
      break;
    default:
      CHECK(false);
      break;
  }
  AlignedArray* array = *helper->array_;
  array->Reset();
  VerifyRead(descriptor, internal_field, array, v8::False(helper->isolate_));
  array->As<T*>()[index] = compare_value;
  VerifyRead(descriptor, internal_field, array, v8::True(helper->isolate_));
  helper->array_->As<T*>()[index] = compare_value & bitmask;
  VerifyRead(descriptor, internal_field, array, v8::True(helper->isolate_));
}


TEST(BitmaskCompareRead) {
  DescriptorTestHelper helper;
  TestBitmaskCompare<uint8_t>(0xf3, 0xa8, &helper);
  TestBitmaskCompare<uint16_t>(0xfefe, 0x7d42, &helper);
  TestBitmaskCompare<uint32_t>(0xfefeab18, 0x1234fdec, &helper);
}


TEST(PointerCompareRead) {
  DescriptorTestHelper helper;
  v8::HandleScope handle_scope(helper.isolate_);
  int index = 35;
  int internal_field = 3;
  void* ptr = helper.isolate_;
  v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
      OOD::NewInternalFieldDereference(helper.isolate_, internal_field)
      ->NewRawShift(helper.isolate_, static_cast<uint16_t>(index*sizeof(ptr)))
      ->NewPointerCompare(helper.isolate_, ptr);
  AlignedArray* array = *helper.array_;
  VerifyRead(descriptor, internal_field, array, v8::False(helper.isolate_));
  array->As<uintptr_t*>()[index] = reinterpret_cast<uintptr_t>(ptr);
  VerifyRead(descriptor, internal_field, array, v8::True(helper.isolate_));
}


TEST(PointerDereferenceRead) {
  DescriptorTestHelper helper;
  v8::HandleScope handle_scope(helper.isolate_);
  int first_index = 13;
  int internal_field = 7;
  int second_index = 11;
  int pointed_to_index = 75;
  uint16_t expected = 0x1425;
  v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
      OOD::NewInternalFieldDereference(helper.isolate_, internal_field)
      ->NewRawShift(helper.isolate_, first_index*kPointerSize)
      ->NewRawDereference(helper.isolate_)
      ->NewRawShift(helper.isolate_,
                    static_cast<uint16_t>(second_index*sizeof(int16_t)))
      ->NewPrimitiveValue(helper.isolate_, v8::kDescriptorInt16Type, 0);
  AlignedArray* array = *helper.array_;
  array->As<uintptr_t**>()[first_index] =
      &array->As<uintptr_t*>()[pointed_to_index];
  VerifyRead(descriptor, internal_field, array, v8::Integer::New(0));
  second_index += pointed_to_index*sizeof(uintptr_t)/sizeof(uint16_t);
  array->As<uint16_t*>()[second_index] = expected;
  VerifyRead(descriptor, internal_field, array, v8::Integer::New(expected));
}


TEST(HandleDereferenceRead) {
  DescriptorTestHelper helper;
  v8::HandleScope handle_scope(helper.isolate_);
  int index = 13;
  int internal_field = 0;
  v8::Handle<v8::DeclaredAccessorDescriptor> descriptor =
      OOD::NewInternalFieldDereference(helper.isolate_, internal_field)
      ->NewRawShift(helper.isolate_, index*kPointerSize)
      ->NewHandleDereference(helper.isolate_);
  HandleArray* array = *helper.handle_array_;
  v8::Handle<v8::String> expected = v8_str("whatever");
  array->handles_[index].Reset(helper.isolate_, expected);
  VerifyRead(descriptor, internal_field, array, expected);
}