test-accessors.cc 22.6 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
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
// 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>

30
#include "src/v8.h"
31

32 33 34 35
#include "src/api.h"
#include "src/frames-inl.h"
#include "src/string-stream.h"
#include "test/cctest/cctest.h"
36 37 38 39 40

using ::v8::ObjectTemplate;
using ::v8::Value;
using ::v8::Context;
using ::v8::Local;
41
using ::v8::Name;
42 43 44 45 46
using ::v8::String;
using ::v8::Script;
using ::v8::Function;
using ::v8::Extension;

47 48
static void handle_property(Local<String> name,
                            const v8::PropertyCallbackInfo<v8::Value>& info) {
49
  ApiTestFuzzer::Fuzz();
50
  info.GetReturnValue().Set(v8_num(900));
51 52
}

53 54 55 56 57 58
static void handle_property_2(Local<String> name,
                              const v8::PropertyCallbackInfo<v8::Value>& info) {
  ApiTestFuzzer::Fuzz();
  info.GetReturnValue().Set(v8_num(902));
}

59

60 61 62 63 64 65 66
static void handle_property(const v8::FunctionCallbackInfo<v8::Value>& info) {
  ApiTestFuzzer::Fuzz();
  CHECK_EQ(0, info.Length());
  info.GetReturnValue().Set(v8_num(907));
}


67
THREADED_TEST(PropertyHandler) {
68
  LocalContext env;
69 70 71
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(isolate);
72
  fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property);
73
  Local<v8::FunctionTemplate> getter_templ =
74
      v8::FunctionTemplate::New(isolate, handle_property);
75 76 77
  getter_templ->SetLength(0);
  fun_templ->
      InstanceTemplate()->SetAccessorProperty(v8_str("bar"), getter_templ);
78 79 80
  fun_templ->InstanceTemplate()->
      SetNativeDataProperty(v8_str("instance_foo"), handle_property);
  fun_templ->SetNativeDataProperty(v8_str("object_foo"), handle_property_2);
81 82
  Local<Function> fun = fun_templ->GetFunction();
  env->Global()->Set(v8_str("Fun"), fun);
83 84 85 86
  Local<Script> getter;
  Local<Script> setter;
  // check function instance accessors
  getter = v8_compile("var obj = new Fun(); obj.instance_foo;");
87
  CHECK_EQ(900, getter->Run()->Int32Value());
88
  setter = v8_compile("obj.instance_foo = 901;");
89
  CHECK_EQ(901, setter->Run()->Int32Value());
90 91 92 93
  getter = v8_compile("obj.bar;");
  CHECK_EQ(907, getter->Run()->Int32Value());
  setter = v8_compile("obj.bar = 908;");
  CHECK_EQ(908, setter->Run()->Int32Value());
94 95 96 97 98
  // check function static accessors
  getter = v8_compile("Fun.object_foo;");
  CHECK_EQ(902, getter->Run()->Int32Value());
  setter = v8_compile("Fun.object_foo = 903;");
  CHECK_EQ(903, setter->Run()->Int32Value());
99 100 101
}


102 103
static void GetIntValue(Local<String> property,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
104 105 106
  ApiTestFuzzer::Fuzz();
  int* value =
      static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value());
107
  info.GetReturnValue().Set(v8_num(*value));
108 109 110 111 112
}


static void SetIntValue(Local<String> property,
                        Local<Value> value,
113
                        const v8::PropertyCallbackInfo<void>& info) {
114 115 116 117 118 119 120 121 122 123 124
  int* field =
      static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value());
  *field = value->Int32Value();
}

int foo, bar, baz;

