test-decls.cc 24.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 30 31 32 33 34 35 36 37 38 39
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdlib.h>

#include "v8.h"

#include "heap.h"
#include "cctest.h"

using namespace v8;


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


// 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_) {
55 56 57 58 59
      Isolate* isolate = Isolate::GetCurrent();
      HandleScope scope(isolate);
      Local<Context> context = Local<Context>::New(isolate, context_);
      context->Exit();
      context_.Dispose(isolate);
60 61 62 63 64 65 66 67 68 69
    }
  }

  void Check(const char* source,
             int get, int set, int has,
             Expectations expectations,
             v8::Handle<Value> value = Local<Value>());

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

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

  void InitializeIfNeeded();

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

83 84 85 86 87 88 89 90
  // 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.
91 92 93 94 95 96 97
  static void HandleGet(Local<String> key,
                        const v8::PropertyCallbackInfo<v8::Value>& info);
  static void HandleSet(Local<String> key,
                        Local<Value> value,
                        const v8::PropertyCallbackInfo<v8::Value>& info);
  static void HandleQuery(Local<String> key,
                          const v8::PropertyCallbackInfo<v8::Integer>& info);
98 99 100 101 102 103 104

 private:
  bool is_initialized_;
  Persistent<Context> context_;

  int get_count_;
  int set_count_;
105
  int query_count_;
106

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


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


void DeclarationContext::InitializeIfNeeded() {
  if (is_initialized_) return;
119 120
  Isolate* isolate = Isolate::GetCurrent();
  HandleScope scope(isolate);
121
  Local<FunctionTemplate> function = FunctionTemplate::New();
122
  Local<Value> data = External::New(this);
123 124
  GetHolder(function)->SetNamedPropertyHandler(&HandleGet,
                                               &HandleSet,
125
                                               &HandleQuery,
126 127
                                               0, 0,
                                               data);
128 129 130 131 132 133
  Local<Context> context = Context::New(isolate,
                                        0,
                                        function->InstanceTemplate(),
                                        Local<Value>());
  context_.Reset(isolate, context);
  context->Enter();
134
  is_initialized_ = true;
135
  PostInitializeContext(context);
136 137 138 139
}


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


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


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


195 196 197 198
void DeclarationContext::HandleQuery(
    Local<String> key,
    const v8::PropertyCallbackInfo<v8::Integer>& info) {
  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 212 213 214 215 216 217 218 219 220
}


v8::Handle<Value> DeclarationContext::Get(Local<String> key) {
  return v8::Handle<Value>();
}


v8::Handle<Value> DeclarationContext::Set(Local<String> key,
                                          Local<Value> value) {
  return v8::Handle<Value>();
}


221 222
v8::Handle<Integer> DeclarationContext::Query(Local<String> key) {
  return v8::Handle<Integer>();
223 224 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(Isolate::GetCurrent());
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

  { DeclarationContext context;
    context.Check("var x; x",
                  1,  // access
                  1,  // declaration
                  2,  // declaration + initialization
                  EXPECT_RESULT, Undefined());
  }

  { DeclarationContext context;
    context.Check("var x = 0; x",
                  1,  // access
                  2,  // declaration + initialization
                  2,  // declaration + initialization
                  EXPECT_RESULT, Number::New(0));
  }

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

  { DeclarationContext context;
    context.Check("const x; x",
                  1,  // access
                  2,  // declaration + initialization
259
                  1,  // declaration
260 261 262 263 264 265 266
                  EXPECT_RESULT, Undefined());
  }

  { DeclarationContext context;
    context.Check("const x = 0; x",
                  1,  // access
                  2,  // declaration + initialization
267
                  1,  // declaration
268 269 270 271 272 273 274 275
                  EXPECT_RESULT, Undefined());  // SB 0 - BUG 1213579
  }
}



class PresentPropertyContext: public DeclarationContext {
 protected:
276 277
  virtual v8::Handle<Integer> Query(Local<String> key) {
    return Integer::New(v8::None);
278 279 280 281 282 283
  }
};



