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

Side by Side Diff: src/messages.js

Issue 6223: Make sure that the name accessor on functions return the expected... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 12 years, 2 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
« no previous file with comments | « src/math.js ('k') | src/mirror-delay.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-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 30 matching lines...) Expand all
41 var mapping = kVowelSounds; 41 var mapping = kVowelSounds;
42 if (cons.length > 1 && (cons.charAt(0) != first)) { 42 if (cons.length > 1 && (cons.charAt(0) != first)) {
43 // First char is upper case 43 // First char is upper case
44 var second = cons.charAt(1).toLowerCase(); 44 var second = cons.charAt(1).toLowerCase();
45 // Second char is upper case 45 // Second char is upper case
46 if (cons.charAt(1) != second) 46 if (cons.charAt(1) != second)
47 mapping = kCapitalVowelSounds; 47 mapping = kCapitalVowelSounds;
48 } 48 }
49 var s = mapping[first] ? "an " : "a "; 49 var s = mapping[first] ? "an " : "a ";
50 return s + cons; 50 return s + cons;
51 }; 51 }
52 52
53 53
54 const kMessages = { 54 const kMessages = {
55 // Error 55 // Error
56 cyclic_proto: "Cyclic __proto__ value", 56 cyclic_proto: "Cyclic __proto__ value",
57 // TypeError 57 // TypeError
58 unexpected_token: "Unexpected token %0", 58 unexpected_token: "Unexpected token %0",
59 unexpected_token_number: "Unexpected number", 59 unexpected_token_number: "Unexpected number",
60 unexpected_token_string: "Unexpected string", 60 unexpected_token_string: "Unexpected string",
61 unexpected_token_identifier: "Unexpected identifier", 61 unexpected_token_identifier: "Unexpected identifier",
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 117
118 function FormatString(format, args) { 118 function FormatString(format, args) {
119 var result = format; 119 var result = format;
120 for (var i = 0; i < args.length; i++) { 120 for (var i = 0; i < args.length; i++) {
121 var str; 121 var str;
122 try { str = ToDetailString(args[i]); } 122 try { str = ToDetailString(args[i]); }
123 catch (e) { str = "#<error>"; } 123 catch (e) { str = "#<error>"; }
124 result = result.split("%" + i).join(str); 124 result = result.split("%" + i).join(str);
125 } 125 }
126 return result; 126 return result;
127 }; 127 }
128 128
129 129
130 function ToDetailString(obj) { 130 function ToDetailString(obj) {
131 if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toStri ng) { 131 if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toStri ng) {
132 var constructor = obj.constructor; 132 var constructor = obj.constructor;
133 if (!constructor) return ToString(obj); 133 if (!constructor) return ToString(obj);
134 var constructorName = constructor.name; 134 var constructorName = constructor.name;
135 if (!constructorName) return ToString(obj); 135 if (!constructorName) return ToString(obj);
136 return "#<" + GetInstanceName(constructorName) + ">"; 136 return "#<" + GetInstanceName(constructorName) + ">";
137 } else { 137 } else {
138 return ToString(obj); 138 return ToString(obj);
139 } 139 }
140 }; 140 }
141 141
142 142
143 function MakeGenericError(constructor, type, args) { 143 function MakeGenericError(constructor, type, args) {
144 if (args instanceof $Array) { 144 if (args instanceof $Array) {
145 for (var i = 0; i < args.length; i++) { 145 for (var i = 0; i < args.length; i++) {
146 var elem = args[i]; 146 var elem = args[i];
147 if (elem instanceof $Array && elem.length > 100) { // arbitrary limit, gra b a reasonable slice to report 147 if (elem instanceof $Array && elem.length > 100) { // arbitrary limit, gra b a reasonable slice to report
148 args[i] = elem.slice(0,20).concat("..."); 148 args[i] = elem.slice(0,20).concat("...");
149 } 149 }
150 } 150 }
151 } else if (IS_UNDEFINED(args)) { 151 } else if (IS_UNDEFINED(args)) {
152 args = []; 152 args = [];
153 } 153 }
154 154
155 var e = new constructor(); 155 var e = new constructor();
156 e.type = type; 156 e.type = type;
157 e.arguments = args; 157 e.arguments = args;
158 return e; 158 return e;
159 }; 159 }
160 160
161 161
162 /** 162 /**
163 * Setup the Script function and constructor. 163 * Setup the Script function and constructor.
164 */ 164 */
165 %FunctionSetInstanceClassName(Script, 'Script'); 165 %FunctionSetInstanceClassName(Script, 'Script');
166 %AddProperty(Script.prototype, 'constructor', Script, DONT_ENUM); 166 %AddProperty(Script.prototype, 'constructor', Script, DONT_ENUM);
167 %SetCode(Script, function(x) { 167 %SetCode(Script, function(x) {
168 // Script objects can only be created by the VM. 168 // Script objects can only be created by the VM.
169 throw new $Error("Not supported"); 169 throw new $Error("Not supported");
170 }); 170 });
171 171
172 172
173 // Helper functions; called from the runtime system. 173 // Helper functions; called from the runtime system.
174 function FormatMessage(message) { 174 function FormatMessage(message) {
175 var format = kMessages[message.type]; 175 var format = kMessages[message.type];
176 if (!format) return "<unknown message " + message.type + ">"; 176 if (!format) return "<unknown message " + message.type + ">";
177 return FormatString(format, message.args); 177 return FormatString(format, message.args);
178 }; 178 }
179 179
180 180
181 function GetLineNumber(message) { 181 function GetLineNumber(message) {
182 if (message.startPos == -1) return -1; 182 if (message.startPos == -1) return -1;
183 var location = message.script.locationFromPosition(message.startPos); 183 var location = message.script.locationFromPosition(message.startPos);
184 if (location == null) return -1; 184 if (location == null) return -1;
185 return location.line + 1; 185 return location.line + 1;
186 }; 186 }
187 187
188 188
189 // Returns the source code line containing the given source 189 // Returns the source code line containing the given source
190 // position, or the empty string if the position is invalid. 190 // position, or the empty string if the position is invalid.
191 function GetSourceLine(message) { 191 function GetSourceLine(message) {
192 var location = message.script.locationFromPosition(message.startPos); 192 var location = message.script.locationFromPosition(message.startPos);
193 if (location == null) return ""; 193 if (location == null) return "";
194 location.restrict(); 194 location.restrict();
195 return location.sourceText(); 195 return location.sourceText();
196 }; 196 }
197 197
198 198
199 function MakeTypeError(type, args) { 199 function MakeTypeError(type, args) {
200 return MakeGenericError($TypeError, type, args); 200 return MakeGenericError($TypeError, type, args);
201 }; 201 }
202 202
203 203
204 function MakeRangeError(type, args) { 204 function MakeRangeError(type, args) {
205 return MakeGenericError($RangeError, type, args); 205 return MakeGenericError($RangeError, type, args);
206 }; 206 }
207 207
208 208
209 function MakeSyntaxError(type, args) { 209 function MakeSyntaxError(type, args) {
210 return MakeGenericError($SyntaxError, type, args); 210 return MakeGenericError($SyntaxError, type, args);
211 }; 211 }
212 212
213 213
214 function MakeReferenceError(type, args) { 214 function MakeReferenceError(type, args) {
215 return MakeGenericError($ReferenceError, type, args); 215 return MakeGenericError($ReferenceError, type, args);
216 }; 216 }
217 217
218 218
219 function MakeEvalError(type, args) { 219 function MakeEvalError(type, args) {
220 return MakeGenericError($EvalError, type, args); 220 return MakeGenericError($EvalError, type, args);
221 }; 221 }
222 222
223 223
224 function MakeError(type, args) { 224 function MakeError(type, args) {
225 return MakeGenericError($Error, type, args); 225 return MakeGenericError($Error, type, args);
226 }; 226 }
227 227
228 228
229 /** 229 /**
230 * Initialize the cached source information in a script. Currently all line 230 * Initialize the cached source information in a script. Currently all line
231 * end positions are cached. 231 * end positions are cached.
232 */ 232 */
233 Script.prototype.initSourceInfo_ = function () { 233 Script.prototype.initSourceInfo_ = function () {
234 // Just return if initialized. 234 // Just return if initialized.
235 if (this.lineEnds_) return; 235 if (this.lineEnds_) return;
236 236
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 * @param {number} end Source position for end of source context 438 * @param {number} end Source position for end of source context
439 * @constructor 439 * @constructor
440 */ 440 */
441 function SourceLocation(script, position, line, column, start, end) { 441 function SourceLocation(script, position, line, column, start, end) {
442 this.script = script; 442 this.script = script;
443 this.position = position; 443 this.position = position;
444 this.line = line; 444 this.line = line;
445 this.column = column; 445 this.column = column;
446 this.start = start; 446 this.start = start;
447 this.end = end; 447 this.end = end;
448 }; 448 }
449 449
450 450
451 const kLineLengthLimit = 78; 451 const kLineLengthLimit = 78;
452 452
453 /** 453 /**
454 * Restrict source location start and end positions to make the source slice 454 * Restrict source location start and end positions to make the source slice
455 * no more that a certain number of characters wide. 455 * no more that a certain number of characters wide.
456 * @param {number} opt_limit The with limit of the source text with a default 456 * @param {number} opt_limit The with limit of the source text with a default
457 * of 78 457 * of 78
458 * @param {number} opt_before The number of characters to prefer before the 458 * @param {number} opt_before The number of characters to prefer before the
459 * position with a default value of 10 less that the limit 459 * position with a default value of 10 less that the limit
460 */ 460 */
461 SourceLocation.prototype.restrict = function (opt_limit, opt_before) { 461 SourceLocation.prototype.restrict = function (opt_limit, opt_before) {
462 // Find the actual limit to use. 462 // Find the actual limit to use.
463 var limit; 463 var limit;
464 var before; 464 var before;
465 if (!IS_UNDEFINED(opt_limit)) { 465 if (!IS_UNDEFINED(opt_limit)) {
466 limit = opt_limit; 466 limit = opt_limit;
467 } else { 467 } else {
468 limit = kLineLengthLimit; 468 limit = kLineLengthLimit;
469 } 469 }
470 if (!IS_UNDEFINED(opt_before)) { 470 if (!IS_UNDEFINED(opt_before)) {
471 before = opt_before; 471 before = opt_before;
472 } else { 472 } else {
473 // If no before is specified center for small limits and perfer more source 473 // If no before is specified center for small limits and perfer more source
474 // before the the position that after for longer limits. 474 // before the the position that after for longer limits.
475 if (limit <= 20) { 475 if (limit <= 20) {
476 before = $Math_floor(limit / 2); 476 before = $floor(limit / 2);
477 } else { 477 } else {
478 before = limit - 10; 478 before = limit - 10;
479 } 479 }
480 } 480 }
481 if (before >= limit) { 481 if (before >= limit) {
482 before = limit - 1; 482 before = limit - 1;
483 } 483 }
484 484
485 // If the [start, end[ interval is too big we restrict 485 // If the [start, end[ interval is too big we restrict
486 // it in one or both ends. We make sure to always produce 486 // it in one or both ends. We make sure to always produce
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 }; 547 };
548 548
549 549
550 // Returns the offset of the given position within the containing 550 // Returns the offset of the given position within the containing
551 // line. 551 // line.
552 function GetPositionInLine(message) { 552 function GetPositionInLine(message) {
553 var location = message.script.locationFromPosition(message.startPos); 553 var location = message.script.locationFromPosition(message.startPos);
554 if (location == null) return -1; 554 if (location == null) return -1;
555 location.restrict(); 555 location.restrict();
556 return message.startPos - location.start; 556 return message.startPos - location.start;
557 }; 557 }
558 558
559 559
560 function ErrorMessage(type, args, startPos, endPos, script, stackTrace) { 560 function ErrorMessage(type, args, startPos, endPos, script, stackTrace) {
561 this.startPos = startPos; 561 this.startPos = startPos;
562 this.endPos = endPos; 562 this.endPos = endPos;
563 this.type = type; 563 this.type = type;
564 this.args = args; 564 this.args = args;
565 this.script = script; 565 this.script = script;
566 this.stackTrace = stackTrace; 566 this.stackTrace = stackTrace;
567 }; 567 }
568 568
569 569
570 function MakeMessage(type, args, startPos, endPos, script, stackTrace) { 570 function MakeMessage(type, args, startPos, endPos, script, stackTrace) {
571 return new ErrorMessage(type, args, startPos, endPos, script, stackTrace); 571 return new ErrorMessage(type, args, startPos, endPos, script, stackTrace);
572 }; 572 }
573 573
574 574
575 function GetStackTraceLine(recv, fun, pos, isGlobal) { 575 function GetStackTraceLine(recv, fun, pos, isGlobal) {
576 try { 576 try {
577 return UnsafeGetStackTraceLine(recv, fun, pos, isGlobal); 577 return UnsafeGetStackTraceLine(recv, fun, pos, isGlobal);
578 } catch (e) { 578 } catch (e) {
579 return "<error: " + e + ">"; 579 return "<error: " + e + ">";
580 } 580 }
581 }; 581 }
582 582
583 583
584 function GetFunctionName(fun, recv) { 584 function GetFunctionName(fun, recv) {
585 var name = %FunctionGetName(fun); 585 var name = %FunctionGetName(fun);
586 if (name) return name; 586 if (name) return name;
587 for (var prop in recv) { 587 for (var prop in recv) {
588 if (recv[prop] === fun) 588 if (recv[prop] === fun)
589 return prop; 589 return prop;
590 } 590 }
591 return "[anonymous]"; 591 return "[anonymous]";
592 }; 592 }
593 593
594 594
595 function UnsafeGetStackTraceLine(recv, fun, pos, isTopLevel) { 595 function UnsafeGetStackTraceLine(recv, fun, pos, isTopLevel) {
596 var result = ""; 596 var result = "";
597 // The global frame has no meaningful function or receiver 597 // The global frame has no meaningful function or receiver
598 if (!isTopLevel) { 598 if (!isTopLevel) {
599 // If the receiver is not the global object then prefix the 599 // If the receiver is not the global object then prefix the
600 // message send 600 // message send
601 if (recv !== global) 601 if (recv !== global)
602 result += ToDetailString(recv) + "."; 602 result += ToDetailString(recv) + ".";
603 result += GetFunctionName(fun, recv); 603 result += GetFunctionName(fun, recv);
604 } 604 }
605 if (pos != -1) { 605 if (pos != -1) {
606 var script = %FunctionGetScript(fun); 606 var script = %FunctionGetScript(fun);
607 var file; 607 var file;
608 if (script) { 608 if (script) {
609 file = %FunctionGetScript(fun).data; 609 file = %FunctionGetScript(fun).data;
610 } 610 }
611 if (file) { 611 if (file) {
612 var location = %FunctionGetScript(fun).locationFromPosition(pos); 612 var location = %FunctionGetScript(fun).locationFromPosition(pos);
613 if (!isTopLevel) result += "("; 613 if (!isTopLevel) result += "(";
614 result += file; 614 result += file;
615 if (location != null) { 615 if (location != null) {
616 result += ":" + (location.line + 1) + ":" + (location.column + 1); 616 result += ":" + (location.line + 1) + ":" + (location.column + 1);
617 } 617 }
618 if (!isTopLevel) result += ")"; 618 if (!isTopLevel) result += ")";
619 } 619 }
620 } 620 }
621 return (result) ? " at " + result : result; 621 return (result) ? " at " + result : result;
622 }; 622 }
623 623
624 624
625 // ---------------------------------------------------------------------------- 625 // ----------------------------------------------------------------------------
626 // Error implementation 626 // Error implementation
627 627
628 function DefineError(name) { 628 function DefineError(f) {
629 var f = function(msg) {};
630 // Store the error function in both the global object 629 // Store the error function in both the global object
631 // and the runtime object. The function is fetched 630 // and the runtime object. The function is fetched
632 // from the runtime object when throwing errors from 631 // from the runtime object when throwing errors from
633 // within the runtime system to avoid strange side 632 // within the runtime system to avoid strange side
634 // effects when overwriting the error functions from 633 // effects when overwriting the error functions from
635 // user code. 634 // user code.
635 var name = f.name;
636 %AddProperty(global, name, f, DONT_ENUM); 636 %AddProperty(global, name, f, DONT_ENUM);
637 this['$' + name] = f; 637 this['$' + name] = f;
638 // Configure the error function. 638 // Configure the error function.
639 // prototype of 'Error' must be as default: new Object(). 639 // prototype of 'Error' must be as default: new Object().
640 if (name != 'Error') %FunctionSetPrototype(f, new $Error()); 640 if (name != 'Error') %FunctionSetPrototype(f, new $Error());
641 %FunctionSetInstanceClassName(f, 'Error'); 641 %FunctionSetInstanceClassName(f, 'Error');
642 f.prototype.name = name; 642 f.prototype.name = name;
643 f.prototype.constructor = f; 643 f.prototype.constructor = f;
644 %SetCode(f, function(m) { 644 %SetCode(f, function(m) {
645 if (%IsConstructCall()) { 645 if (%IsConstructCall()) {
646 if (!IS_UNDEFINED(m)) this.message = ToString(m); 646 if (!IS_UNDEFINED(m)) this.message = ToString(m);
647 } else { 647 } else {
648 return new f(m); 648 return new f(m);
649 } 649 }
650 }); 650 });
651 }; 651 }
652 652
653 $Math.__proto__ = global.Object.prototype; 653 $Math.__proto__ = global.Object.prototype;
654 654
655 DefineError('Error'); 655 DefineError(function Error() { });
656 DefineError('TypeError'); 656 DefineError(function TypeError() { });
657 DefineError('RangeError'); 657 DefineError(function RangeError() { });
658 DefineError('SyntaxError'); 658 DefineError(function SyntaxError() { });
659 DefineError('ReferenceError'); 659 DefineError(function ReferenceError() { });
660 DefineError('EvalError'); 660 DefineError(function EvalError() { });
661 DefineError('URIError'); 661 DefineError(function URIError() { });
662 662
663 // Setup extra properties of the Error.prototype object. 663 // Setup extra properties of the Error.prototype object.
664 $Error.prototype.message = ''; 664 $Error.prototype.message = '';
665 665
666 %AddProperty($Error.prototype, 'toString', function() { 666 %AddProperty($Error.prototype, 'toString', function toString() {
667 var type = this.type; 667 var type = this.type;
668 if (type && !this.hasOwnProperty("message")) { 668 if (type && !this.hasOwnProperty("message")) {
669 return this.name + ": " + FormatMessage({ type: type, args: this.arguments } ); 669 return this.name + ": " + FormatMessage({ type: type, args: this.arguments } );
670 } 670 }
671 var message = this.message; 671 var message = this.message;
672 return this.name + (message ? (": " + message) : ""); 672 return this.name + (message ? (": " + message) : "");
673 }, DONT_ENUM); 673 }, DONT_ENUM);
674 674
675 675
676 // Boilerplate for exceptions for stack overflows. Used from 676 // Boilerplate for exceptions for stack overflows. Used from
677 // Top::StackOverflow(). 677 // Top::StackOverflow().
678 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); 678 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);
OLDNEW
« no previous file with comments | « src/math.js ('k') | src/mirror-delay.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698