test-accessors.cc 30.2 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
#include "src/api-inl.h"
33 34 35
#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.local()).ToLocalChecked();
  CHECK(env->Global()->Set(env.local(), v8_str("Fun"), fun).FromJust());
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 88 89 90 91 92
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(900, getter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
93
  setter = v8_compile("obj.instance_foo = 901;");
94 95 96 97 98 99
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(901, setter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
100
  getter = v8_compile("obj.bar;");
101 102 103 104 105 106
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(907, getter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
107
  setter = v8_compile("obj.bar = 908;");
108 109 110 111 112 113
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(908, setter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
114 115
  // check function static accessors
  getter = v8_compile("Fun.object_foo;");
116 117 118 119 120 121
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(902, getter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
122
  setter = v8_compile("Fun.object_foo = 903;");
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(903, setter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }

  // And now with null prototype.
  CompileRun(env.local(), "obj.__proto__ = null;");
  getter = v8_compile("obj.bar;");
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(907, getter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
  setter = v8_compile("obj.bar = 908;");
  for (int i = 0; i < 4; i++) {
    CHECK_EQ(908, setter->Run(env.local())
                      .ToLocalChecked()
                      ->Int32Value(env.local())
                      .FromJust());
  }
146 147 148
}


149 150
static void GetIntValue(Local<String> property,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
151 152
  ApiTestFuzzer::Fuzz();
  int* value =
153
      static_cast<int*>(v8::Local<v8::External>::Cast(info.Data())->Value());
154
  info.GetReturnValue().Set(v8_num(*value));
155 156 157 158 159
}


static void SetIntValue(Local<String> property,
                        Local<Value> value,
160
                        const v8::PropertyCallbackInfo<void>& info) {
161
  int* field =
162 163
      static_cast<int*>(v8::Local<v8::External>::Cast(info.Data())->Value());
  *field = value->Int32Value(info.GetIsolate()->GetCurrentContext()).FromJust();
164 165 166 167 168 169 170 171
}

int foo, bar, baz;

THREADED_TEST(GlobalVariableAccess) {
  foo = 0;
  bar = -4;
  baz = 10;
172 173
  v8::Isolate* isolate = CcTest::isolate();
  v8::HandleScope scope(isolate);
174
  v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
175 176
  templ->InstanceTemplate()->SetAccessor(
      v8_str("foo"), GetIntValue, SetIntValue,
177
      v8::External::New(isolate, &foo));
178 179
  templ->InstanceTemplate()->SetAccessor(
      v8_str("bar"), GetIntValue, SetIntValue,
180
      v8::External::New(isolate, &bar));
181 182
  templ->InstanceTemplate()->SetAccessor(
      v8_str("baz"), GetIntValue, SetIntValue,
183
      v8::External::New(isolate, &baz));
184
  LocalContext env(nullptr, templ->InstanceTemplate());
185
  v8_compile("foo = (++bar) + baz")->Run(env.local()).ToLocalChecked();
186 187
  CHECK_EQ(-3, bar);
  CHECK_EQ(7, foo);
188 189 190
}


191
static int x_register[2] = {0, 0};
192 193
static v8::Local<v8::Object> x_receiver;
static v8::Local<v8::Object> x_holder;
194

195 196
template<class Info>
static void XGetter(const Info& info, int offset) {
197
  ApiTestFuzzer::Fuzz();
198
  v8::Isolate* isolate = CcTest::isolate();
199
  CHECK_EQ(isolate, info.GetIsolate());
200 201
  CHECK(
      x_receiver->Equals(isolate->GetCurrentContext(), info.This()).FromJust());
202 203 204 205 206 207
  info.GetReturnValue().Set(v8_num(x_register[offset]));
}


static void XGetter(Local<String> name,
                    const v8::PropertyCallbackInfo<v8::Value>& info) {
208 209
  CHECK(x_holder->Equals(info.GetIsolate()->GetCurrentContext(), info.Holder())
            .FromJust());
210
  XGetter(info, 0);
211 212 213
}


214
static void XGetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
215 216 217
  CHECK(
      x_receiver->Equals(info.GetIsolate()->GetCurrentContext(), info.Holder())
          .FromJust());
218 219 220 221 222 223
  XGetter(info, 1);
}


template<class Info>
static void XSetter(Local<Value> value, const Info& info, int offset) {
224
  v8::Isolate* isolate = CcTest::isolate();
225
  CHECK_EQ(isolate, info.GetIsolate());
226 227 228 229 230 231
  CHECK(x_holder->Equals(info.GetIsolate()->GetCurrentContext(), info.This())
            .FromJust());
  CHECK(x_holder->Equals(info.GetIsolate()->GetCurrentContext(), info.Holder())
            .FromJust());
  x_register[offset] =
      value->Int32Value(info.GetIsolate()->GetCurrentContext()).FromJust();
232
  info.GetReturnValue().Set(v8_num(-1));
233 234 235 236 237 238 239 240 241 242 243 244 245
}


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);
246 247 248 249
}


THREADED_TEST(AccessorIC) {
250
  LocalContext context;
251 252
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
253
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
254 255
  obj->SetAccessor(v8_str("x0"), XGetter, XSetter);
  obj->SetAccessorProperty(v8_str("x1"),
256 257
                           v8::FunctionTemplate::New(isolate, XGetter),
                           v8::FunctionTemplate::New(isolate, XSetter));
258 259 260 261
  x_holder = obj->NewInstance(context.local()).ToLocalChecked();
  CHECK(context->Global()
            ->Set(context.local(), v8_str("holder"), x_holder)
            .FromJust());
262
  x_receiver = v8::Object::New(isolate);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  CHECK(context->Global()
            ->Set(context.local(), v8_str("obj"), x_receiver)
            .FromJust());
  v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(
      CompileRun("obj.__proto__ = holder;"
                 "var result = [];"
                 "var key_0 = 'x0';"
                 "var key_1 = 'x1';"
                 "for (var j = 0; j < 10; j++) {"
                 "  var i = 4*j;"
                 "  result.push(holder.x0 = i);"
                 "  result.push(obj.x0);"
                 "  result.push(holder.x1 = i + 1);"
                 "  result.push(obj.x1);"
                 "  result.push(holder[key_0] = i + 2);"
                 "  result.push(obj[key_0]);"
                 "  result.push(holder[key_1] = i + 3);"
                 "  result.push(obj[key_1]);"
                 "}"
                 "result"));
283
  CHECK_EQ(80u, array->Length());
284
  for (int i = 0; i < 80; i++) {
285 286 287 288 289 290
    v8::Local<Value> entry =
        array->Get(context.local(), v8::Integer::New(isolate, i))
            .ToLocalChecked();
    CHECK(v8::Integer::New(isolate, i / 2)
              ->Equals(context.local(), entry)
              .FromJust());
291 292 293 294 295
  }
}


template <int C>
296 297 298
static void HandleAllocatingGetter(
    Local<String> name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
299
  ApiTestFuzzer::Fuzz();
300 301 302 303 304 305 306 307
  for (int i = 0; i < C; i++) {
    v8::String::NewFromUtf8(info.GetIsolate(), "foo",
                            v8::NewStringType::kNormal)
        .ToLocalChecked();
  }
  info.GetReturnValue().Set(v8::String::NewFromUtf8(info.GetIsolate(), "foo",
                                                    v8::NewStringType::kNormal)
                                .ToLocalChecked());
308 309 310 311
}


THREADED_TEST(HandleScopePop) {
312
  LocalContext context;
313 314
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
315
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
316 317
  obj->SetAccessor(v8_str("one"), HandleAllocatingGetter<1>);
  obj->SetAccessor(v8_str("many"), HandleAllocatingGetter<1024>);
318 319 320 321
  v8::Local<v8::Object> inst =
      obj->NewInstance(context.local()).ToLocalChecked();
  CHECK(
      context->Global()->Set(context.local(), v8_str("obj"), inst).FromJust());
322 323
  int count_before =
      i::HandleScope::NumberOfHandles(reinterpret_cast<i::Isolate*>(isolate));
324
  {
325
    v8::HandleScope scope(isolate);
326 327 328 329 330 331
    CompileRun(
        "for (var i = 0; i < 1000; i++) {"
        "  obj.one;"
        "  obj.many;"
        "}");
  }
332 333
  int count_after =
      i::HandleScope::NumberOfHandles(reinterpret_cast<i::Isolate*>(isolate));
334 335 336
  CHECK_EQ(count_before, count_after);
}

337 338 339
static void CheckAccessorArgsCorrect(
    Local<String> name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
340
  CHECK(info.GetIsolate() == CcTest::isolate());
341
  CHECK(info.This() == info.Holder());
342 343 344
  CHECK(info.Data()
            ->Equals(info.GetIsolate()->GetCurrentContext(), v8_str("data"))
            .FromJust());
345
  ApiTestFuzzer::Fuzz();
346
  CHECK(info.GetIsolate() == CcTest::isolate());
347
  CHECK(info.This() == info.Holder());
348 349 350
  CHECK(info.Data()
            ->Equals(info.GetIsolate()->GetCurrentContext(), v8_str("data"))
            .FromJust());
351
  CcTest::CollectAllGarbage();
352
  CHECK(info.GetIsolate() == CcTest::isolate());
353
  CHECK(info.This() == info.Holder());
354 355 356
  CHECK(info.Data()
            ->Equals(info.GetIsolate()->GetCurrentContext(), v8_str("data"))
            .FromJust());
357
  info.GetReturnValue().Set(17);
358 359
}

360

361
THREADED_TEST(DirectCall) {
362
  LocalContext context;
363 364
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
365
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
366
  obj->SetAccessor(v8_str("xxx"), CheckAccessorArgsCorrect, nullptr,
367 368 369 370 371 372 373
                   v8_str("data"));
  v8::Local<v8::Object> inst =
      obj->NewInstance(context.local()).ToLocalChecked();
  CHECK(
      context->Global()->Set(context.local(), v8_str("obj"), inst).FromJust());
  Local<Script> scr =
      v8::Script::Compile(context.local(), v8_str("obj.xxx")).ToLocalChecked();
374
  for (int i = 0; i < 10; i++) {
375
    Local<Value> result = scr->Run(context.local()).ToLocalChecked();
376
    CHECK(!result.IsEmpty());
377
    CHECK_EQ(17, result->Int32Value(context.local()).FromJust());
378 379 380
  }
}

381 382
static void EmptyGetter(Local<String> name,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
383 384 385
  CheckAccessorArgsCorrect(name, info);
  ApiTestFuzzer::Fuzz();
  CheckAccessorArgsCorrect(name, info);
386
  info.GetReturnValue().Set(v8::Local<v8::Value>());
387 388
}

389

390
THREADED_TEST(EmptyResult) {
391
  LocalContext context;
392 393
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope scope(isolate);
394
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
395
  obj->SetAccessor(v8_str("xxx"), EmptyGetter, nullptr, v8_str("data"));
396 397 398 399
  v8::Local<v8::Object> inst =
      obj->NewInstance(context.local()).ToLocalChecked();
  CHECK(
      context->Global()->Set(context.local(), v8_str("obj"), inst).FromJust());
400
  Local<Script> scr =
401
      v8::Script::Compile(context.local(), v8_str("obj.xxx")).ToLocalChecked();
402
  for (int i = 0; i < 10; i++) {
403
    Local<Value> result = scr->Run(context.local()).ToLocalChecked();
404
    CHECK(result == v8::Undefined(isolate));
405 406 407 408 409 410 411
  }
}


THREADED_TEST(NoReuseRegress) {
  // Check that the IC generated for the one test doesn't get reused
  // for the other.
412 413
  v8::Isolate* isolate = CcTest::isolate();
  v8::HandleScope scope(isolate);
414
  {
415
    v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
416
    obj->SetAccessor(v8_str("xxx"), EmptyGetter, nullptr, v8_str("data"));
417
    LocalContext context;
418 419 420 421 422 423 424
    v8::Local<v8::Object> inst =
        obj->NewInstance(context.local()).ToLocalChecked();
    CHECK(context->Global()
              ->Set(context.local(), v8_str("obj"), inst)
              .FromJust());
    Local<Script> scr = v8::Script::Compile(context.local(), v8_str("obj.xxx"))
                            .ToLocalChecked();
425
    for (int i = 0; i < 2; i++) {
426
      Local<Value> result = scr->Run(context.local()).ToLocalChecked();
427
      CHECK(result == v8::Undefined(isolate));
428 429 430
    }
  }
  {
431
    v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
432
    obj->SetAccessor(v8_str("xxx"), CheckAccessorArgsCorrect, nullptr,
433
                     v8_str("data"));
434
    LocalContext context;
435 436 437 438 439 440 441
    v8::Local<v8::Object> inst =
        obj->NewInstance(context.local()).ToLocalChecked();
    CHECK(context->Global()
              ->Set(context.local(), v8_str("obj"), inst)
              .FromJust());
    Local<Script> scr = v8::Script::Compile(context.local(), v8_str("obj.xxx"))
                            .ToLocalChecked();
442
    for (int i = 0; i < 10; i++) {
443
      Local<Value> result = scr->Run(context.local()).ToLocalChecked();
444
      CHECK(!result.IsEmpty());
445
      CHECK_EQ(17, result->Int32Value(context.local()).FromJust());
446 447 448 449
    }
  }
}

450 451 452
static void ThrowingGetAccessor(
    Local<String> name,
    const v8::PropertyCallbackInfo<v8::Value>& info) {
453
  ApiTestFuzzer::Fuzz();
454
  info.GetIsolate()->ThrowException(v8_str("g"));
455 456 457 458 459
}


static void ThrowingSetAccessor(Local<String> name,
                                Local<Value> value,
460
                                const v8::PropertyCallbackInfo<void>& info) {
461
  info.GetIsolate()->ThrowException(value);
462 463 464 465
}


THREADED_TEST(Regress1054726) {
466
  LocalContext env;
467 468
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
469
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
470 471 472 473 474
  obj->SetAccessor(v8_str("x"),
                   ThrowingGetAccessor,
                   ThrowingSetAccessor,
                   Local<Value>());

475 476 477 478
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
479 480 481

  // Use the throwing property setter/getter in a loop to force
  // the accessor ICs to be initialized.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
  v8::Local<Value> result;
  result = Script::Compile(env.local(),
                           v8_str("var result = '';"
                                  "for (var i = 0; i < 5; i++) {"
                                  "  try { obj.x; } catch (e) { result += e; }"
                                  "}; result"))
               .ToLocalChecked()
               ->Run(env.local())
               .ToLocalChecked();
  CHECK(v8_str("ggggg")->Equals(env.local(), result).FromJust());

  result =
      Script::Compile(env.local(),
                      v8_str("var result = '';"
                             "for (var i = 0; i < 5; i++) {"
                             "  try { obj.x = i; } catch (e) { result += e; }"
                             "}; result"))
          .ToLocalChecked()
          ->Run(env.local())
          .ToLocalChecked();
  CHECK(v8_str("01234")->Equals(env.local(), result).FromJust());
503 504 505
}


506 507
static void AllocGetter(Local<String> name,
                        const v8::PropertyCallbackInfo<v8::Value>& info) {
508
  ApiTestFuzzer::Fuzz();
509
  info.GetReturnValue().Set(v8::Array::New(info.GetIsolate(), 1000));
510 511 512 513
}


THREADED_TEST(Gc) {
514
  LocalContext env;
515 516
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
517
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
518
  obj->SetAccessor(v8_str("xxx"), AllocGetter);
519 520 521 522 523 524 525 526 527 528 529 530 531
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
  Script::Compile(env.local(), v8_str("var last = [];"
                                      "for (var i = 0; i < 2048; i++) {"
                                      "  var result = obj.xxx;"
                                      "  result[0] = last;"
                                      "  last = result;"
                                      "}"))
      .ToLocalChecked()
      ->Run(env.local())
      .ToLocalChecked();
532 533 534
}


535 536
static void StackCheck(Local<String> name,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
537
  i::StackFrameIterator iter(reinterpret_cast<i::Isolate*>(info.GetIsolate()));
538 539 540
  for (int i = 0; !iter.done(); i++) {
    i::StackFrame* frame = iter.frame();
    CHECK(i != 0 || (frame->type() == i::StackFrame::EXIT));
541
    i::Code code = frame->LookupCode();
542
    CHECK(code->IsCode());
543
    CHECK(code->contains(frame->pc()));
544 545 546 547 548 549
    iter.Advance();
  }
}


THREADED_TEST(StackIteration) {
550
  LocalContext env;
551 552
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
553
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
554 555
  i::StringStream::ClearMentionedObjectCache(
      reinterpret_cast<i::Isolate*>(isolate));
556
  obj->SetAccessor(v8_str("xxx"), StackCheck);
557 558 559 560 561 562 563 564 565 566 567 568 569
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
  Script::Compile(env.local(), v8_str("function foo() {"
                                      "  return obj.xxx;"
                                      "}"
                                      "for (var i = 0; i < 100; i++) {"
                                      "  foo();"
                                      "}"))
      .ToLocalChecked()
      ->Run(env.local())
      .ToLocalChecked();
570
}
571 572


573 574
static void AllocateHandles(Local<String> name,
                            const v8::PropertyCallbackInfo<v8::Value>& info) {
575
  for (int i = 0; i < i::kHandleBlockSize + 1; i++) {
576
    v8::Local<v8::Value>::New(info.GetIsolate(), name);
577
  }
578
  info.GetReturnValue().Set(v8::Integer::New(info.GetIsolate(), 100));
579 580 581 582 583 584
}


THREADED_TEST(HandleScopeSegment) {
  // Check that we can return values past popping of handle scope
  // segments.
585
  LocalContext env;
586 587
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
588
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
589
  obj->SetAccessor(v8_str("xxx"), AllocateHandles);
590 591 592 593 594 595 596 597 598 599 600 601 602
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
  v8::Local<v8::Value> result =
      Script::Compile(env.local(), v8_str("var result;"
                                          "for (var i = 0; i < 4; i++)"
                                          "  result = obj.xxx;"
                                          "result;"))
          .ToLocalChecked()
          ->Run(env.local())
          .ToLocalChecked();
  CHECK_EQ(100, result->Int32Value(env.local()).FromJust());
603
}
604 605


606
void JSONStringifyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info) {
607 608 609
  v8::Local<v8::Array> array = v8::Array::New(info.GetIsolate(), 1);
  CHECK(array->Set(info.GetIsolate()->GetCurrentContext(), 0, v8_str("regress"))
            .FromJust());
610
  info.GetReturnValue().Set(array);
611 612 613
}


614
void JSONStringifyGetter(Local<Name> name,
615 616
                         const v8::PropertyCallbackInfo<v8::Value>& info) {
  info.GetReturnValue().Set(v8_str("crbug-161028"));
617 618 619 620 621
}


THREADED_TEST(JSONStringifyNamedInterceptorObject) {
  LocalContext env;
622 623
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
624

625
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
626
  obj->SetHandler(v8::NamedPropertyHandlerConfiguration(
627
      JSONStringifyGetter, nullptr, nullptr, nullptr, JSONStringifyEnumerator));
628 629 630 631 632 633 634 635
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
  v8::Local<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}");
  CHECK(CompileRun("JSON.stringify(obj)")
            ->Equals(env.local(), expected)
            .FromJust());
636
}
637 638


