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

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

Issue 88026: Added ForceSet on objects (Closed)
Patch Set: Added docs for ForceSet Created 11 years, 8 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
« src/objects.cc ('K') | « src/runtime.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-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 6220 matching lines...) Expand 10 before | Expand all | Expand 10 after
6231 // Check without 'eval' or 'with'. 6231 // Check without 'eval' or 'with'.
6232 v8::Handle<v8::Value> res = 6232 v8::Handle<v8::Value> res =
6233 CompileRun("function f() { x = 42; return x; }; f()"); 6233 CompileRun("function f() { x = 42; return x; }; f()");
6234 // Check with 'eval'. 6234 // Check with 'eval'.
6235 res = CompileRun("function f() { eval('1'); y = 42; return y; }; f()"); 6235 res = CompileRun("function f() { eval('1'); y = 42; return y; }; f()");
6236 CHECK_EQ(v8::Integer::New(42), res); 6236 CHECK_EQ(v8::Integer::New(42), res);
6237 // Check with 'with'. 6237 // Check with 'with'.
6238 res = CompileRun("function f() { with (this) { y = 42 }; return y; }; f()"); 6238 res = CompileRun("function f() { with (this) { y = 42 }; return y; }; f()");
6239 CHECK_EQ(v8::Integer::New(42), res); 6239 CHECK_EQ(v8::Integer::New(42), res);
6240 } 6240 }
6241
6242 static int force_set_set_count = 0;
6243 static int force_set_get_count = 0;
6244 bool pass_on_get = false;
6245
6246 static v8::Handle<v8::Value> ForceSetGetter(v8::Local<v8::String> name,
6247 const v8::AccessorInfo& info) {
6248 force_set_get_count++;
6249 if (pass_on_get) {
6250 return v8::Handle<v8::Value>();
6251 } else {
6252 return v8::Int32::New(3);
6253 }
6254 }
6255
6256 static void ForceSetSetter(v8::Local<v8::String> name,
6257 v8::Local<v8::Value> value,
6258 const v8::AccessorInfo& info) {
6259 force_set_set_count++;
6260 }
6261
6262 static v8::Handle<v8::Value> ForceSetInterceptSetter(
6263 v8::Local<v8::String> name,
6264 v8::Local<v8::Value> value,
6265 const v8::AccessorInfo& info) {
6266 force_set_set_count++;
6267 return v8::Undefined();
6268 }
6269
6270 TEST(ForceSet) {
6271 force_set_get_count = 0;
6272 force_set_set_count = 0;
6273 pass_on_get = false;
6274
6275 v8::HandleScope scope;
6276 v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New();
6277 v8::Handle<v8::String> access_property = v8::String::New("a");
6278 templ->SetAccessor(access_property, ForceSetGetter, ForceSetSetter);
6279 LocalContext context(NULL, templ);
6280 v8::Handle<v8::Object> global = context->Global();
6281
6282 // Ordinary properties
6283 v8::Handle<v8::String> simple_property = v8::String::New("p");
6284 global->Set(simple_property, v8::Int32::New(4), v8::ReadOnly);
6285 CHECK_EQ(4, global->Get(simple_property)->Int32Value());
6286 // This should fail because the property is read-only
6287 global->Set(simple_property, v8::Int32::New(5));
6288 CHECK_EQ(4, global->Get(simple_property)->Int32Value());
6289 // This should succeed even though the property is read-only
6290 global->ForceSet(simple_property, v8::Int32::New(6));
6291 CHECK_EQ(6, global->Get(simple_property)->Int32Value());
6292
6293 // Accessors
6294 CHECK_EQ(0, force_set_set_count);
6295 CHECK_EQ(0, force_set_get_count);
6296 CHECK_EQ(3, global->Get(access_property)->Int32Value());
6297 // CHECK_EQ the property shouldn't override it, just call the setter
6298 // which in this case does nothing.
6299 global->Set(access_property, v8::Int32::New(7));
6300 CHECK_EQ(3, global->Get(access_property)->Int32Value());
6301 CHECK_EQ(1, force_set_set_count);
6302 CHECK_EQ(2, force_set_get_count);
6303 // Forcing the property to be set should override the accessor without
6304 // calling it
6305 global->ForceSet(access_property, v8::Int32::New(8));
6306 CHECK_EQ(8, global->Get(access_property)->Int32Value());
6307 CHECK_EQ(1, force_set_set_count);
6308 CHECK_EQ(2, force_set_get_count);
6309 }
6310
6311 TEST(ForceSetWithInterceptor) {
6312 force_set_get_count = 0;
6313 force_set_set_count = 0;
6314 pass_on_get = false;
6315
6316 v8::HandleScope scope;
6317 v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New();
6318 templ->SetNamedPropertyHandler(ForceSetGetter, ForceSetInterceptSetter);
6319 LocalContext context(NULL, templ);
6320 v8::Handle<v8::Object> global = context->Global();
6321
6322 v8::Handle<v8::String> some_property = v8::String::New("a");
6323 CHECK_EQ(0, force_set_set_count);
6324 CHECK_EQ(0, force_set_get_count);
6325 CHECK_EQ(3, global->Get(some_property)->Int32Value());
6326 // Setting the property shouldn't override it, just call the setter
6327 // which in this case does nothing.
6328 global->Set(some_property, v8::Int32::New(7));
6329 CHECK_EQ(3, global->Get(some_property)->Int32Value());
6330 CHECK_EQ(1, force_set_set_count);
6331 CHECK_EQ(2, force_set_get_count);
6332 // Getting the property when the interceptor returns an empty handle
6333 // should yield undefined, since the property isn't present on the
6334 // object itself yet.
6335 pass_on_get = true;
6336 CHECK(global->Get(some_property)->IsUndefined());
6337 CHECK_EQ(1, force_set_set_count);
6338 CHECK_EQ(3, force_set_get_count);
6339 // Forcing the property to be set should cause the value to be
6340 // set locally without calling the interceptor.
6341 global->ForceSet(some_property, v8::Int32::New(8));
6342 CHECK_EQ(8, global->Get(some_property)->Int32Value());
6343 CHECK_EQ(1, force_set_set_count);
6344 CHECK_EQ(4, force_set_get_count);
6345 // Reenabling the interceptor should cause it to take precedence over
6346 // the property
6347 pass_on_get = false;
6348 CHECK_EQ(3, global->Get(some_property)->Int32Value());
6349 CHECK_EQ(1, force_set_set_count);
6350 CHECK_EQ(5, force_set_get_count);
6351 // The interceptor should also work for other properties
6352 CHECK_EQ(3, global->Get(v8::String::New("b"))->Int32Value());
6353 CHECK_EQ(1, force_set_set_count);
6354 CHECK_EQ(6, force_set_get_count);
6355 }
OLDNEW
« src/objects.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698