test-decls.cc 38.9 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.h"
33
#include "test/cctest/cctest.h"
34 35 36 37 38 39

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
      Isolate* isolate = CcTest::isolate();
56 57 58
      HandleScope scope(isolate);
      Local<Context> context = Local<Context>::New(isolate, context_);
      context->Exit();
59
      context_.Reset();
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

 protected:
73 74 75
  virtual v8::Handle<Value> Get(Local<Name> key);
  virtual v8::Handle<Value> Set(Local<Name> key, Local<Value> value);
  virtual v8::Handle<Integer> Query(Local<Name> 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
  static void HandleGet(Local<Name> key,
92
                        const v8::PropertyCallbackInfo<v8::Value>& info);
93
  static void HandleSet(Local<Name> key, Local<Value> value,
94
                        const v8::PropertyCallbackInfo<v8::Value>& info);
95
  static void HandleQuery(Local<Name> key,
96
                          const v8::PropertyCallbackInfo<v8::Integer>& info);
97

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

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

  int get_count_;
  int set_count_;
106
  int query_count_;
107

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


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


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


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


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


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


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


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


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


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


221
v8::Handle<Integer> DeclarationContext::Query(Local<Name> key) {
222
  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(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
                  0,
249 250 251 252 253 254 255
                  0,
                  EXPECT_RESULT);
  }

  { DeclarationContext context;
    context.Check("const x; x",
                  1,  // access
256
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
257 258 259 260 261
  }

  { DeclarationContext context;
    context.Check("const x = 0; x",
                  1,  // access
262
                  0,
263
                  0,
264
                  EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
265 266 267 268 269 270
  }
}


class AbsentPropertyContext: public DeclarationContext {
 protected:
271
  virtual v8::Handle<Integer> Query(Local<Name> key) {
272
    return v8::Handle<Integer>();
273 274 275 276 277
  }
};


TEST(Absent) {
278
  v8::Isolate* isolate = CcTest::isolate();
279
  v8::V8::Initialize();
280
  HandleScope scope(isolate);
281 282 283 284

  { AbsentPropertyContext context;
    context.Check("var x; x",
                  1,  // access
285
                  0, 0, EXPECT_RESULT, Undefined(isolate));
286 287 288 289 290
  }

  { AbsentPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
291 292
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(isolate, 0));
293 294 295 296 297
  }

  { AbsentPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
298
                  0,
299 300 301 302 303 304 305
                  0,
                  EXPECT_RESULT);
  }

  { AbsentPropertyContext context;
    context.Check("const x; x",
                  1,  // access
306
                  0, 0, EXPECT_RESULT, Undefined(isolate));
307 308 309 310 311
  }

  { AbsentPropertyContext context;
    context.Check("const x = 0; x",
                  1,  // access
312
                  0, 0, EXPECT_RESULT, Number::New(isolate, 0));
313 314 315 316 317
  }

  { AbsentPropertyContext context;
    context.Check("if (false) { var x = 0 }; x",
                  1,  // access
318
                  0, 0, EXPECT_RESULT, Undefined(isolate));
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  }
}



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

  AppearingPropertyContext() : state_(DECLARE) { }

 protected:
335
  virtual v8::Handle<Integer> Query(Local<Name> key) {
336 337 338 339 340
    switch (state_) {
      case DECLARE:
        // Force declaration by returning that the
        // property is absent.
        state_ = INITIALIZE_IF_ASSIGN;
341
        return Handle<Integer>();
342 343 344 345
      case INITIALIZE_IF_ASSIGN:
        // Return that the property is present so we only get the
        // setter called when initializing with a value.
        state_ = UNKNOWN;
346
        return Integer::New(isolate(), v8::None);
347
      default:
348
        CHECK(state_ == UNKNOWN);
349 350 351
        break;
    }
    // Do the lookup in the object.
352
    return v8::Handle<Integer>();
353 354 355 356 357 358 359 360
  }

 private:
  State state_;
};


