OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "test/cctest/test-api.h" | 7 #include "test/cctest/test-api.h" |
8 | 8 |
9 #include "include/v8-util.h" | 9 #include "include/v8-util.h" |
10 #include "src/api.h" | 10 #include "src/api.h" |
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 .ToLocalChecked(); | 475 .ToLocalChecked(); |
476 CHECK_EQ(query_counter_int, 8); | 476 CHECK_EQ(query_counter_int, 8); |
477 | 477 |
478 v8_compile("Object.isFrozen('obj.x');")->Run(env.local()).ToLocalChecked(); | 478 v8_compile("Object.isFrozen('obj.x');")->Run(env.local()).ToLocalChecked(); |
479 CHECK_EQ(query_counter_int, 8); | 479 CHECK_EQ(query_counter_int, 8); |
480 } | 480 } |
481 | 481 |
482 bool get_was_called = false; | 482 bool get_was_called = false; |
483 bool set_was_called = false; | 483 bool set_was_called = false; |
484 | 484 |
| 485 int set_was_called_counter = 0; |
| 486 |
485 namespace { | 487 namespace { |
486 void GetterCallback(Local<Name> property, | 488 void GetterCallback(Local<Name> property, |
487 const v8::PropertyCallbackInfo<v8::Value>& info) { | 489 const v8::PropertyCallbackInfo<v8::Value>& info) { |
488 get_was_called = true; | 490 get_was_called = true; |
489 } | 491 } |
490 | 492 |
491 void SetterCallback(Local<Name> property, Local<Value> value, | 493 void SetterCallback(Local<Name> property, Local<Value> value, |
492 const v8::PropertyCallbackInfo<v8::Value>& info) { | 494 const v8::PropertyCallbackInfo<v8::Value>& info) { |
493 set_was_called = true; | 495 set_was_called = true; |
| 496 set_was_called_counter++; |
494 } | 497 } |
495 | 498 |
496 } // namespace | 499 } // namespace |
497 | 500 |
498 // Check that get callback is called in defineProperty with accessor descriptor. | 501 // Check that get callback is called in defineProperty with accessor descriptor. |
499 THREADED_TEST(DefinerCallbackAccessorInterceptor) { | 502 THREADED_TEST(DefinerCallbackAccessorInterceptor) { |
500 v8::HandleScope scope(CcTest::isolate()); | 503 v8::HandleScope scope(CcTest::isolate()); |
501 v8::Local<v8::FunctionTemplate> templ = | 504 v8::Local<v8::FunctionTemplate> templ = |
502 v8::FunctionTemplate::New(CcTest::isolate()); | 505 v8::FunctionTemplate::New(CcTest::isolate()); |
503 templ->InstanceTemplate()->SetHandler( | 506 templ->InstanceTemplate()->SetHandler( |
504 v8::NamedPropertyHandlerConfiguration(GetterCallback, SetterCallback)); | 507 v8::NamedPropertyHandlerConfiguration(GetterCallback, SetterCallback)); |
505 LocalContext env; | 508 LocalContext env; |
506 env->Global() | 509 env->Global() |
507 ->Set(env.local(), v8_str("obj"), templ->GetFunction(env.local()) | 510 ->Set(env.local(), v8_str("obj"), templ->GetFunction(env.local()) |
508 .ToLocalChecked() | 511 .ToLocalChecked() |
509 ->NewInstance(env.local()) | 512 ->NewInstance(env.local()) |
510 .ToLocalChecked()) | 513 .ToLocalChecked()) |
511 .FromJust(); | 514 .FromJust(); |
512 | 515 |
513 CHECK_EQ(get_was_called, false); | 516 get_was_called = false; |
514 CHECK_EQ(set_was_called, false); | 517 set_was_called = false; |
515 | 518 |
516 v8_compile("Object.defineProperty(obj, 'x', {set: function() {return 17;}});") | 519 v8_compile("Object.defineProperty(obj, 'x', {set: function() {return 17;}});") |
517 ->Run(env.local()) | 520 ->Run(env.local()) |
518 .ToLocalChecked(); | 521 .ToLocalChecked(); |
519 CHECK_EQ(get_was_called, true); | 522 CHECK_EQ(get_was_called, true); |
520 CHECK_EQ(set_was_called, false); | 523 CHECK_EQ(set_was_called, false); |
521 } | 524 } |
522 | 525 |
| 526 // Check that set callback is called for function declarations. |
| 527 THREADED_TEST(SetterCallbackFunctionDeclarationInterceptor) { |
| 528 v8::HandleScope scope(CcTest::isolate()); |
| 529 LocalContext env; |
| 530 v8::Local<v8::FunctionTemplate> templ = |
| 531 v8::FunctionTemplate::New(CcTest::isolate()); |
| 532 |
| 533 v8::Local<ObjectTemplate> object_template = templ->InstanceTemplate(); |
| 534 object_template->SetHandler( |
| 535 v8::NamedPropertyHandlerConfiguration(nullptr, SetterCallback)); |
| 536 v8::Local<v8::Context> ctx = |
| 537 v8::Context::New(CcTest::isolate(), nullptr, object_template); |
| 538 |
| 539 set_was_called_counter = 0; |
| 540 |
| 541 // Declare function. |
| 542 v8::Local<v8::String> code = v8_str("function x() {return 42;}; x();"); |
| 543 CHECK_EQ(42, v8::Script::Compile(ctx, code) |
| 544 .ToLocalChecked() |
| 545 ->Run(ctx) |
| 546 .ToLocalChecked() |
| 547 ->Int32Value(ctx) |
| 548 .FromJust()); |
| 549 CHECK_EQ(set_was_called_counter, 1); |
| 550 |
| 551 // Redeclare function. |
| 552 code = v8_str("function x() {return 43;}; x();"); |
| 553 CHECK_EQ(43, v8::Script::Compile(ctx, code) |
| 554 .ToLocalChecked() |
| 555 ->Run(ctx) |
| 556 .ToLocalChecked() |
| 557 ->Int32Value(ctx) |
| 558 .FromJust()); |
| 559 CHECK_EQ(set_was_called_counter, 2); |
| 560 |
| 561 // Redefine function. |
| 562 code = v8_str("x = function() {return 44;}; x();"); |
| 563 CHECK_EQ(44, v8::Script::Compile(ctx, code) |
| 564 .ToLocalChecked() |
| 565 ->Run(ctx) |
| 566 .ToLocalChecked() |
| 567 ->Int32Value(ctx) |
| 568 .FromJust()); |
| 569 CHECK_EQ(set_was_called_counter, 3); |
| 570 } |
| 571 |
| 572 // Check that function re-declarations throw if they are read-only. |
| 573 THREADED_TEST(SetterCallbackFunctionDeclarationInterceptorThrow) { |
| 574 v8::HandleScope scope(CcTest::isolate()); |
| 575 LocalContext env; |
| 576 v8::Local<v8::FunctionTemplate> templ = |
| 577 v8::FunctionTemplate::New(CcTest::isolate()); |
| 578 |
| 579 v8::Local<ObjectTemplate> object_template = templ->InstanceTemplate(); |
| 580 object_template->SetHandler( |
| 581 v8::NamedPropertyHandlerConfiguration(nullptr, SetterCallback)); |
| 582 v8::Local<v8::Context> ctx = |
| 583 v8::Context::New(CcTest::isolate(), nullptr, object_template); |
| 584 |
| 585 set_was_called = false; |
| 586 |
| 587 v8::Local<v8::String> code = v8_str( |
| 588 "function x() {return 42;};" |
| 589 "Object.defineProperty(this, 'x', {" |
| 590 "configurable: false, " |
| 591 "writable: false});" |
| 592 "x();"); |
| 593 CHECK_EQ(42, v8::Script::Compile(ctx, code) |
| 594 .ToLocalChecked() |
| 595 ->Run(ctx) |
| 596 .ToLocalChecked() |
| 597 ->Int32Value(ctx) |
| 598 .FromJust()); |
| 599 |
| 600 CHECK_EQ(set_was_called, true); |
| 601 |
| 602 v8::TryCatch try_catch(CcTest::isolate()); |
| 603 set_was_called = false; |
| 604 |
| 605 // Redeclare function that is read-only. |
| 606 code = v8_str("function x() {return 43;};"); |
| 607 CHECK(v8::Script::Compile(ctx, code).ToLocalChecked()->Run(ctx).IsEmpty()); |
| 608 CHECK(try_catch.HasCaught()); |
| 609 |
| 610 CHECK_EQ(set_was_called, false); |
| 611 } |
| 612 |
523 bool get_was_called_in_order = false; | 613 bool get_was_called_in_order = false; |
524 bool define_was_called_in_order = false; | 614 bool define_was_called_in_order = false; |
525 | 615 |
526 namespace { | 616 namespace { |
527 | 617 |
528 void GetterCallbackOrder(Local<Name> property, | 618 void GetterCallbackOrder(Local<Name> property, |
529 const v8::PropertyCallbackInfo<v8::Value>& info) { | 619 const v8::PropertyCallbackInfo<v8::Value>& info) { |
530 get_was_called_in_order = true; | 620 get_was_called_in_order = true; |
531 CHECK_EQ(define_was_called_in_order, true); | 621 CHECK_EQ(define_was_called_in_order, true); |
532 info.GetReturnValue().Set(property); | 622 info.GetReturnValue().Set(property); |
(...skipping 4227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4760 ->Set(env.local(), v8_str("Fun"), | 4850 ->Set(env.local(), v8_str("Fun"), |
4761 fun_templ->GetFunction(env.local()).ToLocalChecked()) | 4851 fun_templ->GetFunction(env.local()).ToLocalChecked()) |
4762 .FromJust()); | 4852 .FromJust()); |
4763 | 4853 |
4764 CompileRun( | 4854 CompileRun( |
4765 "var f = new Fun();" | 4855 "var f = new Fun();" |
4766 "Number.prototype.__proto__ = f;" | 4856 "Number.prototype.__proto__ = f;" |
4767 "var a = 42;" | 4857 "var a = 42;" |
4768 "for (var i = 0; i<3; i++) { a.foo; }"); | 4858 "for (var i = 0; i<3; i++) { a.foo; }"); |
4769 } | 4859 } |
OLD | NEW |