PageRenderTime 36ms CodeModel.GetById 8ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/dynamictable/public/phpolait/jsolait/init.js

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