test-decls.cc 36.8 KB
Newer Older
1
// Copyright 2007-2008 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/heap/heap-inl.h"
33
#include "src/heap/heap.h"
34
#include "test/cctest/cctest.h"
35 36 37

using namespace v8;

38
namespace {
39 40 41

enum Expectations {
  EXPECT_RESULT,
42 43
  EXPECT_EXCEPTION,
  EXPECT_ERROR
44 45 46 47 48 49 50 51 52 53 54 55 56
};


// A DeclarationContext holds a reference to a v8::Context and keeps
// track of various declaration related counters to make it easier to
// track if global declarations in the presence of interceptors behave
// the right way.
class DeclarationContext {
 public:
  DeclarationContext();

  virtual ~DeclarationContext() {
    if (is_initialized_) {
57
      Isolate* isolate = CcTest::isolate();
58 59 60
      HandleScope scope(isolate);
      Local<Context> context = Local<Context>::New(isolate, context_);
      context->Exit();
61
      context_.Reset();
62 63 64
    }
  }

65
  void Check(const char* source, int get, int set, int has,
66
             Expectations expectations,
67
             v8::Local<Value> value = Local<Value>());
68 69 70

  int get_count() const { return get_count_; }
  int set_count() const { return set_count_; }
71
  int query_count() const { return query_count_; }
72 73

 protected:
74 75 76
  virtual v8::Local<Value> Get(Local<Name> key);
  virtual v8::Local<Value> Set(Local<Name> key, Local<Value> value);
  virtual v8::Local<Integer> Query(Local<Name> key);
77 78 79

  void InitializeIfNeeded();

80 81
  // Perform optional initialization steps on the context after it has
  // been created. Defaults to none but may be overwritten.
82
  virtual void PostInitializeContext(Local<Context> context) {}
83

84 85 86 87 88 89 90 91
  // Get the holder for the interceptor. Default to the instance template
  // but may be overwritten.
  virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
    return function->InstanceTemplate();
  }

  // The handlers are called as static functions that forward
  // to the instance specific virtual methods.
92
  static void HandleGet(Local<Name> key,
93
                        const v8::PropertyCallbackInfo<v8::Value>& info);
94
  static void HandleSet(Local<Name> key, Local<Value> value,
95
                        const v8::PropertyCallbackInfo<v8::Value>& info);
96
  static void HandleQuery(Local<Name> key,
97
                          const v8::PropertyCallbackInfo<v8::Integer>& info);
98

99 100
  v8::Isolate* isolate() const { return CcTest::isolate(); }

101 102 103 104 105 106
 private:
  bool is_initialized_;
  Persistent<Context> context_;

  int get_count_;
  int set_count_;
107
  int query_count_;
108

109
  static DeclarationContext* GetInstance(Local<Value> data);
110 111 112 113
};


DeclarationContext::DeclarationContext()
114
    : is_initialized_(false), get_count_(0), set_count_(0), query_count_(0) {
115 116 117 118 119 120
  // Do nothing.
}


void DeclarationContext::InitializeIfNeeded() {
  if (is_initialized_) return;
121
  Isolate* isolate = CcTest::isolate();
122
  HandleScope scope(isolate);
123
  Local<FunctionTemplate> function = FunctionTemplate::New(isolate);
124
  Local<Value> data = External::New(CcTest::isolate(), this);
125 126
  GetHolder(function)->SetHandler(v8::NamedPropertyHandlerConfiguration(
      &HandleGet, &HandleSet, &HandleQuery, 0, 0, data));
127 128 129 130 131 132
  Local<Context> context = Context::New(isolate,
                                        0,
                                        function->InstanceTemplate(),
                                        Local<Value>());
  context_.Reset(isolate, context);
  context->Enter();
133
  is_initialized_ = true;
134 135 136 137
  // Reset counts. Bootstrapping might have called into the interceptor.
  get_count_ = 0;
  set_count_ = 0;
  query_count_ = 0;
138
  PostInitializeContext(context);
139 140 141
}


