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