639 640 641 642 643 644 645 646 647
static v8::Local<v8::Context> expected_current_context;


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


648
THREADED_TEST(AccessorPropertyCrossContext) {
649 650 651
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
652 653
  v8::Local<v8::Function> fun =
      v8::Function::New(env.local(), check_contexts).ToLocalChecked();
654
  LocalContext switch_context;
655 656 657
  CHECK(switch_context->Global()
            ->Set(switch_context.local(), v8_str("fun"), fun)
            .FromJust());
658
  v8::TryCatch try_catch(isolate);
659
  expected_current_context = env.local();
660 661 662 663 664
  CompileRun(
      "var o = Object.create(null, { n: { get:fun } });"
      "for (var i = 0; i < 10; i++) o.n;");
  CHECK(!try_catch.HasCaught());
}
665 666 667 668 669 670 671 672 673 674 675 676 677


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; }"
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
      "function setter() { x = 1; }");

  Local<Script> check_getter = v8_compile("getter()");
  Local<Script> check_setter = v8_compile("setter(); set_value");

  // Ensure that LoadGlobalICs in getter and StoreGlobalICs setter get
  // JSGlobalProxy as a receiver regardless of the current IC state and
  // the order in which ICs are executed.
  for (int i = 0; i < 10; i++) {
    CHECK(
        v8::Utils::OpenHandle(*check_getter->Run(env.local()).ToLocalChecked())
            ->IsJSGlobalProxy());
  }
  for (int i = 0; i < 10; i++) {
    CHECK(
        v8::Utils::OpenHandle(*check_setter->Run(env.local()).ToLocalChecked())
            ->IsJSGlobalProxy());
  }
  for (int i = 0; i < 10; i++) {
    CHECK(
        v8::Utils::OpenHandle(*check_getter->Run(env.local()).ToLocalChecked())
            ->IsJSGlobalProxy());
    CHECK(
        v8::Utils::OpenHandle(*check_setter->Run(env.local()).ToLocalChecked())
            ->IsJSGlobalProxy());
  }