142
void DeclarationContext::Check(const char* source, int get, int set, int query,
143
                               Expectations expectations,
144
                               v8::Local<Value> value) {
145 146 147
  InitializeIfNeeded();
  // A retry after a GC may pollute the counts, so perform gc now
  // to avoid that.
148
  CcTest::CollectGarbage(v8::internal::NEW_SPACE);
149
  HandleScope scope(CcTest::isolate());
150
  TryCatch catcher(CcTest::isolate());
151
  catcher.SetVerbose(true);
152 153 154 155 156
  Local<Context> context = CcTest::isolate()->GetCurrentContext();
  MaybeLocal<Script> script = Script::Compile(
      context,
      String::NewFromUtf8(CcTest::isolate(), source, v8::NewStringType::kNormal)
          .ToLocalChecked());
157 158 159 160 161
  if (expectations == EXPECT_ERROR) {
    CHECK(script.IsEmpty());
    return;
  }
  CHECK(!script.IsEmpty());
162
  MaybeLocal<Value> result = script.ToLocalChecked()->Run(context);
163 164
  CHECK_EQ(get, get_count());
  CHECK_EQ(set, set_count());
165
  CHECK_EQ(query, query_count());
166 167 168
  if (expectations == EXPECT_RESULT) {
    CHECK(!catcher.HasCaught());
    if (!value.IsEmpty()) {
169
      CHECK(value->Equals(context, result.ToLocalChecked()).FromJust());
170 171 172 173 174
    }
  } else {
    CHECK(expectations == EXPECT_EXCEPTION);
    CHECK(catcher.HasCaught());
    if (!value.IsEmpty()) {
175
      CHECK(value->Equals(context, catcher.Exception()).FromJust());
176 177
    }
  }
178
  // Clean slate for the next test.
179
  CcTest::CollectAllAvailableGarbage();
180 181 182
}


183
void DeclarationContext::HandleGet(
184
    Local<Name> key, const v8::PropertyCallbackInfo<v8::Value>& info) {
185
  DeclarationContext* context = GetInstance(info.Data());
186
  context->get_count_++;
187
  info.GetReturnValue().Set(context->Get(key));
188 189 190
}


191
void DeclarationContext::HandleSet(
192
    Local<Name> key, Local<Value> value,
193 194
    const v8::PropertyCallbackInfo<v8::Value>& info) {
  DeclarationContext* context = GetInstance(info.Data());
195
  context->set_count_++;
196
  info.GetReturnValue().Set(context->Set(key, value));
197 198 199
}


200
void DeclarationContext::HandleQuery(
201
    Local<Name> key, const v8::PropertyCallbackInfo<v8::Integer>& info) {
202
  DeclarationContext* context = GetInstance(info.Data());
203
  context->query_count_++;
204
  info.GetReturnValue().Set(context->Query(key));
205 206 207
}


208 209
DeclarationContext* DeclarationContext::GetInstance(Local<Value> data) {
  void* value = Local<External>::Cast(data)->Value();
210
  return static_cast<DeclarationContext*>(value);
211 212 213
}


214 215
v8::Local<Value> DeclarationContext::Get(Local<Name> key) {
  return v8::Local<Value>();
216 217 218
}


219 220
v8::Local<Value> DeclarationContext::Set(Local<Name> key, Local<Value> value) {
  return v8::Local<Value>();
221 222 223
}


224 225
v8::Local<Integer> DeclarationContext::Query(Local<Name> key) {
  return v8::Local<Integer>();
226 227
}

228
}  // namespace
229 230 231 232

// Test global declaration of a property the interceptor doesn't know
// about and doesn't handle.
TEST(Unknown) {
233
  HandleScope scope(CcTest::isolate());
234
  v8::V8::Initialize();
235 236 237 238

  { DeclarationContext context;
    context.Check("var x; x",
                  1,  // access
239
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
240 241 242 243 244
  }

  { DeclarationContext context;
    context.Check("var x = 0; x",
                  1,  // access
245 246
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
247 248 249 250 251
  }

  { DeclarationContext context;
    context.Check("function x() { }; x",
                  1,  // access
252
                  1, 1, EXPECT_RESULT);
253 254 255 256 257 258
  }
}


class AbsentPropertyContext: public DeclarationContext {
 protected:
259 260
  virtual v8::Local<Integer> Query(Local<Name> key) {
    return v8::Local<Integer>();
261 262 263 264 265
  }
};


TEST(Absent) {
266
  v8::Isolate* isolate = CcTest::isolate();
267
  v8::V8::Initialize();
268
  HandleScope scope(isolate);
269 270 271 272

  { AbsentPropertyContext context;
    context.Check("var x; x",
                  1,  // access
273
                  0, 0, EXPECT_RESULT, Undefined(isolate));
274 275 276 277 278
  }

  { AbsentPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
279 280
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(isolate, 0));
281 282 283 284 285
  }

  { AbsentPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
286
                  1, 1, EXPECT_RESULT);
287 288 289 290 291
  }

  { AbsentPropertyContext context;
    context.Check("if (false) { var x = 0 }; x",
                  1,  // access
292
                  0, 0, EXPECT_RESULT, Undefined(isolate));
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  }
}