THREADED_TEST(GlobalVariableAccess) {
  foo = 0;
  bar = -4;
  baz = 10;
125 126 127
  v8::Isolate* isolate = CcTest::isolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
128 129
  templ->InstanceTemplate()->SetAccessor(
      v8_str("foo"), GetIntValue, SetIntValue,
130
      v8::External::New(isolate, &foo));
131 132
  templ->InstanceTemplate()->SetAccessor(
      v8_str("bar"), GetIntValue, SetIntValue,
133
      v8::External::New(isolate, &bar));
134 135
  templ->InstanceTemplate()->SetAccessor(
      v8_str("baz"), GetIntValue, SetIntValue,
136
      v8::External::New(isolate, &baz));
137 138 139 140 141 142 143
  LocalContext env(0, templ->InstanceTemplate());
  v8_compile("foo = (++bar) + baz")->Run();
  CHECK_EQ(bar, -3);
  CHECK_EQ(foo, 7);
}


144
static int x_register[2] = {0, 0};
145 146 147
static v8::Handle<v8::Object> x_receiver;
static v8::Handle<v8::Object> x_holder;

148 149
template<class Info>
static void XGetter(const Info& info, int offset) {
150
  ApiTestFuzzer::Fuzz();
151
  v8::Isolate* isolate = CcTest::isolate();
152
  CHECK_EQ(isolate, info.GetIsolate());
153
  CHECK(x_receiver->Equals(info.This()));
154 155 156 157 158 159
  info.GetReturnValue().Set(v8_num(x_register[offset]));
}


static void XGetter(Local<String> name,
                    const v8::PropertyCallbackInfo<v8::Value>& info) {
160
  CHECK(x_holder->Equals(info.Holder()));
161
  XGetter(info, 0);
162 163 164
}


165
static void XGetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
166
  CHECK(x_receiver->Equals(info.Holder()));
167 168 169 170 171 172
  XGetter(info, 1);
}


template<class Info>
static void XSetter(Local<Value> value, const Info& info, int offset) {
173
  v8::Isolate* isolate = CcTest::isolate();
174
  CHECK_EQ(isolate, info.GetIsolate());
175 176
  CHECK(x_holder->Equals(info.This()));
  CHECK(x_holder->Equals(info.Holder()));
177
  x_register[offset] = value->Int32Value();
178
  info.GetReturnValue().Set(v8_num(-1));
179 180 181 182 183 184 185 186 187 188 189 190 191
}


static void XSetter(Local<String> name,
                    Local<Value> value,
                    const v8::PropertyCallbackInfo<void>& info) {
  XSetter(value, info, 0);
}


static void XSetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
  CHECK_EQ(1, info.Length());
  XSetter(info[0], info, 1);
192 193 194 195
}


THREADED_TEST(AccessorIC) {
196
  LocalContext context;
197 198
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
199
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
200 201
  obj->SetAccessor(v8_str("x0"), XGetter, XSetter);
  obj->SetAccessorProperty(v8_str("x1"),
202 203
                           v8::FunctionTemplate::New(isolate, XGetter),
                           v8::FunctionTemplate::New(isolate, XSetter));
204 205
  x_holder = obj->NewInstance();
  context->Global()->Set(v8_str("holder"), x_holder);
206
  x_receiver = v8::Object::New(isolate);
207 208 209 210
  context->Global()->Set(v8_str("obj"), x_receiver);
  v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(CompileRun(
    "obj.__proto__ = holder;"
    "var result = [];"
211 212
    "var key_0 = 'x0';"
    "var key_1 = 'x1';"
213 214
    "for (var j = 0; j < 10; j++) {"
    "  var i = 4*j;"
215
    "  result.push(holder.x0 = i);"
216
    "  result.push(obj.x0);"
217
    "  result.push(holder.x1 = i + 1);"
218
    "  result.push(obj.x1);"
219
    "  result.push(holder[key_0] = i + 2);"
220
    "  result.push(obj[key_0]);"
221
    "  result.push(holder[key_1] = i + 3);"
222
    "  result.push(obj[key_1]);"
223 224
    "}"
    "result"));
225
  CHECK_EQ(80u, array->Length());
226
  for (int i = 0; i < 80; i++) {
227
    v8::Handle<Value> entry = array->Get(v8::Integer::New(isolate, i));
228
    CHECK(v8::Integer::New(isolate, i / 2)->Equals(entry));
229 230 231 232 233
  }
}