704
}
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723


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);
724
  v8::Local<v8::ObjectTemplate> obj = ObjectTemplate::New(isolate);
725 726
  obj->SetHandler(v8::NamedPropertyHandlerConfiguration(EmptyGetter));
  obj->SetNativeDataProperty(v8_str("prop"), OneProperty);
727 728 729 730
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
731 732 733 734
  CompileRun(
      "Object.defineProperty(obj, 'prop', { writable: false });"
      "Object.defineProperty(obj, 'prop', { writable: true });");
}
735 736 737 738


static bool security_check_value = false;

739
static bool SecurityTestCallback(Local<v8::Context> accessing_context,
740 741
                                 Local<v8::Object> accessed_object,
                                 Local<v8::Value> data) {
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
  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);
757
  obj_templ->SetAccessCheckCallback(SecurityTestCallback);
758 759 760 761 762 763 764 765 766 767 768 769
  CHECK(env->Global()
            ->Set(env.local(), v8_str("Fun"),
                  fun_templ->GetFunction(env.local()).ToLocalChecked())
            .FromJust());
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj"),
                  obj_templ->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
  CHECK(env->Global()
            ->Set(env.local(), v8_str("obj2"),
                  obj_templ->NewInstance(env.local()).ToLocalChecked())
            .FromJust());
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

  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());
  }