class AppearingPropertyContext: public DeclarationContext {
 public:
  enum State {
    DECLARE,
    INITIALIZE_IF_ASSIGN,
    UNKNOWN
  };

  AppearingPropertyContext() : state_(DECLARE) { }

 protected:
309
  virtual v8::Local<Integer> Query(Local<Name> key) {
310 311 312 313 314
    switch (state_) {
      case DECLARE:
        // Force declaration by returning that the
        // property is absent.
        state_ = INITIALIZE_IF_ASSIGN;
315
        return Local<Integer>();
316 317 318 319
      case INITIALIZE_IF_ASSIGN:
        // Return that the property is present so we only get the
        // setter called when initializing with a value.
        state_ = UNKNOWN;
320
        return Integer::New(isolate(), v8::None);
321
      default:
322
        CHECK(state_ == UNKNOWN);
323 324 325
        break;
    }
    // Do the lookup in the object.
326
    return v8::Local<Integer>();
327 328 329 330 331 332 333 334
  }

 private:
  State state_;
};


TEST(Appearing) {
335
  v8::V8::Initialize();
336
  HandleScope scope(CcTest::isolate());
337 338 339 340

  { AppearingPropertyContext context;
    context.Check("var x; x",
                  1,  // access
341
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
342 343 344 345 346
  }

  { AppearingPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
347 348
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
349 350 351 352 353
  }

  { AppearingPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
354
                  1, 1, EXPECT_RESULT);
355 356 357 358 359 360
  }
}



class ExistsInPrototypeContext: public DeclarationContext {
361 362
 public:
  ExistsInPrototypeContext() { InitializeIfNeeded(); }
363
 protected:
364
  virtual v8::Local<Integer> Query(Local<Name> key) {
365
    // Let it seem that the property exists in the prototype object.
366
    return Integer::New(isolate(), v8::None);
367 368 369 370 371 372 373 374 375 376
  }

  // Use the prototype as the holder for the interceptors.
  virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
    return function->PrototypeTemplate();
  }
};


TEST(ExistsInPrototype) {
377
  HandleScope scope(CcTest::isolate());
378 379 380 381

  // Sanity check to make sure that the holder of the interceptor
  // really is the prototype object.
  { ExistsInPrototypeContext context;
382 383
    context.Check("this.x = 87; this.x", 0, 0, 1, EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 87));
384 385 386 387 388
  }

  { ExistsInPrototypeContext context;
    context.Check("var x; x",
                  0,
389 390
                  0,
                  0,
391
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
392 393 394 395 396 397
  }

  { ExistsInPrototypeContext context;
    context.Check("var x = 0; x",
                  0,
                  0,
398
                  0,
399
                  EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
400 401 402 403 404 405 406
  }
}



class AbsentInPrototypeContext: public DeclarationContext {
 protected:
407
  virtual v8::Local<Integer> Query(Local<Name> key) {
408
    // Let it seem that the property is absent in the prototype object.
409
    return Local<Integer>();
410 411 412 413 414 415 416 417 418 419
  }

  // Use the prototype as the holder for the interceptors.
  virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
    return function->PrototypeTemplate();
  }
};


