PageRenderTime 59ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/init.js

http://pyjamas.googlecode.com/
JavaScript | 473 lines | 473 code | 0 blank | 0 comment | 27 complexity | c9736cb48ca2952963b2f83ad2229808 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. globalEval=function(){
  2. return eval(arguments[0]);
  3. }
  4. Class = function(className, superClass, classScope){
  5. if(arguments.length == 2){
  6. classScope = superClass;
  7. if(typeof className != "string"){
  8. superClass = className;
  9. className = "anonymous";
  10. }else{
  11. superClass = Object;
  12. }
  13. }else if(arguments.length == 1){
  14. classScope = className;
  15. superClass = Object;
  16. className = "anonymous";
  17. }
  18. var NewClass = function(calledBy){
  19. if(calledBy !== Class){
  20. return this.init.apply(this, arguments);
  21. }
  22. }
  23. NewClass.createPrototype = function(){
  24. return new NewClass(Class);
  25. }
  26. NewClass.superClass = superClass;
  27. NewClass.className=className;
  28. NewClass.toString = function(){
  29. return "[class %s]".format(NewClass.className);
  30. }
  31. if(superClass.createPrototype!=null){
  32. NewClass.prototype = superClass.createPrototype();
  33. }else{
  34. NewClass.prototype = new superClass();
  35. }
  36. NewClass.prototype.constructor = NewClass;
  37. if(superClass == Object){
  38. NewClass.prototype.toString = function(){
  39. return "[object %s]".format(this.constructor.className);
  40. }
  41. }
  42. if(NewClass.prototype.init==null){
  43. NewClass.prototype.init=function(){
  44. }
  45. }
  46. var supr = function(self){
  47. var wrapper = {};
  48. var superProto = superClass.prototype;
  49. for(var n in superProto){
  50. if(typeof superProto[n] == "function"){
  51. wrapper[n] = function(){
  52. var f = arguments.callee;
  53. return superProto[f._name].apply(self, arguments);
  54. }
  55. wrapper[n]._name = n;
  56. }
  57. }
  58. return wrapper;
  59. }
  60. classScope(NewClass.prototype, supr);
  61. return NewClass;
  62. }
  63. Class.toString = function(){
  64. return "[object Class]";
  65. }
  66. Class.createPrototype=function(){
  67. throw "Can't use Class as a super class.";
  68. }
  69. Module = function(name, version, moduleScope){
  70. var mod = new Object();
  71. mod.version = version;
  72. mod.name = name;
  73. mod.toString=function(){
  74. return "[module '%s' version: %s]".format(mod.name, mod.version);
  75. }
  76. mod.Exception=Class("Exception", function(publ){
  77. publ.init=function(msg, trace){
  78. this.name = this.constructor.className;
  79. this.message = msg;
  80. this.trace = trace;
  81. }
  82. publ.toString=function(){
  83. var s = "%s %s\n\n".format(this.name, this.module);
  84. s += this.message;
  85. return s;
  86. }
  87. publ.toTraceString=function(){
  88. var s = "%s %s:\n ".format(this.name, this.module );
  89. s+="%s\n\n".format(this.message);
  90. if(this.trace){
  91. if(this.trace.toTraceString){
  92. s+= this.trace.toTraceString();
  93. }else{
  94. s+= this.trace;
  95. }
  96. }
  97. return s;
  98. }
  99. publ.name;
  100. publ.message;
  101. publ.module = mod;
  102. publ.trace;
  103. })
  104. moduleScope(mod);
  105. for(var n in mod){
  106. if(mod[n].className == "anonymous"){
  107. mod[n].className = n;
  108. }
  109. }
  110. if(name != "jsolait"){
  111. jsolait.registerModule(mod);
  112. }
  113. return mod;
  114. }
  115. Module.toString = function(){
  116. return "[object Module]";
  117. }
  118. Module.createPrototype=function(){
  119. throw "Can't use Module as a super class.";
  120. }
  121. Module("jsolait", "0.1.0", function(mod){
  122. jsolait=mod;
  123. mod.baseURL=".";
  124. mod.libURL ="./jsolait";
  125. mod.modules = new Array();
  126. mod.moduleURLs = {urllib:"%(libURL)s/lib/urllib.js",
  127. xml:"%(libURL)s/lib/xml.js",
  128. crypto:"%(libURL)s/lib/crypto.js",
  129. codecs:"%(libURL)s/lib/codecs.js",
  130. jsonrpc:"%(libURL)s/lib/jsonrpc.js",
  131. lang:"%(libURL)s/lib/lang.js",
  132. iter:"%(libURL)s/lib/iter.js",
  133. xmlrpc:"%(libURL)s/lib/xmlrpc.js"};
  134. mod.init=function(){
  135. var ws = null;
  136. try{
  137. ws = WScript;
  138. }catch(e){
  139. }
  140. if(ws != null){
  141. initWS();
  142. }
  143. }
  144. var initWS = function(){
  145. print=function(msg){
  146. WScript.echo(msg);
  147. }
  148. alert=function(msg){
  149. print(msg);
  150. }
  151. var args = WScript.arguments;
  152. try{
  153. if(args(0) == "--test"){
  154. var fileURL = args(1);
  155. var doTest = true;
  156. }else{
  157. var fileURL = args(0);
  158. var doTest = false;
  159. }
  160. var baseURL = fileURL.replace(/\\/g, "/");
  161. baseURL = baseURL.split("/");
  162. baseURL = baseURL.slice(0, baseURL.length-1);
  163. mod.baseURL = baseURL.join("/");
  164. }catch(e){
  165. throw new mod.Exception("Missing script filename to be run.", e);
  166. }
  167. urlInit = WScript.ScriptFullName;
  168. urlInit = urlInit.replace(/\\/g, "/");
  169. urlInit = urlInit.split("/");
  170. urlInit = urlInit.slice(0, urlInit.length-1);
  171. mod.libURL = "file://" + urlInit.join("/");
  172. try{
  173. mod.loadScript(fileURL);
  174. }catch(e){
  175. WScript.stdErr.write("%s(1,1) jsolait runtime error:\n%s\n".format(args(0).replace("file://",""), e.toTraceString()));
  176. }
  177. if(doTest){
  178. var modName = fileURL.split("\\");
  179. modName = modName.pop();
  180. modName = modName.slice(0, modName.length -3);
  181. modName.replace(/\//g, ".");
  182. print("importing module: %s".format(modName));
  183. var m = importModule(modName);
  184. print("%s imported\ntesting...\n".format(m));
  185. m.test();
  186. print("\nfinished testing.".format(modName));
  187. }
  188. }
  189. mod.importModule = function(name){
  190. if (mod.modules[name]){
  191. return mod.modules[name];
  192. }else{
  193. var src,modURL;
  194. if(mod.moduleURLs[name]){
  195. modURL = mod.moduleURLs[name].format(mod);
  196. }else{
  197. modURL = "%s/%s.js".format(mod.baseURL, name.split(".").join("/"));
  198. }
  199. try{
  200. src = getFile(modURL);
  201. }catch(e){
  202. throw new mod.ModuleImportFailed(name, modURL, e);
  203. }
  204. try{
  205. globalEval(src);
  206. }catch(e){
  207. throw new mod.ModuleImportFailed(name, modURL, e);
  208. }
  209. return mod.modules[name];
  210. }
  211. }
  212. importModule = mod.importModule;
  213. mod.loadScript=function(url){
  214. var src = getFile(url);
  215. try{
  216. globalEval(src);
  217. }catch(e){
  218. throw new mod.EvalFailed(url, e);
  219. }
  220. }
  221. mod.registerModule = function(module){
  222. this.modules[module.name] = module;
  223. }
  224. var getHTTP=function() {
  225. var obj;
  226. try{
  227. obj = new XMLHttpRequest();
  228. }catch(e){
  229. try{
  230. obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
  231. }catch(e){
  232. try{
  233. obj=new ActiveXObject("Msxml2.XMLHTTP");
  234. }catch(e){
  235. try{
  236. obj = new ActiveXObject("microsoft.XMLHTTP");
  237. }catch(e){
  238. throw new mod.Exception("Unable to get an HTTP request object.");
  239. }
  240. }
  241. }
  242. }
  243. return obj;
  244. }
  245. var getFile=function(url, headers) {
  246. headers = (headers != null) ? headers : [];
  247. try{
  248. var xmlhttp= getHTTP();
  249. xmlhttp.open("GET", url, false);
  250. for(var i=0;i< headers.length;i++){
  251. xmlhttp.setRequestHeader(headers[i][0], headers[i][1]);
  252. }
  253. xmlhttp.send("");
  254. }catch(e){
  255. throw new mod.Exception("Unable to load URL: '%s'.".format(url), e);
  256. }
  257. if(xmlhttp.status == 200 || xmlhttp.status == 0){
  258. return xmlhttp.responseText;
  259. }else{
  260. throw new mod.Exception("File not loaded: '%s'.".format(url));
  261. }
  262. }
  263. Error.prototype.toTraceString = function(){
  264. if(this.message){
  265. return "%s\n".format(this.message);
  266. }
  267. if (this.description){
  268. return "%s\n".format(this.description);
  269. }
  270. return "unknown error\n";
  271. }
  272. mod.ModuleImportFailed=Class(mod.Exception, function(publ, supr){
  273. publ.init=function(moduleName, url, trace){
  274. supr(this).init("Failed to import module: '%s' from URL:'%s'".format(moduleName, url), trace);
  275. this.moduleName = moduleName;
  276. this.url = url;
  277. }
  278. publ.moduleName;
  279. publ.url;
  280. })
  281. mod.EvalFailed=Class(mod.Exception, function(publ, supr){
  282. publ.init=function(url, trace){
  283. supr(this).init("File '%s' Eval of script failed.".format(url), trace);
  284. this.url = url;
  285. }
  286. publ.url;
  287. })
  288. mod.reportException=function(exception){
  289. if(exception.toTraceString){
  290. var s= exception.toTraceString();
  291. }else{
  292. var s = exception.toString();
  293. }
  294. var ws = null;
  295. try{
  296. ws = WScript;
  297. }catch(e){
  298. }
  299. if(ws != null){
  300. WScript.stderr.write(s);
  301. }else{
  302. alert(s);
  303. }
  304. }
  305. reportException = mod.reportException;
  306. })
  307. Module("stringformat", "0.1.0", function(mod){
  308. var FormatSpecifier=function(s){
  309. var s = s.match(/%(\(\w+\)){0,1}([ 0-]){0,1}(\+){0,1}(\d+){0,1}(\.\d+){0,1}(.)/);
  310. if(s[1]){
  311. this.key=s[1].slice(1,-1);
  312. }else{
  313. this.key = null;
  314. }
  315. this.paddingFlag = s[2];
  316. if(this.paddingFlag==""){
  317. this.paddingFlag =" "
  318. }
  319. this.signed=(s[3] == "+");
  320. this.minLength = parseInt(s[4]);
  321. if(isNaN(this.minLength)){
  322. this.minLength=0;
  323. }
  324. if(s[5]){
  325. this.percision = parseInt(s[5].slice(1,s[5].length));
  326. }else{
  327. this.percision=-1;
  328. }
  329. this.type = s[6];
  330. }
  331. String.prototype.format=function(){
  332. var sf = this.match(/(%(\(\w+\)){0,1}[ 0-]{0,1}(\+){0,1}(\d+){0,1}(\.\d+){0,1}[dibouxXeEfFgGcrs%])|([^%]+)/g);
  333. if(sf){
  334. if(sf.join("") != this){
  335. throw new mod.Exception("Unsupported formating string.");
  336. }
  337. }else{
  338. throw new mod.Exception("Unsupported formating string.");
  339. }
  340. var rslt ="";
  341. var s;
  342. var obj;
  343. var cnt=0;
  344. var frmt;
  345. var sign="";
  346. for(var i=0;i<sf.length;i++){
  347. s=sf[i];
  348. if(s == "%%"){
  349. s = "%";
  350. }else if(s.slice(0,1) == "%"){
  351. frmt = new FormatSpecifier(s);
  352. if(frmt.key){
  353. if((typeof arguments[0]) == "object" && arguments.length == 1){
  354. obj = arguments[0][frmt.key];
  355. }else{
  356. throw new mod.Exception("Object or associative array expected as formating value.");
  357. }
  358. }else{
  359. if(cnt>=arguments.length){
  360. throw new mod.Exception("Not enough arguments for format string");
  361. }else{
  362. obj=arguments[cnt];
  363. cnt++;
  364. }
  365. }
  366. if(frmt.type == "s"){
  367. if (obj == null){
  368. obj = "null";
  369. }
  370. s=obj.toString().pad(frmt.paddingFlag, frmt.minLength);
  371. }else if(frmt.type == "c"){
  372. if(frmt.paddingFlag == "0"){
  373. frmt.paddingFlag=" ";
  374. }
  375. if(typeof obj == "number"){
  376. s = String.fromCharCode(obj).pad(frmt.paddingFlag , frmt.minLength) ;
  377. }else if(typeof obj == "string"){
  378. if(obj.length == 1){
  379. s=obj.pad(frmt.paddingFlag, frmt.minLength);
  380. }else{
  381. throw new mod.Exception("Character of length 1 required.");
  382. }
  383. }else{
  384. throw new mod.Exception("Character or Byte required.");
  385. }
  386. }else if(typeof obj == "number"){
  387. if(obj < 0){
  388. obj = -obj;
  389. sign = "-";
  390. }else if(frmt.signed){
  391. sign = "+";
  392. }else{
  393. sign = "";
  394. }
  395. switch(frmt.type){
  396. case "f":
  397. case "F":
  398. if(frmt.percision > -1){
  399. s = obj.toFixed(frmt.percision).toString();
  400. }else{
  401. s = obj.toString();
  402. }
  403. break;
  404. case "E":
  405. case "e":
  406. if(frmt.percision > -1){
  407. s = obj.toExponential(frmt.percision);
  408. }else{
  409. s = obj.toExponential();
  410. }
  411. s = s.replace("e", frmt.type);
  412. break;
  413. case "b":
  414. s = obj.toString(2);
  415. s = s.pad("0", frmt.percision);
  416. break;
  417. case "o":
  418. s = obj.toString(8);
  419. s = s.pad("0", frmt.percision);
  420. break;
  421. case "x":
  422. s = obj.toString(16).toLowerCase();
  423. s = s.pad("0", frmt.percision);
  424. break;
  425. case "X":
  426. s = obj.toString(16).toUpperCase();
  427. s = s.pad("0", frmt.percision);
  428. break;
  429. default:
  430. s = parseInt(obj).toString();
  431. s = s.pad("0", frmt.percision);
  432. break;
  433. }
  434. if(frmt.paddingFlag == "0"){
  435. s=s.pad("0", frmt.minLength - sign.length);
  436. }
  437. s=sign + s;
  438. s=s.pad(frmt.paddingFlag, frmt.minLength);
  439. }else{
  440. throw new mod.Exception("Number required.");
  441. }
  442. }
  443. rslt += s;
  444. }
  445. return rslt;
  446. }
  447. String.prototype.pad = function(flag, len){
  448. var s = "";
  449. if(flag == "-"){
  450. var c = " ";
  451. }else{
  452. var c = flag;
  453. }
  454. for(var i=0;i<len-this.length;i++){
  455. s += c;
  456. }
  457. if(flag == "-"){
  458. s = this + s;
  459. }else{
  460. s += this;
  461. }
  462. return s;
  463. }
  464. String.prototype.mul = function(c){
  465. var a = new Array(this.length * c);
  466. var s=""+ this;
  467. for(var i=0;i<c;i++){
  468. a[i] = s;
  469. }
  470. return a.join("");
  471. }
  472. })
  473. jsolait.init();