template <int C>
234 235 236
static void HandleAllocatingGetter(
    Local<String> name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
237 238
  ApiTestFuzzer::Fuzz();
  for (int i = 0; i < C; i++)
239 240
    v8::String::NewFromUtf8(info.GetIsolate(), "foo");
  info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), "foo"));
241 242 243 244
}


THREADED_TEST(HandleScopePop) {
245
  LocalContext context;
246 247 248
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
249 250 251
  obj->SetAccessor(v8_str("one"), HandleAllocatingGetter<1>);
  obj->SetAccessor(v8_str("many"), HandleAllocatingGetter<1024>);
  v8::Handle<v8::Object> inst = obj->NewInstance();
252 253 254
  context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
  int count_before =
      i::HandleScope::NumberOfHandles(reinterpret_cast<i::Isolate*>(isolate));
255
  {
256
    v8::HandleScope scope(isolate);
257 258 259 260 261 262
    CompileRun(
        "for (var i = 0; i < 1000; i++) {"
        "  obj.one;"
        "  obj.many;"
        "}");
  }
263 264
  int count_after =
      i::HandleScope::NumberOfHandles(reinterpret_cast<i::Isolate*>(isolate));
265 266 267
  CHECK_EQ(count_before, count_after);
}

268 269 270
static void CheckAccessorArgsCorrect(
    Local<String> name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
271
  CHECK(info.GetIsolate() == CcTest::isolate());
272
  CHECK(info.This() == info.Holder());
273 274
  CHECK(
      info.Data()->Equals(v8::String::NewFromUtf8(CcTest::isolate(), "data")));
275
  ApiTestFuzzer::Fuzz();
276
  CHECK(info.GetIsolate() == CcTest::isolate());
277
  CHECK(info.This() == info.Holder());
278 279
  CHECK(
      info.Data()->Equals(v8::String::NewFromUtf8(CcTest::isolate(), "data")));
280
  CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags);
281
  CHECK(info.GetIsolate() == CcTest::isolate());
282
  CHECK(info.This() == info.Holder());
283 284
  CHECK(
      info.Data()->Equals(v8::String::NewFromUtf8(CcTest::isolate(), "data")));
285
  info.GetReturnValue().Set(17);
286 287
}

288

289
THREADED_TEST(DirectCall) {
290
  LocalContext context;
291 292 293
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
294 295 296
  obj->SetAccessor(v8_str("xxx"),
                   CheckAccessorArgsCorrect,
                   NULL,
297
                   v8::String::NewFromUtf8(isolate, "data"));
298
  v8::Handle<v8::Object> inst = obj->NewInstance();
299
  context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"),
300 301
                         inst);
  Local<Script> scr = v8::Script::Compile(
302
      v8::String::NewFromUtf8(isolate, "obj.xxx"));
303 304 305 306 307 308 309
  for (int i = 0; i < 10; i++) {
    Local<Value> result = scr->Run();
    CHECK(!result.IsEmpty());
    CHECK_EQ(17, result->Int32Value());
  }
}

310 311
static void EmptyGetter(Local<String> name,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
312 313 314
  CheckAccessorArgsCorrect(name, info);
  ApiTestFuzzer::Fuzz();
  CheckAccessorArgsCorrect(name, info);
315
  info.GetReturnValue().Set(v8::Handle<v8::Value>());
316 317
}

318

319
THREADED_TEST(EmptyResult) {
320
  LocalContext context;
321 322
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
323
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
324 325
  obj->SetAccessor(v8_str("xxx"), EmptyGetter, NULL,
                   v8::String::NewFromUtf8(isolate, "data"));
326
  v8::Handle<v8::Object> inst = obj->NewInstance();
327 328 329
  context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
  Local<Script> scr =
      v8::Script::Compile(v8::String::NewFromUtf8(isolate, "obj.xxx"));
330 331
  for (int i = 0; i < 10; i++) {
    Local<Value> result = scr->Run();
332
    CHECK(result == v8::Undefined(isolate));
333 334 335 336 337 338 339
  }
}