TEST(AbsentInPrototype) {
420
  v8::V8::Initialize();
421
  HandleScope scope(CcTest::isolate());
422 423 424 425 426

  { AbsentInPrototypeContext context;
    context.Check("if (false) { var x = 0; }; x",
                  0,
                  0,
427
                  0,
428
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
429 430
  }
}
431 432 433 434 435 436



class ExistsInHiddenPrototypeContext: public DeclarationContext {
 public:
  ExistsInHiddenPrototypeContext() {
437
    hidden_proto_ = FunctionTemplate::New(CcTest::isolate());
438 439 440 441
    hidden_proto_->SetHiddenPrototype(true);
  }

 protected:
442
  virtual v8::Local<Integer> Query(Local<Name> key) {
443
    // Let it seem that the property exists in the hidden prototype object.
444
    return Integer::New(isolate(), v8::None);
445 446 447
  }

  // Install the hidden prototype after the global object has been created.
448
  virtual void PostInitializeContext(Local<Context> context) {
449
    Local<Object> global_object = context->Global();
450 451 452 453
    Local<Object> hidden_proto = hidden_proto_->GetFunction(context)
                                     .ToLocalChecked()
                                     ->NewInstance(context)
                                     .ToLocalChecked();
454 455
    Local<Object> inner_global =
        Local<Object>::Cast(global_object->GetPrototype());
456
    inner_global->SetPrototype(context, hidden_proto).FromJust();
457 458 459 460 461 462 463 464 465 466 467 468 469
  }

  // Use the hidden prototype as the holder for the interceptors.
  virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
    return hidden_proto_->InstanceTemplate();
  }

 private:
  Local<FunctionTemplate> hidden_proto_;
};


TEST(ExistsInHiddenPrototype) {
470
  HandleScope scope(CcTest::isolate());
471 472

  { ExistsInHiddenPrototypeContext context;
473 474
    context.Check("var x; x", 0, 0, 0, EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
475 476 477
  }

  { ExistsInHiddenPrototypeContext context;
478 479
    context.Check("var x = 0; x", 0, 0, 0, EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 0));
480 481 482
  }

  { ExistsInHiddenPrototypeContext context;
483
    context.Check("function x() { }; x", 0, 1, 1, EXPECT_RESULT);
484 485
  }
}
486 487 488 489 490



class SimpleContext {
 public:
491
  SimpleContext()
492 493
      : handle_scope_(CcTest::isolate()),
        context_(Context::New(CcTest::isolate())) {
494 495 496
    context_->Enter();
  }

497
  ~SimpleContext() {
498 499 500
    context_->Exit();
  }

501 502
  void Check(const char* source, Expectations expectations,
             v8::Local<Value> value = Local<Value>()) {
503
    HandleScope scope(context_->GetIsolate());
504
    TryCatch catcher(context_->GetIsolate());
505
    catcher.SetVerbose(true);
506 507 508 509
    MaybeLocal<Script> script = Script::Compile(
        context_, String::NewFromUtf8(context_->GetIsolate(), source,
                                      v8::NewStringType::kNormal)
                      .ToLocalChecked());
510 511 512 513 514
    if (expectations == EXPECT_ERROR) {
      CHECK(script.IsEmpty());
      return;
    }
    CHECK(!script.IsEmpty());
515
    MaybeLocal<Value> result = script.ToLocalChecked()->Run(context_);
516 517 518
    if (expectations == EXPECT_RESULT) {
      CHECK(!catcher.HasCaught());
      if (!value.IsEmpty()) {
519
        CHECK(value->Equals(context_, result.ToLocalChecked()).FromJust());
520 521 522 523 524
      }
    } else {
      CHECK(expectations == EXPECT_EXCEPTION);
      CHECK(catcher.HasCaught());
      if (!value.IsEmpty()) {
525
        CHECK(value->Equals(context_, catcher.Exception()).FromJust());
526 527 528 529 530
      }
    }
  }

 private:
531 532
  HandleScope handle_scope_;
  Local<Context> context_;
533 534 535
};


536
TEST(CrossScriptReferences) {
537 538
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
539 540 541

  { SimpleContext context;
    context.Check("var x = 1; x",
542
                  EXPECT_RESULT, Number::New(isolate, 1));
543
    context.Check("var x = 2; x",
544
                  EXPECT_RESULT, Number::New(isolate, 2));
545
    context.Check("x = 5; x",
546
                  EXPECT_RESULT, Number::New(isolate, 5));
547
    context.Check("var x = 6; x",
548
                  EXPECT_RESULT, Number::New(isolate, 6));
549
    context.Check("this.x",
550
                  EXPECT_RESULT, Number::New(isolate, 6));
551
    context.Check("function x() { return 7 }; x()",
552
                  EXPECT_RESULT, Number::New(isolate, 7));
553
  }
554 555
}

