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

Side by Side Diff: src/d8.js

Issue 8701006: Clean up JavaScript files to better follow coding standard. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Remove more empty statments and fix bug. Created 9 years 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/array.js ('k') | src/date.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 2008 the V8 project authors. All rights reserved. 1 // Copyright 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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 String.prototype.startsWith = function (str) { 28 String.prototype.startsWith = function (str) {
29 if (str.length > this.length) 29 if (str.length > this.length) {
30 return false; 30 return false;
31 }
31 return this.substr(0, str.length) == str; 32 return this.substr(0, str.length) == str;
32 } 33 };
33 34
34 function log10(num) { 35 function log10(num) {
35 return Math.log(num)/Math.log(10); 36 return Math.log(num)/Math.log(10);
36 } 37 }
37 38
38 function ToInspectableObject(obj) { 39 function ToInspectableObject(obj) {
39 if (!obj && typeof obj === 'object') { 40 if (!obj && typeof obj === 'object') {
40 return void 0; 41 return void 0;
41 } else { 42 } else {
42 return Object(obj); 43 return Object(obj);
43 } 44 }
44 } 45 }
45 46
46 function GetCompletions(global, last, full) { 47 function GetCompletions(global, last, full) {
47 var full_tokens = full.split(); 48 var full_tokens = full.split();
48 full = full_tokens.pop(); 49 full = full_tokens.pop();
49 var parts = full.split('.'); 50 var parts = full.split('.');
50 parts.pop(); 51 parts.pop();
51 var current = global; 52 var current = global;
52 for (var i = 0; i < parts.length; i++) { 53 for (var i = 0; i < parts.length; i++) {
53 var part = parts[i]; 54 var part = parts[i];
54 var next = current[part]; 55 var next = current[part];
55 if (!next) 56 if (!next) {
56 return []; 57 return [];
58 }
57 current = next; 59 current = next;
58 } 60 }
59 var result = []; 61 var result = [];
60 current = ToInspectableObject(current); 62 current = ToInspectableObject(current);
61 while (typeof current !== 'undefined') { 63 while (typeof current !== 'undefined') {
62 var mirror = new $debug.ObjectMirror(current); 64 var mirror = new $debug.ObjectMirror(current);
63 var properties = mirror.properties(); 65 var properties = mirror.properties();
64 for (var i = 0; i < properties.length; i++) { 66 for (var i = 0; i < properties.length; i++) {
65 var name = properties[i].name(); 67 var name = properties[i].name();
66 if (typeof name === 'string' && name.startsWith(last)) 68 if (typeof name === 'string' && name.startsWith(last)) {
67 result.push(name); 69 result.push(name);
70 }
68 } 71 }
69 current = ToInspectableObject(current.__proto__); 72 current = ToInspectableObject(current.__proto__);
70 } 73 }
71 return result; 74 return result;
72 } 75 }
73 76
74 77
75 // Global object holding debugger related constants and state. 78 // Global object holding debugger related constants and state.
76 const Debug = {}; 79 const Debug = {};
77 80
(...skipping 29 matching lines...) Expand all
107 Block: 5 }; 110 Block: 5 };
108 111
109 112
110 // Current debug state. 113 // Current debug state.
111 const kNoFrame = -1; 114 const kNoFrame = -1;
112 Debug.State = { 115 Debug.State = {
113 currentFrame: kNoFrame, 116 currentFrame: kNoFrame,
114 displaySourceStartLine: -1, 117 displaySourceStartLine: -1,
115 displaySourceEndLine: -1, 118 displaySourceEndLine: -1,
116 currentSourceLine: -1 119 currentSourceLine: -1
117 } 120 };
118 var trace_compile = false; // Tracing all compile events? 121 var trace_compile = false; // Tracing all compile events?
119 var trace_debug_json = false; // Tracing all debug json packets? 122 var trace_debug_json = false; // Tracing all debug json packets?
120 var last_cmd_line = ''; 123 var last_cmd_line = '';
121 //var lol_is_enabled; // Set to true in d8.cc if LIVE_OBJECT_LIST is defined. 124 //var lol_is_enabled; // Set to true in d8.cc if LIVE_OBJECT_LIST is defined.
122 var lol_next_dump_index = 0; 125 var lol_next_dump_index = 0;
123 const kDefaultLolLinesToPrintAtATime = 10; 126 const kDefaultLolLinesToPrintAtATime = 10;
124 const kMaxLolLinesToPrintAtATime = 1000; 127 const kMaxLolLinesToPrintAtATime = 1000;
125 var repeat_cmd_line = ''; 128 var repeat_cmd_line = '';
126 var is_running = true; 129 var is_running = true;
127 130
(...skipping 15 matching lines...) Expand all
143 is_running = response.running(); 146 is_running = response.running();
144 147
145 if (response.type() == 'event') { 148 if (response.type() == 'event') {
146 return DebugEventDetails(response); 149 return DebugEventDetails(response);
147 } else { 150 } else {
148 return DebugResponseDetails(response); 151 return DebugResponseDetails(response);
149 } 152 }
150 } 153 }
151 154
152 function DebugEventDetails(response) { 155 function DebugEventDetails(response) {
153 details = {text:'', running:false} 156 details = {text:'', running:false};
154 157
155 // Get the running state. 158 // Get the running state.
156 details.running = response.running(); 159 details.running = response.running();
157 160
158 var body = response.body(); 161 var body = response.body();
159 var result = ''; 162 var result = '';
160 switch (response.event()) { 163 switch (response.event()) {
161 case 'break': 164 case 'break':
162 if (body.breakpoints) { 165 if (body.breakpoints) {
163 result += 'breakpoint'; 166 result += 'breakpoint';
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 Debug.State.currentSourceLine = -1; 213 Debug.State.currentSourceLine = -1;
211 Debug.State.displaySourceStartLine = -1; 214 Debug.State.displaySourceStartLine = -1;
212 Debug.State.displaySourceEndLine = -1; 215 Debug.State.displaySourceEndLine = -1;
213 Debug.State.currentFrame = kNoFrame; 216 Debug.State.currentFrame = kNoFrame;
214 } 217 }
215 details.text = result; 218 details.text = result;
216 break; 219 break;
217 220
218 case 'afterCompile': 221 case 'afterCompile':
219 if (trace_compile) { 222 if (trace_compile) {
220 result = 'Source ' + body.script.name + ' compiled:\n' 223 result = 'Source ' + body.script.name + ' compiled:\n';
221 var source = body.script.source; 224 var source = body.script.source;
222 if (!(source[source.length - 1] == '\n')) { 225 if (!(source[source.length - 1] == '\n')) {
223 result += source; 226 result += source;
224 } else { 227 } else {
225 result += source.substring(0, source.length - 1); 228 result += source.substring(0, source.length - 1);
226 } 229 }
227 } 230 }
228 details.text = result; 231 details.text = result;
229 break; 232 break;
230 233
231 case 'scriptCollected': 234 case 'scriptCollected':
232 details.text = result; 235 details.text = result;
233 break; 236 break;
234 237
235 default: 238 default:
236 details.text = 'Unknown debug event ' + response.event(); 239 details.text = 'Unknown debug event ' + response.event();
237 } 240 }
238 241
239 return details; 242 return details;
240 }; 243 }
241 244
242 245
243 function SourceInfo(body) { 246 function SourceInfo(body) {
244 var result = ''; 247 var result = '';
245 248
246 if (body.script) { 249 if (body.script) {
247 if (body.script.name) { 250 if (body.script.name) {
248 result += body.script.name; 251 result += body.script.name;
249 } else { 252 } else {
250 result += '[unnamed]'; 253 result += '[unnamed]';
(...skipping 21 matching lines...) Expand all
272 if (source_text[i] == '\t') { 275 if (source_text[i] == '\t') {
273 underline += '\t'; 276 underline += '\t';
274 } else { 277 } else {
275 underline += ' '; 278 underline += ' ';
276 } 279 }
277 } 280 }
278 underline += '^'; 281 underline += '^';
279 282
280 // Return the source line text with the underline beneath. 283 // Return the source line text with the underline beneath.
281 return source_text + '\n' + underline; 284 return source_text + '\n' + underline;
282 }; 285 }
283 286
284 287
285 // Converts a text command to a JSON request. 288 // Converts a text command to a JSON request.
286 function DebugCommandToJSONRequest(cmd_line) { 289 function DebugCommandToJSONRequest(cmd_line) {
287 var result = new DebugRequest(cmd_line).JSONRequest(); 290 var result = new DebugRequest(cmd_line).JSONRequest();
288 if (trace_debug_json && result) { 291 if (trace_debug_json && result) {
289 print("sending: '" + result + "'"); 292 print("sending: '" + result + "'");
290 } 293 }
291 return result; 294 return result;
292 }; 295 }
293 296
294 297
295 function DebugRequest(cmd_line) { 298 function DebugRequest(cmd_line) {
296 // If the very first character is a { assume that a JSON request have been 299 // If the very first character is a { assume that a JSON request have been
297 // entered as a command. Converting that to a JSON request is trivial. 300 // entered as a command. Converting that to a JSON request is trivial.
298 if (cmd_line && cmd_line.length > 0 && cmd_line.charAt(0) == '{') { 301 if (cmd_line && cmd_line.length > 0 && cmd_line.charAt(0) == '{') {
299 this.request_ = cmd_line; 302 this.request_ = cmd_line;
300 return; 303 return;
301 } 304 }
302 305
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 break; 510 break;
508 } 511 }
509 512
510 default: 513 default:
511 throw new Error('Unknown command "' + cmd + '"'); 514 throw new Error('Unknown command "' + cmd + '"');
512 } 515 }
513 } 516 }
514 517
515 DebugRequest.prototype.JSONRequest = function() { 518 DebugRequest.prototype.JSONRequest = function() {
516 return this.request_; 519 return this.request_;
517 } 520 };
518 521
519 522
520 function RequestPacket(command) { 523 function RequestPacket(command) {
521 this.seq = 0; 524 this.seq = 0;
522 this.type = 'request'; 525 this.type = 'request';
523 this.command = command; 526 this.command = command;
524 } 527 }
525 528
526 529
527 RequestPacket.prototype.toJSONProtocol = function() { 530 RequestPacket.prototype.toJSONProtocol = function() {
528 // Encode the protocol header. 531 // Encode the protocol header.
529 var json = '{'; 532 var json = '{';
530 json += '"seq":' + this.seq; 533 json += '"seq":' + this.seq;
531 json += ',"type":"' + this.type + '"'; 534 json += ',"type":"' + this.type + '"';
532 if (this.command) { 535 if (this.command) {
533 json += ',"command":' + StringToJSON_(this.command); 536 json += ',"command":' + StringToJSON_(this.command);
534 } 537 }
535 if (this.arguments) { 538 if (this.arguments) {
536 json += ',"arguments":'; 539 json += ',"arguments":';
537 // Encode the arguments part. 540 // Encode the arguments part.
538 if (this.arguments.toJSONProtocol) { 541 if (this.arguments.toJSONProtocol) {
539 json += this.arguments.toJSONProtocol() 542 json += this.arguments.toJSONProtocol();
540 } else { 543 } else {
541 json += SimpleObjectToJSON_(this.arguments); 544 json += SimpleObjectToJSON_(this.arguments);
542 } 545 }
543 } 546 }
544 json += '}'; 547 json += '}';
545 return json; 548 return json;
546 } 549 };
547 550
548 551
549 DebugRequest.prototype.createRequest = function(command) { 552 DebugRequest.prototype.createRequest = function(command) {
550 return new RequestPacket(command); 553 return new RequestPacket(command);
551 }; 554 };
552 555
553 556
554 // Note: we use detected command repetition as a signal for continuation here. 557 // Note: we use detected command repetition as a signal for continuation here.
555 DebugRequest.prototype.createLOLRequest = function(command, 558 DebugRequest.prototype.createLOLRequest = function(command,
556 start_index, 559 start_index,
(...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after
1303 request.arguments.filter.type = type_filter; 1306 request.arguments.filter.type = type_filter;
1304 } 1307 }
1305 if (space_filter) { 1308 if (space_filter) {
1306 request.arguments.filter.space = space_filter; 1309 request.arguments.filter.space = space_filter;
1307 } 1310 }
1308 if (prop_filter) { 1311 if (prop_filter) {
1309 request.arguments.filter.prop = prop_filter; 1312 request.arguments.filter.prop = prop_filter;
1310 } 1313 }
1311 1314
1312 return request; 1315 return request;
1313 } 1316 };
1314 1317
1315 1318
1316 function extractObjId(args) { 1319 function extractObjId(args) {
1317 var id = args; 1320 var id = args;
1318 id = id.match(/^@([0-9]+)$/); 1321 id = id.match(/^@([0-9]+)$/);
1319 if (id) { 1322 if (id) {
1320 id = id[1]; 1323 id = id[1];
1321 } else { 1324 } else {
1322 throw new Error('Invalid obj id ' + args + '.'); 1325 throw new Error('Invalid obj id ' + args + '.');
1323 } 1326 }
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1492 } else if (args === 'debug json' || args === 'json' || args === 'packets') { 1495 } else if (args === 'debug json' || args === 'json' || args === 'packets') {
1493 trace_debug_json = !trace_debug_json; 1496 trace_debug_json = !trace_debug_json;
1494 print('Tracing of debug json packets ' + 1497 print('Tracing of debug json packets ' +
1495 (trace_debug_json ? 'on' : 'off')); 1498 (trace_debug_json ? 'on' : 'off'));
1496 } else { 1499 } else {
1497 throw new Error('Invalid trace arguments.'); 1500 throw new Error('Invalid trace arguments.');
1498 } 1501 }
1499 } else { 1502 } else {
1500 throw new Error('Invalid trace arguments.'); 1503 throw new Error('Invalid trace arguments.');
1501 } 1504 }
1502 } 1505 };
1503 1506
1504 // Handle the help command. 1507 // Handle the help command.
1505 DebugRequest.prototype.helpCommand_ = function(args) { 1508 DebugRequest.prototype.helpCommand_ = function(args) {
1506 // Help os quite simple. 1509 // Help os quite simple.
1507 if (args && args.length > 0) { 1510 if (args && args.length > 0) {
1508 print('warning: arguments to \'help\' are ignored'); 1511 print('warning: arguments to \'help\' are ignored');
1509 } 1512 }
1510 1513
1511 print('Note: <> denotes symbollic values to be replaced with real values.'); 1514 print('Note: <> denotes symbollic values to be replaced with real values.');
1512 print('Note: [] denotes optional parts of commands, or optional options / argu ments.'); 1515 print('Note: [] denotes optional parts of commands, or optional options / argu ments.');
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1601 print(' is specified, then a verbose dump will requested. Else, a summa ry dump'); 1604 print(' is specified, then a verbose dump will requested. Else, a summa ry dump');
1602 print(' will be requested.'); 1605 print(' will be requested.');
1603 print(''); 1606 print('');
1604 } 1607 }
1605 1608
1606 print('trace compile'); 1609 print('trace compile');
1607 // hidden command: trace debug json - toggles tracing of debug json packets 1610 // hidden command: trace debug json - toggles tracing of debug json packets
1608 print(''); 1611 print('');
1609 print('disconnect|exit|quit - disconnects and quits the debugger'); 1612 print('disconnect|exit|quit - disconnects and quits the debugger');
1610 print('help - prints this help information'); 1613 print('help - prints this help information');
1611 } 1614 };
1612 1615
1613 1616
1614 function formatHandleReference_(value) { 1617 function formatHandleReference_(value) {
1615 if (value.handle() >= 0) { 1618 if (value.handle() >= 0) {
1616 return '#' + value.handle() + '#'; 1619 return '#' + value.handle() + '#';
1617 } else { 1620 } else {
1618 return '#Transient#'; 1621 return '#Transient#';
1619 } 1622 }
1620 } 1623 }
1621 1624
1622 1625
1623 function formatObject_(value, include_properties) { 1626 function formatObject_(value, include_properties) {
1624 var result = ''; 1627 var result = '';
1625 result += formatHandleReference_(value); 1628 result += formatHandleReference_(value);
1626 result += ', type: object' 1629 result += ', type: object';
1627 result += ', constructor '; 1630 result += ', constructor ';
1628 var ctor = value.constructorFunctionValue(); 1631 var ctor = value.constructorFunctionValue();
1629 result += formatHandleReference_(ctor); 1632 result += formatHandleReference_(ctor);
1630 result += ', __proto__ '; 1633 result += ', __proto__ ';
1631 var proto = value.protoObjectValue(); 1634 var proto = value.protoObjectValue();
1632 result += formatHandleReference_(proto); 1635 result += formatHandleReference_(proto);
1633 result += ', '; 1636 result += ', ';
1634 result += value.propertyCount(); 1637 result += value.propertyCount();
1635 result += ' properties.'; 1638 result += ' properties.';
1636 if (include_properties) { 1639 if (include_properties) {
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1936 1939
1937 // Rounds number 'num' to 'length' decimal places. 1940 // Rounds number 'num' to 'length' decimal places.
1938 function roundNumber(num, length) { 1941 function roundNumber(num, length) {
1939 var factor = Math.pow(10, length); 1942 var factor = Math.pow(10, length);
1940 return Math.round(num * factor) / factor; 1943 return Math.round(num * factor) / factor;
1941 } 1944 }
1942 1945
1943 1946
1944 // Convert a JSON response to text for display in a text based debugger. 1947 // Convert a JSON response to text for display in a text based debugger.
1945 function DebugResponseDetails(response) { 1948 function DebugResponseDetails(response) {
1946 details = {text:'', running:false} 1949 details = { text: '', running: false };
1947 1950
1948 try { 1951 try {
1949 if (!response.success()) { 1952 if (!response.success()) {
1950 details.text = response.message(); 1953 details.text = response.message();
1951 return details; 1954 return details;
1952 } 1955 }
1953 1956
1954 // Get the running state. 1957 // Get the running state.
1955 details.running = response.running(); 1958 details.running = response.running();
1956 1959
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
2301 default: 2304 default:
2302 details.text = 2305 details.text =
2303 'Response for unknown command \'' + response.command() + '\'' + 2306 'Response for unknown command \'' + response.command() + '\'' +
2304 ' (' + response.raw_json() + ')'; 2307 ' (' + response.raw_json() + ')';
2305 } 2308 }
2306 } catch (e) { 2309 } catch (e) {
2307 details.text = 'Error: "' + e + '" formatting response'; 2310 details.text = 'Error: "' + e + '" formatting response';
2308 } 2311 }
2309 2312
2310 return details; 2313 return details;
2311 }; 2314 }
2312 2315
2313 2316
2314 /** 2317 /**
2315 * Protocol packages send from the debugger. 2318 * Protocol packages send from the debugger.
2316 * @param {string} json - raw protocol packet as JSON string. 2319 * @param {string} json - raw protocol packet as JSON string.
2317 * @constructor 2320 * @constructor
2318 */ 2321 */
2319 function ProtocolPackage(json) { 2322 function ProtocolPackage(json) {
2320 this.raw_json_ = json; 2323 this.raw_json_ = json;
2321 this.packet_ = JSON.parse(json); 2324 this.packet_ = JSON.parse(json);
2322 this.refs_ = []; 2325 this.refs_ = [];
2323 if (this.packet_.refs) { 2326 if (this.packet_.refs) {
2324 for (var i = 0; i < this.packet_.refs.length; i++) { 2327 for (var i = 0; i < this.packet_.refs.length; i++) {
2325 this.refs_[this.packet_.refs[i].handle] = this.packet_.refs[i]; 2328 this.refs_[this.packet_.refs[i].handle] = this.packet_.refs[i];
2326 } 2329 }
2327 } 2330 }
2328 } 2331 }
2329 2332
2330 2333
2331 /** 2334 /**
2332 * Get the packet type. 2335 * Get the packet type.
2333 * @return {String} the packet type 2336 * @return {String} the packet type
2334 */ 2337 */
2335 ProtocolPackage.prototype.type = function() { 2338 ProtocolPackage.prototype.type = function() {
2336 return this.packet_.type; 2339 return this.packet_.type;
2337 } 2340 };
2338 2341
2339 2342
2340 /** 2343 /**
2341 * Get the packet event. 2344 * Get the packet event.
2342 * @return {Object} the packet event 2345 * @return {Object} the packet event
2343 */ 2346 */
2344 ProtocolPackage.prototype.event = function() { 2347 ProtocolPackage.prototype.event = function() {
2345 return this.packet_.event; 2348 return this.packet_.event;
2346 } 2349 };
2347 2350
2348 2351
2349 /** 2352 /**
2350 * Get the packet request sequence. 2353 * Get the packet request sequence.
2351 * @return {number} the packet request sequence 2354 * @return {number} the packet request sequence
2352 */ 2355 */
2353 ProtocolPackage.prototype.requestSeq = function() { 2356 ProtocolPackage.prototype.requestSeq = function() {
2354 return this.packet_.request_seq; 2357 return this.packet_.request_seq;
2355 } 2358 };
2356 2359
2357 2360
2358 /** 2361 /**
2359 * Get the packet request sequence. 2362 * Get the packet request sequence.
2360 * @return {number} the packet request sequence 2363 * @return {number} the packet request sequence
2361 */ 2364 */
2362 ProtocolPackage.prototype.running = function() { 2365 ProtocolPackage.prototype.running = function() {
2363 return this.packet_.running ? true : false; 2366 return this.packet_.running ? true : false;
2364 } 2367 };
2365 2368
2366 2369
2367 ProtocolPackage.prototype.success = function() { 2370 ProtocolPackage.prototype.success = function() {
2368 return this.packet_.success ? true : false; 2371 return this.packet_.success ? true : false;
2369 } 2372 };
2370 2373
2371 2374
2372 ProtocolPackage.prototype.message = function() { 2375 ProtocolPackage.prototype.message = function() {
2373 return this.packet_.message; 2376 return this.packet_.message;
2374 } 2377 };
2375 2378
2376 2379
2377 ProtocolPackage.prototype.command = function() { 2380 ProtocolPackage.prototype.command = function() {
2378 return this.packet_.command; 2381 return this.packet_.command;
2379 } 2382 };
2380 2383
2381 2384
2382 ProtocolPackage.prototype.body = function() { 2385 ProtocolPackage.prototype.body = function() {
2383 return this.packet_.body; 2386 return this.packet_.body;
2384 } 2387 };
2385 2388
2386 2389
2387 ProtocolPackage.prototype.bodyValue = function(index) { 2390 ProtocolPackage.prototype.bodyValue = function(index) {
2388 if (index != null) { 2391 if (index != null) {
2389 return new ProtocolValue(this.packet_.body[index], this); 2392 return new ProtocolValue(this.packet_.body[index], this);
2390 } else { 2393 } else {
2391 return new ProtocolValue(this.packet_.body, this); 2394 return new ProtocolValue(this.packet_.body, this);
2392 } 2395 }
2393 } 2396 };
2394 2397
2395 2398
2396 ProtocolPackage.prototype.body = function() { 2399 ProtocolPackage.prototype.body = function() {
2397 return this.packet_.body; 2400 return this.packet_.body;
2398 } 2401 };
2399 2402
2400 2403
2401 ProtocolPackage.prototype.lookup = function(handle) { 2404 ProtocolPackage.prototype.lookup = function(handle) {
2402 var value = this.refs_[handle]; 2405 var value = this.refs_[handle];
2403 if (value) { 2406 if (value) {
2404 return new ProtocolValue(value, this); 2407 return new ProtocolValue(value, this);
2405 } else { 2408 } else {
2406 return new ProtocolReference(handle); 2409 return new ProtocolReference(handle);
2407 } 2410 }
2408 } 2411 };
2409 2412
2410 2413
2411 ProtocolPackage.prototype.raw_json = function() { 2414 ProtocolPackage.prototype.raw_json = function() {
2412 return this.raw_json_; 2415 return this.raw_json_;
2413 } 2416 };
2414 2417
2415 2418
2416 function ProtocolValue(value, packet) { 2419 function ProtocolValue(value, packet) {
2417 this.value_ = value; 2420 this.value_ = value;
2418 this.packet_ = packet; 2421 this.packet_ = packet;
2419 } 2422 }
2420 2423
2421 2424
2422 /** 2425 /**
2423 * Get the value type. 2426 * Get the value type.
2424 * @return {String} the value type 2427 * @return {String} the value type
2425 */ 2428 */
2426 ProtocolValue.prototype.type = function() { 2429 ProtocolValue.prototype.type = function() {
2427 return this.value_.type; 2430 return this.value_.type;
2428 } 2431 };
2429 2432
2430 2433
2431 /** 2434 /**
2432 * Get a metadata field from a protocol value. 2435 * Get a metadata field from a protocol value.
2433 * @return {Object} the metadata field value 2436 * @return {Object} the metadata field value
2434 */ 2437 */
2435 ProtocolValue.prototype.field = function(name) { 2438 ProtocolValue.prototype.field = function(name) {
2436 return this.value_[name]; 2439 return this.value_[name];
2437 } 2440 };
2438 2441
2439 2442
2440 /** 2443 /**
2441 * Check is the value is a primitive value. 2444 * Check is the value is a primitive value.
2442 * @return {boolean} true if the value is primitive 2445 * @return {boolean} true if the value is primitive
2443 */ 2446 */
2444 ProtocolValue.prototype.isPrimitive = function() { 2447 ProtocolValue.prototype.isPrimitive = function() {
2445 return this.isUndefined() || this.isNull() || this.isBoolean() || 2448 return this.isUndefined() || this.isNull() || this.isBoolean() ||
2446 this.isNumber() || this.isString(); 2449 this.isNumber() || this.isString();
2447 } 2450 };
2448 2451
2449 2452
2450 /** 2453 /**
2451 * Get the object handle. 2454 * Get the object handle.
2452 * @return {number} the value handle 2455 * @return {number} the value handle
2453 */ 2456 */
2454 ProtocolValue.prototype.handle = function() { 2457 ProtocolValue.prototype.handle = function() {
2455 return this.value_.handle; 2458 return this.value_.handle;
2456 } 2459 };
2457 2460
2458 2461
2459 /** 2462 /**
2460 * Check is the value is undefined. 2463 * Check is the value is undefined.
2461 * @return {boolean} true if the value is undefined 2464 * @return {boolean} true if the value is undefined
2462 */ 2465 */
2463 ProtocolValue.prototype.isUndefined = function() { 2466 ProtocolValue.prototype.isUndefined = function() {
2464 return this.value_.type == 'undefined'; 2467 return this.value_.type == 'undefined';
2465 } 2468 };
2466 2469
2467 2470
2468 /** 2471 /**
2469 * Check is the value is null. 2472 * Check is the value is null.
2470 * @return {boolean} true if the value is null 2473 * @return {boolean} true if the value is null
2471 */ 2474 */
2472 ProtocolValue.prototype.isNull = function() { 2475 ProtocolValue.prototype.isNull = function() {
2473 return this.value_.type == 'null'; 2476 return this.value_.type == 'null';
2474 } 2477 };
2475 2478
2476 2479
2477 /** 2480 /**
2478 * Check is the value is a boolean. 2481 * Check is the value is a boolean.
2479 * @return {boolean} true if the value is a boolean 2482 * @return {boolean} true if the value is a boolean
2480 */ 2483 */
2481 ProtocolValue.prototype.isBoolean = function() { 2484 ProtocolValue.prototype.isBoolean = function() {
2482 return this.value_.type == 'boolean'; 2485 return this.value_.type == 'boolean';
2483 } 2486 };
2484 2487
2485 2488
2486 /** 2489 /**
2487 * Check is the value is a number. 2490 * Check is the value is a number.
2488 * @return {boolean} true if the value is a number 2491 * @return {boolean} true if the value is a number
2489 */ 2492 */
2490 ProtocolValue.prototype.isNumber = function() { 2493 ProtocolValue.prototype.isNumber = function() {
2491 return this.value_.type == 'number'; 2494 return this.value_.type == 'number';
2492 } 2495 };
2493 2496
2494 2497
2495 /** 2498 /**
2496 * Check is the value is a string. 2499 * Check is the value is a string.
2497 * @return {boolean} true if the value is a string 2500 * @return {boolean} true if the value is a string
2498 */ 2501 */
2499 ProtocolValue.prototype.isString = function() { 2502 ProtocolValue.prototype.isString = function() {
2500 return this.value_.type == 'string'; 2503 return this.value_.type == 'string';
2501 } 2504 };
2502 2505
2503 2506
2504 /** 2507 /**
2505 * Check is the value is an object. 2508 * Check is the value is an object.
2506 * @return {boolean} true if the value is an object 2509 * @return {boolean} true if the value is an object
2507 */ 2510 */
2508 ProtocolValue.prototype.isObject = function() { 2511 ProtocolValue.prototype.isObject = function() {
2509 return this.value_.type == 'object' || this.value_.type == 'function' || 2512 return this.value_.type == 'object' || this.value_.type == 'function' ||
2510 this.value_.type == 'error' || this.value_.type == 'regexp'; 2513 this.value_.type == 'error' || this.value_.type == 'regexp';
2511 } 2514 };
2512 2515
2513 2516
2514 /** 2517 /**
2515 * Get the constructor function 2518 * Get the constructor function
2516 * @return {ProtocolValue} constructor function 2519 * @return {ProtocolValue} constructor function
2517 */ 2520 */
2518 ProtocolValue.prototype.constructorFunctionValue = function() { 2521 ProtocolValue.prototype.constructorFunctionValue = function() {
2519 var ctor = this.value_.constructorFunction; 2522 var ctor = this.value_.constructorFunction;
2520 return this.packet_.lookup(ctor.ref); 2523 return this.packet_.lookup(ctor.ref);
2521 } 2524 };
2522 2525
2523 2526
2524 /** 2527 /**
2525 * Get the __proto__ value 2528 * Get the __proto__ value
2526 * @return {ProtocolValue} __proto__ value 2529 * @return {ProtocolValue} __proto__ value
2527 */ 2530 */
2528 ProtocolValue.prototype.protoObjectValue = function() { 2531 ProtocolValue.prototype.protoObjectValue = function() {
2529 var proto = this.value_.protoObject; 2532 var proto = this.value_.protoObject;
2530 return this.packet_.lookup(proto.ref); 2533 return this.packet_.lookup(proto.ref);
2531 } 2534 };
2532 2535
2533 2536
2534 /** 2537 /**
2535 * Get the number og properties. 2538 * Get the number og properties.
2536 * @return {number} the number of properties 2539 * @return {number} the number of properties
2537 */ 2540 */
2538 ProtocolValue.prototype.propertyCount = function() { 2541 ProtocolValue.prototype.propertyCount = function() {
2539 return this.value_.properties ? this.value_.properties.length : 0; 2542 return this.value_.properties ? this.value_.properties.length : 0;
2540 } 2543 };
2541 2544
2542 2545
2543 /** 2546 /**
2544 * Get the specified property name. 2547 * Get the specified property name.
2545 * @return {string} property name 2548 * @return {string} property name
2546 */ 2549 */
2547 ProtocolValue.prototype.propertyName = function(index) { 2550 ProtocolValue.prototype.propertyName = function(index) {
2548 var property = this.value_.properties[index]; 2551 var property = this.value_.properties[index];
2549 return property.name; 2552 return property.name;
2550 } 2553 };
2551 2554
2552 2555
2553 /** 2556 /**
2554 * Return index for the property name. 2557 * Return index for the property name.
2555 * @param name The property name to look for 2558 * @param name The property name to look for
2556 * @return {number} index for the property name 2559 * @return {number} index for the property name
2557 */ 2560 */
2558 ProtocolValue.prototype.propertyIndex = function(name) { 2561 ProtocolValue.prototype.propertyIndex = function(name) {
2559 for (var i = 0; i < this.propertyCount(); i++) { 2562 for (var i = 0; i < this.propertyCount(); i++) {
2560 if (this.value_.properties[i].name == name) { 2563 if (this.value_.properties[i].name == name) {
2561 return i; 2564 return i;
2562 } 2565 }
2563 } 2566 }
2564 return null; 2567 return null;
2565 } 2568 };
2566 2569
2567 2570
2568 /** 2571 /**
2569 * Get the specified property value. 2572 * Get the specified property value.
2570 * @return {ProtocolValue} property value 2573 * @return {ProtocolValue} property value
2571 */ 2574 */
2572 ProtocolValue.prototype.propertyValue = function(index) { 2575 ProtocolValue.prototype.propertyValue = function(index) {
2573 var property = this.value_.properties[index]; 2576 var property = this.value_.properties[index];
2574 return this.packet_.lookup(property.ref); 2577 return this.packet_.lookup(property.ref);
2575 } 2578 };
2576 2579
2577 2580
2578 /** 2581 /**
2579 * Check is the value is a string. 2582 * Check is the value is a string.
2580 * @return {boolean} true if the value is a string 2583 * @return {boolean} true if the value is a string
2581 */ 2584 */
2582 ProtocolValue.prototype.value = function() { 2585 ProtocolValue.prototype.value = function() {
2583 return this.value_.value; 2586 return this.value_.value;
2584 } 2587 };
2585 2588
2586 2589
2587 ProtocolValue.prototype.valueString = function() { 2590 ProtocolValue.prototype.valueString = function() {
2588 return this.value_.text; 2591 return this.value_.text;
2589 } 2592 };
2590 2593
2591 2594
2592 function ProtocolReference(handle) { 2595 function ProtocolReference(handle) {
2593 this.handle_ = handle; 2596 this.handle_ = handle;
2594 } 2597 }
2595 2598
2596 2599
2597 ProtocolReference.prototype.handle = function() { 2600 ProtocolReference.prototype.handle = function() {
2598 return this.handle_; 2601 return this.handle_;
2599 } 2602 };
2600 2603
2601 2604
2602 function MakeJSONPair_(name, value) { 2605 function MakeJSONPair_(name, value) {
2603 return '"' + name + '":' + value; 2606 return '"' + name + '":' + value;
2604 } 2607 }
2605 2608
2606 2609
2607 function ArrayToJSONObject_(content) { 2610 function ArrayToJSONObject_(content) {
2608 return '{' + content.join(',') + '}'; 2611 return '{' + content.join(',') + '}';
2609 } 2612 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2660 // Replace ", \ and control characters (0x00 - 0x1F). 2663 // Replace ", \ and control characters (0x00 - 0x1F).
2661 return '"' + 2664 return '"' +
2662 value.replace(ctrlCharMatch_, function (char) { 2665 value.replace(ctrlCharMatch_, function (char) {
2663 // Use charmap if possible. 2666 // Use charmap if possible.
2664 var mapped = ctrlCharMap_[char]; 2667 var mapped = ctrlCharMap_[char];
2665 if (mapped) return mapped; 2668 if (mapped) return mapped;
2666 mapped = char.charCodeAt(); 2669 mapped = char.charCodeAt();
2667 // Convert control character to unicode escape sequence. 2670 // Convert control character to unicode escape sequence.
2668 return '\\u00' + 2671 return '\\u00' +
2669 '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) + 2672 '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) +
2670 '0' // TODO %NumberToRadixString(mapped % 16, 16); 2673 '0'; // TODO %NumberToRadixString(mapped % 16, 16)
2671 }) 2674 })
2672 + '"'; 2675 + '"';
2673 } 2676 }
2674 2677
2675 // Simple string with no special characters. 2678 // Simple string with no special characters.
2676 return '"' + value + '"'; 2679 return '"' + value + '"';
2677 } 2680 }
2678 2681
2679 2682
2680 /** 2683 /**
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2731 if (typeof key == 'string') { 2734 if (typeof key == 'string') {
2732 var property_value = object[key]; 2735 var property_value = object[key];
2733 2736
2734 // Format the value based on its type. 2737 // Format the value based on its type.
2735 var property_value_json; 2738 var property_value_json;
2736 switch (typeof property_value) { 2739 switch (typeof property_value) {
2737 case 'object': 2740 case 'object':
2738 if (property_value === null) { 2741 if (property_value === null) {
2739 property_value_json = 'null'; 2742 property_value_json = 'null';
2740 } else if (typeof property_value.toJSONProtocol == 'function') { 2743 } else if (typeof property_value.toJSONProtocol == 'function') {
2741 property_value_json = property_value.toJSONProtocol(true) 2744 property_value_json = property_value.toJSONProtocol(true);
2742 } else if (property_value.constructor.name == 'Array'){ 2745 } else if (property_value.constructor.name == 'Array'){
2743 property_value_json = SimpleArrayToJSON_(property_value); 2746 property_value_json = SimpleArrayToJSON_(property_value);
2744 } else { 2747 } else {
2745 property_value_json = SimpleObjectToJSON_(property_value); 2748 property_value_json = SimpleObjectToJSON_(property_value);
2746 } 2749 }
2747 break; 2750 break;
2748 2751
2749 case 'boolean': 2752 case 'boolean':
2750 property_value_json = BooleanToJSON_(property_value); 2753 property_value_json = BooleanToJSON_(property_value);
2751 break; 2754 break;
(...skipping 30 matching lines...) Expand all
2782 */ 2785 */
2783 function SimpleArrayToJSON_(array) { 2786 function SimpleArrayToJSON_(array) {
2784 // Make JSON array representation. 2787 // Make JSON array representation.
2785 var json = '['; 2788 var json = '[';
2786 for (var i = 0; i < array.length; i++) { 2789 for (var i = 0; i < array.length; i++) {
2787 if (i != 0) { 2790 if (i != 0) {
2788 json += ','; 2791 json += ',';
2789 } 2792 }
2790 var elem = array[i]; 2793 var elem = array[i];
2791 if (elem.toJSONProtocol) { 2794 if (elem.toJSONProtocol) {
2792 json += elem.toJSONProtocol(true) 2795 json += elem.toJSONProtocol(true);
2793 } else if (typeof(elem) === 'object') { 2796 } else if (typeof(elem) === 'object') {
2794 json += SimpleObjectToJSON_(elem); 2797 json += SimpleObjectToJSON_(elem);
2795 } else if (typeof(elem) === 'boolean') { 2798 } else if (typeof(elem) === 'boolean') {
2796 json += BooleanToJSON_(elem); 2799 json += BooleanToJSON_(elem);
2797 } else if (typeof(elem) === 'number') { 2800 } else if (typeof(elem) === 'number') {
2798 json += NumberToJSON_(elem); 2801 json += NumberToJSON_(elem);
2799 } else if (typeof(elem) === 'string') { 2802 } else if (typeof(elem) === 'string') {
2800 json += StringToJSON_(elem); 2803 json += StringToJSON_(elem);
2801 } else { 2804 } else {
2802 json += elem; 2805 json += elem;
2803 } 2806 }
2804 } 2807 }
2805 json += ']'; 2808 json += ']';
2806 return json; 2809 return json;
2807 } 2810 }
OLDNEW
« no previous file with comments | « src/array.js ('k') | src/date.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698