THREADED_TEST(NoReuseRegress) {
  // Check that the IC generated for the one test doesn't get reused
  // for the other.
340 341
  v8::Isolate* isolate = CcTest::isolate();
  v8::HandleScope scope(isolate);
342
  {
343
    v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
344 345
    obj->SetAccessor(v8_str("xxx"), EmptyGetter, NULL,
                     v8::String::NewFromUtf8(isolate, "data"));
346 347
    LocalContext context;
    v8::Handle<v8::Object> inst = obj->NewInstance();
348 349 350
    context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
    Local<Script> scr =
        v8::Script::Compile(v8::String::NewFromUtf8(isolate, "obj.xxx"));
351 352
    for (int i = 0; i < 2; i++) {
      Local<Value> result = scr->Run();
353
      CHECK(result == v8::Undefined(isolate));
354 355 356
    }
  }
  {
357
    v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
358 359 360
    obj->SetAccessor(v8_str("xxx"),
                     CheckAccessorArgsCorrect,
                     NULL,
361
                     v8::String::NewFromUtf8(isolate, "data"));
362 363
    LocalContext context;
    v8::Handle<v8::Object> inst = obj->NewInstance();
364 365 366
    context->Global()->Set(v8::String::NewFromUtf8(isolate, "obj"), inst);
    Local<Script> scr =
        v8::Script::Compile(v8::String::NewFromUtf8(isolate, "obj.xxx"));
367 368 369 370 371 372 373 374
    for (int i = 0; i < 10; i++) {
      Local<Value> result = scr->Run();
      CHECK(!result.IsEmpty());
      CHECK_EQ(17, result->Int32Value());
    }
  }
}

375 376 377
static void ThrowingGetAccessor(
    Local<String> name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
378
  ApiTestFuzzer::Fuzz();
379
  info.GetIsolate()->ThrowException(v8_str("g"));
380 381 382 383 384
}


static void ThrowingSetAccessor(Local<String> name,
                                Local<Value> value,
385
                                const v8::PropertyCallbackInfo<void>& info) {
386
  info.GetIsolate()->ThrowException(value);
387 388 389 390
}


THREADED_TEST(Regress1054726) {
391
  LocalContext env;
392 393 394
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  obj->SetAccessor(v8_str("x"),
                   ThrowingGetAccessor,
                   ThrowingSetAccessor,
                   Local<Value>());

  env->Global()->Set(v8_str("obj"), obj->NewInstance());

  // Use the throwing property setter/getter in a loop to force
  // the accessor ICs to be initialized.
  v8::Handle<Value> result;
  result = Script::Compile(v8_str(
      "var result = '';"
      "for (var i = 0; i < 5; i++) {"
      "  try { obj.x; } catch (e) { result += e; }"
      "}; result"))->Run();
410
  CHECK(v8_str("ggggg")->Equals(result));
411

412
  result = Script::Compile(String::NewFromUtf8(
413
      isolate,
414 415 416 417
      "var result = '';"
      "for (var i = 0; i < 5; i++) {"
      "  try { obj.x = i; } catch (e) { result += e; }"
      "}; result"))->Run();
418
  CHECK(v8_str("01234")->Equals(result));
419 420 421
}


422 423
static void AllocGetter(Local<String> name,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
424
  ApiTestFuzzer::Fuzz();
425
  info.GetReturnValue().Set(v8::Array::New(info.GetIsolate(), 1000));
426 427 428 429
}


THREADED_TEST(Gc) {
430
  LocalContext env;
431 432 433
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
434 435
  obj->SetAccessor(v8_str("xxx"), AllocGetter);
  env->Global()->Set(v8_str("obj"), obj->NewInstance());
436
  Script::Compile(String::NewFromUtf8(
437
      isolate,
438 439 440 441 442 443 444 445 446
      "var last = [];"
      "for (var i = 0; i < 2048; i++) {"
      "  var result = obj.xxx;"
      "  result[0] = last;"
      "  last = result;"
      "}"))->Run();
}