556

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
TEST(CrossScriptReferences_Simple) {
  i::FLAG_use_strict = true;

  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);

  {
    SimpleContext context;
    context.Check("let x = 1; x", EXPECT_RESULT, Number::New(isolate, 1));
    context.Check("let x = 5; x", EXPECT_EXCEPTION);
  }
}


TEST(CrossScriptReferences_Simple2) {
  i::FLAG_use_strict = true;

  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);

  for (int k = 0; k < 100; k++) {
    SimpleContext context;
    bool cond = (k % 2) == 0;
    if (cond) {
      context.Check("let x = 1; x", EXPECT_RESULT, Number::New(isolate, 1));
      context.Check("let z = 4; z", EXPECT_RESULT, Number::New(isolate, 4));
    } else {
      context.Check("let z = 1; z", EXPECT_RESULT, Number::New(isolate, 1));
      context.Check("let x = 4; x", EXPECT_RESULT, Number::New(isolate, 4));
    }
    context.Check("let y = 2; x", EXPECT_RESULT,
                  Number::New(isolate, cond ? 1 : 4));
  }
}


593
TEST(CrossScriptReferencesHarmony) {
594 595
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
596

597
  // Check that simple cross-script global scope access works.
598
  const char* decs[] = {
599 600 601 602
    "'use strict'; var x = 1; x", "x",
    "'use strict'; function x() { return 1 }; x()", "x()",
    "'use strict'; let x = 1; x", "x",
    "'use strict'; const x = 1; x", "x",
603 604
    NULL
  };
605

606
  for (int i = 0; decs[i] != NULL; i += 2) {
607
    SimpleContext context;
608 609
    context.Check(decs[i], EXPECT_RESULT, Number::New(isolate, 1));
    context.Check(decs[i+1], EXPECT_RESULT, Number::New(isolate, 1));
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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

  // Check that cross-script global scope access works with late declarations.
  {
    SimpleContext context;
    context.Check("function d0() { return x0 }",  // dynamic lookup
                  EXPECT_RESULT, Undefined(isolate));
    context.Check("this.x0 = -1;"
                  "d0()",
                  EXPECT_RESULT, Number::New(isolate, -1));
    context.Check("'use strict';"
                  "function f0() { let y = 10; return x0 + y }"
                  "function g0() { let y = 10; return eval('x0 + y') }"
                  "function h0() { let y = 10; return (1,eval)('x0') + y }"
                  "x0 + f0() + g0() + h0()",
                  EXPECT_RESULT, Number::New(isolate, 26));

    context.Check("'use strict';"
                  "let x1 = 1;"
                  "function f1() { let y = 10; return x1 + y }"
                  "function g1() { let y = 10; return eval('x1 + y') }"
                  "function h1() { let y = 10; return (1,eval)('x1') + y }"
                  "function i1() { "
                  "  let y = 10; return (typeof x2 === 'undefined' ? 0 : 2) + y"
                  "}"
                  "function j1() { let y = 10; return eval('x2 + y') }"
                  "function k1() { let y = 10; return (1,eval)('x2') + y }"
                  "function cl() { "
                  "  let y = 10; "
                  "  return { "
                  "    f: function(){ return x1 + y },"
                  "    g: function(){ return eval('x1 + y') },"
                  "    h: function(){ return (1,eval)('x1') + y },"
                  "    i: function(){"
                  "      return (typeof x2 == 'undefined' ? 0 : 2) + y"
                  "    },"
                  "    j: function(){ return eval('x2 + y') },"
                  "    k: function(){ return (1,eval)('x2') + y },"
                  "  }"
                  "}"
                  "let o = cl();"
                  "x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
                  EXPECT_RESULT, Number::New(isolate, 36));
    context.Check("x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
                  EXPECT_RESULT, Number::New(isolate, 36));
    context.Check("o.f() + o.g() + o.h();",
                  EXPECT_RESULT, Number::New(isolate, 33));
    context.Check("i1() + o.i();",
                  EXPECT_RESULT, Number::New(isolate, 20));

    context.Check("'use strict';"
                  "let x2 = 2;"
                  "function f2() { let y = 20; return x2 + y }"
                  "function g2() { let y = 20; return eval('x2 + y') }"
                  "function h2() { let y = 20; return (1,eval)('x2') + y }"
                  "function i2() { let y = 20; return x1 + y }"
                  "function j2() { let y = 20; return eval('x1 + y') }"
                  "function k2() { let y = 20; return (1,eval)('x1') + y }"
                  "x2 + eval('x2') + (1,eval)('x2') + f2() + g2() + h2();",
                  EXPECT_RESULT, Number::New(isolate, 72));
    context.Check("x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
                  EXPECT_RESULT, Number::New(isolate, 36));
    context.Check("i1() + j1() + k1();",
                  EXPECT_RESULT, Number::New(isolate, 36));
    context.Check("i2() + j2() + k2();",
                  EXPECT_RESULT, Number::New(isolate, 63));
    context.Check("o.f() + o.g() + o.h();",
                  EXPECT_RESULT, Number::New(isolate, 33));
    context.Check("o.i() + o.j() + o.k();",
                  EXPECT_RESULT, Number::New(isolate, 36));
    context.Check("i1() + o.i();",
                  EXPECT_RESULT, Number::New(isolate, 24));

    context.Check("'use strict';"
                  "let x0 = 100;"
                  "x0 + eval('x0') + (1,eval)('x0') + "
                  "    d0() + f0() + g0() + h0();",
                  EXPECT_RESULT, Number::New(isolate, 730));
    context.Check("x0 + eval('x0') + (1,eval)('x0') + "
                  "    d0() + f0() + g0() + h0();",
                  EXPECT_RESULT, Number::New(isolate, 730));
    context.Check("delete this.x0;"
                  "x0 + eval('x0') + (1,eval)('x0') + "
                  "    d0() + f0() + g0() + h0();",
                  EXPECT_RESULT, Number::New(isolate, 730));
    context.Check("this.x1 = 666;"
                  "x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
                  EXPECT_RESULT, Number::New(isolate, 36));
    context.Check("delete this.x1;"
                  "x1 + eval('x1') + (1,eval)('x1') + f1() + g1() + h1();",
                  EXPECT_RESULT, Number::New(isolate, 36));
  }

  // Check that caching does respect scopes.
  {
    SimpleContext context;
    const char* script1 = "(function(){ return y1 })()";
    const char* script2 = "(function(){ return y2 })()";

    context.Check(script1, EXPECT_EXCEPTION);
    context.Check("this.y1 = 1; this.y2 = 2; 0;",
                  EXPECT_RESULT, Number::New(isolate, 0));
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 1));
    context.Check("'use strict'; let y1 = 3; 0;",
                  EXPECT_RESULT, Number::New(isolate, 0));
716 717
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 3));
718 719
    context.Check("y1 = 4;",
                  EXPECT_RESULT, Number::New(isolate, 4));
