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