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

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

Issue 21761002: Improve internal stringifcation for custom Error objects. (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: GetDataProperty. 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
« no previous file with comments | « src/messages.js ('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 4370 matching lines...) Expand 10 before | Expand all | Expand 10 after
4381 CHECK(string->Equals(v8_str("Whoops"))); 4381 CHECK(string->Equals(v8_str("Whoops")));
4382 CompileRun("ReferenceError.prototype.constructor = new Object();" 4382 CompileRun("ReferenceError.prototype.constructor = new Object();"
4383 "ReferenceError.prototype.constructor.name = 1;" 4383 "ReferenceError.prototype.constructor.name = 1;"
4384 "Number.prototype.toString = function() { return 'Whoops'; };" 4384 "Number.prototype.toString = function() { return 'Whoops'; };"
4385 "ReferenceError.prototype.toString = Object.prototype.toString;"); 4385 "ReferenceError.prototype.toString = Object.prototype.toString;");
4386 CompileRun("asdf;"); 4386 CompileRun("asdf;");
4387 v8::V8::RemoveMessageListeners(check_reference_error_message); 4387 v8::V8::RemoveMessageListeners(check_reference_error_message);
4388 } 4388 }
4389 4389
4390 4390
4391 static void check_custom_error_message( 4391 static void check_custom_error_tostring(
4392 v8::Handle<v8::Message> message, 4392 v8::Handle<v8::Message> message,
4393 v8::Handle<v8::Value> data) { 4393 v8::Handle<v8::Value> data) {
4394 const char* uncaught_error = "Uncaught MyError toString"; 4394 const char* uncaught_error = "Uncaught MyError toString";
4395 CHECK(message->Get()->Equals(v8_str(uncaught_error))); 4395 CHECK(message->Get()->Equals(v8_str(uncaught_error)));
4396 } 4396 }
4397 4397
4398 4398
4399 TEST(CustomErrorToString) { 4399 TEST(CustomErrorToString) {
4400 LocalContext context; 4400 LocalContext context;
4401 v8::HandleScope scope(context->GetIsolate()); 4401 v8::HandleScope scope(context->GetIsolate());
4402 v8::V8::AddMessageListener(check_custom_error_message); 4402 v8::V8::AddMessageListener(check_custom_error_tostring);
4403 CompileRun( 4403 CompileRun(
4404 "function MyError(name, message) { " 4404 "function MyError(name, message) { "
4405 " this.name = name; " 4405 " this.name = name; "
4406 " this.message = message; " 4406 " this.message = message; "
4407 "} " 4407 "} "
4408 "MyError.prototype = Object.create(Error.prototype); " 4408 "MyError.prototype = Object.create(Error.prototype); "
4409 "MyError.prototype.toString = function() { " 4409 "MyError.prototype.toString = function() { "
4410 " return 'MyError toString'; " 4410 " return 'MyError toString'; "
4411 "}; " 4411 "}; "
4412 "throw new MyError('my name', 'my message'); "); 4412 "throw new MyError('my name', 'my message'); ");
4413 v8::V8::RemoveMessageListeners(check_custom_error_tostring);
4414 }
4415
4416
4417 static void check_custom_error_message(
4418 v8::Handle<v8::Message> message,
4419 v8::Handle<v8::Value> data) {
4420 const char* uncaught_error = "Uncaught MyError: my message";
4421 printf("%s\n", *v8::String::Utf8Value(message->Get()));
4422 CHECK(message->Get()->Equals(v8_str(uncaught_error)));
4423 }
4424
4425
4426 TEST(CustomErrorMessage) {
4427 LocalContext context;
4428 v8::HandleScope scope(context->GetIsolate());
4429 v8::V8::AddMessageListener(check_custom_error_message);
4430
4431 // Handlebars.
4432 CompileRun(
4433 "function MyError(msg) { "
4434 " this.name = 'MyError'; "
4435 " this.message = msg; "
4436 "} "
4437 "MyError.prototype = new Error(); "
4438 "throw new MyError('my message'); ");
4439
4440 // Closure.
4441 CompileRun(
4442 "function MyError(msg) { "
4443 " this.name = 'MyError'; "
4444 " this.message = msg; "
4445 "} "
4446 "inherits = function(childCtor, parentCtor) { "
4447 " function tempCtor() {}; "
4448 " tempCtor.prototype = parentCtor.prototype; "
4449 " childCtor.superClass_ = parentCtor.prototype; "
4450 " childCtor.prototype = new tempCtor(); "
4451 " childCtor.prototype.constructor = childCtor; "
4452 "}; "
4453 "inherits(MyError, Error); "
4454 "throw new MyError('my message'); ");
4455
4456 // Object.create.
4457 CompileRun(
4458 "function MyError(msg) { "
4459 " this.name = 'MyError'; "
4460 " this.message = msg; "
4461 "} "
4462 "MyError.prototype = Object.create(Error.prototype); "
4463 "throw new MyError('my message'); ");
4464
4413 v8::V8::RemoveMessageListeners(check_custom_error_message); 4465 v8::V8::RemoveMessageListeners(check_custom_error_message);
4414 } 4466 }
4415 4467
4416 4468
4417 static void receive_message(v8::Handle<v8::Message> message, 4469 static void receive_message(v8::Handle<v8::Message> message,
4418 v8::Handle<v8::Value> data) { 4470 v8::Handle<v8::Value> data) {
4419 message->Get(); 4471 message->Get();
4420 message_received = true; 4472 message_received = true;
4421 } 4473 }
4422 4474
(...skipping 15682 matching lines...) Expand 10 before | Expand all | Expand 10 after
20105 CheckCorrectThrow("%HasProperty(other, 'x')"); 20157 CheckCorrectThrow("%HasProperty(other, 'x')");
20106 CheckCorrectThrow("%HasElement(other, 1)"); 20158 CheckCorrectThrow("%HasElement(other, 1)");
20107 CheckCorrectThrow("%IsPropertyEnumerable(other, 'x')"); 20159 CheckCorrectThrow("%IsPropertyEnumerable(other, 'x')");
20108 CheckCorrectThrow("%GetPropertyNames(other)"); 20160 CheckCorrectThrow("%GetPropertyNames(other)");
20109 CheckCorrectThrow("%GetLocalPropertyNames(other, true)"); 20161 CheckCorrectThrow("%GetLocalPropertyNames(other, true)");
20110 CheckCorrectThrow("%DefineOrRedefineAccessorProperty(" 20162 CheckCorrectThrow("%DefineOrRedefineAccessorProperty("
20111 "other, 'x', null, null, 1)"); 20163 "other, 'x', null, null, 1)");
20112 } 20164 }
20113 20165
20114 #endif // WIN32 20166 #endif // WIN32
OLDNEW
« no previous file with comments | « src/messages.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698