720 721
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 4));
722 723 724 725 726

    context.Check(script2,
                  EXPECT_RESULT, Number::New(isolate, 2));
    context.Check("'use strict'; let y2 = 5; 0;",
                  EXPECT_RESULT, Number::New(isolate, 0));
727 728 729 730
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 4));
    context.Check(script2,
                  EXPECT_RESULT, Number::New(isolate, 5));
731 732 733 734
  }
}


735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
TEST(CrossScriptReferencesHarmonyRegress) {
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
  SimpleContext context;
  context.Check(
      "'use strict';"
      "function i1() { "
      "  let y = 10; return (typeof x2 === 'undefined' ? 0 : 2) + y"
      "}"
      "i1();"
      "i1();",
      EXPECT_RESULT, Number::New(isolate, 10));
  context.Check(
      "'use strict';"
      "let x2 = 2; i1();",
      EXPECT_RESULT, Number::New(isolate, 12));
}


754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
TEST(GlobalLexicalOSR) {
  i::FLAG_use_strict = true;

  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
  SimpleContext context;

  context.Check("'use strict';"
                "let x = 1; x;",
                EXPECT_RESULT, Number::New(isolate, 1));
  context.Check("'use strict';"
                "let y = 2*x;"
                "++x;"
                "let z = 0;"
                "const limit = 100000;"
                "for (var i = 0; i < limit; ++i) {"
                "  z += x + y;"
                "}"
                "z;",
                EXPECT_RESULT, Number::New(isolate, 400000));
774
}
775 776


