T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | var Thrift = { |
Jake Farrell | 9901069 | 2011-11-30 02:09:46 +0000 | [diff] [blame] | 20 | Version: '0.9.0-dev', |
Roger Meier | 0244e93 | 2011-03-09 15:25:01 +0000 | [diff] [blame] | 21 | /* |
| 22 | Description: 'JavaScript bindings for the Apache Thrift RPC system', |
| 23 | License: 'http://www.apache.org/licenses/LICENSE-2.0', |
| 24 | Homepage: 'http://thrift.apache.org', |
| 25 | BugReports: 'https://issues.apache.org/jira/browse/THRIFT', |
| 26 | Maintainer: 'dev@thrift.apache.org', |
| 27 | */ |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 28 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 29 | Type: { |
| 30 | 'STOP' : 0, |
| 31 | 'VOID' : 1, |
| 32 | 'BOOL' : 2, |
| 33 | 'BYTE' : 3, |
| 34 | 'I08' : 3, |
| 35 | 'DOUBLE' : 4, |
| 36 | 'I16' : 6, |
| 37 | 'I32' : 8, |
| 38 | 'I64' : 10, |
| 39 | 'STRING' : 11, |
| 40 | 'UTF7' : 11, |
| 41 | 'STRUCT' : 12, |
| 42 | 'MAP' : 13, |
| 43 | 'SET' : 14, |
| 44 | 'LIST' : 15, |
| 45 | 'UTF8' : 16, |
| 46 | 'UTF16' : 17 |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 47 | }, |
| 48 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 49 | MessageType: { |
| 50 | 'CALL' : 1, |
| 51 | 'REPLY' : 2, |
| 52 | 'EXCEPTION' : 3 |
Roger Meier | 55ea68f | 2011-02-16 19:29:50 +0000 | [diff] [blame] | 53 | }, |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 54 | |
| 55 | objectLength: function(obj) { |
| 56 | var length = 0; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 57 | for (var k in obj) { |
| 58 | if (obj.hasOwnProperty(k)) { |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 59 | length++; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 63 | return length; |
Roger Meier | 55ea68f | 2011-02-16 19:29:50 +0000 | [diff] [blame] | 64 | }, |
| 65 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 66 | inherits: function(constructor, superConstructor) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 67 | //Prototypal Inheritance http://javascript.crockford.com/prototypal.html |
| 68 | function F() {} |
| 69 | F.prototype = superConstructor.prototype; |
| 70 | constructor.prototype = new F(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 71 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 72 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 73 | |
Roger Meier | 55ea68f | 2011-02-16 19:29:50 +0000 | [diff] [blame] | 74 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 75 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 76 | Thrift.TException = function(message) { |
| 77 | this.message = message; |
| 78 | }; |
| 79 | Thrift.inherits(Thrift.TException, Error); |
| 80 | Thrift.TException.prototype.name = 'TException'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 81 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 82 | Thrift.TApplicationExceptionType = { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 83 | 'UNKNOWN' : 0, |
| 84 | 'UNKNOWN_METHOD' : 1, |
| 85 | 'INVALID_MESSAGE_TYPE' : 2, |
| 86 | 'WRONG_METHOD_NAME' : 3, |
| 87 | 'BAD_SEQUENCE_ID' : 4, |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame] | 88 | 'MISSING_RESULT' : 5, |
| 89 | 'INTERNAL_ERROR' : 6, |
| 90 | 'PROTOCOL_ERROR' : 7 |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 91 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 92 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 93 | Thrift.TApplicationException = function(message, code) { |
| 94 | this.message = message; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 95 | this.code = (code === null) ? 0 : code; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 96 | }; |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 97 | Thrift.inherits(Thrift.TApplicationException, Thrift.TException); |
| 98 | Thrift.TApplicationException.prototype.name = 'TApplicationException'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 99 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 100 | Thrift.TApplicationException.prototype.read = function(input) { |
| 101 | while (1) { |
| 102 | var ret = input.readFieldBegin(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 103 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 104 | if (ret.ftype == Thrift.Type.STOP) { |
| 105 | break; |
| 106 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 107 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 108 | var fid = ret.fid; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 109 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 110 | switch (fid) { |
| 111 | case 1: |
| 112 | if (ret.ftype == Thrift.Type.STRING) { |
| 113 | ret = input.readString(); |
| 114 | this.message = ret.value; |
| 115 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 116 | ret = input.skip(ret.ftype); |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 117 | } |
| 118 | break; |
| 119 | case 2: |
| 120 | if (ret.ftype == Thrift.Type.I32) { |
| 121 | ret = input.readI32(); |
| 122 | this.code = ret.value; |
| 123 | } else { |
| 124 | ret = input.skip(ret.ftype); |
| 125 | } |
| 126 | break; |
| 127 | default: |
| 128 | ret = input.skip(ret.ftype); |
| 129 | break; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 130 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 131 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 132 | input.readFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 133 | } |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 134 | |
| 135 | input.readStructEnd(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 136 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 137 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 138 | Thrift.TApplicationException.prototype.write = function(output) { |
| 139 | var xfer = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 140 | |
Roger Meier | 6b0d456 | 2011-07-08 12:44:29 +0000 | [diff] [blame] | 141 | output.writeStructBegin('TApplicationException'); |
| 142 | |
| 143 | if (this.message) { |
| 144 | output.writeFieldBegin('message', Thrift.Type.STRING, 1); |
| 145 | output.writeString(this.getMessage()); |
| 146 | output.writeFieldEnd(); |
| 147 | } |
| 148 | |
| 149 | if (this.code) { |
| 150 | output.writeFieldBegin('type', Thrift.Type.I32, 2); |
| 151 | output.writeI32(this.code); |
| 152 | output.writeFieldEnd(); |
| 153 | } |
| 154 | |
| 155 | output.writeFieldStop(); |
| 156 | output.writeStructEnd(); |
| 157 | }; |
| 158 | |
| 159 | Thrift.TApplicationException.prototype.getCode = function() { |
| 160 | return this.code; |
| 161 | }; |
| 162 | |
| 163 | Thrift.TApplicationException.prototype.getMessage = function() { |
| 164 | return this.message; |
| 165 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | *If you do not specify a url then you must handle ajax on your own. |
| 169 | *This is how to use js bindings in a async fashion. |
| 170 | */ |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 171 | Thrift.Transport = function(url) { |
| 172 | this.url = url; |
| 173 | this.wpos = 0; |
| 174 | this.rpos = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 175 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 176 | this.send_buf = ''; |
| 177 | this.recv_buf = ''; |
| 178 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 179 | |
| 180 | Thrift.Transport.prototype = { |
| 181 | |
| 182 | //Gets the browser specific XmlHttpRequest Object |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 183 | getXmlHttpRequestObject: function() { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 184 | try { return new XMLHttpRequest(); } catch (e1) { } |
| 185 | try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { } |
| 186 | try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 187 | |
| 188 | throw "Your browser doesn't support the XmlHttpRequest object."; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 189 | }, |
| 190 | |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 191 | flush: function(async) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 192 | //async mode |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 193 | if (async || this.url === undefined || this.url === '') { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 194 | return this.send_buf; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 195 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 196 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 197 | var xreq = this.getXmlHttpRequestObject(); |
| 198 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 199 | if (xreq.overrideMimeType) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 200 | xreq.overrideMimeType('application/json'); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 201 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 202 | |
| 203 | xreq.open('POST', this.url, false); |
| 204 | xreq.send(this.send_buf); |
| 205 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 206 | if (xreq.readyState != 4) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 207 | throw 'encountered an unknown ajax ready state: ' + xreq.readyState; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 208 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 209 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 210 | if (xreq.status != 200) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 211 | throw 'encountered a unknown request status: ' + xreq.status; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 212 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 213 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 214 | this.recv_buf = xreq.responseText; |
| 215 | this.recv_buf_sz = this.recv_buf.length; |
| 216 | this.wpos = this.recv_buf.length; |
| 217 | this.rpos = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 218 | }, |
| 219 | |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 220 | jqRequest: function(client, postData, args, recv_method) { |
| 221 | if (typeof jQuery === 'undefined' || |
| 222 | typeof jQuery.Deferred === 'undefined') { |
| 223 | throw 'Thrift.js requires jQuery 1.5+ to use asynchronous requests'; |
| 224 | } |
| 225 | |
Roger Meier | f666d07 | 2012-05-01 11:20:12 +0000 | [diff] [blame] | 226 | var thriftTransport = this; |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 227 | |
| 228 | var jqXHR = jQuery.ajax({ |
| 229 | url: this.url, |
| 230 | data: postData, |
| 231 | type: 'POST', |
| 232 | cache: false, |
Roger Meier | f666d07 | 2012-05-01 11:20:12 +0000 | [diff] [blame] | 233 | dataType: 'text thrift', |
| 234 | converters: { |
| 235 | 'text thrift' : function(responseData) { |
| 236 | thriftTransport.setRecvBuffer(responseData); |
| 237 | var value = recv_method.call(client); |
| 238 | return value; |
| 239 | } |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 240 | }, |
Roger Meier | f666d07 | 2012-05-01 11:20:12 +0000 | [diff] [blame] | 241 | context: client, |
| 242 | success: jQuery.makeArray(args).pop() |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 243 | }); |
| 244 | |
Roger Meier | f666d07 | 2012-05-01 11:20:12 +0000 | [diff] [blame] | 245 | return jqXHR; |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 246 | }, |
| 247 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 248 | setRecvBuffer: function(buf) { |
| 249 | this.recv_buf = buf; |
| 250 | this.recv_buf_sz = this.recv_buf.length; |
| 251 | this.wpos = this.recv_buf.length; |
| 252 | this.rpos = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 253 | }, |
| 254 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 255 | isOpen: function() { |
| 256 | return true; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 257 | }, |
| 258 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 259 | open: function() {}, |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 260 | |
| 261 | close: function() {}, |
| 262 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 263 | read: function(len) { |
| 264 | var avail = this.wpos - this.rpos; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 265 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 266 | if (avail === 0) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 267 | return ''; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 268 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 269 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 270 | var give = len; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 271 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 272 | if (avail < len) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 273 | give = avail; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 274 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 275 | |
| 276 | var ret = this.read_buf.substr(this.rpos, give); |
| 277 | this.rpos += give; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 278 | |
| 279 | //clear buf when complete? |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 280 | return ret; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 281 | }, |
| 282 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 283 | readAll: function() { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 284 | return this.recv_buf; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 285 | }, |
| 286 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 287 | write: function(buf) { |
| 288 | this.send_buf = buf; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 289 | }, |
| 290 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 291 | getSendBuffer: function() { |
| 292 | return this.send_buf; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 295 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 296 | |
| 297 | |
| 298 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 299 | Thrift.Protocol = function(transport) { |
| 300 | this.transport = transport; |
| 301 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 302 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 303 | Thrift.Protocol.Type = {}; |
| 304 | Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"'; |
| 305 | Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"'; |
| 306 | Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"'; |
| 307 | Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"'; |
| 308 | Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"'; |
| 309 | Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"'; |
| 310 | Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"'; |
| 311 | Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"'; |
| 312 | Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"'; |
| 313 | Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"'; |
| 314 | Thrift.Protocol.Type[Thrift.Type.SET] = '"set"'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 315 | |
| 316 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 317 | Thrift.Protocol.RType = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 318 | Thrift.Protocol.RType.tf = Thrift.Type.BOOL; |
| 319 | Thrift.Protocol.RType.i8 = Thrift.Type.BYTE; |
| 320 | Thrift.Protocol.RType.i16 = Thrift.Type.I16; |
| 321 | Thrift.Protocol.RType.i32 = Thrift.Type.I32; |
| 322 | Thrift.Protocol.RType.i64 = Thrift.Type.I64; |
| 323 | Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE; |
| 324 | Thrift.Protocol.RType.rec = Thrift.Type.STRUCT; |
| 325 | Thrift.Protocol.RType.str = Thrift.Type.STRING; |
| 326 | Thrift.Protocol.RType.map = Thrift.Type.MAP; |
| 327 | Thrift.Protocol.RType.lst = Thrift.Type.LIST; |
| 328 | Thrift.Protocol.RType.set = Thrift.Type.SET; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 329 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 330 | Thrift.Protocol.Version = 1; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 331 | |
| 332 | Thrift.Protocol.prototype = { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 333 | |
| 334 | getTransport: function() { |
| 335 | return this.transport; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 336 | }, |
| 337 | |
| 338 | //Write functions |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 339 | writeMessageBegin: function(name, messageType, seqid) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 340 | this.tstack = []; |
| 341 | this.tpos = []; |
| 342 | |
| 343 | this.tstack.push([Thrift.Protocol.Version, '"' + |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 344 | name + '"', messageType, seqid]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 345 | }, |
| 346 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 347 | writeMessageEnd: function() { |
| 348 | var obj = this.tstack.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 349 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 350 | this.wobj = this.tstack.pop(); |
| 351 | this.wobj.push(obj); |
| 352 | |
| 353 | this.wbuf = '[' + this.wobj.join(',') + ']'; |
| 354 | |
| 355 | this.transport.write(this.wbuf); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 356 | }, |
| 357 | |
| 358 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 359 | writeStructBegin: function(name) { |
| 360 | this.tpos.push(this.tstack.length); |
| 361 | this.tstack.push({}); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 362 | }, |
| 363 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 364 | writeStructEnd: function() { |
| 365 | |
| 366 | var p = this.tpos.pop(); |
| 367 | var struct = this.tstack[p]; |
| 368 | var str = '{'; |
| 369 | var first = true; |
| 370 | for (var key in struct) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 371 | if (first) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 372 | first = false; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 373 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 374 | str += ','; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 375 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 376 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 377 | str += key + ':' + struct[key]; |
| 378 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 379 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 380 | str += '}'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 381 | this.tstack[p] = str; |
| 382 | }, |
| 383 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 384 | writeFieldBegin: function(name, fieldType, fieldId) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 385 | this.tpos.push(this.tstack.length); |
| 386 | this.tstack.push({ 'fieldId': '"' + |
| 387 | fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType] |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 388 | }); |
| 389 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 390 | }, |
| 391 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 392 | writeFieldEnd: function() { |
| 393 | var value = this.tstack.pop(); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 394 | var fieldInfo = this.tstack.pop(); |
| 395 | |
| 396 | this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' + |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 397 | fieldInfo.fieldType + ':' + value + '}'; |
| 398 | this.tpos.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 399 | }, |
| 400 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 401 | writeFieldStop: function() { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 402 | //na |
| 403 | }, |
| 404 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 405 | writeMapBegin: function(keyType, valType, size) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 406 | //size is invalid, we'll set it on end. |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 407 | this.tpos.push(this.tstack.length); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 408 | this.tstack.push([Thrift.Protocol.Type[keyType], |
| 409 | Thrift.Protocol.Type[valType], 0]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 410 | }, |
| 411 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 412 | writeMapEnd: function() { |
| 413 | var p = this.tpos.pop(); |
| 414 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 415 | if (p == this.tstack.length) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 416 | return; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 417 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 418 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 419 | if ((this.tstack.length - p - 1) % 2 !== 0) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 420 | this.tstack.push(''); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 421 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 422 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 423 | var size = (this.tstack.length - p - 1) / 2; |
| 424 | |
| 425 | this.tstack[p][this.tstack[p].length - 1] = size; |
| 426 | |
| 427 | var map = '}'; |
| 428 | var first = true; |
| 429 | while (this.tstack.length > p + 1) { |
| 430 | var v = this.tstack.pop(); |
| 431 | var k = this.tstack.pop(); |
| 432 | if (first) { |
| 433 | first = false; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 434 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 435 | map = ',' + map; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 436 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 437 | |
Roger Meier | 2079bfe | 2011-06-21 14:09:13 +0000 | [diff] [blame] | 438 | if (! isNaN(k)) { k = '"' + k + '"'; } //json "keys" need to be strings |
Roger Meier | 9d8e8f8 | 2011-06-14 19:38:27 +0000 | [diff] [blame] | 439 | map = k + ':' + v + map; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 440 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 441 | map = '{' + map; |
| 442 | |
| 443 | this.tstack[p].push(map); |
| 444 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 445 | }, |
| 446 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 447 | writeListBegin: function(elemType, size) { |
| 448 | this.tpos.push(this.tstack.length); |
| 449 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 450 | }, |
| 451 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 452 | writeListEnd: function() { |
| 453 | var p = this.tpos.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 454 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 455 | while (this.tstack.length > p + 1) { |
| 456 | var tmpVal = this.tstack[p + 1]; |
| 457 | this.tstack.splice(p + 1, 1); |
| 458 | this.tstack[p].push(tmpVal); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 461 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 462 | }, |
| 463 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 464 | writeSetBegin: function(elemType, size) { |
| 465 | this.tpos.push(this.tstack.length); |
| 466 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 467 | }, |
| 468 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 469 | writeSetEnd: function() { |
| 470 | var p = this.tpos.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 471 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 472 | while (this.tstack.length > p + 1) { |
| 473 | var tmpVal = this.tstack[p + 1]; |
| 474 | this.tstack.splice(p + 1, 1); |
| 475 | this.tstack[p].push(tmpVal); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 478 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 479 | }, |
| 480 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 481 | writeBool: function(value) { |
| 482 | this.tstack.push(value ? 1 : 0); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 483 | }, |
| 484 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 485 | writeByte: function(i8) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 486 | this.tstack.push(i8); |
| 487 | }, |
| 488 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 489 | writeI16: function(i16) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 490 | this.tstack.push(i16); |
| 491 | }, |
| 492 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 493 | writeI32: function(i32) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 494 | this.tstack.push(i32); |
| 495 | }, |
| 496 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 497 | writeI64: function(i64) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 498 | this.tstack.push(i64); |
| 499 | }, |
| 500 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 501 | writeDouble: function(dbl) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 502 | this.tstack.push(dbl); |
| 503 | }, |
| 504 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 505 | writeString: function(str) { |
| 506 | // We do not encode uri components for wire transfer: |
| 507 | if (str === null) { |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 508 | this.tstack.push(null); |
| 509 | } else { |
| 510 | // concat may be slower than building a byte buffer |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 511 | var escapedString = ''; |
| 512 | for (var i = 0; i < str.length; i++) { |
| 513 | var ch = str.charAt(i); // a single double quote: " |
| 514 | if (ch === '\"') { |
| 515 | escapedString += '\\\"'; // write out as: \" |
| 516 | } else if (ch === '\\') { // a single backslash: \ |
| 517 | escapedString += '\\\\'; // write out as: \\ |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 518 | /* Currently escaped forward slashes break TJSONProtocol. |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 519 | * As it stands, we can simply pass forward slashes into |
| 520 | * our strings across the wire without being escaped. |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 521 | * I think this is the protocol's bug, not thrift.js |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 522 | * } else if(ch === '/') { // a single forward slash: / |
| 523 | * escapedString += '\\/'; // write out as \/ |
| 524 | * } |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 525 | */ |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 526 | } else if (ch === '\b') { // a single backspace: invisible |
| 527 | escapedString += '\\b'; // write out as: \b" |
| 528 | } else if (ch === '\f') { // a single formfeed: invisible |
| 529 | escapedString += '\\f'; // write out as: \f" |
| 530 | } else if (ch === '\n') { // a single newline: invisible |
| 531 | escapedString += '\\n'; // write out as: \n" |
| 532 | } else if (ch === '\r') { // a single return: invisible |
| 533 | escapedString += '\\r'; // write out as: \r" |
| 534 | } else if (ch === '\t') { // a single tab: invisible |
| 535 | escapedString += '\\t'; // write out as: \t" |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 536 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 537 | escapedString += ch; // Else it need not be escaped |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | this.tstack.push('"' + escapedString + '"'); |
| 541 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 542 | }, |
| 543 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 544 | writeBinary: function(str) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 545 | this.writeString(str); |
| 546 | }, |
| 547 | |
| 548 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 549 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 550 | // Reading functions |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 551 | readMessageBegin: function(name, messageType, seqid) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 552 | this.rstack = []; |
| 553 | this.rpos = []; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 554 | |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 555 | if (typeof jQuery !== 'undefined') { |
| 556 | this.robj = jQuery.parseJSON(this.transport.readAll()); |
| 557 | } else { |
| 558 | this.robj = eval(this.transport.readAll()); |
| 559 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 560 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 561 | var r = {}; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 562 | var version = this.robj.shift(); |
| 563 | |
| 564 | if (version != Thrift.Protocol.Version) { |
| 565 | throw 'Wrong thrift protocol version: ' + version; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 566 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 567 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 568 | r.fname = this.robj.shift(); |
| 569 | r.mtype = this.robj.shift(); |
| 570 | r.rseqid = this.robj.shift(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 571 | |
| 572 | |
| 573 | //get to the main obj |
| 574 | this.rstack.push(this.robj.shift()); |
| 575 | |
| 576 | return r; |
| 577 | }, |
| 578 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 579 | readMessageEnd: function() { |
| 580 | }, |
| 581 | |
| 582 | readStructBegin: function(name) { |
| 583 | var r = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 584 | r.fname = ''; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 585 | |
| 586 | //incase this is an array of structs |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 587 | if (this.rstack[this.rstack.length - 1] instanceof Array) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 588 | this.rstack.push(this.rstack[this.rstack.length - 1].shift()); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 589 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 590 | |
| 591 | return r; |
| 592 | }, |
| 593 | |
| 594 | readStructEnd: function() { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 595 | if (this.rstack[this.rstack.length - 2] instanceof Array) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 596 | this.rstack.pop(); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 597 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 598 | }, |
| 599 | |
| 600 | readFieldBegin: function() { |
| 601 | var r = {}; |
| 602 | |
| 603 | var fid = -1; |
| 604 | var ftype = Thrift.Type.STOP; |
| 605 | |
| 606 | //get a fieldId |
| 607 | for (var f in (this.rstack[this.rstack.length - 1])) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 608 | if (f === null) { |
| 609 | continue; |
| 610 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 611 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 612 | fid = parseInt(f, 10); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 613 | this.rpos.push(this.rstack.length); |
| 614 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 615 | var field = this.rstack[this.rstack.length - 1][fid]; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 616 | |
| 617 | //remove so we don't see it again |
| 618 | delete this.rstack[this.rstack.length - 1][fid]; |
| 619 | |
| 620 | this.rstack.push(field); |
| 621 | |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | if (fid != -1) { |
| 626 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 627 | //should only be 1 of these but this is the only |
| 628 | //way to match a key |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 629 | for (var i in (this.rstack[this.rstack.length - 1])) { |
| 630 | if (Thrift.Protocol.RType[i] === null) { |
| 631 | continue; |
| 632 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 633 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 634 | ftype = Thrift.Protocol.RType[i]; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 635 | this.rstack[this.rstack.length - 1] = |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 636 | this.rstack[this.rstack.length - 1][i]; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 640 | r.fname = ''; |
| 641 | r.ftype = ftype; |
| 642 | r.fid = fid; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 643 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 644 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 645 | }, |
| 646 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 647 | readFieldEnd: function() { |
| 648 | var pos = this.rpos.pop(); |
| 649 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 650 | //get back to the right place in the stack |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 651 | while (this.rstack.length > pos) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 652 | this.rstack.pop(); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 653 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 654 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 655 | }, |
| 656 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 657 | readMapBegin: function(keyType, valType, size) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 658 | var map = this.rstack.pop(); |
| 659 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 660 | var r = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 661 | r.ktype = Thrift.Protocol.RType[map.shift()]; |
| 662 | r.vtype = Thrift.Protocol.RType[map.shift()]; |
| 663 | r.size = map.shift(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 664 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 665 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 666 | this.rpos.push(this.rstack.length); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 667 | this.rstack.push(map.shift()); |
| 668 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 669 | return r; |
| 670 | }, |
| 671 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 672 | readMapEnd: function() { |
| 673 | this.readFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 674 | }, |
| 675 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 676 | readListBegin: function(elemType, size) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 677 | var list = this.rstack[this.rstack.length - 1]; |
| 678 | |
| 679 | var r = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 680 | r.etype = Thrift.Protocol.RType[list.shift()]; |
| 681 | r.size = list.shift(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 682 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 683 | this.rpos.push(this.rstack.length); |
| 684 | this.rstack.push(list); |
| 685 | |
| 686 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 687 | }, |
| 688 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 689 | readListEnd: function() { |
| 690 | this.readFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 691 | }, |
| 692 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 693 | readSetBegin: function(elemType, size) { |
| 694 | return this.readListBegin(elemType, size); |
| 695 | }, |
| 696 | |
| 697 | readSetEnd: function() { |
| 698 | return this.readListEnd(); |
| 699 | }, |
| 700 | |
| 701 | readBool: function() { |
| 702 | var r = this.readI32(); |
| 703 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 704 | if (r !== null && r.value == '1') { |
| 705 | r.value = true; |
| 706 | } else { |
| 707 | r.value = false; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 708 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 709 | |
| 710 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 711 | }, |
| 712 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 713 | readByte: function() { |
| 714 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 715 | }, |
| 716 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 717 | readI16: function() { |
| 718 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 719 | }, |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 720 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 721 | readI32: function(f) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 722 | if (f === undefined) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 723 | f = this.rstack[this.rstack.length - 1]; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 724 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 725 | |
| 726 | var r = {}; |
| 727 | |
| 728 | if (f instanceof Array) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 729 | if (f.length === 0) { |
| 730 | r.value = undefined; |
| 731 | } else { |
| 732 | r.value = f.shift(); |
| 733 | } |
| 734 | } else if (f instanceof Object) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 735 | for (var i in f) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 736 | if (i === null) { |
| 737 | continue; |
| 738 | } |
| 739 | this.rstack.push(f[i]); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 740 | delete f[i]; |
| 741 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 742 | r.value = i; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 743 | break; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 744 | } |
| 745 | } else { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 746 | r.value = f; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 747 | this.rstack.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 748 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 749 | |
| 750 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 751 | }, |
| 752 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 753 | readI64: function() { |
| 754 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 755 | }, |
| 756 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 757 | readDouble: function() { |
| 758 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 759 | }, |
| 760 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 761 | readString: function() { |
| 762 | var r = this.readI32(); |
| 763 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 764 | }, |
| 765 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 766 | readBinary: function() { |
| 767 | return this.readString(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 768 | }, |
| 769 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 770 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 771 | //Method to arbitrarily skip over data. |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 772 | skip: function(type) { |
| 773 | throw 'skip not supported yet'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 774 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 775 | }; |