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

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

Issue 22903012: js accessor creation on Template (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 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
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 using ::v8::AccessorInfo; 44 using ::v8::AccessorInfo;
45 using ::v8::Extension; 45 using ::v8::Extension;
46 46
47 static void handle_property(Local<String> name, 47 static void handle_property(Local<String> name,
48 const v8::PropertyCallbackInfo<v8::Value>& info) { 48 const v8::PropertyCallbackInfo<v8::Value>& info) {
49 ApiTestFuzzer::Fuzz(); 49 ApiTestFuzzer::Fuzz();
50 info.GetReturnValue().Set(v8_num(900)); 50 info.GetReturnValue().Set(v8_num(900));
51 } 51 }
52 52
53 53
54 static void handle_property(const v8::FunctionCallbackInfo<v8::Value>& info) {
55 ApiTestFuzzer::Fuzz();
56 CHECK_EQ(0, info.Length());
57 info.GetReturnValue().Set(v8_num(907));
58 }
59
60
54 THREADED_TEST(PropertyHandler) { 61 THREADED_TEST(PropertyHandler) {
55 LocalContext env; 62 LocalContext env;
56 v8::HandleScope scope(env->GetIsolate()); 63 v8::HandleScope scope(env->GetIsolate());
57 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); 64 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New();
58 fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property); 65 fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property);
66 Local<v8::FunctionTemplate> getter_templ =
67 v8::FunctionTemplate::New(handle_property);
68 getter_templ->SetLength(0);
69 fun_templ->InstanceTemplate()->SetJsAccessor(env->GetIsolate(),
70 v8_str("bar"),
71 getter_templ);
59 Local<Function> fun = fun_templ->GetFunction(); 72 Local<Function> fun = fun_templ->GetFunction();
60 env->Global()->Set(v8_str("Fun"), fun); 73 env->Global()->Set(v8_str("Fun"), fun);
61 Local<Script> getter = v8_compile("var obj = new Fun(); obj.foo;"); 74 Local<Script> getter = v8_compile("var obj = new Fun(); obj.foo;");
62 CHECK_EQ(900, getter->Run()->Int32Value()); 75 CHECK_EQ(900, getter->Run()->Int32Value());
63 Local<Script> setter = v8_compile("obj.foo = 901;"); 76 Local<Script> setter = v8_compile("obj.foo = 901;");
64 CHECK_EQ(901, setter->Run()->Int32Value()); 77 CHECK_EQ(901, setter->Run()->Int32Value());
78 getter = v8_compile("obj.bar;");
79 CHECK_EQ(907, getter->Run()->Int32Value());
80 setter = v8_compile("obj.bar = 908;");
81 CHECK_EQ(908, setter->Run()->Int32Value());
65 } 82 }
66 83
67 84
68 static void GetIntValue(Local<String> property, 85 static void GetIntValue(Local<String> property,
69 const v8::PropertyCallbackInfo<v8::Value>& info) { 86 const v8::PropertyCallbackInfo<v8::Value>& info) {
70 ApiTestFuzzer::Fuzz(); 87 ApiTestFuzzer::Fuzz();
71 int* value = 88 int* value =
72 static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value()); 89 static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value());
73 info.GetReturnValue().Set(v8_num(*value)); 90 info.GetReturnValue().Set(v8_num(*value));
74 } 91 }
(...skipping 27 matching lines...) Expand all
102 GetIntValue, 119 GetIntValue,
103 SetIntValue, 120 SetIntValue,
104 v8::External::New(&baz)); 121 v8::External::New(&baz));
105 LocalContext env(0, templ->InstanceTemplate()); 122 LocalContext env(0, templ->InstanceTemplate());
106 v8_compile("foo = (++bar) + baz")->Run(); 123 v8_compile("foo = (++bar) + baz")->Run();
107 CHECK_EQ(bar, -3); 124 CHECK_EQ(bar, -3);
108 CHECK_EQ(foo, 7); 125 CHECK_EQ(foo, 7);
109 } 126 }
110 127
111 128
112 static int x_register = 0; 129 static int x_register[2] = {0, 0};
113 static v8::Handle<v8::Object> x_receiver; 130 static v8::Handle<v8::Object> x_receiver;
114 static v8::Handle<v8::Object> x_holder; 131 static v8::Handle<v8::Object> x_holder;
115 132
133 template<class Info>
134 static void XGetter(const Info& info, int offset) {
135 ApiTestFuzzer::Fuzz();
136 v8::Isolate* isolate = v8::Isolate::GetCurrent();
137 CHECK_EQ(isolate, info.GetIsolate());
138 CHECK_EQ(x_receiver, info.This());
139 info.GetReturnValue().Set(v8_num(x_register[offset]));
140 }
141
116 142
117 static void XGetter(Local<String> name, 143 static void XGetter(Local<String> name,
118 const v8::PropertyCallbackInfo<v8::Value>& info) { 144 const v8::PropertyCallbackInfo<v8::Value>& info) {
119 ApiTestFuzzer::Fuzz(); 145 CHECK_EQ(x_holder, info.Holder());
146 XGetter(info, 0);
147 }
148
149
150 static void XGetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
151 XGetter(info, 1);
152 }
153
154
155 template<class Info>
156 static void XSetter(Local<Value> value, const Info& info, int offset) {
120 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 157 v8::Isolate* isolate = v8::Isolate::GetCurrent();
121 CHECK_EQ(isolate, info.GetIsolate()); 158 CHECK_EQ(isolate, info.GetIsolate());
122 CHECK_EQ(x_receiver, info.This()); 159 CHECK_EQ(x_holder, info.This());
123 CHECK_EQ(x_holder, info.Holder()); 160 x_register[offset] = value->Int32Value();
124 info.GetReturnValue().Set(v8_num(x_register));
125 } 161 }
126 162
127 163
128 static void XSetter(Local<String> name, 164 static void XSetter(Local<String> name,
129 Local<Value> value, 165 Local<Value> value,
130 const v8::PropertyCallbackInfo<void>& info) { 166 const v8::PropertyCallbackInfo<void>& info) {
131 v8::Isolate* isolate = v8::Isolate::GetCurrent();
132 CHECK_EQ(isolate, info.GetIsolate());
133 CHECK_EQ(x_holder, info.This());
134 CHECK_EQ(x_holder, info.Holder()); 167 CHECK_EQ(x_holder, info.Holder());
135 x_register = value->Int32Value(); 168 XSetter(value, info, 0);
136 } 169 }
137 170
138 171
172 static void XSetter(const v8::FunctionCallbackInfo<v8::Value>& info) {
173 CHECK_EQ(1, info.Length());
174 XSetter(info[0], info, 1);
175 }
176
177
139 THREADED_TEST(AccessorIC) { 178 THREADED_TEST(AccessorIC) {
140 LocalContext context; 179 LocalContext context;
141 v8::HandleScope scope(context->GetIsolate()); 180 v8::HandleScope scope(context->GetIsolate());
142 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); 181 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New();
143 obj->SetAccessor(v8_str("x"), XGetter, XSetter); 182 obj->SetAccessor(v8_str("x0"), XGetter, XSetter);
183 obj->SetJsAccessor(context->GetIsolate(),
184 v8_str("x1"),
185 v8::FunctionTemplate::New(XGetter),
186 v8::FunctionTemplate::New(XSetter));
144 x_holder = obj->NewInstance(); 187 x_holder = obj->NewInstance();
145 context->Global()->Set(v8_str("holder"), x_holder); 188 context->Global()->Set(v8_str("holder"), x_holder);
146 x_receiver = v8::Object::New(); 189 x_receiver = v8::Object::New();
147 context->Global()->Set(v8_str("obj"), x_receiver); 190 context->Global()->Set(v8_str("obj"), x_receiver);
148 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(CompileRun( 191 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(CompileRun(
149 "obj.__proto__ = holder;" 192 "obj.__proto__ = holder;"
150 "var result = [];" 193 "var result = [];"
151 "for (var i = 0; i < 10; i++) {" 194 "for (var i = 0; i < 10; i++) {"
152 " holder.x = i;" 195 " holder.x0 = i;"
153 " result.push(obj.x);" 196 " holder.x1 = i;"
197 " result.push(obj.x0);"
198 " result.push(obj.x1);"
154 "}" 199 "}"
155 "result")); 200 "result"));
156 CHECK_EQ(10, array->Length()); 201 CHECK_EQ(20, array->Length());
157 for (int i = 0; i < 10; i++) { 202 for (int i = 0; i < 20; i++) {
158 v8::Handle<Value> entry = array->Get(v8::Integer::New(i)); 203 v8::Handle<Value> entry = array->Get(v8::Integer::New(i));
159 CHECK_EQ(v8::Integer::New(i), entry); 204 CHECK_EQ(v8::Integer::New(i/2), entry);
160 } 205 }
161 } 206 }
162 207
163 208
164 static void AccessorProhibitsOverwritingGetter( 209 static void AccessorProhibitsOverwritingGetter(
165 Local<String> name, 210 Local<String> name,
166 const v8::PropertyCallbackInfo<v8::Value>& info) { 211 const v8::PropertyCallbackInfo<v8::Value>& info) {
167 ApiTestFuzzer::Fuzz(); 212 ApiTestFuzzer::Fuzz();
168 info.GetReturnValue().Set(true); 213 info.GetReturnValue().Set(true);
169 } 214 }
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 LocalContext env; 523 LocalContext env;
479 v8::HandleScope scope(env->GetIsolate()); 524 v8::HandleScope scope(env->GetIsolate());
480 525
481 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); 526 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New();
482 obj->SetNamedPropertyHandler( 527 obj->SetNamedPropertyHandler(
483 JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator); 528 JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator);
484 env->Global()->Set(v8_str("obj"), obj->NewInstance()); 529 env->Global()->Set(v8_str("obj"), obj->NewInstance());
485 v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}"); 530 v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}");
486 CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected)); 531 CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected));
487 } 532 }
OLDNEW
« src/objects-inl.h ('K') | « src/runtime.cc ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698