815
  // Test TurboFan.
816 817 818 819 820 821 822 823 824 825 826
  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());
  }
}
827

828 829
static void CheckReceiver(Local<String> name,
                          const v8::PropertyCallbackInfo<v8::Value>& info) {
830 831 832 833 834 835 836 837 838
  CHECK(info.This()->IsObject());
}

TEST(Regress609134) {
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  auto fun_templ = v8::FunctionTemplate::New(isolate);
  fun_templ->InstanceTemplate()->SetNativeDataProperty(v8_str("foo"),
839
                                                       CheckReceiver);
840 841 842 843 844 845 846 847 848

  CHECK(env->Global()
            ->Set(env.local(), v8_str("Fun"),
                  fun_templ->GetFunction(env.local()).ToLocalChecked())
            .FromJust());

  CompileRun(
      "var f = new Fun();"
      "Number.prototype.__proto__ = f;"
849 850
      "var a = 42;"
      "for (var i = 0; i<3; i++) { a.foo; }");
851
}
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886

TEST(ObjectSetLazyDataProperty) {
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  v8::HandleScope scope(isolate);
  v8::Local<v8::Object> obj = v8::Object::New(isolate);
  CHECK(env->Global()->Set(env.local(), v8_str("obj"), obj).FromJust());

  // Despite getting the property multiple times, the getter should only be
  // called once and data property reads should continue to produce the same
  // value.
  static int getter_call_count;
  getter_call_count = 0;
  auto result = obj->SetLazyDataProperty(
      env.local(), v8_str("foo"),
      [](Local<Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
        getter_call_count++;
        info.GetReturnValue().Set(getter_call_count);
      });
  CHECK(result.FromJust());
  CHECK_EQ(0, getter_call_count);
  for (int i = 0; i < 2; i++) {
    ExpectInt32("obj.foo", 1);
    CHECK_EQ(1, getter_call_count);
  }

  // Setting should overwrite the data property.
  result = obj->SetLazyDataProperty(
      env.local(), v8_str("bar"),
      [](Local<Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
        CHECK(false);
      });
  CHECK(result.FromJust());
  ExpectInt32("obj.bar = -1; obj.bar;", -1);
}