TEST(Present) {
284
  HandleScope scope(Isolate::GetCurrent());
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

  { PresentPropertyContext context;
    context.Check("var x; x",
                  1,  // access
                  0,
                  2,  // declaration + initialization
                  EXPECT_EXCEPTION);  // x is not defined!
  }

  { PresentPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
                  1,  // initialization
                  2,  // declaration + initialization
                  EXPECT_RESULT, Number::New(0));
  }

  { PresentPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
305
                  0,
306 307 308 309 310 311
                  0,
                  EXPECT_RESULT);
  }

  { PresentPropertyContext context;
    context.Check("const x; x",
312 313
                  1,  // access
                  1,  // initialization
314
                  1,  // (re-)declaration
315
                  EXPECT_RESULT, Undefined());
316 317 318 319
  }

  { PresentPropertyContext context;
    context.Check("const x = 0; x",
320 321
                  1,  // access
                  1,  // initialization
322
                  1,  // (re-)declaration
323
                  EXPECT_RESULT, Number::New(0));
324 325 326 327 328 329 330
  }
}



class AbsentPropertyContext: public DeclarationContext {
 protected:
331 332
  virtual v8::Handle<Integer> Query(Local<String> key) {
    return v8::Handle<Integer>();
333 334 335 336 337
  }
};


TEST(Absent) {
338
  HandleScope scope(Isolate::GetCurrent());
339 340 341 342

  { AbsentPropertyContext context;
    context.Check("var x; x",
                  1,  // access
343
                  1,  // declaration
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                  2,  // declaration + initialization
                  EXPECT_RESULT, Undefined());
  }

  { AbsentPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
                  2,  // declaration + initialization
                  2,  // declaration + initialization
                  EXPECT_RESULT, Number::New(0));
  }

  { AbsentPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
359
                  0,
360 361 362 363 364 365 366 367
                  0,
                  EXPECT_RESULT);
  }

  { AbsentPropertyContext context;
    context.Check("const x; x",
                  1,  // access
                  2,  // declaration + initialization
368
                  1,  // declaration
369 370 371 372 373 374 375
                  EXPECT_RESULT, Undefined());
  }

  { AbsentPropertyContext context;
    context.Check("const x = 0; x",
                  1,  // access
                  2,  // declaration + initialization
376
                  1,  // declaration
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                  EXPECT_RESULT, Undefined());  // SB 0 - BUG 1213579
  }

  { AbsentPropertyContext context;
    context.Check("if (false) { var x = 0 }; x",
                  1,  // access
                  1,  // declaration
                  1,  // declaration + initialization
                  EXPECT_RESULT, Undefined());
  }
}



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

  AppearingPropertyContext() : state_(DECLARE) { }

 protected:
402
  virtual v8::Handle<Integer> Query(Local<String> key) {
403 404 405 406 407
    switch (state_) {
      case DECLARE:
        // Force declaration by returning that the
        // property is absent.
        state_ = INITIALIZE_IF_ASSIGN;
408
        return Handle<Integer>();
409 410 411 412
      case INITIALIZE_IF_ASSIGN:
        // Return that the property is present so we only get the
        // setter called when initializing with a value.
        state_ = UNKNOWN;
413
        return Integer::New(v8::None);
414
      default:
415
        CHECK(state_ == UNKNOWN);
416 417 418
        break;
    }
    // Do the lookup in the object.
419
    return v8::Handle<Integer>();
420 421 422 423 424 425 426 427
  }

 private:
  State state_;
};


TEST(Appearing) {
428
  HandleScope scope(Isolate::GetCurrent());
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

  { AppearingPropertyContext context;
    context.Check("var x; x",
                  1,  // access
                  1,  // declaration
                  2,  // declaration + initialization
                  EXPECT_RESULT, Undefined());
  }

  { AppearingPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
                  2,  // declaration + initialization
                  2,  // declaration + initialization
                  EXPECT_RESULT, Number::New(0));
  }

  { AppearingPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
449
                  0,
450 451 452 453 454 455
                  0,
                  EXPECT_RESULT);
  }

  { AppearingPropertyContext context;
    context.Check("const x; x",
456
                  1,  // access
457
                  2,  // declaration + initialization
458 459
                  1,  // declaration
                  EXPECT_RESULT, Undefined());
460 461 462 463
  }

  { AppearingPropertyContext context;
    context.Check("const x = 0; x",
464
                  1,  // access
465
                  2,  // declaration + initialization
466 467 468 469
                  1,  // declaration
                  EXPECT_RESULT, Undefined());
                  // Result is undefined because declaration succeeded but
                  // initialization to 0 failed (due to context behavior).
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  }
}



