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

Side by Side Diff: src/messages.js

Issue 40293: Added .message accessor to messages. (Closed)
Patch Set: Created 11 years, 9 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 | « no previous file | 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 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 for (var i = 0; i < args.length; i++) { 146 for (var i = 0; i < args.length; i++) {
147 var elem = args[i]; 147 var elem = args[i];
148 if (elem instanceof $Array && elem.length > 100) { // arbitrary limit, gra b a reasonable slice to report 148 if (elem instanceof $Array && elem.length > 100) { // arbitrary limit, gra b a reasonable slice to report
149 args[i] = elem.slice(0,20).concat("..."); 149 args[i] = elem.slice(0,20).concat("...");
150 } 150 }
151 } 151 }
152 } else if (IS_UNDEFINED(args)) { 152 } else if (IS_UNDEFINED(args)) {
153 args = []; 153 args = [];
154 } 154 }
155 155
156 var e = new constructor(); 156 var e = new constructor(kAddMessageAccessorsMarker);
157 e.type = type; 157 e.type = type;
158 e.arguments = args; 158 e.arguments = args;
159 return e; 159 return e;
160 } 160 }
161 161
162 162
163 /** 163 /**
164 * Setup the Script function and constructor. 164 * Setup the Script function and constructor.
165 */ 165 */
166 %FunctionSetInstanceClassName(Script, 'Script'); 166 %FunctionSetInstanceClassName(Script, 'Script');
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 if (!isTopLevel) result += ")"; 582 if (!isTopLevel) result += ")";
583 } 583 }
584 } 584 }
585 return (result) ? " at " + result : result; 585 return (result) ? " at " + result : result;
586 } 586 }
587 587
588 588
589 // ---------------------------------------------------------------------------- 589 // ----------------------------------------------------------------------------
590 // Error implementation 590 // Error implementation
591 591
592 // If this object gets passed to an error constructor the error will
593 // get an accessor for .message that constructs a descriptive error
594 // message on access.
595 var kAddMessageAccessorsMarker = { };
596
597 // Defines accessors for a property that is calculated the first time
598 // the property is read and then replaces the accessor with the value.
599 // Also, setting the property causes the accessors to be deleted.
600 function DefineOneShotAccessor(obj, name, fun) {
601 // Note that the accessors consistently operate on 'obj', not 'this'.
602 // Since the object may occur in someone else's prototype chain we
603 // can't rely on 'this' being the same as 'obj'.
604 obj.__defineGetter__(name, function () {
605 var value = fun(obj);
606 obj[name] = value;
607 return value;
608 });
609 obj.__defineSetter__(name, function (v) {
610 delete obj[name];
611 obj[name] = v;
612 });
613 }
614
592 function DefineError(f) { 615 function DefineError(f) {
593 // Store the error function in both the global object 616 // Store the error function in both the global object
594 // and the runtime object. The function is fetched 617 // and the runtime object. The function is fetched
595 // from the runtime object when throwing errors from 618 // from the runtime object when throwing errors from
596 // within the runtime system to avoid strange side 619 // within the runtime system to avoid strange side
597 // effects when overwriting the error functions from 620 // effects when overwriting the error functions from
598 // user code. 621 // user code.
599 var name = f.name; 622 var name = f.name;
600 %SetProperty(global, name, f, DONT_ENUM); 623 %SetProperty(global, name, f, DONT_ENUM);
601 this['$' + name] = f; 624 this['$' + name] = f;
602 // Configure the error function. 625 // Configure the error function.
603 // prototype of 'Error' must be as default: new Object(). 626 // prototype of 'Error' must be as default: new Object().
604 if (name != 'Error') %FunctionSetPrototype(f, new $Error()); 627 if (name != 'Error') %FunctionSetPrototype(f, new $Error());
605 %FunctionSetInstanceClassName(f, 'Error'); 628 %FunctionSetInstanceClassName(f, 'Error');
606 %SetProperty(f.prototype, 'constructor', f, DONT_ENUM); 629 %SetProperty(f.prototype, 'constructor', f, DONT_ENUM);
607 f.prototype.name = name; 630 f.prototype.name = name;
608 %SetCode(f, function(m) { 631 %SetCode(f, function(m) {
609 if (%IsConstructCall()) { 632 if (%IsConstructCall()) {
610 if (!IS_UNDEFINED(m)) this.message = ToString(m); 633 if (m === kAddMessageAccessorsMarker) {
634 DefineOneShotAccessor(this, 'message', function (obj) {
635 return FormatMessage({type: obj.type, args: obj.arguments});
636 });
637 } else if (!IS_UNDEFINED(m)) {
638 this.message = ToString(m);
639 }
611 } else { 640 } else {
612 return new f(m); 641 return new f(m);
613 } 642 }
614 }); 643 });
615 } 644 }
616 645
617 $Math.__proto__ = global.Object.prototype; 646 $Math.__proto__ = global.Object.prototype;
618 647
619 DefineError(function Error() { }); 648 DefineError(function Error() { });
620 DefineError(function TypeError() { }); 649 DefineError(function TypeError() { });
(...skipping 12 matching lines...) Expand all
633 return this.name + ": " + FormatMessage({ type: type, args: this.arguments } ); 662 return this.name + ": " + FormatMessage({ type: type, args: this.arguments } );
634 } 663 }
635 var message = this.message; 664 var message = this.message;
636 return this.name + (message ? (": " + message) : ""); 665 return this.name + (message ? (": " + message) : "");
637 }, DONT_ENUM); 666 }, DONT_ENUM);
638 667
639 668
640 // Boilerplate for exceptions for stack overflows. Used from 669 // Boilerplate for exceptions for stack overflows. Used from
641 // Top::StackOverflow(). 670 // Top::StackOverflow().
642 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); 671 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698