447 448
static void StackCheck(Local<String> name,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
449
  i::StackFrameIterator iter(reinterpret_cast<i::Isolate*>(info.GetIsolate()));
450 451 452
  for (int i = 0; !iter.done(); i++) {
    i::StackFrame* frame = iter.frame();
    CHECK(i != 0 || (frame->type() == i::StackFrame::EXIT));
453
    i::Code* code = frame->LookupCode();
454
    CHECK(code->IsCode());
455 456 457 458 459 460 461 462
    i::Address pc = frame->pc();
    CHECK(code->contains(pc));
    iter.Advance();
  }
}


THREADED_TEST(StackIteration) {
463
  LocalContext env;
464 465 466 467 468
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
  i::StringStream::ClearMentionedObjectCache(
      reinterpret_cast<i::Isolate*>(isolate));
469 470
  obj->SetAccessor(v8_str("xxx"), StackCheck);
  env->Global()->Set(v8_str("obj"), obj->NewInstance());
471
  Script::Compile(String::NewFromUtf8(
472
      isolate,
473 474 475 476 477 478 479
      "function foo() {"
      "  return obj.xxx;"
      "}"
      "for (var i = 0; i < 100; i++) {"
      "  foo();"
      "}"))->Run();
}
480 481


482 483
static void AllocateHandles(Local<String> name,
                            const v8::PropertyCallbackInfo<v8::Value>& info) {
484
  for (int i = 0; i < i::kHandleBlockSize + 1; i++) {
485
    v8::Local<v8::Value>::New(info.GetIsolate(), name);
486
  }
487
  info.GetReturnValue().Set(v8::Integer::New(info.GetIsolate(), 100));
488 489 490 491 492 493
}


THREADED_TEST(HandleScopeSegment) {
  // Check that we can return values past popping of handle scope
  // segments.
494
  LocalContext env;
495 496 497
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
498 499
  obj->SetAccessor(v8_str("xxx"), AllocateHandles);
  env->Global()->Set(v8_str("obj"), obj->NewInstance());
500
  v8::Handle<v8::Value> result = Script::Compile(String::NewFromUtf8(
501
      isolate,
502 503 504 505 506 507
      "var result;"
      "for (var i = 0; i < 4; i++)"
      "  result = obj.xxx;"
      "result;"))->Run();
  CHECK_EQ(100, result->Int32Value());
}
508 509


510
void JSONStringifyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info) {
511
  v8::Handle<v8::Array> array = v8::Array::New(info.GetIsolate(), 1);
512
  array->Set(0, v8_str("regress"));
513
  info.GetReturnValue().Set(array);
514 515 516
}


517
void JSONStringifyGetter(Local<Name> name,
518 519
                         const v8::PropertyCallbackInfo<v8::Value>& info) {
  info.GetReturnValue().Set(v8_str("crbug-161028"));
520 521 522 523 524
}


THREADED_TEST(JSONStringifyNamedInterceptorObject) {
  LocalContext env;
525 526
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
527

528
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
529 530
  obj->SetHandler(v8::NamedPropertyHandlerConfiguration(
      JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator));
531 532 533 534
  env->Global()->Set(v8_str("obj"), obj->NewInstance());
  v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}");
  CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected));
}
535 536


537 538 539 540 541 542 543 544 545 546 547
static v8::Local<v8::Context> expected_current_context;
static v8::Local<v8::Context> expected_calling_context;


static void check_contexts(const v8::FunctionCallbackInfo<v8::Value>& info) {
  ApiTestFuzzer::Fuzz();
  CHECK(expected_current_context == info.GetIsolate()->GetCurrentContext());
  CHECK(expected_calling_context == info.GetIsolate()->GetCallingContext());
}


548
THREADED_TEST(AccessorPropertyCrossContext) {
549 550 551
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
552
  v8::Handle<v8::Function> fun = v8::Function::New(isolate, check_contexts);
553 554 555
  LocalContext switch_context;
  switch_context->Global()->Set(v8_str("fun"), fun);
  v8::TryCatch try_catch;
556 557
  expected_current_context = env.local();
  expected_calling_context = switch_context.local();
558 559 560 561 562
  CompileRun(
      "var o = Object.create(null, { n: { get:fun } });"
      "for (var i = 0; i < 10; i++) o.n;");
  CHECK(!try_catch.HasCaught());
}
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580


