test-decls.cc 35.4 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 31 32
#include "include/v8-external.h"
#include "include/v8-initialization.h"
#include "include/v8-template.h"
33
#include "src/init/v8.h"
34
#include "test/cctest/cctest.h"
35

36
namespace v8 {
37

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
  GetHolder(function)->SetHandler(v8::NamedPropertyHandlerConfiguration(
126 127 128
      &HandleGet, &HandleSet, &HandleQuery, nullptr, nullptr, data));
  Local<Context> context = Context::New(
      isolate, nullptr, function->InstanceTemplate(), Local<Value>());
129 130
  context_.Reset(isolate, context);
  context->Enter();
131
  is_initialized_ = true;
132 133 134 135
  // Reset counts. Bootstrapping might have called into the interceptor.
  get_count_ = 0;
  set_count_ = 0;
  query_count_ = 0;
136
  PostInitializeContext(context);
137 138 139
}


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


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


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


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


204 205
DeclarationContext* DeclarationContext::GetInstance(Local<Value> data) {
  void* value = Local<External>::Cast(data)->Value();
206
  return static_cast<DeclarationContext*>(value);
207 208 209
}


210 211
v8::Local<Value> DeclarationContext::Get(Local<Name> key) {
  return v8::Local<Value>();
212 213 214
}


215 216
v8::Local<Value> DeclarationContext::Set(Local<Name> key, Local<Value> value) {
  return v8::Local<Value>();
217 218 219
}


220 221
v8::Local<Integer> DeclarationContext::Query(Local<Name> key) {
  return v8::Local<Integer>();
222 223
}

224
}  // namespace
225 226 227 228

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

  { DeclarationContext context;
    context.Check("var x; x",
                  1,  // access
235
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
236 237 238 239 240
  }

  { DeclarationContext context;
    context.Check("var x = 0; x",
                  1,  // access
241 242
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
243 244 245 246 247
  }

  { DeclarationContext context;
    context.Check("function x() { }; x",
                  1,  // access
248
                  1, 1, EXPECT_RESULT);
249 250 251 252 253 254
  }
}


class AbsentPropertyContext: public DeclarationContext {
 protected:
255
  v8::Local<Integer> Query(Local<Name> key) override {
256
    return v8::Local<Integer>();
257 258 259 260 261
  }
};


TEST(Absent) {
262
  v8::Isolate* isolate = CcTest::isolate();
263
  v8::V8::Initialize();
264
  HandleScope scope(isolate);
265 266 267 268

  { AbsentPropertyContext context;
    context.Check("var x; x",
                  1,  // access
269
                  0, 0, EXPECT_RESULT, Undefined(isolate));
270 271 272 273 274
  }

  { AbsentPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
275 276
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(isolate, 0));
277 278 279 280 281
  }

  { AbsentPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
282
                  1, 1, EXPECT_RESULT);
283 284 285 286 287
  }

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



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

  AppearingPropertyContext() : state_(DECLARE) { }

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

 private:
  State state_;
};


TEST(Appearing) {
331
  v8::V8::Initialize();
332
  HandleScope scope(CcTest::isolate());
333 334 335 336

  { AppearingPropertyContext context;
    context.Check("var x; x",
                  1,  // access
337
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
338 339 340 341 342
  }

  { AppearingPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
343 344
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
345 346 347 348 349
  }

  { AppearingPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
350
                  1, 1, EXPECT_RESULT);
351 352 353 354 355 356
  }
}



class ExistsInPrototypeContext: public DeclarationContext {
357 358
 public:
  ExistsInPrototypeContext() { InitializeIfNeeded(); }
359
 protected:
360
  v8::Local<Integer> Query(Local<Name> key) override {
361
    // Let it seem that the property exists in the prototype object.
362
    return Integer::New(isolate(), v8::None);
363 364 365
  }

  // Use the prototype as the holder for the interceptors.
366
  Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) override {
367 368 369 370 371 372
    return function->PrototypeTemplate();
  }
};


