├── LICENSE ├── README.md └── src └── ubjson.js /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | L16 has the syntax of Universal Binary JSON (UBJSON, http://ubjson.org/). 2 | 3 | L16 has the semantics of parsing and rendering UBJSON to and from JSON compatible object values. 4 | 5 | L16 is written primarily in ASM.JS, a high performance subset of JavaScript. 6 | 7 | -------------------------------------------------------------------------------- /src/ubjson.js: -------------------------------------------------------------------------------- 1 | /* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil; tab-width: 2 -*- */ 2 | /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ 3 | /* 4 | * Copyright 2013 Art Compiler LLC 5 | * Copyright 2013 Mozilla Foundation 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | "use strict"; 21 | 22 | /* 23 | This module implements a builder of UBJSON objects. 24 | 25 | startObject(flags) 26 | finishObject(size) 27 | startArray(flags) 28 | finishArray(size) 29 | startString(flags) 30 | finishString(size) 31 | writeByte(val) 32 | writeI16(val) 33 | writeI32(val) 34 | writeI64(val) 35 | writeF32(val) 36 | writeF64(val) 37 | 38 | imported functions 39 | 40 | imports = { 41 | resizeHeap, 42 | } 43 | 44 | */ 45 | 46 | var DEBUG = true; 47 | 48 | var assert = !DEBUG 49 | ? function () { } 50 | : function (val, str) { 51 | if ( str === void 0 ) { 52 | str = "failed!"; 53 | } 54 | if ( !val ) { 55 | throw("assert: " + str); 56 | } 57 | }; 58 | 59 | var global = this; 60 | 61 | var trace = function trace(str) { 62 | if (global.console && global.console.log) { 63 | console.log(str); 64 | } else if (global.print) { 65 | print(str); 66 | } else { 67 | throw "No trace function defined!"; 68 | } 69 | } 70 | 71 | var UBJSON = (function () { 72 | 73 | var Core = function Core(stdlib, imports, heap) { 74 | 75 | "use asm"; 76 | 77 | // BEGIN SNIP 78 | 79 | var pos = 0; 80 | var bytesU8 = new stdlib.Uint8Array(heap); 81 | var bytesD32 = new stdlib.Float32Array(heap); 82 | var bytesD64 = new stdlib.Float64Array(heap); 83 | 84 | var trace = imports.trace; 85 | var imul = stdlib.Math.imul; 86 | var pushNull = imports.pushNull; 87 | var pushTrue = imports.pushTrue; 88 | var pushFalse = imports.pushFalse; 89 | var pushInt = imports.pushInt; 90 | var pushFloat32 = imports.pushFloat32; 91 | var pushFloat64 = imports.pushFloat64; 92 | var pushString = imports.pushString; 93 | var newObject = imports.newObject; 94 | var newArray = imports.newArray; 95 | 96 | // Markers for UBJSON types 97 | var $o = 111; 98 | var $O = 79; 99 | var $a = 97; 100 | var $A = 65; 101 | var $B = 66; 102 | var $i = 105; 103 | var $I = 73; 104 | var $L = 76; 105 | var $d = 100; 106 | var $D = 68; 107 | var $s = 115; 108 | var $S = 83; 109 | var $Z = 90; 110 | var $T = 84; 111 | var $F = 70; 112 | 113 | var $_ = 95; // place holder byte 114 | 115 | // Flag bits 116 | var BIG = 0x00000001; 117 | 118 | // Return the current position in the ArrayBuffer. 119 | function getPos() { 120 | return pos | 0; 121 | } 122 | 123 | function setPos(p) { 124 | p = p | 0; 125 | pos = p; 126 | } 127 | 128 | // BEGIN Builder 129 | 130 | // Write a null value. 131 | function writeNull() { 132 | bytesU8[pos] = $Z; 133 | pos = pos + 1 | 0; 134 | } 135 | 136 | // Write a true value. 137 | function writeTrue() { 138 | bytesU8[pos] = $T; 139 | pos = pos + 1 | 0; 140 | } 141 | 142 | // Write a false value. 143 | function writeFalse() { 144 | bytesU8[pos] = $F; 145 | pos = pos + 1 | 0; 146 | } 147 | 148 | // Write a byte (int8) value. 149 | function writeByte(val) { 150 | val = val | 0; 151 | bytesU8[pos] = $B; 152 | bytesU8[pos + 1 | 0] = val; 153 | pos = pos + 2 | 0; 154 | } 155 | 156 | // Write an int16 value. 157 | function writeI16(val) { 158 | val = val | 0; 159 | bytesU8[pos] = $i; 160 | bytesU8[pos + 1 | 0] = val >> 8 & 0xFF; 161 | bytesU8[pos + 2 | 0] = val & 0xFF; 162 | pos = pos + 3 | 0; 163 | } 164 | 165 | // Write an int32 value. 166 | function writeI32(val) { 167 | val = val | 0; 168 | bytesU8[pos] = $I; 169 | bytesU8[pos + 1 | 0] = val >> 24 & 0xFF; 170 | bytesU8[pos + 2 | 0] = val >> 16 & 0xFF; 171 | bytesU8[pos + 3 | 0] = val >> 8 & 0xFF; 172 | bytesU8[pos + 4 | 0] = val & 0xFF; 173 | pos = pos + 5 | 0; 174 | } 175 | 176 | // WARNING writeD32() and writeD64() write bytes out with the reverse 177 | // endian-ness of the host computer. The order is reversed because UBJSON 178 | // demands big endian-ness and most computers use litte endian as their 179 | // native encoding. Either way the dependency of this implementation on the 180 | // native endian-ness of the host computer creates an incompatibility with 181 | // the UBJSON spec. This bug will only manifest itself when reading and 182 | // writing UBJSON values from a computer or UBJSON implementation with a 183 | // different endian-ness. However, these are not use cases that are in scope 184 | // for the current implementation. 185 | 186 | // Write an float32 value. 187 | function writeD32(val) { 188 | val = +val; 189 | var scratchPos = 0; 190 | scratchPos = imul(pos + 1, 4) | 0; 191 | bytesD32[scratchPos >> 2] = val; // Write out float32 to get bytes. 192 | bytesU8[pos] = $d; 193 | // Copy bytes in reverse order to produce big endian on Intel hardward. 194 | bytesU8[pos + 1 | 0] = bytesU8[scratchPos + 3 | 0]; 195 | bytesU8[pos + 2 | 0] = bytesU8[scratchPos + 2 | 0]; 196 | bytesU8[pos + 3 | 0] = bytesU8[scratchPos + 1 | 0]; 197 | bytesU8[pos + 4 | 0] = bytesU8[scratchPos | 0]; 198 | pos = pos + 5 | 0; 199 | //trace("pos="+pos); 200 | } 201 | 202 | // Write an float64 value. 203 | function writeD64(val) { 204 | val = +val; 205 | var scratchPos = 0; 206 | scratchPos = imul(pos + 1, 8) | 0; 207 | bytesD64[scratchPos >> 3] = val; // Write out float64 to get bytes. 208 | bytesU8[pos] = $D; 209 | // Copy bytes in reverse order to produce big endian on Intel hardward. 210 | bytesU8[pos + 1 | 0] = bytesU8[scratchPos + 7 | 0]; 211 | bytesU8[pos + 2 | 0] = bytesU8[scratchPos + 6 | 0]; 212 | bytesU8[pos + 3 | 0] = bytesU8[scratchPos + 5 | 0]; 213 | bytesU8[pos + 4 | 0] = bytesU8[scratchPos + 4 | 0]; 214 | bytesU8[pos + 5 | 0] = bytesU8[scratchPos + 3 | 0]; 215 | bytesU8[pos + 6 | 0] = bytesU8[scratchPos + 2 | 0]; 216 | bytesU8[pos + 7 | 0] = bytesU8[scratchPos + 1 | 0]; 217 | bytesU8[pos + 8 | 0] = bytesU8[scratchPos | 0]; 218 | pos = pos + 9 | 0; 219 | //trace("pos="+pos); 220 | } 221 | 222 | // Start an object. Allocate space for a new object. (flags & BIG) means 223 | // more than 255 properties. 224 | function startObject(flags) { 225 | flags = flags | 0; 226 | if ((flags & BIG | 0) == 0) { 227 | bytesU8[pos] = $o; 228 | bytesU8[pos + 1 | 0] = $_; 229 | pos = pos + 2 | 0; 230 | } else { 231 | bytesU8[pos] = $O; 232 | bytesU8[pos + 1 | 0] = $_; 233 | bytesU8[pos + 2 | 0] = $_; 234 | bytesU8[pos + 3 | 0] = $_; 235 | bytesU8[pos + 4 | 0] = $_; 236 | pos = pos + 5 | 0; 237 | } 238 | } 239 | 240 | // Finish an object. Offset is position before calling startObject(). 241 | function finishObject(offset, count, flags) { 242 | offset = offset | 0; 243 | count = count | 0; 244 | flags = flags | 0; 245 | if ((flags & BIG | 0) == 0) { 246 | bytesU8[offset + 1 | 0] = count; 247 | } else { 248 | bytesU8[offset + 1 | 0] = count >> 24 & 0xFF; 249 | bytesU8[offset + 2 | 0] = count >> 16 & 0xFF; 250 | bytesU8[offset + 3 | 0] = count >> 8 & 0xFF; 251 | bytesU8[offset + 4 | 0] = count & 0xFF; 252 | } 253 | } 254 | 255 | // Start an array. Allocate space for a new array. (flags & BIG) means 256 | // more than 255 elements. 257 | function startArray(flags) { 258 | flags = flags | 0; 259 | if ((flags & BIG | 0) == 0) { 260 | bytesU8[pos] = $a; 261 | bytesU8[pos + 1 | 0] = $_; 262 | pos = pos + 2 | 0; 263 | } else { 264 | bytesU8[pos] = $A; 265 | bytesU8[pos + 1 | 0] = $_; 266 | bytesU8[pos + 2 | 0] = $_; 267 | bytesU8[pos + 3 | 0] = $_; 268 | bytesU8[pos + 4 | 0] = $_; 269 | pos = pos + 5 | 0; 270 | } 271 | } 272 | 273 | // Finish an array. Offset is position before calling startArray(). 274 | function finishArray(offset, count, flags) { 275 | offset = offset | 0; 276 | count = count | 0; 277 | flags = flags | 0; 278 | if ((flags & BIG | 0) == 0) { 279 | bytesU8[offset + 1 | 0] = count; 280 | } else { 281 | bytesU8[offset + 1 | 0] = count >> 24 & 0xFF; 282 | bytesU8[offset + 2 | 0] = count >> 16 & 0xFF; 283 | bytesU8[offset + 3 | 0] = count >> 8 & 0xFF; 284 | bytesU8[offset + 4 | 0] = count & 0xFF; 285 | } 286 | } 287 | 288 | // Start a string value. Allocate space for a new string. (flags & BIG) 289 | // means contains more 255 bytes. Call writeStringChar() to add characters, 290 | // and finishString() to patch the byte count. Notice that characters are 291 | // encoded as UTF8 so they may consist of more than one byte. 292 | function startString(flags) { 293 | flags = flags | 0; 294 | if ((flags & BIG | 0) == 0) { 295 | bytesU8[pos] = $s; 296 | bytesU8[pos + 1 | 0] = $_; 297 | pos = pos + 2 | 0; 298 | } else { 299 | bytesU8[pos] = $S; 300 | bytesU8[pos + 1 | 0] = $_; 301 | bytesU8[pos + 2 | 0] = $_; 302 | bytesU8[pos + 3 | 0] = $_; 303 | bytesU8[pos + 4 | 0] = $_; 304 | pos = pos + 5 | 0; 305 | } 306 | } 307 | 308 | // Finish a string value. Patch its byte count. 309 | function finishString(offset, count, flags) { 310 | offset = offset | 0; 311 | count = count | 0; 312 | flags = flags | 0; 313 | if ((flags & BIG | 0) == 0) { 314 | bytesU8[offset + 1 | 0] = count; 315 | } else { 316 | bytesU8[offset + 1 | 0] = count >> 24 & 0xFF; 317 | bytesU8[offset + 2 | 0] = count >> 16 & 0xFF; 318 | bytesU8[offset + 3 | 0] = count >> 8 & 0xFF; 319 | bytesU8[offset + 4 | 0] = count & 0xFF; 320 | } 321 | } 322 | 323 | // Write a UTF8 character into a string value. 324 | function writeStringChar(val) { 325 | val = val | 0; 326 | // FIXME decode multibyte characters. 327 | bytesU8[pos] = val; 328 | pos = pos + 1 | 0; 329 | } 330 | // END Builder 331 | 332 | 333 | // BEGIN Decoder 334 | // Construct a UBJSON value from a sequence of bytes. 335 | function decode() { 336 | var marker = 0; 337 | var count = 0; 338 | var i = 0; 339 | while (1) { 340 | marker = readI8() | 0; 341 | //trace("decode() marker=" + marker); 342 | switch (marker | 0) { 343 | case 90: // $Z 344 | pushNull(); 345 | return; 346 | case 84: // $T 347 | pushTrue(); 348 | return; 349 | case 70: // $F 350 | pushFalse(); 351 | return; 352 | case 66: // $B 353 | pushInt(readI8() | 0); 354 | return; 355 | case 105: // $i 356 | pushInt(readI16() | 0); 357 | return; 358 | case 73: // $I 359 | pushInt(readI32() | 0); 360 | return; 361 | case 100: // $d 362 | pushFloat32(); 363 | return; 364 | case 68: // $D 365 | pushFloat64(); 366 | return; 367 | case 115: // $s 368 | pushString(readI8() | 0) 369 | return; 370 | case 83: // $S 371 | pushString(readI32() | 0) 372 | return; 373 | case 111: // $o 374 | //trace("$o"); 375 | count = readI8() | 0; 376 | for (i = 0; ~~i < ~~count; i = i + 1 | 0) { 377 | decode(); 378 | decode(); 379 | } 380 | newObject(count | 0); 381 | return; 382 | case 79: // $O 383 | count = readI32() | 0; 384 | for (i = 0; ~~i < ~~count; i = i + 1 | 0) { 385 | decode(); 386 | decode(); 387 | } 388 | newObject(count | 0); 389 | return; 390 | case 97: // $a 391 | count = readI8() | 0; 392 | for (i = 0; ~~i < ~~count; i = i + 1 | 0) { 393 | decode(); 394 | } 395 | newArray(count | 0); 396 | return; 397 | case 65: // $A 398 | count = readI32() | 0; 399 | for (i = 0; ~~i < ~~count; i = i + 1 | 0) { 400 | decode(); 401 | } 402 | newArray(count | 0); 403 | return; 404 | default: 405 | //trace("done"); 406 | return; 407 | } 408 | } 409 | } 410 | 411 | function readI8() { 412 | var val = 0; 413 | val = bytesU8[pos] | 0; 414 | pos = pos + 1 | 0; 415 | return val | 0; 416 | } 417 | 418 | // Read an int16 value. 419 | function readI16() { 420 | var val = 0; 421 | val = bytesU8[pos] << 8 | 0; 422 | val = (val + (bytesU8[pos + 1 | 0] | 0)) | 0; 423 | pos = pos + 2 | 0; 424 | return val | 0; 425 | } 426 | 427 | // Write an int32 value. 428 | function readI32() { 429 | var val = 0; 430 | val = bytesU8[pos] << 24; 431 | val = (val + (bytesU8[pos + 1 | 0] << 16 | 0)) | 0; 432 | val = (val + (bytesU8[pos + 2 | 0] << 8 | 0)) | 0; 433 | val = (val + (bytesU8[pos + 3 | 0] | 0)) | 0; 434 | pos = pos + 4 | 0; 435 | return val | 0; 436 | } 437 | // END Decoder 438 | // END SNIP 439 | 440 | // Exports 441 | return { 442 | // Builder functions 443 | writeNull: writeNull, 444 | writeTrue: writeTrue, 445 | writeFalse: writeFalse, 446 | writeByte: writeByte, 447 | writeI16: writeI16, 448 | writeI32: writeI32, 449 | writeD32: writeD32, 450 | writeD64: writeD64, 451 | startObject: startObject, 452 | finishObject: finishObject, 453 | startArray: startArray, 454 | finishArray: finishArray, 455 | startString: startString, 456 | finishString: finishString, 457 | writeStringChar: writeStringChar, 458 | // Decode function 459 | decode: decode, 460 | // Utils 461 | getPos: getPos, 462 | setPos: setPos, 463 | }; 464 | } 465 | 466 | var UBJSON = function UBJSON(buffer) { 467 | var view = new DataView(buffer); 468 | var values = []; 469 | var imports = { 470 | trace: trace, 471 | pushNull: function () { 472 | print("pushNull()"); 473 | values.push(null); 474 | }, 475 | pushTrue: function () { 476 | print("pushTrue()"); 477 | values.push(true); 478 | }, 479 | pushFalse: function () { 480 | print("pushFalse()"); 481 | values.push(false); 482 | }, 483 | pushInt: function (val) { 484 | print("pushInt() val=" + val); 485 | values.push(val); 486 | }, 487 | pushFloat32: function () { 488 | var pos = core.getPos(); 489 | var val = view.getFloat32(pos); 490 | print("pushFloat32() val=" + val); 491 | values.push(val); 492 | core.setPos(pos+4); 493 | }, 494 | pushFloat64: function () { 495 | var pos = core.getPos(); 496 | var val = view.getFloat64(pos); 497 | print("pushFloat64() val=" + val); 498 | values.push(val); 499 | core.setPos(pos+8); 500 | }, 501 | pushString: function (size) { 502 | print("pushString() size=" + size); 503 | var start = core.getPos(); 504 | var stop = start + size; 505 | var val = ""; 506 | for (var i = start; i < stop; i++) { 507 | val += String.fromCharCode(view.getInt8(i)); 508 | } 509 | values.push(val); 510 | core.setPos(stop); 511 | }, 512 | newObject: function (count) { 513 | print("newObject() count=" + count); 514 | var val = {}; 515 | for (var i = count - 1; i >= 0; i--) { 516 | var v = values.pop(); 517 | var n = values.pop(); 518 | val[n] = v; 519 | } 520 | values.push(val); 521 | }, 522 | newArray: function (count) { 523 | print("newArray() count=" + count); 524 | var val = []; 525 | for (var i = count - 1; i >= 0; i--) { 526 | var v = values.pop(); 527 | val[i] = v; 528 | } 529 | values.push(val); 530 | }, 531 | } 532 | var core = Core(global, imports, buffer); 533 | function decode() { 534 | core.decode(); 535 | return values.pop(); 536 | } 537 | var ubjson = Object.create(core); 538 | ubjson.decode = decode; 539 | return ubjson; 540 | } 541 | 542 | // Self test 543 | function test() { 544 | var buffer = new ArrayBuffer(4096); 545 | var ubjson = UBJSON(buffer); 546 | // TEST Builder functions 547 | var pos = []; 548 | var BIG = 0x01; 549 | pos.push(ubjson.getPos()); 550 | ubjson.startArray(BIG); 551 | pos.push(ubjson.getPos()); 552 | ubjson.startObject(); 553 | ubjson.writeByte(10); 554 | pos.push(ubjson.getPos()); 555 | ubjson.startString(); 556 | ubjson.writeStringChar("h".charCodeAt(0)); 557 | ubjson.writeStringChar("e".charCodeAt(0)); 558 | ubjson.writeStringChar("l".charCodeAt(0)); 559 | ubjson.writeStringChar("l".charCodeAt(0)); 560 | ubjson.writeStringChar("o".charCodeAt(0)); 561 | ubjson.finishString(pos.pop(), 5); 562 | ubjson.finishObject(pos.pop(), 1); 563 | ubjson.writeI32(0xFFFF); 564 | ubjson.writeByte(1); 565 | ubjson.writeD32(1.23); 566 | ubjson.writeD64(1.23); 567 | ubjson.writeD32(1.23); 568 | ubjson.finishArray(pos.pop(), 6, BIG); 569 | dumpView(new DataView(buffer)); 570 | function dumpView(view) { 571 | for (var i = 0 ; i < 128 ; ) { 572 | var s = ""; 573 | for (var j = 0; j < 16; i++, j++) { 574 | s += view.getUint8(i) + " "; 575 | } 576 | trace(s); 577 | } 578 | } 579 | // Test Decode function 580 | ubjson.setPos(5); 581 | var value = ubjson.decode(); 582 | print(JSON.stringify(value, null, 2)); 583 | } 584 | 585 | if (DEBUG) { 586 | test(); 587 | } 588 | 589 | return UBJSON; 590 | 591 | })(); 592 | 593 | 594 | --------------------------------------------------------------------------------