OLD | NEW |
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 2279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2290 templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); | 2290 templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); |
2291 LocalContext context; | 2291 LocalContext context; |
2292 context->Global()->Set(v8_str("obj"), templ->NewInstance()); | 2292 context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
2293 Local<Script> script = Script::Compile(v8_str("obj.x")); | 2293 Local<Script> script = Script::Compile(v8_str("obj.x")); |
2294 for (int i = 0; i < 10; i++) { | 2294 for (int i = 0; i < 10; i++) { |
2295 Local<Value> result = script->Run(); | 2295 Local<Value> result = script->Run(); |
2296 CHECK_EQ(result, v8_str("x")); | 2296 CHECK_EQ(result, v8_str("x")); |
2297 } | 2297 } |
2298 } | 2298 } |
2299 | 2299 |
| 2300 THREADED_TEST(DefinePropertyOnAPIAccessor) { |
| 2301 v8::HandleScope scope; |
| 2302 Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 2303 templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); |
| 2304 LocalContext context; |
| 2305 context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 2306 |
| 2307 // Uses getOwnPropertyDescriptor to check the configurable status |
| 2308 Local<Script> script_desc |
| 2309 = Script::Compile(v8_str("var prop =Object.getOwnPropertyDescriptor( " |
| 2310 "obj, 'x');" |
| 2311 "prop.configurable;")); |
| 2312 Local<Value> result = script_desc->Run(); |
| 2313 CHECK_EQ(result->BooleanValue(), true); |
| 2314 |
| 2315 // Redefine get - but still configurable |
| 2316 Local<Script> script_define |
| 2317 = Script::Compile(v8_str("var desc = { get: function(){return 42; }," |
| 2318 " configurable: true };" |
| 2319 "Object.defineProperty(obj, 'x', desc);" |
| 2320 "obj.x")); |
| 2321 result = script_define->Run(); |
| 2322 CHECK_EQ(result, v8_num(42)); |
| 2323 |
| 2324 // Check that the accessor is still configurable |
| 2325 result = script_desc->Run(); |
| 2326 CHECK_EQ(result->BooleanValue(), true); |
| 2327 |
| 2328 // Redefine to a non-configurable |
| 2329 script_define |
| 2330 = Script::Compile(v8_str("var desc = { get: function(){return 43; }," |
| 2331 " configurable: false };" |
| 2332 "Object.defineProperty(obj, 'x', desc);" |
| 2333 "obj.x")); |
| 2334 result = script_define->Run(); |
| 2335 CHECK_EQ(result, v8_num(43)); |
| 2336 result = script_desc->Run(); |
| 2337 CHECK_EQ(result->BooleanValue(), false); |
| 2338 |
| 2339 // Make sure that it is not possible to redefine again |
| 2340 v8::TryCatch try_catch; |
| 2341 result = script_define->Run(); |
| 2342 CHECK(try_catch.HasCaught()); |
| 2343 String::AsciiValue exception_value(try_catch.Exception()); |
| 2344 CHECK_EQ(*exception_value, |
| 2345 "TypeError: Cannot redefine property: defineProperty"); |
| 2346 } |
| 2347 |
| 2348 THREADED_TEST(DefinePropertyOnDefineGetterSetter) { |
| 2349 v8::HandleScope scope; |
| 2350 Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 2351 templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); |
| 2352 LocalContext context; |
| 2353 context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 2354 |
| 2355 Local<Script> script_desc = Script::Compile(v8_str("var prop =" |
| 2356 "Object.getOwnPropertyDescriptor( " |
| 2357 "obj, 'x');" |
| 2358 "prop.configurable;")); |
| 2359 Local<Value> result = script_desc->Run(); |
| 2360 CHECK_EQ(result->BooleanValue(), true); |
| 2361 |
| 2362 Local<Script> script_define = |
| 2363 Script::Compile(v8_str("var desc = {get: function(){return 42; }," |
| 2364 " configurable: true };" |
| 2365 "Object.defineProperty(obj, 'x', desc);" |
| 2366 "obj.x")); |
| 2367 result = script_define->Run(); |
| 2368 CHECK_EQ(result, v8_num(42)); |
| 2369 |
| 2370 |
| 2371 result = script_desc->Run(); |
| 2372 CHECK_EQ(result->BooleanValue(), true); |
| 2373 |
| 2374 |
| 2375 script_define = |
| 2376 Script::Compile(v8_str("var desc = {get: function(){return 43; }," |
| 2377 " configurable: false };" |
| 2378 "Object.defineProperty(obj, 'x', desc);" |
| 2379 "obj.x")); |
| 2380 result = script_define->Run(); |
| 2381 CHECK_EQ(result, v8_num(43)); |
| 2382 result = script_desc->Run(); |
| 2383 |
| 2384 CHECK_EQ(result->BooleanValue(), false); |
| 2385 |
| 2386 v8::TryCatch try_catch; |
| 2387 result = script_define->Run(); |
| 2388 CHECK(try_catch.HasCaught()); |
| 2389 String::AsciiValue exception_value(try_catch.Exception()); |
| 2390 CHECK_EQ(*exception_value, |
| 2391 "TypeError: Cannot redefine property: defineProperty"); |
| 2392 } |
| 2393 |
| 2394 |
| 2395 |
| 2396 |
2300 | 2397 |
2301 v8::Persistent<Value> xValue; | 2398 v8::Persistent<Value> xValue; |
2302 | 2399 |
2303 | 2400 |
2304 static void SetXValue(Local<String> name, | 2401 static void SetXValue(Local<String> name, |
2305 Local<Value> value, | 2402 Local<Value> value, |
2306 const AccessorInfo& info) { | 2403 const AccessorInfo& info) { |
2307 CHECK_EQ(value, v8_num(4)); | 2404 CHECK_EQ(value, v8_num(4)); |
2308 CHECK_EQ(info.Data(), v8_str("donut")); | 2405 CHECK_EQ(info.Data(), v8_str("donut")); |
2309 CHECK_EQ(name, v8_str("x")); | 2406 CHECK_EQ(name, v8_str("x")); |
(...skipping 6512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8822 CompileRun(source_exception); | 8919 CompileRun(source_exception); |
8823 other_context->Exit(); | 8920 other_context->Exit(); |
8824 v8::internal::Heap::CollectAllGarbage(false); | 8921 v8::internal::Heap::CollectAllGarbage(false); |
8825 if (GetGlobalObjectsCount() == 1) break; | 8922 if (GetGlobalObjectsCount() == 1) break; |
8826 } | 8923 } |
8827 CHECK_GE(2, gc_count); | 8924 CHECK_GE(2, gc_count); |
8828 CHECK_EQ(1, GetGlobalObjectsCount()); | 8925 CHECK_EQ(1, GetGlobalObjectsCount()); |
8829 | 8926 |
8830 other_context.Dispose(); | 8927 other_context.Dispose(); |
8831 } | 8928 } |
OLD | NEW |