777 778
TEST(CrossScriptConflicts) {
  i::FLAG_use_strict = true;
779

780
  HandleScope scope(CcTest::isolate());
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795
  const char* firsts[] = {
    "var x = 1; x",
    "function x() { return 1 }; x()",
    "let x = 1; x",
    "const x = 1; x",
    NULL
  };
  const char* seconds[] = {
    "var x = 2; x",
    "function x() { return 2 }; x()",
    "let x = 2; x",
    "const x = 2; x",
    NULL
  };
796

797 798 799
  for (int i = 0; firsts[i] != NULL; ++i) {
    for (int j = 0; seconds[j] != NULL; ++j) {
      SimpleContext context;
800 801
      context.Check(firsts[i], EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 1));
802 803 804 805 806 807
      bool success_case = i < 2 && j < 2;
      Local<Value> success_result;
      if (success_case) success_result = Number::New(CcTest::isolate(), 2);

      context.Check(seconds[j], success_case ? EXPECT_RESULT : EXPECT_EXCEPTION,
                    success_result);
808
    }
809 810
  }
}
811 812 813 814 815 816 817


TEST(CrossScriptDynamicLookup) {
  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
818 819 820 821 822 823 824 825
    Local<String> undefined_string =
        String::NewFromUtf8(CcTest::isolate(), "undefined",
                            v8::NewStringType::kInternalized)
            .ToLocalChecked();
    Local<String> number_string =
        String::NewFromUtf8(CcTest::isolate(), "number",
                            v8::NewStringType::kInternalized)
            .ToLocalChecked();
826 827 828 829 830 831 832 833 834 835 836 837 838 839

    context.Check(
        "function f(o) { with(o) { return x; } }"
        "function g(o) { with(o) { x = 15; } }"
        "function h(o) { with(o) { return typeof x; } }",
        EXPECT_RESULT, Undefined(CcTest::isolate()));
    context.Check("h({})", EXPECT_RESULT, undefined_string);
    context.Check(
        "'use strict';"
        "let x = 1;"
        "f({})",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
    context.Check(
        "'use strict';"
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        "g({});0",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
    context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
  }
}


TEST(CrossScriptGlobal) {
  HandleScope handle_scope(CcTest::isolate());
  {
    SimpleContext context;

    context.Check(
        "var global = this;"
        "global.x = 255;"
856
        "x",
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
        EXPECT_RESULT, Number::New(CcTest::isolate(), 255));
    context.Check(
        "'use strict';"
        "let x = 1;"
        "global.x",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 255));
    context.Check("global.x = 15; x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 1));
    context.Check("x = 221; global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 15));
    context.Check(
        "z = 15;"
        "function f() { return z; };"
        "for (var k = 0; k < 3; k++) { f(); }"
        "f()",
872
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    context.Check(
        "'use strict';"
        "let z = 5; f()",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
    context.Check(
        "function f() { konst = 10; return konst; };"
        "f()",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 10));
    context.Check(
        "'use strict';"
        "const konst = 255;"
        "f()",
        EXPECT_EXCEPTION);
  }
}


TEST(CrossScriptStaticLookupUndeclared) {
  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
895 896 897 898 899 900 901 902
    Local<String> undefined_string =
        String::NewFromUtf8(CcTest::isolate(), "undefined",
                            v8::NewStringType::kInternalized)
            .ToLocalChecked();
    Local<String> number_string =
        String::NewFromUtf8(CcTest::isolate(), "number",
                            v8::NewStringType::kInternalized)
            .ToLocalChecked();
903 904 905

    context.Check(
        "function f(o) { return x; }"
906
        "function g(v) { x = v; }"
907 908 909 910 911 912 913 914 915 916
        "function h(o) { return typeof x; }",
        EXPECT_RESULT, Undefined(CcTest::isolate()));
    context.Check("h({})", EXPECT_RESULT, undefined_string);
    context.Check(
        "'use strict';"
        "let x = 1;"
        "f({})",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
    context.Check(
        "'use strict';"
917
        "g(15);x",
918 919
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
920 921 922 923
    context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
  }
}
924 925


926
TEST(CrossScriptLoadICs) {
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
    context.Check(
        "x = 15;"
        "function f() { return x; }"
        "function g() { return x; }"
        "f()",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check(
        "'use strict';"
        "let x = 5;"
        "f()",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
    for (int k = 0; k < 3; k++) {
      context.Check("g()", EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
    }
    for (int k = 0; k < 3; k++) {
      context.Check("f()", EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
    }
    context.Check("%OptimizeFunctionOnNextCall(g); g()", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 5));
    context.Check("%OptimizeFunctionOnNextCall(f); f()", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 5));
  }
  {
    SimpleContext context;
    context.Check(
        "x = 15;"
        "function f() { return x; }"
        "f()",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    for (int k = 0; k < 3; k++) {
      context.Check("f()", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    }
    context.Check("%OptimizeFunctionOnNextCall(f); f()", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 15));
    context.Check(
        "'use strict';"
        "let x = 5;"
        "f()",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
    for (int k = 0; k < 3; k++) {
      context.Check("f()", EXPECT_RESULT, Number::New(CcTest::isolate(), 5));
    }
    context.Check("%OptimizeFunctionOnNextCall(f); f()", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 5));
  }
}
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055


TEST(CrossScriptStoreICs) {
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
    context.Check(
        "var global = this;"
        "x = 15;"
        "function f(v) { x = v; }"
        "function g(v) { x = v; }"
        "f(10); x",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 10));
    context.Check(
        "'use strict';"
        "let x = 5;"
        "f(7); x",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 7));
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 10));
    for (int k = 0; k < 3; k++) {
      context.Check("g(31); x", EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 31));
    }
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 10));
    for (int k = 0; k < 3; k++) {
      context.Check("f(32); x", EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 32));
    }
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 10));
    context.Check("%OptimizeFunctionOnNextCall(g); g(18); x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 18));
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 10));
    context.Check("%OptimizeFunctionOnNextCall(f); f(33); x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 33));
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 10));
  }
  {
    SimpleContext context;
    context.Check(
        "var global = this;"
        "x = 15;"
        "function f(v) { x = v; }"
        "f(10); x",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 10));
    for (int k = 0; k < 3; k++) {
      context.Check("f(18); x", EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 18));
    }
    context.Check("%OptimizeFunctionOnNextCall(f); f(20); x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 20));
    context.Check(
        "'use strict';"
        "let x = 5;"
        "f(8); x",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 8));
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 20));
    for (int k = 0; k < 3; k++) {
      context.Check("f(13); x", EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 13));
    }
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 20));
    context.Check("%OptimizeFunctionOnNextCall(f); f(41); x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 41));
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 20));
  }
}
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077