THREADED_TEST(GlobalObjectAccessor) {
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  CompileRun(
      "var set_value = 1;"
      "Object.defineProperty(this.__proto__, 'x', {"
      "    get : function() { return this; },"
      "    set : function() { set_value = this; }"
      "});"
      "function getter() { return x; }"
      "function setter() { x = 1; }"
      "for (var i = 0; i < 4; i++) { getter(); setter(); }");
  CHECK(v8::Utils::OpenHandle(*CompileRun("getter()"))->IsJSGlobalProxy());
  CHECK(v8::Utils::OpenHandle(*CompileRun("set_value"))->IsJSGlobalProxy());
}
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607


static void EmptyGetter(Local<Name> name,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
  ApiTestFuzzer::Fuzz();
}


static void OneProperty(Local<String> name,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
  ApiTestFuzzer::Fuzz();
  info.GetReturnValue().Set(v8_num(1));
}


THREADED_TEST(Regress433458) {
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
  obj->SetHandler(v8::NamedPropertyHandlerConfiguration(EmptyGetter));
  obj->SetNativeDataProperty(v8_str("prop"), OneProperty);
  env->Global()->Set(v8_str("obj"), obj->NewInstance());
  CompileRun(
      "Object.defineProperty(obj, 'prop', { writable: false });"
      "Object.defineProperty(obj, 'prop', { writable: true });");
}
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690


static bool security_check_value = false;


static bool SecurityTestCallback(Local<v8::Object> global, Local<Value> name,
                                 v8::AccessType type, Local<Value> data) {
  return security_check_value;
}


TEST(PrototypeGetterAccessCheck) {
  i::FLAG_allow_natives_syntax = true;
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  auto fun_templ = v8::FunctionTemplate::New(isolate);
  auto getter_templ = v8::FunctionTemplate::New(isolate, handle_property);
  getter_templ->SetAcceptAnyReceiver(false);
  fun_templ->InstanceTemplate()->SetAccessorProperty(v8_str("foo"),
                                                     getter_templ);
  auto obj_templ = v8::ObjectTemplate::New(isolate);
  obj_templ->SetAccessCheckCallbacks(SecurityTestCallback, nullptr);
  env->Global()->Set(v8_str("Fun"), fun_templ->GetFunction());
  env->Global()->Set(v8_str("obj"), obj_templ->NewInstance());
  env->Global()->Set(v8_str("obj2"), obj_templ->NewInstance());

  security_check_value = true;
  CompileRun("var proto = new Fun();");
  CompileRun("obj.__proto__ = proto;");
  ExpectInt32("proto.foo", 907);

  // Test direct.
  security_check_value = true;
  ExpectInt32("obj.foo", 907);
  security_check_value = false;
  {
    v8::TryCatch try_catch(isolate);
    CompileRun("obj.foo");
    CHECK(try_catch.HasCaught());
  }

  // Test through call.
  security_check_value = true;
  ExpectInt32("proto.__lookupGetter__('foo').call(obj)", 907);
  security_check_value = false;
  {
    v8::TryCatch try_catch(isolate);
    CompileRun("proto.__lookupGetter__('foo').call(obj)");
    CHECK(try_catch.HasCaught());
  }

  // Test ics.
  CompileRun(
      "function f() {"
      "   var x;"
      "  for (var i = 0; i < 4; i++) {"
      "    x = obj.foo;"
      "  }"
      "  return x;"
      "}");

  security_check_value = true;
  ExpectInt32("f()", 907);
  security_check_value = false;
  {
    v8::TryCatch try_catch(isolate);
    CompileRun("f();");
    CHECK(try_catch.HasCaught());
  }

  // Test crankshaft.
  CompileRun("%OptimizeFunctionOnNextCall(f);");

  security_check_value = true;
  ExpectInt32("f()", 907);
  security_check_value = false;
  {
    v8::TryCatch try_catch(isolate);
    CompileRun("f();");
    CHECK(try_catch.HasCaught());
  }
}