TEST(ExistsInPrototype) {
373
  HandleScope scope(CcTest::isolate());
374 375 376 377

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

  { ExistsInPrototypeContext context;
    context.Check("var x; x",
                  0,
385 386
                  0,
                  0,
387
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
388 389 390 391 392 393
  }

  { ExistsInPrototypeContext context;
    context.Check("var x = 0; x",
                  0,
                  0,
394
                  0,
395
                  EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
396 397 398 399 400 401 402
  }
}



class AbsentInPrototypeContext: public DeclarationContext {
 protected:
403
  v8::Local<Integer> Query(Local<Name> key) override {
404
    // Let it seem that the property is absent in the prototype object.
405
    return Local<Integer>();
406 407 408
  }

  // Use the prototype as the holder for the interceptors.
409
  Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) override {
410 411 412 413 414 415
    return function->PrototypeTemplate();
  }
};


TEST(AbsentInPrototype) {
416
  v8::V8::Initialize();
417
  HandleScope scope(CcTest::isolate());
418 419 420 421 422

  { AbsentInPrototypeContext context;
    context.Check("if (false) { var x = 0; }; x",
                  0,
                  0,
423
                  0,
424
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
425 426
  }
}
427 428 429



430 431
class SimpleContext {
 public:
432
  SimpleContext()
433 434
      : handle_scope_(CcTest::isolate()),
        context_(Context::New(CcTest::isolate())) {
435 436 437
    context_->Enter();
  }

438
  ~SimpleContext() {
439 440 441
    context_->Exit();
  }

442 443
  void Check(const char* source, Expectations expectations,
             v8::Local<Value> value = Local<Value>()) {
444
    HandleScope scope(context_->GetIsolate());
445
    TryCatch catcher(context_->GetIsolate());
446
    catcher.SetVerbose(true);
447
    MaybeLocal<Script> script = Script::Compile(
448 449
        context_,
        String::NewFromUtf8(context_->GetIsolate(), source).ToLocalChecked());
450 451 452 453 454
    if (expectations == EXPECT_ERROR) {
      CHECK(script.IsEmpty());
      return;
    }
    CHECK(!script.IsEmpty());
455
    MaybeLocal<Value> result = script.ToLocalChecked()->Run(context_);
456 457 458
    if (expectations == EXPECT_RESULT) {
      CHECK(!catcher.HasCaught());
      if (!value.IsEmpty()) {
459
        CHECK(value->Equals(context_, result.ToLocalChecked()).FromJust());
460 461 462 463 464
      }
    } else {
      CHECK(expectations == EXPECT_EXCEPTION);
      CHECK(catcher.HasCaught());
      if (!value.IsEmpty()) {
465
        CHECK(value->Equals(context_, catcher.Exception()).FromJust());
466 467 468 469 470
      }
    }
  }

 private:
471 472
  HandleScope handle_scope_;
  Local<Context> context_;
473 474 475
};


476
TEST(CrossScriptReferences) {
477 478
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
479 480 481

  { SimpleContext context;
    context.Check("var x = 1; x",
482
                  EXPECT_RESULT, Number::New(isolate, 1));
483
    context.Check("var x = 2; x",
484
                  EXPECT_RESULT, Number::New(isolate, 2));
485
    context.Check("x = 5; x",
486
                  EXPECT_RESULT, Number::New(isolate, 5));
487
    context.Check("var x = 6; x",
488
                  EXPECT_RESULT, Number::New(isolate, 6));
489
    context.Check("this.x",
490
                  EXPECT_RESULT, Number::New(isolate, 6));
491
    context.Check("function x() { return 7 }; x()",
492
                  EXPECT_RESULT, Number::New(isolate, 7));
493
  }
494 495
}

496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
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));
  }
}


533
TEST(CrossScriptReferencesHarmony) {
534 535
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
536

537
  // Check that simple cross-script global scope access works.
538 539 540 541 542 543 544 545 546 547 548
  const char* decs[] = {"'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",
                        nullptr};

  for (int i = 0; decs[i] != nullptr; i += 2) {
549
    SimpleContext context;
550 551
    context.Check(decs[i], EXPECT_RESULT, Number::New(isolate, 1));
    context.Check(decs[i+1], EXPECT_RESULT, Number::New(isolate, 1));
552
  }
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 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 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

  // 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));
658 659
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 3));
660 661
    context.Check("y1 = 4;",
                  EXPECT_RESULT, Number::New(isolate, 4));
662 663
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 4));
664 665 666 667 668

    context.Check(script2,
                  EXPECT_RESULT, Number::New(isolate, 2));
    context.Check("'use strict'; let y2 = 5; 0;",
                  EXPECT_RESULT, Number::New(isolate, 0));
669 670 671 672
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 4));
    context.Check(script2,
                  EXPECT_RESULT, Number::New(isolate, 5));