class ReappearingPropertyContext: public DeclarationContext {
 public:
  enum State {
    DECLARE,
    DONT_DECLARE,
    INITIALIZE,
    UNKNOWN
  };

  ReappearingPropertyContext() : state_(DECLARE) { }

 protected:
487
  virtual v8::Handle<Integer> Query(Local<String> key) {
488 489 490 491 492
    switch (state_) {
      case DECLARE:
        // Force the first declaration by returning that
        // the property is absent.
        state_ = DONT_DECLARE;
493
        return Handle<Integer>();
494 495 496 497
      case DONT_DECLARE:
        // Ignore the second declaration by returning
        // that the property is already there.
        state_ = INITIALIZE;
498
        return Integer::New(v8::None);
499 500 501 502 503 504
      case INITIALIZE:
        // Force an initialization by returning that
        // the property is absent. This will make sure
        // that the setter is called and it will not
        // lead to redeclaration conflicts (yet).
        state_ = UNKNOWN;
505
        return Handle<Integer>();
506
      default:
507
        CHECK(state_ == UNKNOWN);
508 509 510
        break;
    }
    // Do the lookup in the object.
511
    return Handle<Integer>();
512 513 514 515 516 517 518 519
  }

 private:
  State state_;
};


TEST(Reappearing) {
520
  HandleScope scope(Isolate::GetCurrent());
521 522 523 524

  { ReappearingPropertyContext context;
    context.Check("const x; var x = 0",
                  0,
525 526 527
                  3,  // const declaration+initialization, var initialization
                  3,  // 2 x declaration + var initialization
                  EXPECT_RESULT, Undefined());
528 529 530 531 532 533 534
  }
}



class ExistsInPrototypeContext: public DeclarationContext {
 protected:
535
  virtual v8::Handle<Integer> Query(Local<String> key) {
536
    // Let it seem that the property exists in the prototype object.
537
    return Integer::New(v8::None);
538 539 540 541 542 543 544 545 546 547
  }

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


TEST(ExistsInPrototype) {
548
  i::FLAG_es52_globals = true;
549
  HandleScope scope(Isolate::GetCurrent());
550 551 552 553 554 555 556 557 558 559 560 561 562 563

  // Sanity check to make sure that the holder of the interceptor
  // really is the prototype object.
  { ExistsInPrototypeContext context;
    context.Check("this.x = 87; this.x",
                  0,
                  0,
                  0,
                  EXPECT_RESULT, Number::New(87));
  }

  { ExistsInPrototypeContext context;
    context.Check("var x; x",
                  0,
564 565
                  0,
                  0,
566
                  EXPECT_RESULT, Undefined());
567 568 569 570 571 572
  }

  { ExistsInPrototypeContext context;
    context.Check("var x = 0; x",
                  0,
                  0,
573
                  0,
574 575 576 577 578 579 580
                  EXPECT_RESULT, Number::New(0));
  }

  { ExistsInPrototypeContext context;
    context.Check("const x; x",
                  0,
                  0,
581
                  0,
582 583 584 585 586 587 588
                  EXPECT_RESULT, Undefined());
  }

  { ExistsInPrototypeContext context;
    context.Check("const x = 0; x",
                  0,
                  0,
589
                  0,
590 591 592 593 594 595 596 597
                  EXPECT_RESULT, Number::New(0));
  }
}



class AbsentInPrototypeContext: public DeclarationContext {
 protected:
598
  virtual v8::Handle<Integer> Query(Local<String> key) {
599
    // Let it seem that the property is absent in the prototype object.
600
    return Handle<Integer>();
601 602 603 604 605 606 607 608 609 610
  }

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


TEST(AbsentInPrototype) {
611
  i::FLAG_es52_globals = true;
612
  HandleScope scope(Isolate::GetCurrent());
613 614 615 616 617

  { AbsentInPrototypeContext context;
    context.Check("if (false) { var x = 0; }; x",
                  0,
                  0,
618
                  0,
619 620 621
                  EXPECT_RESULT, Undefined());
  }
}
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



class ExistsInHiddenPrototypeContext: public DeclarationContext {
 public:
  ExistsInHiddenPrototypeContext() {
    hidden_proto_ = FunctionTemplate::New();
    hidden_proto_->SetHiddenPrototype(true);
  }

