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

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

Issue 459413002: Support symbol-named properties in API (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Include a bit that was missing from the previous patch Created 6 years, 4 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
« src/api.cc ('K') | « test/cctest/test-alloc.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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 56
57 using ::v8::Boolean; 57 using ::v8::Boolean;
58 using ::v8::BooleanObject; 58 using ::v8::BooleanObject;
59 using ::v8::Context; 59 using ::v8::Context;
60 using ::v8::Extension; 60 using ::v8::Extension;
61 using ::v8::Function; 61 using ::v8::Function;
62 using ::v8::FunctionTemplate; 62 using ::v8::FunctionTemplate;
63 using ::v8::Handle; 63 using ::v8::Handle;
64 using ::v8::HandleScope; 64 using ::v8::HandleScope;
65 using ::v8::Local; 65 using ::v8::Local;
66 using ::v8::Name;
66 using ::v8::Message; 67 using ::v8::Message;
67 using ::v8::MessageCallback; 68 using ::v8::MessageCallback;
68 using ::v8::Object; 69 using ::v8::Object;
69 using ::v8::ObjectTemplate; 70 using ::v8::ObjectTemplate;
70 using ::v8::Persistent; 71 using ::v8::Persistent;
71 using ::v8::Script; 72 using ::v8::Script;
72 using ::v8::StackTrace; 73 using ::v8::StackTrace;
73 using ::v8::String; 74 using ::v8::String;
75 using ::v8::Symbol;
74 using ::v8::TryCatch; 76 using ::v8::TryCatch;
75 using ::v8::Undefined; 77 using ::v8::Undefined;
76 using ::v8::UniqueId; 78 using ::v8::UniqueId;
77 using ::v8::V8; 79 using ::v8::V8;
78 using ::v8::Value; 80 using ::v8::Value;
79 81
80 82
81 #define THREADED_PROFILED_TEST(Name) \ 83 #define THREADED_PROFILED_TEST(Name) \
82 static void Test##Name(); \ 84 static void Test##Name(); \
83 TEST(Name##WithProfiler) { \ 85 TEST(Name##WithProfiler) { \
(...skipping 1787 matching lines...) Expand 10 before | Expand all | Expand 10 after
1871 info.GetReturnValue().Set( 1873 info.GetReturnValue().Set(
1872 self->Get(String::Concat(v8_str("accessor_"), name))); 1874 self->Get(String::Concat(v8_str("accessor_"), name)));
1873 } 1875 }
1874 1876
1875 void SimpleAccessorSetter(Local<String> name, Local<Value> value, 1877 void SimpleAccessorSetter(Local<String> name, Local<Value> value,
1876 const v8::PropertyCallbackInfo<void>& info) { 1878 const v8::PropertyCallbackInfo<void>& info) {
1877 Handle<Object> self = Handle<Object>::Cast(info.This()); 1879 Handle<Object> self = Handle<Object>::Cast(info.This());
1878 self->Set(String::Concat(v8_str("accessor_"), name), value); 1880 self->Set(String::Concat(v8_str("accessor_"), name), value);
1879 } 1881 }
1880 1882
1883 void SymbolAccessorGetter(Local<Name> name,
1884 const v8::PropertyCallbackInfo<v8::Value>& info) {
1885 CHECK(name->IsSymbol());
1886 Local<Symbol> sym = Local<Symbol>::Cast(name);
1887 if (sym->Name()->IsUndefined())
1888 return;
1889 SimpleAccessorGetter(Local<String>::Cast(sym->Name()), info);
1890 }
1891
1892 void SymbolAccessorSetter(Local<Name> name, Local<Value> value,
1893 const v8::PropertyCallbackInfo<void>& info) {
1894 CHECK(name->IsSymbol());
1895 Local<Symbol> sym = Local<Symbol>::Cast(name);
1896 if (sym->Name()->IsUndefined())
1897 return;
1898 SimpleAccessorSetter(Local<String>::Cast(sym->Name()), value, info);
1899 }
1900
1881 void EmptyInterceptorGetter(Local<String> name, 1901 void EmptyInterceptorGetter(Local<String> name,
1882 const v8::PropertyCallbackInfo<v8::Value>& info) { 1902 const v8::PropertyCallbackInfo<v8::Value>& info) {
1883 } 1903 }
1884 1904
1885 void EmptyInterceptorSetter(Local<String> name, 1905 void EmptyInterceptorSetter(Local<String> name,
1886 Local<Value> value, 1906 Local<Value> value,
1887 const v8::PropertyCallbackInfo<v8::Value>& info) { 1907 const v8::PropertyCallbackInfo<v8::Value>& info) {
1888 } 1908 }
1889 1909
1890 void InterceptorGetter(Local<String> name, 1910 void InterceptorGetter(Local<String> name,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1929 templ->PrototypeTemplate()->SetAccessor(name, getter, setter); 1949 templ->PrototypeTemplate()->SetAccessor(name, getter, setter);
1930 } 1950 }
1931 1951
1932 void AddInterceptor(Handle<FunctionTemplate> templ, 1952 void AddInterceptor(Handle<FunctionTemplate> templ,
1933 v8::NamedPropertyGetterCallback getter, 1953 v8::NamedPropertyGetterCallback getter,
1934 v8::NamedPropertySetterCallback setter) { 1954 v8::NamedPropertySetterCallback setter) {
1935 templ->InstanceTemplate()->SetNamedPropertyHandler(getter, setter); 1955 templ->InstanceTemplate()->SetNamedPropertyHandler(getter, setter);
1936 } 1956 }
1937 1957
1938 1958
1959 void AddAccessor(Handle<FunctionTemplate> templ,
1960 Handle<Name> name,
1961 v8::AccessorNameGetterCallback getter,
1962 v8::AccessorNameSetterCallback setter) {
1963 templ->PrototypeTemplate()->SetAccessor(name, getter, setter);
1964 }
1965
1966
1939 THREADED_TEST(EmptyInterceptorDoesNotShadowAccessors) { 1967 THREADED_TEST(EmptyInterceptorDoesNotShadowAccessors) {
1940 v8::HandleScope scope(CcTest::isolate()); 1968 v8::HandleScope scope(CcTest::isolate());
1941 Handle<FunctionTemplate> parent = FunctionTemplate::New(CcTest::isolate()); 1969 Handle<FunctionTemplate> parent = FunctionTemplate::New(CcTest::isolate());
1942 Handle<FunctionTemplate> child = FunctionTemplate::New(CcTest::isolate()); 1970 Handle<FunctionTemplate> child = FunctionTemplate::New(CcTest::isolate());
1943 child->Inherit(parent); 1971 child->Inherit(parent);
1944 AddAccessor(parent, v8_str("age"), 1972 AddAccessor(parent, v8_str("age"),
1945 SimpleAccessorGetter, SimpleAccessorSetter); 1973 SimpleAccessorGetter, SimpleAccessorSetter);
1946 AddInterceptor(child, EmptyInterceptorGetter, EmptyInterceptorSetter); 1974 AddInterceptor(child, EmptyInterceptorGetter, EmptyInterceptorSetter);
1947 LocalContext env; 1975 LocalContext env;
1948 env->Global()->Set(v8_str("Child"), child->GetFunction()); 1976 env->Global()->Set(v8_str("Child"), child->GetFunction());
(...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after
2740 2768
2741 THREADED_TEST(SymbolProperties) { 2769 THREADED_TEST(SymbolProperties) {
2742 LocalContext env; 2770 LocalContext env;
2743 v8::Isolate* isolate = env->GetIsolate(); 2771 v8::Isolate* isolate = env->GetIsolate();
2744 v8::HandleScope scope(isolate); 2772 v8::HandleScope scope(isolate);
2745 2773
2746 v8::Local<v8::Object> obj = v8::Object::New(isolate); 2774 v8::Local<v8::Object> obj = v8::Object::New(isolate);
2747 v8::Local<v8::Symbol> sym1 = v8::Symbol::New(isolate); 2775 v8::Local<v8::Symbol> sym1 = v8::Symbol::New(isolate);
2748 v8::Local<v8::Symbol> sym2 = 2776 v8::Local<v8::Symbol> sym2 =
2749 v8::Symbol::New(isolate, v8_str("my-symbol")); 2777 v8::Symbol::New(isolate, v8_str("my-symbol"));
2778 v8::Local<v8::Symbol> sym3 =
2779 v8::Symbol::New(isolate, v8_str("sym3"));
2750 2780
2751 CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); 2781 CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags);
2752 2782
2753 // Check basic symbol functionality. 2783 // Check basic symbol functionality.
2754 CHECK(sym1->IsSymbol()); 2784 CHECK(sym1->IsSymbol());
2755 CHECK(sym2->IsSymbol()); 2785 CHECK(sym2->IsSymbol());
2756 CHECK(!obj->IsSymbol()); 2786 CHECK(!obj->IsSymbol());
2757 2787
2758 CHECK(sym1->Equals(sym1)); 2788 CHECK(sym1->Equals(sym1));
2759 CHECK(sym2->Equals(sym2)); 2789 CHECK(sym2->Equals(sym2));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2795 2825
2796 CHECK_EQ(0, obj->GetOwnPropertyNames()->Length()); 2826 CHECK_EQ(0, obj->GetOwnPropertyNames()->Length());
2797 int num_props = obj->GetPropertyNames()->Length(); 2827 int num_props = obj->GetPropertyNames()->Length();
2798 CHECK(obj->Set(v8::String::NewFromUtf8(isolate, "bla"), 2828 CHECK(obj->Set(v8::String::NewFromUtf8(isolate, "bla"),
2799 v8::Integer::New(isolate, 20))); 2829 v8::Integer::New(isolate, 20)));
2800 CHECK_EQ(1, obj->GetOwnPropertyNames()->Length()); 2830 CHECK_EQ(1, obj->GetOwnPropertyNames()->Length());
2801 CHECK_EQ(num_props + 1, obj->GetPropertyNames()->Length()); 2831 CHECK_EQ(num_props + 1, obj->GetPropertyNames()->Length());
2802 2832
2803 CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); 2833 CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags);
2804 2834
2835 CHECK(obj->SetAccessor(sym3, SymbolAccessorGetter, SymbolAccessorSetter));
2836 CHECK(obj->Get(sym3)->IsUndefined());
2837 CHECK(obj->Set(sym3, v8::Integer::New(isolate, 42)));
2838 CHECK(obj->Get(sym3)->Equals(v8::Integer::New(isolate, 42)));
2839 CHECK(obj->Get(v8::String::NewFromUtf8(isolate, "accessor_sym3"))->Equals(
2840 v8::Integer::New(isolate, 42)));
2841
2805 // Add another property and delete it afterwards to force the object in 2842 // Add another property and delete it afterwards to force the object in
2806 // slow case. 2843 // slow case.
2807 CHECK(obj->Set(sym2, v8::Integer::New(isolate, 2008))); 2844 CHECK(obj->Set(sym2, v8::Integer::New(isolate, 2008)));
2808 CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); 2845 CHECK_EQ(2002, obj->Get(sym1)->Int32Value());
2809 CHECK_EQ(2008, obj->Get(sym2)->Int32Value()); 2846 CHECK_EQ(2008, obj->Get(sym2)->Int32Value());
2810 CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); 2847 CHECK_EQ(2002, obj->Get(sym1)->Int32Value());
2811 CHECK_EQ(1, obj->GetOwnPropertyNames()->Length()); 2848 CHECK_EQ(2, obj->GetOwnPropertyNames()->Length());
2812 2849
2813 CHECK(obj->Has(sym1)); 2850 CHECK(obj->Has(sym1));
2814 CHECK(obj->Has(sym2)); 2851 CHECK(obj->Has(sym2));
2852 CHECK(obj->Has(sym3));
2853 CHECK(obj->Has(v8::String::NewFromUtf8(isolate, "accessor_sym3")));
2815 CHECK(obj->Delete(sym2)); 2854 CHECK(obj->Delete(sym2));
2816 CHECK(obj->Has(sym1)); 2855 CHECK(obj->Has(sym1));
2817 CHECK(!obj->Has(sym2)); 2856 CHECK(!obj->Has(sym2));
2857 CHECK(obj->Has(sym3));
2858 CHECK(obj->Has(v8::String::NewFromUtf8(isolate, "accessor_sym3")));
2818 CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); 2859 CHECK_EQ(2002, obj->Get(sym1)->Int32Value());
2819 CHECK_EQ(1, obj->GetOwnPropertyNames()->Length()); 2860 CHECK(obj->Get(sym3)->Equals(v8::Integer::New(isolate, 42)));
2861 CHECK(obj->Get(v8::String::NewFromUtf8(isolate, "accessor_sym3"))->Equals(
2862 v8::Integer::New(isolate, 42)));
2863 CHECK_EQ(2, obj->GetOwnPropertyNames()->Length());
2820 2864
2821 // Symbol properties are inherited. 2865 // Symbol properties are inherited.
2822 v8::Local<v8::Object> child = v8::Object::New(isolate); 2866 v8::Local<v8::Object> child = v8::Object::New(isolate);
2823 child->SetPrototype(obj); 2867 child->SetPrototype(obj);
2824 CHECK(child->Has(sym1)); 2868 CHECK(child->Has(sym1));
2825 CHECK_EQ(2002, child->Get(sym1)->Int32Value()); 2869 CHECK_EQ(2002, child->Get(sym1)->Int32Value());
2870 CHECK(obj->Get(sym3)->Equals(v8::Integer::New(isolate, 42)));
2871 CHECK(obj->Get(v8::String::NewFromUtf8(isolate, "accessor_sym3"))->Equals(
2872 v8::Integer::New(isolate, 42)));
2826 CHECK_EQ(0, child->GetOwnPropertyNames()->Length()); 2873 CHECK_EQ(0, child->GetOwnPropertyNames()->Length());
2827 } 2874 }
2828 2875
2829 2876
2830 THREADED_TEST(PrivateProperties) { 2877 THREADED_TEST(PrivateProperties) {
2831 LocalContext env; 2878 LocalContext env;
2832 v8::Isolate* isolate = env->GetIsolate(); 2879 v8::Isolate* isolate = env->GetIsolate();
2833 v8::HandleScope scope(isolate); 2880 v8::HandleScope scope(isolate);
2834 2881
2835 v8::Local<v8::Object> obj = v8::Object::New(isolate); 2882 v8::Local<v8::Object> obj = v8::Object::New(isolate);
(...skipping 20027 matching lines...) Expand 10 before | Expand all | Expand 10 after
22863 desc = x->GetOwnPropertyDescriptor(v8_str("p1")); 22910 desc = x->GetOwnPropertyDescriptor(v8_str("p1"));
22864 Local<Function> set = 22911 Local<Function> set =
22865 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set"))); 22912 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set")));
22866 Local<Function> get = 22913 Local<Function> get =
22867 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get"))); 22914 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get")));
22868 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL)); 22915 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL));
22869 Handle<Value> args[] = { v8_num(14) }; 22916 Handle<Value> args[] = { v8_num(14) };
22870 set->Call(x, 1, args); 22917 set->Call(x, 1, args);
22871 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL)); 22918 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL));
22872 } 22919 }
OLDNEW
« src/api.cc ('K') | « test/cctest/test-alloc.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698