Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: test/cctest/test-decls.cc

Issue 12716010: Added a version of the v8::HandleScope constructor with an Isolate and use that consistently. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixed rest Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. 1 // Copyright 2007-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 106
107 107
108 DeclarationContext::DeclarationContext() 108 DeclarationContext::DeclarationContext()
109 : is_initialized_(false), get_count_(0), set_count_(0), query_count_(0) { 109 : is_initialized_(false), get_count_(0), set_count_(0), query_count_(0) {
110 // Do nothing. 110 // Do nothing.
111 } 111 }
112 112
113 113
114 void DeclarationContext::InitializeIfNeeded() { 114 void DeclarationContext::InitializeIfNeeded() {
115 if (is_initialized_) return; 115 if (is_initialized_) return;
116 HandleScope scope; 116 HandleScope scope(Isolate::GetCurrent());
117 Local<FunctionTemplate> function = FunctionTemplate::New(); 117 Local<FunctionTemplate> function = FunctionTemplate::New();
118 Local<Value> data = External::New(this); 118 Local<Value> data = External::New(this);
119 GetHolder(function)->SetNamedPropertyHandler(&HandleGet, 119 GetHolder(function)->SetNamedPropertyHandler(&HandleGet,
120 &HandleSet, 120 &HandleSet,
121 &HandleQuery, 121 &HandleQuery,
122 0, 0, 122 0, 0,
123 data); 123 data);
124 context_ = Context::New(0, function->InstanceTemplate(), Local<Value>()); 124 context_ = Context::New(0, function->InstanceTemplate(), Local<Value>());
125 context_->Enter(); 125 context_->Enter();
126 is_initialized_ = true; 126 is_initialized_ = true;
127 PostInitializeContext(context_); 127 PostInitializeContext(context_);
128 } 128 }
129 129
130 130
131 void DeclarationContext::Check(const char* source, 131 void DeclarationContext::Check(const char* source,
132 int get, int set, int query, 132 int get, int set, int query,
133 Expectations expectations, 133 Expectations expectations,
134 v8::Handle<Value> value) { 134 v8::Handle<Value> value) {
135 InitializeIfNeeded(); 135 InitializeIfNeeded();
136 // A retry after a GC may pollute the counts, so perform gc now 136 // A retry after a GC may pollute the counts, so perform gc now
137 // to avoid that. 137 // to avoid that.
138 HEAP->CollectGarbage(v8::internal::NEW_SPACE); 138 HEAP->CollectGarbage(v8::internal::NEW_SPACE);
139 HandleScope scope; 139 HandleScope scope(Isolate::GetCurrent());
140 TryCatch catcher; 140 TryCatch catcher;
141 catcher.SetVerbose(true); 141 catcher.SetVerbose(true);
142 Local<Script> script = Script::Compile(String::New(source)); 142 Local<Script> script = Script::Compile(String::New(source));
143 if (expectations == EXPECT_ERROR) { 143 if (expectations == EXPECT_ERROR) {
144 CHECK(script.IsEmpty()); 144 CHECK(script.IsEmpty());
145 return; 145 return;
146 } 146 }
147 CHECK(!script.IsEmpty()); 147 CHECK(!script.IsEmpty());
148 Local<Value> result = script->Run(); 148 Local<Value> result = script->Run();
149 CHECK_EQ(get, get_count()); 149 CHECK_EQ(get, get_count());
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 208
209 209
210 v8::Handle<Integer> DeclarationContext::Query(Local<String> key) { 210 v8::Handle<Integer> DeclarationContext::Query(Local<String> key) {
211 return v8::Handle<Integer>(); 211 return v8::Handle<Integer>();
212 } 212 }
213 213
214 214
215 // Test global declaration of a property the interceptor doesn't know 215 // Test global declaration of a property the interceptor doesn't know
216 // about and doesn't handle. 216 // about and doesn't handle.
217 TEST(Unknown) { 217 TEST(Unknown) {
218 HandleScope scope; 218 HandleScope scope(Isolate::GetCurrent());
219 219
220 { DeclarationContext context; 220 { DeclarationContext context;
221 context.Check("var x; x", 221 context.Check("var x; x",
222 1, // access 222 1, // access
223 1, // declaration 223 1, // declaration
224 2, // declaration + initialization 224 2, // declaration + initialization
225 EXPECT_RESULT, Undefined()); 225 EXPECT_RESULT, Undefined());
226 } 226 }
227 227
228 { DeclarationContext context; 228 { DeclarationContext context;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 class PresentPropertyContext: public DeclarationContext { 263 class PresentPropertyContext: public DeclarationContext {
264 protected: 264 protected:
265 virtual v8::Handle<Integer> Query(Local<String> key) { 265 virtual v8::Handle<Integer> Query(Local<String> key) {
266 return Integer::New(v8::None); 266 return Integer::New(v8::None);
267 } 267 }
268 }; 268 };
269 269
270 270
271 271
272 TEST(Present) { 272 TEST(Present) {
273 HandleScope scope; 273 HandleScope scope(Isolate::GetCurrent());
274 274
275 { PresentPropertyContext context; 275 { PresentPropertyContext context;
276 context.Check("var x; x", 276 context.Check("var x; x",
277 1, // access 277 1, // access
278 0, 278 0,
279 2, // declaration + initialization 279 2, // declaration + initialization
280 EXPECT_EXCEPTION); // x is not defined! 280 EXPECT_EXCEPTION); // x is not defined!
281 } 281 }
282 282
283 { PresentPropertyContext context; 283 { PresentPropertyContext context;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 317
318 class AbsentPropertyContext: public DeclarationContext { 318 class AbsentPropertyContext: public DeclarationContext {
319 protected: 319 protected:
320 virtual v8::Handle<Integer> Query(Local<String> key) { 320 virtual v8::Handle<Integer> Query(Local<String> key) {
321 return v8::Handle<Integer>(); 321 return v8::Handle<Integer>();
322 } 322 }
323 }; 323 };
324 324
325 325
326 TEST(Absent) { 326 TEST(Absent) {
327 HandleScope scope; 327 HandleScope scope(Isolate::GetCurrent());
328 328
329 { AbsentPropertyContext context; 329 { AbsentPropertyContext context;
330 context.Check("var x; x", 330 context.Check("var x; x",
331 1, // access 331 1, // access
332 1, // declaration 332 1, // declaration
333 2, // declaration + initialization 333 2, // declaration + initialization
334 EXPECT_RESULT, Undefined()); 334 EXPECT_RESULT, Undefined());
335 } 335 }
336 336
337 { AbsentPropertyContext context; 337 { AbsentPropertyContext context;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 // Do the lookup in the object. 407 // Do the lookup in the object.
408 return v8::Handle<Integer>(); 408 return v8::Handle<Integer>();
409 } 409 }
410 410
411 private: 411 private:
412 State state_; 412 State state_;
413 }; 413 };
414 414
415 415
416 TEST(Appearing) { 416 TEST(Appearing) {
417 HandleScope scope; 417 HandleScope scope(Isolate::GetCurrent());
418 418
419 { AppearingPropertyContext context; 419 { AppearingPropertyContext context;
420 context.Check("var x; x", 420 context.Check("var x; x",
421 1, // access 421 1, // access
422 1, // declaration 422 1, // declaration
423 2, // declaration + initialization 423 2, // declaration + initialization
424 EXPECT_RESULT, Undefined()); 424 EXPECT_RESULT, Undefined());
425 } 425 }
426 426
427 { AppearingPropertyContext context; 427 { AppearingPropertyContext context;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 // Do the lookup in the object. 499 // Do the lookup in the object.
500 return Handle<Integer>(); 500 return Handle<Integer>();
501 } 501 }
502 502
503 private: 503 private:
504 State state_; 504 State state_;
505 }; 505 };
506 506
507 507
508 TEST(Reappearing) { 508 TEST(Reappearing) {
509 HandleScope scope; 509 HandleScope scope(Isolate::GetCurrent());
510 510
511 { ReappearingPropertyContext context; 511 { ReappearingPropertyContext context;
512 context.Check("const x; var x = 0", 512 context.Check("const x; var x = 0",
513 0, 513 0,
514 3, // const declaration+initialization, var initialization 514 3, // const declaration+initialization, var initialization
515 3, // 2 x declaration + var initialization 515 3, // 2 x declaration + var initialization
516 EXPECT_RESULT, Undefined()); 516 EXPECT_RESULT, Undefined());
517 } 517 }
518 } 518 }
519 519
520 520
521 521
522 class ExistsInPrototypeContext: public DeclarationContext { 522 class ExistsInPrototypeContext: public DeclarationContext {
523 protected: 523 protected:
524 virtual v8::Handle<Integer> Query(Local<String> key) { 524 virtual v8::Handle<Integer> Query(Local<String> key) {
525 // Let it seem that the property exists in the prototype object. 525 // Let it seem that the property exists in the prototype object.
526 return Integer::New(v8::None); 526 return Integer::New(v8::None);
527 } 527 }
528 528
529 // Use the prototype as the holder for the interceptors. 529 // Use the prototype as the holder for the interceptors.
530 virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) { 530 virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
531 return function->PrototypeTemplate(); 531 return function->PrototypeTemplate();
532 } 532 }
533 }; 533 };
534 534
535 535
536 TEST(ExistsInPrototype) { 536 TEST(ExistsInPrototype) {
537 i::FLAG_es52_globals = true; 537 i::FLAG_es52_globals = true;
538 HandleScope scope; 538 HandleScope scope(Isolate::GetCurrent());
539 539
540 // Sanity check to make sure that the holder of the interceptor 540 // Sanity check to make sure that the holder of the interceptor
541 // really is the prototype object. 541 // really is the prototype object.
542 { ExistsInPrototypeContext context; 542 { ExistsInPrototypeContext context;
543 context.Check("this.x = 87; this.x", 543 context.Check("this.x = 87; this.x",
544 0, 544 0,
545 0, 545 0,
546 0, 546 0,
547 EXPECT_RESULT, Number::New(87)); 547 EXPECT_RESULT, Number::New(87));
548 } 548 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 591
592 // Use the prototype as the holder for the interceptors. 592 // Use the prototype as the holder for the interceptors.
593 virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) { 593 virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) {
594 return function->PrototypeTemplate(); 594 return function->PrototypeTemplate();
595 } 595 }
596 }; 596 };
597 597
598 598
599 TEST(AbsentInPrototype) { 599 TEST(AbsentInPrototype) {
600 i::FLAG_es52_globals = true; 600 i::FLAG_es52_globals = true;
601 HandleScope scope; 601 HandleScope scope(Isolate::GetCurrent());
602 602
603 { AbsentInPrototypeContext context; 603 { AbsentInPrototypeContext context;
604 context.Check("if (false) { var x = 0; }; x", 604 context.Check("if (false) { var x = 0; }; x",
605 0, 605 0,
606 0, 606 0,
607 0, 607 0,
608 EXPECT_RESULT, Undefined()); 608 EXPECT_RESULT, Undefined());
609 } 609 }
610 } 610 }
611 611
(...skipping 26 matching lines...) Expand all
638 return hidden_proto_->InstanceTemplate(); 638 return hidden_proto_->InstanceTemplate();
639 } 639 }
640 640
641 private: 641 private:
642 Local<FunctionTemplate> hidden_proto_; 642 Local<FunctionTemplate> hidden_proto_;
643 }; 643 };
644 644
645 645
646 TEST(ExistsInHiddenPrototype) { 646 TEST(ExistsInHiddenPrototype) {
647 i::FLAG_es52_globals = true; 647 i::FLAG_es52_globals = true;
648 HandleScope scope; 648 HandleScope scope(Isolate::GetCurrent());
649 649
650 { ExistsInHiddenPrototypeContext context; 650 { ExistsInHiddenPrototypeContext context;
651 context.Check("var x; x", 651 context.Check("var x; x",
652 1, // access 652 1, // access
653 0, 653 0,
654 2, // declaration + initialization 654 2, // declaration + initialization
655 EXPECT_EXCEPTION); // x is not defined! 655 EXPECT_EXCEPTION); // x is not defined!
656 } 656 }
657 657
658 { ExistsInHiddenPrototypeContext context; 658 { ExistsInHiddenPrototypeContext context;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 } 700 }
701 701
702 virtual ~SimpleContext() { 702 virtual ~SimpleContext() {
703 context_->Exit(); 703 context_->Exit();
704 context_.Dispose(context_->GetIsolate()); 704 context_.Dispose(context_->GetIsolate());
705 } 705 }
706 706
707 void Check(const char* source, 707 void Check(const char* source,
708 Expectations expectations, 708 Expectations expectations,
709 v8::Handle<Value> value = Local<Value>()) { 709 v8::Handle<Value> value = Local<Value>()) {
710 HandleScope scope; 710 HandleScope scope(context_->GetIsolate());
711 TryCatch catcher; 711 TryCatch catcher;
712 catcher.SetVerbose(true); 712 catcher.SetVerbose(true);
713 Local<Script> script = Script::Compile(String::New(source)); 713 Local<Script> script = Script::Compile(String::New(source));
714 if (expectations == EXPECT_ERROR) { 714 if (expectations == EXPECT_ERROR) {
715 CHECK(script.IsEmpty()); 715 CHECK(script.IsEmpty());
716 return; 716 return;
717 } 717 }
718 CHECK(!script.IsEmpty()); 718 CHECK(!script.IsEmpty());
719 Local<Value> result = script->Run(); 719 Local<Value> result = script->Run();
720 if (expectations == EXPECT_RESULT) { 720 if (expectations == EXPECT_RESULT) {
721 CHECK(!catcher.HasCaught()); 721 CHECK(!catcher.HasCaught());
722 if (!value.IsEmpty()) { 722 if (!value.IsEmpty()) {
723 CHECK_EQ(value, result); 723 CHECK_EQ(value, result);
724 } 724 }
725 } else { 725 } else {
726 CHECK(expectations == EXPECT_EXCEPTION); 726 CHECK(expectations == EXPECT_EXCEPTION);
727 CHECK(catcher.HasCaught()); 727 CHECK(catcher.HasCaught());
728 if (!value.IsEmpty()) { 728 if (!value.IsEmpty()) {
729 CHECK_EQ(value, catcher.Exception()); 729 CHECK_EQ(value, catcher.Exception());
730 } 730 }
731 } 731 }
732 } 732 }
733 733
734 private: 734 private:
735 Persistent<Context> context_; 735 Persistent<Context> context_;
736 }; 736 };
737 737
738 738
739 TEST(CrossScriptReferences) { 739 TEST(CrossScriptReferences) {
740 HandleScope scope; 740 HandleScope scope(Isolate::GetCurrent());
741 741
742 { SimpleContext context; 742 { SimpleContext context;
743 context.Check("var x = 1; x", 743 context.Check("var x = 1; x",
744 EXPECT_RESULT, Number::New(1)); 744 EXPECT_RESULT, Number::New(1));
745 context.Check("var x = 2; x", 745 context.Check("var x = 2; x",
746 EXPECT_RESULT, Number::New(2)); 746 EXPECT_RESULT, Number::New(2));
747 context.Check("const x = 3; x", 747 context.Check("const x = 3; x",
748 EXPECT_RESULT, Number::New(3)); 748 EXPECT_RESULT, Number::New(3));
749 context.Check("const x = 4; x", 749 context.Check("const x = 4; x",
750 EXPECT_RESULT, Number::New(4)); 750 EXPECT_RESULT, Number::New(4));
(...skipping 24 matching lines...) Expand all
775 EXPECT_EXCEPTION); 775 EXPECT_EXCEPTION);
776 } 776 }
777 } 777 }
778 778
779 779
780 TEST(CrossScriptReferencesHarmony) { 780 TEST(CrossScriptReferencesHarmony) {
781 i::FLAG_use_strict = true; 781 i::FLAG_use_strict = true;
782 i::FLAG_harmony_scoping = true; 782 i::FLAG_harmony_scoping = true;
783 i::FLAG_harmony_modules = true; 783 i::FLAG_harmony_modules = true;
784 784
785 HandleScope scope; 785 HandleScope scope(Isolate::GetCurrent());
786 786
787 const char* decs[] = { 787 const char* decs[] = {
788 "var x = 1; x", "x", "this.x", 788 "var x = 1; x", "x", "this.x",
789 "function x() { return 1 }; x()", "x()", "this.x()", 789 "function x() { return 1 }; x()", "x()", "this.x()",
790 "let x = 1; x", "x", "this.x", 790 "let x = 1; x", "x", "this.x",
791 "const x = 1; x", "x", "this.x", 791 "const x = 1; x", "x", "this.x",
792 "module x { export let a = 1 }; x.a", "x.a", "this.x.a", 792 "module x { export let a = 1 }; x.a", "x.a", "this.x.a",
793 NULL 793 NULL
794 }; 794 };
795 795
796 for (int i = 0; decs[i] != NULL; i += 3) { 796 for (int i = 0; decs[i] != NULL; i += 3) {
797 SimpleContext context; 797 SimpleContext context;
798 context.Check(decs[i], EXPECT_RESULT, Number::New(1)); 798 context.Check(decs[i], EXPECT_RESULT, Number::New(1));
799 context.Check(decs[i+1], EXPECT_RESULT, Number::New(1)); 799 context.Check(decs[i+1], EXPECT_RESULT, Number::New(1));
800 // TODO(rossberg): The current ES6 draft spec does not reflect lexical 800 // TODO(rossberg): The current ES6 draft spec does not reflect lexical
801 // bindings on the global object. However, this will probably change, in 801 // bindings on the global object. However, this will probably change, in
802 // which case we reactivate the following test. 802 // which case we reactivate the following test.
803 if (i/3 < 2) context.Check(decs[i+2], EXPECT_RESULT, Number::New(1)); 803 if (i/3 < 2) context.Check(decs[i+2], EXPECT_RESULT, Number::New(1));
804 } 804 }
805 } 805 }
806 806
807 807
808 TEST(CrossScriptConflicts) { 808 TEST(CrossScriptConflicts) {
809 i::FLAG_use_strict = true; 809 i::FLAG_use_strict = true;
810 i::FLAG_harmony_scoping = true; 810 i::FLAG_harmony_scoping = true;
811 i::FLAG_harmony_modules = true; 811 i::FLAG_harmony_modules = true;
812 812
813 HandleScope scope; 813 HandleScope scope(Isolate::GetCurrent());
814 814
815 const char* firsts[] = { 815 const char* firsts[] = {
816 "var x = 1; x", 816 "var x = 1; x",
817 "function x() { return 1 }; x()", 817 "function x() { return 1 }; x()",
818 "let x = 1; x", 818 "let x = 1; x",
819 "const x = 1; x", 819 "const x = 1; x",
820 "module x { export let a = 1 }; x.a", 820 "module x { export let a = 1 }; x.a",
821 NULL 821 NULL
822 }; 822 };
823 const char* seconds[] = { 823 const char* seconds[] = {
(...skipping 10 matching lines...) Expand all
834 SimpleContext context; 834 SimpleContext context;
835 context.Check(firsts[i], EXPECT_RESULT, Number::New(1)); 835 context.Check(firsts[i], EXPECT_RESULT, Number::New(1));
836 // TODO(rossberg): All tests should actually be errors in Harmony, 836 // TODO(rossberg): All tests should actually be errors in Harmony,
837 // but we currently do not detect the cases where the first declaration 837 // but we currently do not detect the cases where the first declaration
838 // is not lexical. 838 // is not lexical.
839 context.Check(seconds[j], 839 context.Check(seconds[j],
840 i < 2 ? EXPECT_RESULT : EXPECT_ERROR, Number::New(2)); 840 i < 2 ? EXPECT_RESULT : EXPECT_ERROR, Number::New(2));
841 } 841 }
842 } 842 }
843 } 843 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698