 protected:
  virtual v8::Handle<Integer> Query(Local<String> key) {
    // Let it seem that the property exists in the hidden prototype object.
    return Integer::New(v8::None);
  }

  // Install the hidden prototype after the global object has been created.
  virtual void PostInitializeContext(Handle<Context> context) {
    Local<Object> global_object = context->Global();
    Local<Object> hidden_proto = hidden_proto_->GetFunction()->NewInstance();
    context->DetachGlobal();
    context->Global()->SetPrototype(hidden_proto);
    context->ReattachGlobal(global_object);
  }

  // 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) {
  i::FLAG_es52_globals = true;
659
  HandleScope scope(Isolate::GetCurrent());
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

  { ExistsInHiddenPrototypeContext context;
    context.Check("var x; x",
                  1,  // access
                  0,
                  2,  // declaration + initialization
                  EXPECT_EXCEPTION);  // x is not defined!
  }

  { ExistsInHiddenPrototypeContext context;
    context.Check("var x = 0; x",
                  1,  // access
                  1,  // initialization
                  2,  // declaration + initialization
                  EXPECT_RESULT, Number::New(0));
  }

  { ExistsInHiddenPrototypeContext context;
    context.Check("function x() { }; x",
                  0,
                  0,
                  0,
                  EXPECT_RESULT);
  }

  // TODO(mstarzinger): The semantics of global const is vague.
  { ExistsInHiddenPrototypeContext context;
    context.Check("const x; x",
                  0,
                  0,
                  1,  // (re-)declaration
                  EXPECT_RESULT, Undefined());
  }

  // TODO(mstarzinger): The semantics of global const is vague.
  { ExistsInHiddenPrototypeContext context;
    context.Check("const x = 0; x",
                  0,
                  0,
                  1,  // (re-)declaration
                  EXPECT_RESULT, Number::New(0));
  }
}
703 704 705 706 707



class SimpleContext {
 public:
708 709 710
  SimpleContext()
      : handle_scope_(Isolate::GetCurrent()),
        context_(Context::New(Isolate::GetCurrent())) {
711 712 713
    context_->Enter();
  }

714
  ~SimpleContext() {
715 716 717 718 719 720
    context_->Exit();
  }

  void Check(const char* source,
             Expectations expectations,
             v8::Handle<Value> value = Local<Value>()) {
721
    HandleScope scope(context_->GetIsolate());
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    TryCatch catcher;
    catcher.SetVerbose(true);
    Local<Script> script = Script::Compile(String::New(source));
    if (expectations == EXPECT_ERROR) {
      CHECK(script.IsEmpty());
      return;
    }
    CHECK(!script.IsEmpty());
    Local<Value> result = script->Run();
    if (expectations == EXPECT_RESULT) {
      CHECK(!catcher.HasCaught());
      if (!value.IsEmpty()) {
        CHECK_EQ(value, result);
      }
    } else {
      CHECK(expectations == EXPECT_EXCEPTION);
      CHECK(catcher.HasCaught());
      if (!value.IsEmpty()) {
        CHECK_EQ(value, catcher.Exception());
      }
    }
  }

