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

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

Issue 2123012: Allow to define accessors on objects. (Closed)
Patch Set: Last round of comments Created 10 years, 7 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
« no previous file with comments | « src/objects.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. 1 // Copyright 2007-2009 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 71
72 static void ExpectBoolean(const char* code, bool expected) { 72 static void ExpectBoolean(const char* code, bool expected) {
73 Local<Value> result = CompileRun(code); 73 Local<Value> result = CompileRun(code);
74 CHECK(result->IsBoolean()); 74 CHECK(result->IsBoolean());
75 CHECK_EQ(expected, result->BooleanValue()); 75 CHECK_EQ(expected, result->BooleanValue());
76 } 76 }
77 77
78 78
79 static void ExpectTrue(const char* code) {
80 ExpectBoolean(code, true);
81 }
82
83
79 static void ExpectObject(const char* code, Local<Value> expected) { 84 static void ExpectObject(const char* code, Local<Value> expected) {
80 Local<Value> result = CompileRun(code); 85 Local<Value> result = CompileRun(code);
81 CHECK(result->Equals(expected)); 86 CHECK(result->Equals(expected));
82 } 87 }
83 88
84 89
85 static int signature_callback_count; 90 static int signature_callback_count;
86 static v8::Handle<Value> IncrementingSignatureCallback( 91 static v8::Handle<Value> IncrementingSignatureCallback(
87 const v8::Arguments& args) { 92 const v8::Arguments& args) {
88 ApiTestFuzzer::Fuzz(); 93 ApiTestFuzzer::Fuzz();
(...skipping 2410 matching lines...) Expand 10 before | Expand all | Expand 10 after
2499 2504
2500 THREADED_TEST(DefinePropertyOnAPIAccessor) { 2505 THREADED_TEST(DefinePropertyOnAPIAccessor) {
2501 v8::HandleScope scope; 2506 v8::HandleScope scope;
2502 Local<ObjectTemplate> templ = ObjectTemplate::New(); 2507 Local<ObjectTemplate> templ = ObjectTemplate::New();
2503 templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); 2508 templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"));
2504 LocalContext context; 2509 LocalContext context;
2505 context->Global()->Set(v8_str("obj"), templ->NewInstance()); 2510 context->Global()->Set(v8_str("obj"), templ->NewInstance());
2506 2511
2507 // Uses getOwnPropertyDescriptor to check the configurable status 2512 // Uses getOwnPropertyDescriptor to check the configurable status
2508 Local<Script> script_desc 2513 Local<Script> script_desc
2509 = Script::Compile(v8_str("var prop =Object.getOwnPropertyDescriptor( " 2514 = Script::Compile(v8_str("var prop = Object.getOwnPropertyDescriptor( "
2510 "obj, 'x');" 2515 "obj, 'x');"
2511 "prop.configurable;")); 2516 "prop.configurable;"));
2512 Local<Value> result = script_desc->Run(); 2517 Local<Value> result = script_desc->Run();
2513 CHECK_EQ(result->BooleanValue(), true); 2518 CHECK_EQ(result->BooleanValue(), true);
2514 2519
2515 // Redefine get - but still configurable 2520 // Redefine get - but still configurable
2516 Local<Script> script_define 2521 Local<Script> script_define
2517 = Script::Compile(v8_str("var desc = { get: function(){return 42; }," 2522 = Script::Compile(v8_str("var desc = { get: function(){return 42; },"
2518 " configurable: true };" 2523 " configurable: true };"
2519 "Object.defineProperty(obj, 'x', desc);" 2524 "Object.defineProperty(obj, 'x', desc);"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
2585 2590
2586 v8::TryCatch try_catch; 2591 v8::TryCatch try_catch;
2587 result = script_define->Run(); 2592 result = script_define->Run();
2588 CHECK(try_catch.HasCaught()); 2593 CHECK(try_catch.HasCaught());
2589 String::AsciiValue exception_value(try_catch.Exception()); 2594 String::AsciiValue exception_value(try_catch.Exception());
2590 CHECK_EQ(*exception_value, 2595 CHECK_EQ(*exception_value,
2591 "TypeError: Cannot redefine property: defineProperty"); 2596 "TypeError: Cannot redefine property: defineProperty");
2592 } 2597 }
2593 2598
2594 2599
2600 static v8::Handle<v8::Object> GetGlobalProperty(LocalContext* context,
2601 char const* name) {
2602 return v8::Handle<v8::Object>::Cast((*context)->Global()->Get(v8_str(name)));
2603 }
2595 2604
2596 2605
2606 THREADED_TEST(DefineAPIAccessorOnObject) {
2607 v8::HandleScope scope;
2608 Local<ObjectTemplate> templ = ObjectTemplate::New();
2609 LocalContext context;
2610
2611 context->Global()->Set(v8_str("obj1"), templ->NewInstance());
2612 CompileRun("var obj2 = {};");
2613
2614 CHECK(CompileRun("obj1.x")->IsUndefined());
2615 CHECK(CompileRun("obj2.x")->IsUndefined());
2616
2617 CHECK(GetGlobalProperty(&context, "obj1")->
2618 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2619
2620 ExpectString("obj1.x", "x");
2621 CHECK(CompileRun("obj2.x")->IsUndefined());
2622
2623 CHECK(GetGlobalProperty(&context, "obj2")->
2624 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2625
2626 ExpectString("obj1.x", "x");
2627 ExpectString("obj2.x", "x");
2628
2629 ExpectTrue("Object.getOwnPropertyDescriptor(obj1, 'x').configurable");
2630 ExpectTrue("Object.getOwnPropertyDescriptor(obj2, 'x').configurable");
2631
2632 CompileRun("Object.defineProperty(obj1, 'x',"
2633 "{ get: function() { return 'y'; }, configurable: true })");
2634
2635 ExpectString("obj1.x", "y");
2636 ExpectString("obj2.x", "x");
2637
2638 CompileRun("Object.defineProperty(obj2, 'x',"
2639 "{ get: function() { return 'y'; }, configurable: true })");
2640
2641 ExpectString("obj1.x", "y");
2642 ExpectString("obj2.x", "y");
2643
2644 ExpectTrue("Object.getOwnPropertyDescriptor(obj1, 'x').configurable");
2645 ExpectTrue("Object.getOwnPropertyDescriptor(obj2, 'x').configurable");
2646
2647 CHECK(GetGlobalProperty(&context, "obj1")->
2648 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2649 CHECK(GetGlobalProperty(&context, "obj2")->
2650 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2651
2652 ExpectString("obj1.x", "x");
2653 ExpectString("obj2.x", "x");
2654
2655 ExpectTrue("Object.getOwnPropertyDescriptor(obj1, 'x').configurable");
2656 ExpectTrue("Object.getOwnPropertyDescriptor(obj2, 'x').configurable");
2657
2658 // Define getters/setters, but now make them not configurable.
2659 CompileRun("Object.defineProperty(obj1, 'x',"
2660 "{ get: function() { return 'z'; }, configurable: false })");
2661 CompileRun("Object.defineProperty(obj2, 'x',"
2662 "{ get: function() { return 'z'; }, configurable: false })");
2663
2664 ExpectTrue("!Object.getOwnPropertyDescriptor(obj1, 'x').configurable");
2665 ExpectTrue("!Object.getOwnPropertyDescriptor(obj2, 'x').configurable");
2666
2667 ExpectString("obj1.x", "z");
2668 ExpectString("obj2.x", "z");
2669
2670 CHECK(!GetGlobalProperty(&context, "obj1")->
2671 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2672 CHECK(!GetGlobalProperty(&context, "obj2")->
2673 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2674
2675 ExpectString("obj1.x", "z");
2676 ExpectString("obj2.x", "z");
2677 }
2678
2679
2680 THREADED_TEST(DontDeleteAPIAccessorsCannotBeOverriden) {
2681 v8::HandleScope scope;
2682 Local<ObjectTemplate> templ = ObjectTemplate::New();
2683 LocalContext context;
2684
2685 context->Global()->Set(v8_str("obj1"), templ->NewInstance());
2686 CompileRun("var obj2 = {};");
2687
2688 CHECK(GetGlobalProperty(&context, "obj1")->SetAccessor(
2689 v8_str("x"),
2690 GetXValue, NULL,
2691 v8_str("donut"), v8::DEFAULT, v8::DontDelete));
2692 CHECK(GetGlobalProperty(&context, "obj2")->SetAccessor(
2693 v8_str("x"),
2694 GetXValue, NULL,
2695 v8_str("donut"), v8::DEFAULT, v8::DontDelete));
2696
2697 ExpectString("obj1.x", "x");
2698 ExpectString("obj2.x", "x");
2699
2700 ExpectTrue("!Object.getOwnPropertyDescriptor(obj1, 'x').configurable");
2701 ExpectTrue("!Object.getOwnPropertyDescriptor(obj2, 'x').configurable");
2702
2703 CHECK(!GetGlobalProperty(&context, "obj1")->
2704 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2705 CHECK(!GetGlobalProperty(&context, "obj2")->
2706 SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")));
2707
2708 {
2709 v8::TryCatch try_catch;
2710 CompileRun("Object.defineProperty(obj1, 'x',"
2711 "{get: function() { return 'func'; }})");
2712 CHECK(try_catch.HasCaught());
2713 String::AsciiValue exception_value(try_catch.Exception());
2714 CHECK_EQ(*exception_value,
2715 "TypeError: Cannot redefine property: defineProperty");
2716 }
2717 {
2718 v8::TryCatch try_catch;
2719 CompileRun("Object.defineProperty(obj2, 'x',"
2720 "{get: function() { return 'func'; }})");
2721 CHECK(try_catch.HasCaught());
2722 String::AsciiValue exception_value(try_catch.Exception());
2723 CHECK_EQ(*exception_value,
2724 "TypeError: Cannot redefine property: defineProperty");
2725 }
2726 }
2727
2728
2729 static v8::Handle<Value> Get239Value(Local<String> name,
2730 const AccessorInfo& info) {
2731 ApiTestFuzzer::Fuzz();
2732 CHECK_EQ(info.Data(), v8_str("donut"));
2733 CHECK_EQ(name, v8_str("239"));
2734 return name;
2735 }
2736
2737
2738 THREADED_TEST(ElementAPIAccessor) {
2739 v8::HandleScope scope;
2740 Local<ObjectTemplate> templ = ObjectTemplate::New();
2741 LocalContext context;
2742
2743 context->Global()->Set(v8_str("obj1"), templ->NewInstance());
2744 CompileRun("var obj2 = {};");
2745
2746 CHECK(GetGlobalProperty(&context, "obj1")->SetAccessor(
2747 v8_str("239"),
2748 Get239Value, NULL,
2749 v8_str("donut")));
2750 CHECK(GetGlobalProperty(&context, "obj2")->SetAccessor(
2751 v8_str("239"),
2752 Get239Value, NULL,
2753 v8_str("donut")));
2754
2755 ExpectString("obj1[239]", "239");
2756 ExpectString("obj2[239]", "239");
2757 ExpectString("obj1['239']", "239");
2758 ExpectString("obj2['239']", "239");
2759 }
2760
2597 2761
2598 v8::Persistent<Value> xValue; 2762 v8::Persistent<Value> xValue;
2599 2763
2600 2764
2601 static void SetXValue(Local<String> name, 2765 static void SetXValue(Local<String> name,
2602 Local<Value> value, 2766 Local<Value> value,
2603 const AccessorInfo& info) { 2767 const AccessorInfo& info) {
2604 CHECK_EQ(value, v8_num(4)); 2768 CHECK_EQ(value, v8_num(4));
2605 CHECK_EQ(info.Data(), v8_str("donut")); 2769 CHECK_EQ(info.Data(), v8_str("donut"));
2606 CHECK_EQ(name, v8_str("x")); 2770 CHECK_EQ(name, v8_str("x"));
(...skipping 7772 matching lines...) Expand 10 before | Expand all | Expand 10 after
10379 const char* code = 10543 const char* code =
10380 "(function() {" 10544 "(function() {"
10381 " for (var i = 0; i < 2*16; i++) {" 10545 " for (var i = 0; i < 2*16; i++) {"
10382 " %_GetFromCache(0, 'a' + i);" 10546 " %_GetFromCache(0, 'a' + i);"
10383 " };" 10547 " };"
10384 " return 'PASSED';" 10548 " return 'PASSED';"
10385 "})()"; 10549 "})()";
10386 v8::internal::Heap::ClearJSFunctionResultCaches(); 10550 v8::internal::Heap::ClearJSFunctionResultCaches();
10387 ExpectString(code, "PASSED"); 10551 ExpectString(code, "PASSED");
10388 } 10552 }
OLDNEW
« no previous file with comments | « src/objects.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698