TEST(CrossScriptAssignmentToConst) {
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;

    context.Check("function f() { x = 27; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    context.Check("'use strict';const x = 1; x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 1));
    context.Check("f();", EXPECT_EXCEPTION);
    context.Check("x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
    context.Check("f();", EXPECT_EXCEPTION);
    context.Check("x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
    context.Check("%OptimizeFunctionOnNextCall(f);f();", EXPECT_EXCEPTION);
    context.Check("x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
  }
}
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094


TEST(Regress425510) {
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;

    context.Check("'use strict'; o; const o = 10", EXPECT_EXCEPTION);

    for (int i = 0; i < 100; i++) {
      context.Check("o.prototype", EXPECT_EXCEPTION);
    }
  }
}
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176


TEST(Regress3941) {
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
    context.Check("function f() { x = 1; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
  }


  {
    // Train ICs.
    SimpleContext context;
    context.Check("function f() { x = 1; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    for (int i = 0; i < 4; i++) {
      context.Check("f(); x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
    }
    context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
  }


  {
    // Optimize.
    SimpleContext context;
    context.Check("function f() { x = 1; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    for (int i = 0; i < 4; i++) {
      context.Check("f(); x", EXPECT_RESULT, Number::New(CcTest::isolate(), 1));
    }
    context.Check("%OptimizeFunctionOnNextCall(f); f(); x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 1));

    context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
  }
}


TEST(Regress3941_Reads) {
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
    context.Check("function f() { return x; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
  }


  {
    // Train ICs.
    SimpleContext context;
    context.Check("function f() { return x; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    for (int i = 0; i < 4; i++) {
      context.Check("f()", EXPECT_EXCEPTION);
    }
    context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
  }


  {
    // Optimize.
    SimpleContext context;
    context.Check("function f() { return x; }", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
    for (int i = 0; i < 4; i++) {
      context.Check("f()", EXPECT_EXCEPTION);
    }
    context.Check("%OptimizeFunctionOnNextCall(f);", EXPECT_RESULT,
                  Undefined(CcTest::isolate()));

    context.Check("'use strict'; f(); let x = 2; x", EXPECT_EXCEPTION);
  }
}