TEST(Appearing) {
361
  v8::V8::Initialize();
362
  HandleScope scope(CcTest::isolate());
363 364 365 366

  { AppearingPropertyContext context;
    context.Check("var x; x",
                  1,  // access
367
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
368 369 370 371 372
  }

  { AppearingPropertyContext context;
    context.Check("var x = 0; x",
                  1,  // access
373 374
                  1,  // initialization
                  0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
375 376 377 378 379
  }

  { AppearingPropertyContext context;
    context.Check("function x() { }; x",
                  1,  // access
380
                  0,
381 382 383 384 385 386
                  0,
                  EXPECT_RESULT);
  }

  { AppearingPropertyContext context;
    context.Check("const x; x",
387
                  1,  // access
388
                  0, 0, EXPECT_RESULT, Undefined(CcTest::isolate()));
389 390 391 392
  }

  { AppearingPropertyContext context;
    context.Check("const x = 0; x",
393
                  1,  // access
394
                  0, 0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
395 396 397 398 399 400
  }
}



class ExistsInPrototypeContext: public DeclarationContext {
401 402
 public:
  ExistsInPrototypeContext() { InitializeIfNeeded(); }
403
 protected:
404
  virtual v8::Handle<Integer> Query(Local<Name> key) {
405
    // Let it seem that the property exists in the prototype object.
406
    return Integer::New(isolate(), v8::None);
407 408 409 410 411 412 413 414 415 416
  }

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


TEST(ExistsInPrototype) {
417
  HandleScope scope(CcTest::isolate());
418 419 420 421

  // Sanity check to make sure that the holder of the interceptor
  // really is the prototype object.
  { ExistsInPrototypeContext context;
422 423
    context.Check("this.x = 87; this.x", 0, 0, 1, EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 87));
424 425 426 427 428
  }

  { ExistsInPrototypeContext context;
    context.Check("var x; x",
                  0,
429 430
                  0,
                  0,
431
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
432 433 434 435 436 437
  }

  { ExistsInPrototypeContext context;
    context.Check("var x = 0; x",
                  0,
                  0,
438
                  0,
439
                  EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
440 441 442 443 444 445
  }

  { ExistsInPrototypeContext context;
    context.Check("const x; x",
                  0,
                  0,
446
                  0,
447
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
448 449 450 451 452 453
  }

  { ExistsInPrototypeContext context;
    context.Check("const x = 0; x",
                  0,
                  0,
454
                  0,
455
                  EXPECT_RESULT, Number::New(CcTest::isolate(), 0));
456 457 458 459 460 461 462
  }
}



class AbsentInPrototypeContext: public DeclarationContext {
 protected:
463
  virtual v8::Handle<Integer> Query(Local<Name> key) {
464
    // Let it seem that the property is absent in the prototype object.
465
    return Handle<Integer>();
466 467 468 469 470 471 472 473 474 475
  }

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


TEST(AbsentInPrototype) {
476
  v8::V8::Initialize();
477
  HandleScope scope(CcTest::isolate());
478 479 480 481 482

  { AbsentInPrototypeContext context;
    context.Check("if (false) { var x = 0; }; x",
                  0,
                  0,
483
                  0,
484
                  EXPECT_RESULT, Undefined(CcTest::isolate()));
485 486
  }
}
487 488 489 490 491 492



class ExistsInHiddenPrototypeContext: public DeclarationContext {
 public:
  ExistsInHiddenPrototypeContext() {
493
    hidden_proto_ = FunctionTemplate::New(CcTest::isolate());
494 495 496 497
    hidden_proto_->SetHiddenPrototype(true);
  }

 protected:
498
  virtual v8::Handle<Integer> Query(Local<Name> key) {
499
    // Let it seem that the property exists in the hidden prototype object.
500
    return Integer::New(isolate(), v8::None);
501 502 503 504 505 506
  }

  // 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();
507 508 509
    Local<Object> inner_global =
        Local<Object>::Cast(global_object->GetPrototype());
    inner_global->SetPrototype(hidden_proto);
510 511 512 513 514 515 516 517 518 519 520 521 522
  }

  // 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) {
523
  HandleScope scope(CcTest::isolate());
524 525

  { ExistsInHiddenPrototypeContext context;
526 527
    context.Check("var x; x", 0, 0, 0, EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
528 529 530
  }

  { ExistsInHiddenPrototypeContext context;
531 532
    context.Check("var x = 0; x", 0, 0, 0, EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 0));
533 534 535 536 537 538 539 540 541 542 543 544
  }

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

  // TODO(mstarzinger): The semantics of global const is vague.
  { ExistsInHiddenPrototypeContext context;
545 546
    context.Check("const x; x", 0, 0, 0, EXPECT_RESULT,
                  Undefined(CcTest::isolate()));
547 548 549 550
  }

  // TODO(mstarzinger): The semantics of global const is vague.
  { ExistsInHiddenPrototypeContext context;
551 552
    context.Check("const x = 0; x", 0, 0, 0, EXPECT_RESULT,
                  Number::New(CcTest::isolate(), 0));
553 554
  }
}
555 556 557 558 559



class SimpleContext {
 public:
560
  SimpleContext()
561 562
      : handle_scope_(CcTest::isolate()),
        context_(Context::New(CcTest::isolate())) {
563 564 565
    context_->Enter();
  }

566
  ~SimpleContext() {
567 568 569 570 571 572
    context_->Exit();
  }

  void Check(const char* source,
             Expectations expectations,
             v8::Handle<Value> value = Local<Value>()) {
573
    HandleScope scope(context_->GetIsolate());
574
    TryCatch catcher(context_->GetIsolate());
575
    catcher.SetVerbose(true);
576 577
    Local<Script> script =
        Script::Compile(String::NewFromUtf8(context_->GetIsolate(), source));
578 579 580 581 582 583 584 585 586
    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()) {
587
        CHECK(value->Equals(result));
588 589 590 591 592
      }
    } else {
      CHECK(expectations == EXPECT_EXCEPTION);
      CHECK(catcher.HasCaught());
      if (!value.IsEmpty()) {
593
        CHECK(value->Equals(catcher.Exception()));
594 595 596 597 598
      }
    }
  }

 private:
599 600
  HandleScope handle_scope_;
  Local<Context> context_;
601 602 603
};


604
TEST(CrossScriptReferences) {
605 606
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
607 608 609

  { SimpleContext context;
    context.Check("var x = 1; x",
610
                  EXPECT_RESULT, Number::New(isolate, 1));
611
    context.Check("var x = 2; x",
612
                  EXPECT_RESULT, Number::New(isolate, 2));
613 614
    context.Check("const x = 3; x", EXPECT_EXCEPTION);
    context.Check("const x = 4; x", EXPECT_EXCEPTION);
615
    context.Check("x = 5; x",
616
                  EXPECT_RESULT, Number::New(isolate, 5));
617
    context.Check("var x = 6; x",
618
                  EXPECT_RESULT, Number::New(isolate, 6));
619
    context.Check("this.x",
620
                  EXPECT_RESULT, Number::New(isolate, 6));
621
    context.Check("function x() { return 7 }; x()",
622
                  EXPECT_RESULT, Number::New(isolate, 7));
623 624 625 626
  }

  { SimpleContext context;
    context.Check("const x = 1; x",
627
                  EXPECT_RESULT, Number::New(isolate, 1));
628
    context.Check("var x = 2; x",  // assignment ignored
629
                  EXPECT_RESULT, Number::New(isolate, 1));
630
    context.Check("const x = 3; x", EXPECT_EXCEPTION);
631
    context.Check("x = 4; x",  // assignment ignored
632
                  EXPECT_RESULT, Number::New(isolate, 1));
633
    context.Check("var x = 5; x",  // assignment ignored
634
                  EXPECT_RESULT, Number::New(isolate, 1));
635
    context.Check("this.x",
636
                  EXPECT_RESULT, Number::New(isolate, 1));
637 638 639
    context.Check("function x() { return 7 }; x",
                  EXPECT_EXCEPTION);
  }
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
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));
  }
}


679
TEST(CrossScriptReferencesHarmony) {
680 681
  v8::Isolate* isolate = CcTest::isolate();
  HandleScope scope(isolate);
682

683
  // Check that simple cross-script global scope access works.
684
  const char* decs[] = {
685 686 687 688
    "'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",
689 690
    NULL
  };
691

692
  for (int i = 0; decs[i] != NULL; i += 2) {
693
    SimpleContext context;
694 695
    context.Check(decs[i], EXPECT_RESULT, Number::New(isolate, 1));
    context.Check(decs[i+1], EXPECT_RESULT, Number::New(isolate, 1));
696
  }
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 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 789 790 791 792 793 794 795 796 797 798 799 800 801

  // 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));
802 803
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 3));
804 805
    context.Check("y1 = 4;",
                  EXPECT_RESULT, Number::New(isolate, 4));
