PageRenderTime 15ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Play/util.js

http://github.com/mbebenita/Broadway
JavaScript | 266 lines | 211 code | 33 blank | 22 comment | 45 complexity | c8b560a0810d8bdb1a2c45e96cf555bc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. 'use strict';
  2. var ERRORS = 0, WARNINGS = 1, TODOS = 5;
  3. var verbosity = WARNINGS;
  4. function dump(object) {
  5. var output = '{ ';
  6. for ( var property in object) {
  7. output += property + ': ' + object[property] + '; ';
  8. }
  9. output += '}';
  10. log(output);
  11. }
  12. function getProperties(object, verbose) {
  13. if (verbose) {
  14. var output = '{\n';
  15. for ( var property in object) {
  16. if (typeof (object[property]) != "function") {
  17. if (typeof (object[property]) == "number" && (property.toString().indexOf("word") >= 0)) {
  18. output += " " + property + ': ' + getNumberInfo(object[property]) + '\n';
  19. } else {
  20. output += " " + property + ': ' + object[property] + '\n';
  21. }
  22. }
  23. }
  24. return output + '}';
  25. } else {
  26. var output = '{ ';
  27. for ( var property in object) {
  28. if (typeof (object[property]) != "function") {
  29. output += property + ': ' + object[property] + ' ';
  30. }
  31. }
  32. return output + '}';
  33. }
  34. }
  35. function getPrettyEnumValue(object, value) {
  36. return getPropertyByValue(object, value) + " (" + value + ")";
  37. }
  38. function getPropertyByValue(object, value) {
  39. for ( var property in object) {
  40. if (object[property] == value) {
  41. return property;
  42. }
  43. }
  44. return null;
  45. }
  46. function log(message) {
  47. console.log(message);
  48. }
  49. function warn(message) {
  50. if (verbosity >= WARNINGS) {
  51. log('Warning: ' + message);
  52. }
  53. }
  54. function backtrace() {
  55. var stackStr;
  56. try {
  57. throw new Error();
  58. } catch (e) {
  59. stackStr = e.stack;
  60. }
  61. return stackStr.split('\n').slice(1).join('\n');
  62. }
  63. function notImplemented() {
  64. error("not implemented");
  65. }
  66. function unexpected() {
  67. error("unexpected");
  68. }
  69. function error(message) {
  70. console.error(message);
  71. console.trace();
  72. }
  73. function assert(condition, message) {
  74. if (!condition) {
  75. error(message);
  76. }
  77. }
  78. function assertFalse(condition, message) {
  79. if (condition) {
  80. error(message);
  81. }
  82. }
  83. function assertRange(value, min, max) {
  84. if (value < min || value > max) {
  85. error("Value " + value + " is out of range [" + min + "," + max + "]");
  86. }
  87. }
  88. function bytesToString(bytes) {
  89. var str = '';
  90. var length = bytes.length;
  91. for ( var n = 0; n < length; ++n) {
  92. str += String.fromCharCode(bytes[n]);
  93. }
  94. return str;
  95. }
  96. function stringToBytes(str) {
  97. var length = str.length;
  98. var bytes = new Uint8Array(length);
  99. for ( var n = 0; n < length; ++n) {
  100. bytes[n] = str.charCodeAt(n) & 0xFF;
  101. }
  102. return bytes;
  103. }
  104. //
  105. // getPdf()
  106. // Convenience function to perform binary Ajax GET
  107. // Usage: getPdf('http://...', callback)
  108. // getPdf({
  109. // url:String ,
  110. // [,progress:Function, error:Function]
  111. // },
  112. // callback)
  113. //
  114. function getFile(arg, callback) {
  115. var params = arg;
  116. if (typeof arg === 'string')
  117. params = {
  118. url : arg
  119. };
  120. var xhr = new XMLHttpRequest();
  121. xhr.open('GET', params.url);
  122. xhr.mozResponseType = xhr.responseType = 'arraybuffer';
  123. xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
  124. if ('progress' in params) {
  125. xhr.onprogrss = params.progress || undefined;
  126. }
  127. if ('error' in params) {
  128. xhr.onerror = params.error || undefined;
  129. }
  130. xhr.onreadystatechange = function getPdfOnreadystatechange() {
  131. if (xhr.readyState === 4 && xhr.status === xhr.expected) {
  132. var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse
  133. || xhr.responseArrayBuffer || xhr.response);
  134. callback(data);
  135. }
  136. };
  137. xhr.send(null);
  138. }
  139. function getBinaryDigits(x, digits, withLeadingZeros) {
  140. var t = "";
  141. var z = withLeadingZeros ? 0 : countLeadingZeros32(x);
  142. for (var i = digits - z - 1; i >= 0; i--) {
  143. t += x & (1 << i) ? "1" : "0";
  144. }
  145. return t;
  146. }
  147. function padLeft(str, char, num) {
  148. var pad = "";
  149. for ( var i = 0; i < num - str.length; i++) {
  150. pad += char;
  151. }
  152. return pad + str;
  153. }
  154. function getNumberInfo(x) {
  155. return "0x" + getHexBytes(x, 8) +
  156. ", 0b" + getBinaryDigits(x, 16, true) + " (" + countLeadingZeros16(x) + ")" +
  157. ", 0b" + getBinaryDigits(x, 32, true) + " (" + countLeadingZeros32(x) + ") " +
  158. x.toString();
  159. }
  160. function getHexBytes(number, length) {
  161. if (number < 0) {
  162. var u = 0xFFFFFFFF + number + 1;
  163. var hex = u.toString(16).toUpperCase();
  164. return padLeft(hex, "0", length);
  165. }
  166. return padLeft(number.toString(16).toUpperCase(), "0", length);
  167. }
  168. function printArrayBuffer(buffer) {
  169. printArray(new Uint8Array(buffer));
  170. }
  171. function printArray(uint8View) {
  172. var group = 64;
  173. var subGroup = 4;
  174. print("Size: " + uint8View.length + " (" + (uint8View.length * 8) + ")" + ", Buffer: ");
  175. for ( var i = 0; i < uint8View.length; i++) {
  176. if (i % group == 0) {
  177. print("\n");
  178. print(getHexBytes(i, 4) + ": ");
  179. } else if (i % subGroup == 0) {
  180. print(" ");
  181. }
  182. print(getHexBytes(uint8View[i], 2));
  183. }
  184. print("\n");
  185. }
  186. function traceln(s) {
  187. println(s);
  188. }
  189. function isPowerOfTwo(x) {
  190. return (x & (x - 1)) == 0;
  191. }
  192. /**
  193. * Rounds up to the next highest power of two.
  194. */
  195. function nextHighestPowerOfTwo(x) {
  196. --x;
  197. for (var i = 1; i < 32; i <<= 1) {
  198. x = x | x >> i;
  199. }
  200. return x + 1;
  201. }
  202. /**
  203. * Represents a 2-dimensional size value.
  204. */
  205. var Size = (function size() {
  206. function constructor(w, h) {
  207. this.w = w;
  208. this.h = h;
  209. }
  210. constructor.prototype = {
  211. toString: function () {
  212. return "(" + this.w + ", " + this.h + ")";
  213. },
  214. getNextHighestPowerOfTwo: function() {
  215. return new Size(nextHighestPowerOfTwo(this.w),
  216. nextHighestPowerOfTwo(this.h));
  217. }
  218. }
  219. return constructor;
  220. })();
  221. /**
  222. * Creates a new prototype object derived from another objects prototype along with a list of additional properties.
  223. *
  224. * @param base object whose prototype to use as the created prototype object's prototype
  225. * @param properties additional properties to add to the created prototype object
  226. */
  227. function inherit(base, properties) {
  228. var prot = Object.create(base.prototype);
  229. for (var p in properties) {
  230. prot[p] = properties[p];
  231. }
  232. return prot;
  233. }