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