| 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 2350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2361 | 2361 |
| 2362 | 2362 |
| 2363 static void check_reference_error_message( | 2363 static void check_reference_error_message( |
| 2364 v8::Handle<v8::Message> message, | 2364 v8::Handle<v8::Message> message, |
| 2365 v8::Handle<v8::Value> data) { | 2365 v8::Handle<v8::Value> data) { |
| 2366 const char* reference_error = "Uncaught ReferenceError: asdf is not defined"; | 2366 const char* reference_error = "Uncaught ReferenceError: asdf is not defined"; |
| 2367 CHECK(message->Get()->Equals(v8_str(reference_error))); | 2367 CHECK(message->Get()->Equals(v8_str(reference_error))); |
| 2368 } | 2368 } |
| 2369 | 2369 |
| 2370 | 2370 |
| 2371 // Test that overwritten toString methods are not invoked on uncaught | 2371 static v8::Handle<Value> Fail(const v8::Arguments& args) { |
| 2372 // exception formatting. However, they are invoked when performing | 2372 ApiTestFuzzer::Fuzz(); |
| 2373 // normal error string conversions. | 2373 CHECK(false); |
| 2374 return v8::Undefined(); |
| 2375 } |
| 2376 |
| 2377 |
| 2378 // Test that overwritten methods are not invoked on uncaught exception |
| 2379 // formatting. However, they are invoked when performing normal error |
| 2380 // string conversions. |
| 2374 TEST(APIThrowMessageOverwrittenToString) { | 2381 TEST(APIThrowMessageOverwrittenToString) { |
| 2375 v8::HandleScope scope; | 2382 v8::HandleScope scope; |
| 2376 v8::V8::AddMessageListener(check_reference_error_message); | 2383 v8::V8::AddMessageListener(check_reference_error_message); |
| 2377 LocalContext context; | 2384 Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 2385 templ->Set(v8_str("fail"), v8::FunctionTemplate::New(Fail)); |
| 2386 LocalContext context(NULL, templ); |
| 2387 CompileRun("asdf;"); |
| 2388 CompileRun("var limit = {};" |
| 2389 "limit.valueOf = fail;" |
| 2390 "Error.stackTraceLimit = limit;"); |
| 2391 CompileRun("asdf"); |
| 2392 CompileRun("Array.prototype.pop = fail;"); |
| 2393 CompileRun("Object.prototype.hasOwnProperty = fail;"); |
| 2394 CompileRun("Object.prototype.toString = function f() { return 'Yikes'; }"); |
| 2395 CompileRun("Number.prototype.toString = function f() { return 'Yikes'; }"); |
| 2396 CompileRun("String.prototype.toString = function f() { return 'Yikes'; }"); |
| 2378 CompileRun("ReferenceError.prototype.toString =" | 2397 CompileRun("ReferenceError.prototype.toString =" |
| 2379 " function() { return 'Whoops' }"); | 2398 " function() { return 'Whoops' }"); |
| 2380 CompileRun("asdf;"); | 2399 CompileRun("asdf;"); |
| 2381 CompileRun("ReferenceError.prototype.constructor.name = void 0;"); | 2400 CompileRun("ReferenceError.prototype.constructor.name = void 0;"); |
| 2382 CompileRun("asdf;"); | 2401 CompileRun("asdf;"); |
| 2383 CompileRun("ReferenceError.prototype.constructor = void 0;"); | 2402 CompileRun("ReferenceError.prototype.constructor = void 0;"); |
| 2384 CompileRun("asdf;"); | 2403 CompileRun("asdf;"); |
| 2385 CompileRun("ReferenceError.prototype.__proto__ = new Object();"); | 2404 CompileRun("ReferenceError.prototype.__proto__ = new Object();"); |
| 2386 CompileRun("asdf;"); | 2405 CompileRun("asdf;"); |
| 2387 CompileRun("ReferenceError.prototype = new Object();"); | 2406 CompileRun("ReferenceError.prototype = new Object();"); |
| (...skipping 2894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5282 // the global object for env2 which has the same security token as env1. | 5301 // the global object for env2 which has the same security token as env1. |
| 5283 result = CompileRun("other.p"); | 5302 result = CompileRun("other.p"); |
| 5284 CHECK(result->IsInt32()); | 5303 CHECK(result->IsInt32()); |
| 5285 CHECK_EQ(42, result->Int32Value()); | 5304 CHECK_EQ(42, result->Int32Value()); |
| 5286 | 5305 |
| 5287 env2.Dispose(); | 5306 env2.Dispose(); |
| 5288 env3.Dispose(); | 5307 env3.Dispose(); |
| 5289 } | 5308 } |
| 5290 | 5309 |
| 5291 | 5310 |
| 5311 static bool allowed_access_type[v8::ACCESS_KEYS] = { false }; |
| 5292 static bool NamedAccessBlocker(Local<v8::Object> global, | 5312 static bool NamedAccessBlocker(Local<v8::Object> global, |
| 5293 Local<Value> name, | 5313 Local<Value> name, |
| 5294 v8::AccessType type, | 5314 v8::AccessType type, |
| 5295 Local<Value> data) { | 5315 Local<Value> data) { |
| 5296 return Context::GetCurrent()->Global()->Equals(global); | 5316 return Context::GetCurrent()->Global()->Equals(global) || |
| 5317 allowed_access_type[type]; |
| 5297 } | 5318 } |
| 5298 | 5319 |
| 5299 | 5320 |
| 5300 static bool IndexedAccessBlocker(Local<v8::Object> global, | 5321 static bool IndexedAccessBlocker(Local<v8::Object> global, |
| 5301 uint32_t key, | 5322 uint32_t key, |
| 5302 v8::AccessType type, | 5323 v8::AccessType type, |
| 5303 Local<Value> data) { | 5324 Local<Value> data) { |
| 5304 return Context::GetCurrent()->Global()->Equals(global); | 5325 return Context::GetCurrent()->Global()->Equals(global); |
| 5305 } | 5326 } |
| 5306 | 5327 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 5326 return v8::Undefined(); | 5347 return v8::Undefined(); |
| 5327 } | 5348 } |
| 5328 | 5349 |
| 5329 | 5350 |
| 5330 static void UnreachableSetter(Local<String>, Local<Value>, | 5351 static void UnreachableSetter(Local<String>, Local<Value>, |
| 5331 const AccessorInfo&) { | 5352 const AccessorInfo&) { |
| 5332 CHECK(false); // This function should nto be called. | 5353 CHECK(false); // This function should nto be called. |
| 5333 } | 5354 } |
| 5334 | 5355 |
| 5335 | 5356 |
| 5336 THREADED_TEST(AccessControl) { | 5357 TEST(AccessControl) { |
| 5337 v8::HandleScope handle_scope; | 5358 v8::HandleScope handle_scope; |
| 5338 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); | 5359 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 5339 | 5360 |
| 5340 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, | 5361 global_template->SetAccessCheckCallbacks(NamedAccessBlocker, |
| 5341 IndexedAccessBlocker); | 5362 IndexedAccessBlocker); |
| 5342 | 5363 |
| 5343 // Add an accessor accessible by cross-domain JS code. | 5364 // Add an accessor accessible by cross-domain JS code. |
| 5344 global_template->SetAccessor( | 5365 global_template->SetAccessor( |
| 5345 v8_str("accessible_prop"), | 5366 v8_str("accessible_prop"), |
| 5346 EchoGetter, EchoSetter, | 5367 EchoGetter, EchoSetter, |
| 5347 v8::Handle<Value>(), | 5368 v8::Handle<Value>(), |
| 5348 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); | 5369 v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); |
| 5349 | 5370 |
| 5350 // Add an accessor that is not accessible by cross-domain JS code. | 5371 // Add an accessor that is not accessible by cross-domain JS code. |
| 5351 global_template->SetAccessor(v8_str("blocked_prop"), | 5372 global_template->SetAccessor(v8_str("blocked_prop"), |
| 5352 UnreachableGetter, UnreachableSetter, | 5373 UnreachableGetter, UnreachableSetter, |
| 5353 v8::Handle<Value>(), | 5374 v8::Handle<Value>(), |
| 5354 v8::DEFAULT); | 5375 v8::DEFAULT); |
| 5355 | 5376 |
| 5356 // Create an environment | 5377 // Create an environment |
| 5357 v8::Persistent<Context> context0 = Context::New(NULL, global_template); | 5378 v8::Persistent<Context> context0 = Context::New(NULL, global_template); |
| 5358 context0->Enter(); | 5379 context0->Enter(); |
| 5359 | 5380 |
| 5360 v8::Handle<v8::Object> global0 = context0->Global(); | 5381 v8::Handle<v8::Object> global0 = context0->Global(); |
| 5361 | 5382 |
| 5383 // Define a property with JS getter and setter. |
| 5384 CompileRun( |
| 5385 "function getter() { return 'getter'; };\n" |
| 5386 "function setter() { return 'setter'; }\n" |
| 5387 "Object.defineProperty(this, 'js_accessor_p', {get:getter, set:setter})"); |
| 5388 |
| 5389 Local<Value> getter = global0->Get(v8_str("getter")); |
| 5390 Local<Value> setter = global0->Get(v8_str("setter")); |
| 5391 |
| 5362 v8::HandleScope scope1; | 5392 v8::HandleScope scope1; |
| 5363 | 5393 |
| 5364 v8::Persistent<Context> context1 = Context::New(); | 5394 v8::Persistent<Context> context1 = Context::New(); |
| 5365 context1->Enter(); | 5395 context1->Enter(); |
| 5366 | 5396 |
| 5367 v8::Handle<v8::Object> global1 = context1->Global(); | 5397 v8::Handle<v8::Object> global1 = context1->Global(); |
| 5368 global1->Set(v8_str("other"), global0); | 5398 global1->Set(v8_str("other"), global0); |
| 5369 | 5399 |
| 5400 // Access blocked property |
| 5401 CompileRun("other.blocked_prop = 1"); |
| 5402 |
| 5403 ExpectUndefined("other.blocked_prop"); |
| 5404 ExpectUndefined( |
| 5405 "Object.getOwnPropertyDescriptor(other, 'blocked_prop')"); |
| 5406 ExpectFalse("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 5407 |
| 5408 // Enable ACCESS_HAS |
| 5409 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5410 ExpectUndefined("other.blocked_prop"); |
| 5411 // ... and now we can get the descriptor... |
| 5412 ExpectUndefined( |
| 5413 "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); |
| 5414 // ... and enumerate the property. |
| 5415 ExpectTrue("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 5416 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5417 |
| 5418 CompileRun("other.js_accessor_p = 2"); |
| 5419 |
| 5420 ExpectUndefined("other.js_accessor_p"); |
| 5421 ExpectUndefined( |
| 5422 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p')"); |
| 5423 |
| 5424 // Enable ACCESS_HAS. |
| 5425 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5426 ExpectUndefined("other.js_accessor_p"); |
| 5427 ExpectUndefined( |
| 5428 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 5429 ExpectUndefined( |
| 5430 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 5431 ExpectUndefined( |
| 5432 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5433 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5434 |
| 5435 // Enable both ACCESS_HAS and ACCESS_GET. |
| 5436 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5437 allowed_access_type[v8::ACCESS_GET] = true; |
| 5438 |
| 5439 ExpectString("other.js_accessor_p", "getter"); |
| 5440 ExpectObject( |
| 5441 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 5442 ExpectUndefined( |
| 5443 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 5444 ExpectUndefined( |
| 5445 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5446 |
| 5447 allowed_access_type[v8::ACCESS_GET] = false; |
| 5448 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5449 |
| 5450 // Enable both ACCESS_HAS and ACCESS_SET. |
| 5451 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5452 allowed_access_type[v8::ACCESS_SET] = true; |
| 5453 |
| 5454 ExpectUndefined("other.js_accessor_p"); |
| 5455 ExpectUndefined( |
| 5456 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 5457 ExpectObject( |
| 5458 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 5459 ExpectUndefined( |
| 5460 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5461 |
| 5462 allowed_access_type[v8::ACCESS_SET] = false; |
| 5463 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5464 |
| 5465 // Enable both ACCESS_HAS, ACCESS_GET and ACCESS_SET. |
| 5466 allowed_access_type[v8::ACCESS_HAS] = true; |
| 5467 allowed_access_type[v8::ACCESS_GET] = true; |
| 5468 allowed_access_type[v8::ACCESS_SET] = true; |
| 5469 |
| 5470 ExpectString("other.js_accessor_p", "getter"); |
| 5471 ExpectObject( |
| 5472 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 5473 ExpectObject( |
| 5474 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 5475 ExpectUndefined( |
| 5476 "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 5477 |
| 5478 allowed_access_type[v8::ACCESS_SET] = false; |
| 5479 allowed_access_type[v8::ACCESS_GET] = false; |
| 5480 allowed_access_type[v8::ACCESS_HAS] = false; |
| 5481 |
| 5370 v8::Handle<Value> value; | 5482 v8::Handle<Value> value; |
| 5371 | 5483 |
| 5372 // Access blocked property | |
| 5373 value = v8_compile("other.blocked_prop = 1")->Run(); | |
| 5374 value = v8_compile("other.blocked_prop")->Run(); | |
| 5375 CHECK(value->IsUndefined()); | |
| 5376 | |
| 5377 value = v8_compile("propertyIsEnumerable.call(other, 'blocked_prop')")->Run(); | |
| 5378 CHECK(value->IsFalse()); | |
| 5379 | |
| 5380 // Access accessible property | 5484 // Access accessible property |
| 5381 value = v8_compile("other.accessible_prop = 3")->Run(); | 5485 value = CompileRun("other.accessible_prop = 3"); |
| 5382 CHECK(value->IsNumber()); | 5486 CHECK(value->IsNumber()); |
| 5383 CHECK_EQ(3, value->Int32Value()); | 5487 CHECK_EQ(3, value->Int32Value()); |
| 5384 CHECK_EQ(3, g_echo_value); | 5488 CHECK_EQ(3, g_echo_value); |
| 5385 | 5489 |
| 5386 value = v8_compile("other.accessible_prop")->Run(); | 5490 value = CompileRun("other.accessible_prop"); |
| 5387 CHECK(value->IsNumber()); | 5491 CHECK(value->IsNumber()); |
| 5388 CHECK_EQ(3, value->Int32Value()); | 5492 CHECK_EQ(3, value->Int32Value()); |
| 5389 | 5493 |
| 5390 value = | 5494 value = CompileRun( |
| 5391 v8_compile("propertyIsEnumerable.call(other, 'accessible_prop')")->Run(); | 5495 "Object.getOwnPropertyDescriptor(other, 'accessible_prop').value"); |
| 5496 CHECK(value->IsNumber()); |
| 5497 CHECK_EQ(3, value->Int32Value()); |
| 5498 |
| 5499 value = CompileRun("propertyIsEnumerable.call(other, 'accessible_prop')"); |
| 5392 CHECK(value->IsTrue()); | 5500 CHECK(value->IsTrue()); |
| 5393 | 5501 |
| 5394 // Enumeration doesn't enumerate accessors from inaccessible objects in | 5502 // Enumeration doesn't enumerate accessors from inaccessible objects in |
| 5395 // the prototype chain even if the accessors are in themselves accessible. | 5503 // the prototype chain even if the accessors are in themselves accessible. |
| 5396 Local<Value> result = | 5504 value = |
| 5397 CompileRun("(function(){var obj = {'__proto__':other};" | 5505 CompileRun("(function(){var obj = {'__proto__':other};" |
| 5398 "for (var p in obj)" | 5506 "for (var p in obj)" |
| 5399 " if (p == 'accessible_prop' || p == 'blocked_prop') {" | 5507 " if (p == 'accessible_prop' || p == 'blocked_prop') {" |
| 5400 " return false;" | 5508 " return false;" |
| 5401 " }" | 5509 " }" |
| 5402 "return true;})()"); | 5510 "return true;})()"); |
| 5403 CHECK(result->IsTrue()); | 5511 CHECK(value->IsTrue()); |
| 5404 | 5512 |
| 5405 context1->Exit(); | 5513 context1->Exit(); |
| 5406 context0->Exit(); | 5514 context0->Exit(); |
| 5407 context1.Dispose(); | 5515 context1.Dispose(); |
| 5408 context0.Dispose(); | 5516 context0.Dispose(); |
| 5409 } | 5517 } |
| 5410 | 5518 |
| 5411 | 5519 |
| 5412 static bool GetOwnPropertyNamesNamedBlocker(Local<v8::Object> global, | 5520 static bool GetOwnPropertyNamesNamedBlocker(Local<v8::Object> global, |
| 5413 Local<Value> name, | 5521 Local<Value> name, |
| (...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6238 Local<Function> cons = templ->GetFunction(); | 6346 Local<Function> cons = templ->GetFunction(); |
| 6239 context->Global()->Set(v8_str("Fun"), cons); | 6347 context->Global()->Set(v8_str("Fun"), cons); |
| 6240 Local<Value> value = CompileRun( | 6348 Local<Value> value = CompileRun( |
| 6241 "function test() {" | 6349 "function test() {" |
| 6242 " try {" | 6350 " try {" |
| 6243 " (new Fun()).blah()" | 6351 " (new Fun()).blah()" |
| 6244 " } catch (e) {" | 6352 " } catch (e) {" |
| 6245 " var str = String(e);" | 6353 " var str = String(e);" |
| 6246 " if (str.indexOf('TypeError') == -1) return 1;" | 6354 " if (str.indexOf('TypeError') == -1) return 1;" |
| 6247 " if (str.indexOf('[object Fun]') != -1) return 2;" | 6355 " if (str.indexOf('[object Fun]') != -1) return 2;" |
| 6248 " if (str.indexOf('#<a Fun>') == -1) return 3;" | 6356 " if (str.indexOf('#<Fun>') == -1) return 3;" |
| 6249 " return 0;" | 6357 " return 0;" |
| 6250 " }" | 6358 " }" |
| 6251 " return 4;" | 6359 " return 4;" |
| 6252 "}" | 6360 "}" |
| 6253 "test();"); | 6361 "test();"); |
| 6254 CHECK_EQ(0, value->Int32Value()); | 6362 CHECK_EQ(0, value->Int32Value()); |
| 6255 } | 6363 } |
| 6256 | 6364 |
| 6257 | 6365 |
| 6258 THREADED_TEST(EvalAliasedDynamic) { | 6366 THREADED_TEST(EvalAliasedDynamic) { |
| (...skipping 6255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12514 v8::Context::Scope context_scope(context.local()); | 12622 v8::Context::Scope context_scope(context.local()); |
| 12515 | 12623 |
| 12516 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); | 12624 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
| 12517 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); | 12625 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
| 12518 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); | 12626 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
| 12519 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( | 12627 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
| 12520 "var result = []; for (var k in o) result.push(k); result")); | 12628 "var result = []; for (var k in o) result.push(k); result")); |
| 12521 CHECK_EQ(1, result->Length()); | 12629 CHECK_EQ(1, result->Length()); |
| 12522 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); | 12630 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
| 12523 } | 12631 } |
| OLD | NEW |