OLD | NEW |
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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 229 |
230 // Test global declaration of a property the interceptor doesn't know | 230 // Test global declaration of a property the interceptor doesn't know |
231 // about and doesn't handle. | 231 // about and doesn't handle. |
232 TEST(Unknown) { | 232 TEST(Unknown) { |
233 HandleScope scope(CcTest::isolate()); | 233 HandleScope scope(CcTest::isolate()); |
234 v8::V8::Initialize(); | 234 v8::V8::Initialize(); |
235 | 235 |
236 { DeclarationContext context; | 236 { DeclarationContext context; |
237 context.Check("var x; x", | 237 context.Check("var x; x", |
238 1, // access | 238 1, // access |
239 1, // declaration | 239 0, 0, EXPECT_RESULT, Undefined(CcTest::isolate())); |
240 2, // declaration + initialization | |
241 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
242 } | 240 } |
243 | 241 |
244 { DeclarationContext context; | 242 { DeclarationContext context; |
245 context.Check("var x = 0; x", | 243 context.Check("var x = 0; x", |
246 1, // access | 244 1, // access |
247 2, // declaration + initialization | 245 1, // initialization |
248 2, // declaration + initialization | 246 0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); |
249 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); | |
250 } | 247 } |
251 | 248 |
252 { DeclarationContext context; | 249 { DeclarationContext context; |
253 context.Check("function x() { }; x", | 250 context.Check("function x() { }; x", |
254 1, // access | 251 1, // access |
255 0, | 252 0, |
256 0, | 253 0, |
257 EXPECT_RESULT); | 254 EXPECT_RESULT); |
258 } | 255 } |
259 | 256 |
260 { DeclarationContext context; | 257 { DeclarationContext context; |
261 context.Check("const x; x", | 258 context.Check("const x; x", |
262 1, // access | 259 1, // access |
263 2, // declaration + initialization | 260 0, 0, EXPECT_RESULT, Undefined(CcTest::isolate())); |
264 1, // declaration | |
265 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
266 } | 261 } |
267 | 262 |
268 { DeclarationContext context; | 263 { DeclarationContext context; |
269 // SB 0 - BUG 1213579 | |
270 context.Check("const x = 0; x", | 264 context.Check("const x = 0; x", |
271 1, // access | 265 1, // access |
272 2, // declaration + initialization | |
273 1, // declaration | |
274 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
275 } | |
276 } | |
277 | |
278 | |
279 | |
280 class PresentPropertyContext: public DeclarationContext { | |
281 protected: | |
282 virtual v8::Handle<Integer> Query(Local<String> key) { | |
283 return Integer::New(isolate(), v8::None); | |
284 } | |
285 }; | |
286 | |
287 | |
288 | |
289 TEST(Present) { | |
290 HandleScope scope(CcTest::isolate()); | |
291 | |
292 { PresentPropertyContext context; | |
293 context.Check("var x; x", | |
294 1, // access | |
295 0, | |
296 2, // declaration + initialization | |
297 EXPECT_EXCEPTION); // x is not defined! | |
298 } | |
299 | |
300 { PresentPropertyContext context; | |
301 context.Check("var x = 0; x", | |
302 1, // access | |
303 1, // initialization | |
304 2, // declaration + initialization | |
305 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); | |
306 } | |
307 | |
308 { PresentPropertyContext context; | |
309 context.Check("function x() { }; x", | |
310 1, // access | |
311 0, | 266 0, |
312 0, | 267 0, |
313 EXPECT_RESULT); | |
314 } | |
315 | |
316 { PresentPropertyContext context; | |
317 context.Check("const x; x", | |
318 1, // access | |
319 1, // initialization | |
320 1, // (re-)declaration | |
321 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
322 } | |
323 | |
324 { PresentPropertyContext context; | |
325 context.Check("const x = 0; x", | |
326 1, // access | |
327 1, // initialization | |
328 1, // (re-)declaration | |
329 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); | 268 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); |
330 } | 269 } |
331 } | 270 } |
332 | 271 |
333 | 272 |
334 | |
335 class AbsentPropertyContext: public DeclarationContext { | 273 class AbsentPropertyContext: public DeclarationContext { |
336 protected: | 274 protected: |
337 virtual v8::Handle<Integer> Query(Local<String> key) { | 275 virtual v8::Handle<Integer> Query(Local<String> key) { |
338 return v8::Handle<Integer>(); | 276 return v8::Handle<Integer>(); |
339 } | 277 } |
340 }; | 278 }; |
341 | 279 |
342 | 280 |
343 TEST(Absent) { | 281 TEST(Absent) { |
344 v8::Isolate* isolate = CcTest::isolate(); | 282 v8::Isolate* isolate = CcTest::isolate(); |
345 v8::V8::Initialize(); | 283 v8::V8::Initialize(); |
346 HandleScope scope(isolate); | 284 HandleScope scope(isolate); |
347 | 285 |
348 { AbsentPropertyContext context; | 286 { AbsentPropertyContext context; |
349 context.Check("var x; x", | 287 context.Check("var x; x", |
350 1, // access | 288 1, // access |
351 1, // declaration | 289 0, 0, EXPECT_RESULT, Undefined(isolate)); |
352 2, // declaration + initialization | |
353 EXPECT_RESULT, Undefined(isolate)); | |
354 } | 290 } |
355 | 291 |
356 { AbsentPropertyContext context; | 292 { AbsentPropertyContext context; |
357 context.Check("var x = 0; x", | 293 context.Check("var x = 0; x", |
358 1, // access | 294 1, // access |
359 2, // declaration + initialization | 295 1, // initialization |
360 2, // declaration + initialization | 296 0, EXPECT_RESULT, Number::New(isolate, 0)); |
361 EXPECT_RESULT, Number::New(isolate, 0)); | |
362 } | 297 } |
363 | 298 |
364 { AbsentPropertyContext context; | 299 { AbsentPropertyContext context; |
365 context.Check("function x() { }; x", | 300 context.Check("function x() { }; x", |
366 1, // access | 301 1, // access |
367 0, | 302 0, |
368 0, | 303 0, |
369 EXPECT_RESULT); | 304 EXPECT_RESULT); |
370 } | 305 } |
371 | 306 |
372 { AbsentPropertyContext context; | 307 { AbsentPropertyContext context; |
373 context.Check("const x; x", | 308 context.Check("const x; x", |
374 1, // access | 309 1, // access |
375 2, // declaration + initialization | 310 0, 0, EXPECT_RESULT, Undefined(isolate)); |
376 1, // declaration | |
377 EXPECT_RESULT, Undefined(isolate)); | |
378 } | 311 } |
379 | 312 |
380 { AbsentPropertyContext context; | 313 { AbsentPropertyContext context; |
381 context.Check("const x = 0; x", | 314 context.Check("const x = 0; x", |
382 1, // access | 315 1, // access |
383 2, // declaration + initialization | 316 0, 0, EXPECT_RESULT, Number::New(isolate, 0)); |
384 1, // declaration | |
385 EXPECT_RESULT, Undefined(isolate)); // SB 0 - BUG 1213579 | |
386 } | 317 } |
387 | 318 |
388 { AbsentPropertyContext context; | 319 { AbsentPropertyContext context; |
389 context.Check("if (false) { var x = 0 }; x", | 320 context.Check("if (false) { var x = 0 }; x", |
390 1, // access | 321 1, // access |
391 1, // declaration | 322 0, 0, EXPECT_RESULT, Undefined(isolate)); |
392 1, // declaration + initialization | |
393 EXPECT_RESULT, Undefined(isolate)); | |
394 } | 323 } |
395 } | 324 } |
396 | 325 |
397 | 326 |
398 | 327 |
399 class AppearingPropertyContext: public DeclarationContext { | 328 class AppearingPropertyContext: public DeclarationContext { |
400 public: | 329 public: |
401 enum State { | 330 enum State { |
402 DECLARE, | 331 DECLARE, |
403 INITIALIZE_IF_ASSIGN, | 332 INITIALIZE_IF_ASSIGN, |
(...skipping 28 matching lines...) Expand all Loading... |
432 }; | 361 }; |
433 | 362 |
434 | 363 |
435 TEST(Appearing) { | 364 TEST(Appearing) { |
436 v8::V8::Initialize(); | 365 v8::V8::Initialize(); |
437 HandleScope scope(CcTest::isolate()); | 366 HandleScope scope(CcTest::isolate()); |
438 | 367 |
439 { AppearingPropertyContext context; | 368 { AppearingPropertyContext context; |
440 context.Check("var x; x", | 369 context.Check("var x; x", |
441 1, // access | 370 1, // access |
442 1, // declaration | 371 0, 0, EXPECT_RESULT, Undefined(CcTest::isolate())); |
443 2, // declaration + initialization | |
444 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
445 } | 372 } |
446 | 373 |
447 { AppearingPropertyContext context; | 374 { AppearingPropertyContext context; |
448 context.Check("var x = 0; x", | 375 context.Check("var x = 0; x", |
449 1, // access | 376 1, // access |
450 2, // declaration + initialization | 377 1, // initialization |
451 2, // declaration + initialization | 378 0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); |
452 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); | |
453 } | 379 } |
454 | 380 |
455 { AppearingPropertyContext context; | 381 { AppearingPropertyContext context; |
456 context.Check("function x() { }; x", | 382 context.Check("function x() { }; x", |
457 1, // access | 383 1, // access |
458 0, | 384 0, |
459 0, | 385 0, |
460 EXPECT_RESULT); | 386 EXPECT_RESULT); |
461 } | 387 } |
462 | 388 |
463 { AppearingPropertyContext context; | 389 { AppearingPropertyContext context; |
464 context.Check("const x; x", | 390 context.Check("const x; x", |
465 1, // access | 391 1, // access |
466 2, // declaration + initialization | 392 0, 0, EXPECT_RESULT, Undefined(CcTest::isolate())); |
467 1, // declaration | |
468 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
469 } | 393 } |
470 | 394 |
471 { AppearingPropertyContext context; | 395 { AppearingPropertyContext context; |
472 context.Check("const x = 0; x", | 396 context.Check("const x = 0; x", |
473 1, // access | 397 1, // access |
474 2, // declaration + initialization | 398 0, 0, EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); |
475 1, // declaration | |
476 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
477 // Result is undefined because declaration succeeded but | |
478 // initialization to 0 failed (due to context behavior). | |
479 } | 399 } |
480 } | 400 } |
481 | 401 |
482 | |
483 | |
484 class ReappearingPropertyContext: public DeclarationContext { | |
485 public: | |
486 enum State { | |
487 DECLARE, | |
488 DONT_DECLARE, | |
489 INITIALIZE, | |
490 UNKNOWN | |
491 }; | |
492 | |
493 ReappearingPropertyContext() : state_(DECLARE) { } | |
494 | |
495 protected: | |
496 virtual v8::Handle<Integer> Query(Local<String> key) { | |
497 switch (state_) { | |
498 case DECLARE: | |
499 // Force the first declaration by returning that | |
500 // the property is absent. | |
501 state_ = DONT_DECLARE; | |
502 return Handle<Integer>(); | |
503 case DONT_DECLARE: | |
504 // Ignore the second declaration by returning | |
505 // that the property is already there. | |
506 state_ = INITIALIZE; | |
507 return Integer::New(isolate(), v8::None); | |
508 case INITIALIZE: | |
509 // Force an initialization by returning that | |
510 // the property is absent. This will make sure | |
511 // that the setter is called and it will not | |
512 // lead to redeclaration conflicts (yet). | |
513 state_ = UNKNOWN; | |
514 return Handle<Integer>(); | |
515 default: | |
516 CHECK(state_ == UNKNOWN); | |
517 break; | |
518 } | |
519 // Do the lookup in the object. | |
520 return Handle<Integer>(); | |
521 } | |
522 | |
523 private: | |
524 State state_; | |
525 }; | |
526 | |
527 | |
528 TEST(Reappearing) { | |
529 v8::V8::Initialize(); | |
530 HandleScope scope(CcTest::isolate()); | |
531 | |
532 { ReappearingPropertyContext context; | |
533 context.Check("const x; var x = 0", | |
534 0, | |
535 3, // const declaration+initialization, var initialization | |
536 3, // 2 x declaration + var initialization | |
537 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
538 } | |
539 } | |
540 | |
541 | 402 |
542 | 403 |
543 class ExistsInPrototypeContext: public DeclarationContext { | 404 class ExistsInPrototypeContext: public DeclarationContext { |
544 public: | 405 public: |
545 ExistsInPrototypeContext() { InitializeIfNeeded(); } | 406 ExistsInPrototypeContext() { InitializeIfNeeded(); } |
546 protected: | 407 protected: |
547 virtual v8::Handle<Integer> Query(Local<String> key) { | 408 virtual v8::Handle<Integer> Query(Local<String> key) { |
548 // Let it seem that the property exists in the prototype object. | 409 // Let it seem that the property exists in the prototype object. |
549 return Integer::New(isolate(), v8::None); | 410 return Integer::New(isolate(), v8::None); |
550 } | 411 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 | 523 |
663 private: | 524 private: |
664 Local<FunctionTemplate> hidden_proto_; | 525 Local<FunctionTemplate> hidden_proto_; |
665 }; | 526 }; |
666 | 527 |
667 | 528 |
668 TEST(ExistsInHiddenPrototype) { | 529 TEST(ExistsInHiddenPrototype) { |
669 HandleScope scope(CcTest::isolate()); | 530 HandleScope scope(CcTest::isolate()); |
670 | 531 |
671 { ExistsInHiddenPrototypeContext context; | 532 { ExistsInHiddenPrototypeContext context; |
672 context.Check("var x; x", | 533 context.Check("var x; x", 0, 0, 0, EXPECT_RESULT, |
673 1, // access | 534 Undefined(CcTest::isolate())); |
674 0, | |
675 2, // declaration + initialization | |
676 EXPECT_EXCEPTION); // x is not defined! | |
677 } | 535 } |
678 | 536 |
679 { ExistsInHiddenPrototypeContext context; | 537 { ExistsInHiddenPrototypeContext context; |
680 context.Check("var x = 0; x", | 538 context.Check("var x = 0; x", 0, 0, 0, EXPECT_RESULT, |
681 1, // access | 539 Number::New(CcTest::isolate(), 0)); |
682 1, // initialization | |
683 2, // declaration + initialization | |
684 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); | |
685 } | 540 } |
686 | 541 |
687 { ExistsInHiddenPrototypeContext context; | 542 { ExistsInHiddenPrototypeContext context; |
688 context.Check("function x() { }; x", | 543 context.Check("function x() { }; x", |
689 0, | 544 0, |
690 0, | 545 0, |
691 0, | 546 0, |
692 EXPECT_RESULT); | 547 EXPECT_RESULT); |
693 } | 548 } |
694 | 549 |
695 // TODO(mstarzinger): The semantics of global const is vague. | 550 // TODO(mstarzinger): The semantics of global const is vague. |
696 { ExistsInHiddenPrototypeContext context; | 551 { ExistsInHiddenPrototypeContext context; |
697 context.Check("const x; x", | 552 context.Check("const x; x", 0, 0, 0, EXPECT_RESULT, |
698 0, | 553 Undefined(CcTest::isolate())); |
699 0, | |
700 1, // (re-)declaration | |
701 EXPECT_RESULT, Undefined(CcTest::isolate())); | |
702 } | 554 } |
703 | 555 |
704 // TODO(mstarzinger): The semantics of global const is vague. | 556 // TODO(mstarzinger): The semantics of global const is vague. |
705 { ExistsInHiddenPrototypeContext context; | 557 { ExistsInHiddenPrototypeContext context; |
706 context.Check("const x = 0; x", | 558 context.Check("const x = 0; x", 0, 0, 0, EXPECT_RESULT, |
707 0, | 559 Number::New(CcTest::isolate(), 0)); |
708 0, | |
709 1, // (re-)declaration | |
710 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); | |
711 } | 560 } |
712 } | 561 } |
713 | 562 |
714 | 563 |
715 | 564 |
716 class SimpleContext { | 565 class SimpleContext { |
717 public: | 566 public: |
718 SimpleContext() | 567 SimpleContext() |
719 : handle_scope_(CcTest::isolate()), | 568 : handle_scope_(CcTest::isolate()), |
720 context_(Context::New(CcTest::isolate())) { | 569 context_(Context::New(CcTest::isolate())) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 | 610 |
762 TEST(CrossScriptReferences) { | 611 TEST(CrossScriptReferences) { |
763 v8::Isolate* isolate = CcTest::isolate(); | 612 v8::Isolate* isolate = CcTest::isolate(); |
764 HandleScope scope(isolate); | 613 HandleScope scope(isolate); |
765 | 614 |
766 { SimpleContext context; | 615 { SimpleContext context; |
767 context.Check("var x = 1; x", | 616 context.Check("var x = 1; x", |
768 EXPECT_RESULT, Number::New(isolate, 1)); | 617 EXPECT_RESULT, Number::New(isolate, 1)); |
769 context.Check("var x = 2; x", | 618 context.Check("var x = 2; x", |
770 EXPECT_RESULT, Number::New(isolate, 2)); | 619 EXPECT_RESULT, Number::New(isolate, 2)); |
771 context.Check("const x = 3; x", | 620 context.Check("const x = 3; x", EXPECT_EXCEPTION); |
772 EXPECT_RESULT, Number::New(isolate, 3)); | 621 context.Check("const x = 4; x", EXPECT_EXCEPTION); |
773 context.Check("const x = 4; x", | |
774 EXPECT_RESULT, Number::New(isolate, 4)); | |
775 context.Check("x = 5; x", | 622 context.Check("x = 5; x", |
776 EXPECT_RESULT, Number::New(isolate, 5)); | 623 EXPECT_RESULT, Number::New(isolate, 5)); |
777 context.Check("var x = 6; x", | 624 context.Check("var x = 6; x", |
778 EXPECT_RESULT, Number::New(isolate, 6)); | 625 EXPECT_RESULT, Number::New(isolate, 6)); |
779 context.Check("this.x", | 626 context.Check("this.x", |
780 EXPECT_RESULT, Number::New(isolate, 6)); | 627 EXPECT_RESULT, Number::New(isolate, 6)); |
781 context.Check("function x() { return 7 }; x()", | 628 context.Check("function x() { return 7 }; x()", |
782 EXPECT_RESULT, Number::New(isolate, 7)); | 629 EXPECT_RESULT, Number::New(isolate, 7)); |
783 } | 630 } |
784 | 631 |
785 { SimpleContext context; | 632 { SimpleContext context; |
786 context.Check("const x = 1; x", | 633 context.Check("const x = 1; x", |
787 EXPECT_RESULT, Number::New(isolate, 1)); | 634 EXPECT_RESULT, Number::New(isolate, 1)); |
788 context.Check("var x = 2; x", // assignment ignored | 635 context.Check("var x = 2; x", // assignment ignored |
789 EXPECT_RESULT, Number::New(isolate, 1)); | 636 EXPECT_RESULT, Number::New(isolate, 1)); |
790 context.Check("const x = 3; x", | 637 context.Check("const x = 3; x", EXPECT_EXCEPTION); |
791 EXPECT_RESULT, Number::New(isolate, 1)); | |
792 context.Check("x = 4; x", // assignment ignored | 638 context.Check("x = 4; x", // assignment ignored |
793 EXPECT_RESULT, Number::New(isolate, 1)); | 639 EXPECT_RESULT, Number::New(isolate, 1)); |
794 context.Check("var x = 5; x", // assignment ignored | 640 context.Check("var x = 5; x", // assignment ignored |
795 EXPECT_RESULT, Number::New(isolate, 1)); | 641 EXPECT_RESULT, Number::New(isolate, 1)); |
796 context.Check("this.x", | 642 context.Check("this.x", |
797 EXPECT_RESULT, Number::New(isolate, 1)); | 643 EXPECT_RESULT, Number::New(isolate, 1)); |
798 context.Check("function x() { return 7 }; x", | 644 context.Check("function x() { return 7 }; x", |
799 EXPECT_EXCEPTION); | 645 EXPECT_EXCEPTION); |
800 } | 646 } |
801 } | 647 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 Number::New(CcTest::isolate(), 1)); | 709 Number::New(CcTest::isolate(), 1)); |
864 // TODO(rossberg): All tests should actually be errors in Harmony, | 710 // TODO(rossberg): All tests should actually be errors in Harmony, |
865 // but we currently do not detect the cases where the first declaration | 711 // but we currently do not detect the cases where the first declaration |
866 // is not lexical. | 712 // is not lexical. |
867 context.Check(seconds[j], | 713 context.Check(seconds[j], |
868 i < 2 ? EXPECT_RESULT : EXPECT_ERROR, | 714 i < 2 ? EXPECT_RESULT : EXPECT_ERROR, |
869 Number::New(CcTest::isolate(), 2)); | 715 Number::New(CcTest::isolate(), 2)); |
870 } | 716 } |
871 } | 717 } |
872 } | 718 } |
OLD | NEW |