 private:
746 747
  HandleScope handle_scope_;
  Local<Context> context_;
748 749 750
};


751
TEST(CrossScriptReferences) {
752
  HandleScope scope(Isolate::GetCurrent());
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

  { SimpleContext context;
    context.Check("var x = 1; x",
                  EXPECT_RESULT, Number::New(1));
    context.Check("var x = 2; x",
                  EXPECT_RESULT, Number::New(2));
    context.Check("const x = 3; x",
                  EXPECT_RESULT, Number::New(3));
    context.Check("const x = 4; x",
                  EXPECT_RESULT, Number::New(4));
    context.Check("x = 5; x",
                  EXPECT_RESULT, Number::New(5));
    context.Check("var x = 6; x",
                  EXPECT_RESULT, Number::New(6));
    context.Check("this.x",
                  EXPECT_RESULT, Number::New(6));
    context.Check("function x() { return 7 }; x()",
                  EXPECT_RESULT, Number::New(7));
  }

  { SimpleContext context;
    context.Check("const x = 1; x",
                  EXPECT_RESULT, Number::New(1));
    context.Check("var x = 2; x",  // assignment ignored
                  EXPECT_RESULT, Number::New(1));
    context.Check("const x = 3; x",
                  EXPECT_RESULT, Number::New(1));
    context.Check("x = 4; x",  // assignment ignored
                  EXPECT_RESULT, Number::New(1));
    context.Check("var x = 5; x",  // assignment ignored
                  EXPECT_RESULT, Number::New(1));
    context.Check("this.x",
                  EXPECT_RESULT, Number::New(1));
    context.Check("function x() { return 7 }; x",
                  EXPECT_EXCEPTION);
  }
789 790
}

791

792
TEST(CrossScriptReferencesHarmony) {
793 794
  i::FLAG_use_strict = true;
  i::FLAG_harmony_scoping = true;
795
  i::FLAG_harmony_modules = true;
796

797
  HandleScope scope(Isolate::GetCurrent());
798

799 800 801 802 803 804 805 806
  const char* decs[] = {
    "var x = 1; x", "x", "this.x",
    "function x() { return 1 }; x()", "x()", "this.x()",
    "let x = 1; x", "x", "this.x",
    "const x = 1; x", "x", "this.x",
    "module x { export let a = 1 }; x.a", "x.a", "this.x.a",
    NULL
  };
807

808 809 810 811
  for (int i = 0; decs[i] != NULL; i += 3) {
    SimpleContext context;
    context.Check(decs[i], EXPECT_RESULT, Number::New(1));
    context.Check(decs[i+1], EXPECT_RESULT, Number::New(1));
812 813 814
    // TODO(rossberg): The current ES6 draft spec does not reflect lexical
    // bindings on the global object. However, this will probably change, in
    // which case we reactivate the following test.
815
    if (i/3 < 2) context.Check(decs[i+2], EXPECT_RESULT, Number::New(1));
816
  }
817
}
818 819


820 821 822 823
TEST(CrossScriptConflicts) {
  i::FLAG_use_strict = true;
  i::FLAG_harmony_scoping = true;
  i::FLAG_harmony_modules = true;
824

825
  HandleScope scope(Isolate::GetCurrent());
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
  const char* firsts[] = {
    "var x = 1; x",
    "function x() { return 1 }; x()",
    "let x = 1; x",
    "const x = 1; x",
    "module x { export let a = 1 }; x.a",
    NULL
  };
  const char* seconds[] = {
    "var x = 2; x",
    "function x() { return 2 }; x()",
    "let x = 2; x",
    "const x = 2; x",
    "module x { export let a = 2 }; x.a",
    NULL
  };
843

844 845 846 847 848 849 850 851 852 853
  for (int i = 0; firsts[i] != NULL; ++i) {
    for (int j = 0; seconds[j] != NULL; ++j) {
      SimpleContext context;
      context.Check(firsts[i], EXPECT_RESULT, Number::New(1));
      // TODO(rossberg): All tests should actually be errors in Harmony,
      // but we currently do not detect the cases where the first declaration
      // is not lexical.
      context.Check(seconds[j],
                    i < 2 ? EXPECT_RESULT : EXPECT_ERROR, Number::New(2));
    }
854 855
  }
}