673 674 675 676
  }
}


677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
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));
}


696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
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));
716
}
717 718


719 720
TEST(CrossScriptConflicts) {
  i::FLAG_use_strict = true;
721

722
  HandleScope scope(CcTest::isolate());
723

724 725 726 727
  const char* firsts[] = {"var x = 1; x", "function x() { return 1 }; x()",
                          "let x = 1; x", "const x = 1; x", nullptr};
  const char* seconds[] = {"var x = 2; x", "function x() { return 2 }; x()",
                           "let x = 2; x", "const x = 2; x", nullptr};
728

729 730
  for (int i = 0; firsts[i] != nullptr; ++i) {
    for (int j = 0; seconds[j] != nullptr; ++j) {
731
      SimpleContext context;
732 733
      context.Check(firsts[i], EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 1));
734 735 736 737 738 739
      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);
740
    }
741 742
  }
}
743 744 745 746 747 748 749


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

  {
    SimpleContext context;
750 751 752 753
    Local<String> undefined_string = String::NewFromUtf8Literal(
        CcTest::isolate(), "undefined", v8::NewStringType::kInternalized);
    Local<String> number_string = String::NewFromUtf8Literal(
        CcTest::isolate(), "number", v8::NewStringType::kInternalized);
754 755 756 757 758 759 760 761 762 763 764 765 766 767

    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';"
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
        "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;"
784
        "x",
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
        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()",
800
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    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;
823 824 825 826
    Local<String> undefined_string = String::NewFromUtf8Literal(
        CcTest::isolate(), "undefined", v8::NewStringType::kInternalized);
    Local<String> number_string = String::NewFromUtf8Literal(
        CcTest::isolate(), "number", v8::NewStringType::kInternalized);
827 828 829

    context.Check(
        "function f(o) { return x; }"
830
        "function g(v) { x = v; }"
831 832 833 834 835 836 837 838 839 840
        "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';"
841
        "g(15);x",
842 843
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
844 845 846 847
    context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
  }
}
848 849


850
TEST(CrossScriptLoadICs) {
851 852 853 854 855 856 857 858
  i::FLAG_allow_natives_syntax = true;

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
    context.Check(
        "x = 15;"
859 860 861 862
        "function f() { return x; };"
        "function g() { return x; };"
        "%PrepareFunctionForOptimization(f);"
        "%PrepareFunctionForOptimization(g);"
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
        "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; }"
886
        "%PrepareFunctionForOptimization(f);"
887 888 889 890 891 892 893 894 895 896
        "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;"
897
        "%PrepareFunctionForOptimization(f);"
898 899 900 901 902 903 904 905 906
        "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));
  }
}
907 908 909 910 911 912 913 914 915 916 917 918


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

  HandleScope handle_scope(CcTest::isolate());

  {
    SimpleContext context;
    context.Check(
        "var global = this;"
        "x = 15;"
919 920 921 922
        "function f(v) { x = v; };"
        "function g(v) { x = v; };"
        "%PrepareFunctionForOptimization(f);"
        "%PrepareFunctionForOptimization(g);"
923 924 925 926 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
        "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;"
958 959
        "function f(v) { x = v; };"
        "%PrepareFunctionForOptimization(f);"
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
        "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));
981 982 983 984
    context.Check(
        "%PrepareFunctionForOptimization(f);"
        "%OptimizeFunctionOnNextCall(f); f(41); x",
        EXPECT_RESULT, Number::New(CcTest::isolate(), 41));
985 986 987 988
    context.Check("global.x", EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 20));
  }
}
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002


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));
1003
    context.Check("%PrepareFunctionForOptimization(f);f();", EXPECT_EXCEPTION);
1004 1005 1006 1007 1008 1009 1010
    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));
  }
}
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027


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);
    }
  }
}
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 1056 1057


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;
1058 1059 1060 1061
    context.Check(
        "function f() { x = 1; };"
        "%PrepareFunctionForOptimization(f);",
        EXPECT_RESULT, Undefined(CcTest::isolate()));
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    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;
1101 1102 1103 1104
    context.Check(
        "function f() { return x; };"
        "%PrepareFunctionForOptimization(f);",
        EXPECT_RESULT, Undefined(CcTest::isolate()));
1105 1106 1107 1108 1109 1110 1111 1112 1113
    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);
  }
}
1114 1115

}  // namespace v8