806 807
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 4));
808 809 810 811 812

    context.Check(script2,
                  EXPECT_RESULT, Number::New(isolate, 2));
    context.Check("'use strict'; let y2 = 5; 0;",
                  EXPECT_RESULT, Number::New(isolate, 0));
813 814 815 816
    context.Check(script1,
                  EXPECT_RESULT, Number::New(isolate, 4));
    context.Check(script2,
                  EXPECT_RESULT, Number::New(isolate, 5));
817 818 819 820
  }
}


821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
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));
}


840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
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));
860
}
861 862


863 864
TEST(CrossScriptConflicts) {
  i::FLAG_use_strict = true;
865

866
  HandleScope scope(CcTest::isolate());
867

868 869 870 871 872 873 874 875 876 877 878 879 880 881
  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
  };
882

883 884 885
  for (int i = 0; firsts[i] != NULL; ++i) {
    for (int j = 0; seconds[j] != NULL; ++j) {
      SimpleContext context;
886 887
      context.Check(firsts[i], EXPECT_RESULT,
                    Number::New(CcTest::isolate(), 1));
888 889 890 891 892 893
      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);
894
    }
895 896
  }
}
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921


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

  {
    SimpleContext context;
    Local<String> undefined_string = String::NewFromUtf8(
        CcTest::isolate(), "undefined", String::kInternalizedString);
    Local<String> number_string = String::NewFromUtf8(
        CcTest::isolate(), "number", String::kInternalizedString);

    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';"
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
        "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;"
938
        "x",
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
        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()",
954
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
    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;
    Local<String> undefined_string = String::NewFromUtf8(
        CcTest::isolate(), "undefined", String::kInternalizedString);
    Local<String> number_string = String::NewFromUtf8(
        CcTest::isolate(), "number", String::kInternalizedString);

    context.Check(
        "function f(o) { return x; }"
984
        "function g(v) { x = v; }"
985 986 987 988 989 990 991 992 993 994
        "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';"
995
        "g(15);x",
996 997
        EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
998 999 1000 1001
    context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15));
    context.Check("h({})", EXPECT_RESULT, number_string);
  }
}
1002 1003


1004
TEST(CrossScriptLoadICs) {
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 1056
  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));
  }
}
1057 1058 1059 1060 1061 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 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


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));
  }
}
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155


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));
  }
}
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172


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);
    }
  }
}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254


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