Changeset 8
- Timestamp:
- 10/20/05 18:05:13 (3 years ago)
- Files:
-
- jsolait/trunk/jsolait/jsolait.js (modified) (8 diffs)
- jsolait/trunk/jsolait/jsolait.wsf (modified) (1 diff)
- jsolait/trunk/jsolait/lib/jsonrpc.js (modified) (3 diffs)
- jsolait/trunk/jsolait/lib/lang.js (modified) (4 diffs)
- jsolait/trunk/jsolait/lib/sets.js (modified) (3 diffs)
- jsolait/trunk/jsolait/lib/testing.js (modified) (2 diffs)
- jsolait/trunk/jsolait/lib/urllib.js (modified) (3 diffs)
- jsolait/trunk/jsolait/lib/xml.js (modified) (2 diffs)
- jsolait/trunk/jsolait/lib/xmlrpc.js (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
jsolait/trunk/jsolait/jsolait.js
r7 r8 171 171 } 172 172 } 173 174 //create a supr function to be used to call methods of the super class 175 var supr = function(self){ 176 //make sure its a legal call to supr 177 if(! (self.constructor.isSubclassOf(superClass))){ 178 throw "Illegal call of supr:\n %s is not an instance of %s".format(self, superClass); 179 } 180 var wrapper; 181 //if a wrapper has been created already then use it 182 if(self[" super_this_" + superClass.__name__]){ 183 wrapper = self[" super_this_" + superClass.__name__]; 184 }else{ 185 //set up super class functionality so a call to super(this) will return an object with all super class methods 186 //the methods can be called like super(this).foo and the this object will be bound to that method. 187 wrapper = {}; 188 var superProto = superClass.prototype; 189 for(var n in superProto){ 190 if(superProto[n] instanceof Function){ 191 wrapper[n] = function(){ 192 var f = arguments.callee; 193 return superProto[f._name].apply(self, arguments); 194 }; 195 wrapper[n]._name = n; 196 } 197 } 198 //save the wrapper so calling of supr a second time is much faster and skips the wrapper setup 199 self[" super_this_" + superClass.__name__]=wrapper; 200 } 201 return wrapper; 202 }; 203 173 204 174 //execute the scope of the class 205 classScope(NewClass.prototype, supr); 175 switch(classScope.length){ 176 case 3: //publ, statc, supr 177 classScope(NewClass.prototype, NewClass, superClass.prototype); 178 break; 179 default://publ, supr 180 classScope(NewClass.prototype, superClass.prototype); 181 break; 182 } 183 206 184 return NewClass; 207 185 }; 186 208 187 Class.toString = function(){ 209 188 return "[object Class]"; 210 189 }; 190 211 191 Class.__createProto__=function(){ 212 192 throw "Can't use Class as a super class."; … … 262 242 throw "Can't use Module as a super class."; 263 243 }; 244 245 264 246 /** 265 247 Base class for all module-Exceptions. … … 275 257 publ.__init__=function(msg, trace){ 276 258 this.name = this.constructor.__name__; 277 this.message = msg;259 this.message = ''+msg; 278 260 this.trace = trace; 279 261 }; 280 262 263 281 264 publ.toString=function(){ 282 var s = "%s %s\n\n".format(this.name, this.module); 283 s += this.message; 265 var s = "%s %s".format(this.name, this.module); 284 266 return s; 285 267 }; … … 288 270 @return The error trace. 289 271 **/ 290 publ.toTraceString=function(){ 291 //todo:use constructor.__name__ 292 var s = "%s in %s:\n ".format(this.name, this.module ); 293 s+="%s\n\n".format(this.message); 272 publ.toTraceString=function(indent){ 273 indent = indent==null ? 0 : indent; 274 275 //todo:use constructor.__name__ 276 var s="%s in %s:\n%s".format(this.name, this.module, this.message.indent(4)).indent(indent); 294 277 if(this.trace){ 295 278 if(this.trace.toTraceString){ 296 s+= this.trace.toTraceString();279 s+='\n\n'+ this.trace.toTraceString(indent + 8); 297 280 }else{ 298 s+= this.trace + 'sdfsdf';281 s+=(this.trace +'\n').indent(indent); 299 282 } 300 283 } 301 284 return s; 302 285 }; 286 287 303 288 304 289 ///The name of the Exception. … … 322 307 **/ 323 308 publ.__init__=function(module, trace){ 324 supr (this).__init__("Failed to run the module scope for %s".format(module), trace);309 supr.__init__.call(this, "Failed to run the module scope for %s".format(module), trace); 325 310 this.failedModule = module; 326 311 }; … … 426 411 **/ 427 412 publ.__init__=function(sourceURI, trace){ 428 supr (this).__init__("Failed to load file: '%s'".format(sourceURI), trace);413 supr.__init__.call(this, "Failed to load file: '%s'".format(sourceURI), trace); 429 414 this.sourceURI = sourceURI; 430 415 }; … … 504 489 **/ 505 490 publ.__init__=function(moduleName, moduleURIs, trace){ 506 supr (this).__init__("Failed to import module: '%s' from:\n%s".format(moduleName, moduleURIs.join(',\n')), trace);491 supr.__init__.call(this, "Failed to import module: '%s' from:\n%s".format(moduleName, moduleURIs.join(',\n')), trace); 507 492 this.moduleName = moduleName; 508 493 this.moduleURIs = moduleURIs; … … 761 746 return s; 762 747 }; 763 748 749 String.prototype.indent=function(indent){ 750 var out=[]; 751 var s=this.split('\n'); 752 for(var i=0;i<s.length;i++){ 753 var pr=''; 754 for(var k=0;k<indent;k++){ 755 pr +=' '; 756 } 757 out.push(pr + s[i]); 758 } 759 return out.join('\n'); 760 } 761 764 762 ///Tests the module. 765 763 mod.test=function(){ jsolait/trunk/jsolait/jsolait.wsf
r5 r8 159 159 if(modl.__main__){ 160 160 //todo find arguments 161 modl.__main__.call(modl,[]); 161 try{ 162 modl.__main__.call(modl,[]); 163 }catch(e){ 164 throw new mod.Exception("runing %s __main__() failed\n".format(modl),e) 165 } 162 166 } 163 167 return; jsolait/trunk/jsolait/lib/jsonrpc.js
r5 r8 37 37 */ 38 38 publ.__init__= function(status){ 39 supr (this).__init__("The server did not respond with a status 200 (OK) but with: " + status);39 supr.__init__.call(this, "The server did not respond with a status 200 (OK) but with: " + status); 40 40 this.status = status; 41 41 }; … … 55 55 */ 56 56 publ.__init__= function(msg, s, trace){ 57 supr (this).__init__(msg,trace);57 supr.__init__.call(this, msg,trace); 58 58 this.source = s; 59 59 }; … … 71 71 */ 72 72 publ.__init__= function(err, trace){ 73 supr (this).__init__(err,trace);73 supr.__init__.call(this, err,trace); 74 74 }; 75 75 }); jsolait/trunk/jsolait/lib/lang.js
r7 r8 384 384 385 385 publ.__init__=function(s, globalNode){ 386 supr (this).__init__(s);386 supr.__init__.call(this, s); 387 387 globalNode = globalNode === undefined ? new mod.GlobalNode() : globalNode; 388 388 this.currentNode=globalNode; … … 1011 1011 mod.Compressor=Class(mod.Tokenizer, function(publ,supr){ 1012 1012 publ.__init__=function(source){ 1013 supr (this).__init__(source);1013 supr.__init__.call(this, source); 1014 1014 this.wsNeeded=false; 1015 1015 }; … … 1166 1166 mod.__main__=function(){ 1167 1167 //var s='switch(a){\n case "as":\n\na=2;\nbreak;case "df":\nbreak; } a.'; 1168 var i =imprt('iter');1168 var it =imprt('iter'); 1169 1169 var filenames= ['jsolait.js', 1170 1170 'lib/codecs.js', … … 1202 1202 }); 1203 1203 1204 1205 jsolait/trunk/jsolait/lib/sets.js
r5 r8 56 56 iterable = iterable.split(""); 57 57 } 58 for(var i=0;i<iterable.length;i++){ 59 this.add(iterable[i]); 60 } 58 //todo do iterable impl 59 /* 60 if(iterable.__iter__){ 61 var i=iterable.__iter__(); 62 var item; 63 while(item=i.next()!==undefined){ 64 this.add(item); 65 } 66 }else{*/ 67 for(var i=0;i<iterable.length;i++){ 68 this.add(iterable[i]); 69 } 70 //} 61 71 }; 62 72 … … 164 174 }; 165 175 176 publ.__equals__=function(setObj){ 177 if(setObj instanceof publ.constructor){ 178 return this.equals(setObj); 179 }else{ 180 return false; 181 } 182 }; 183 166 184 /** 167 185 Creates a new set containing all elements of set and setObj (newSet = set | setObj). … … 309 327 310 328 mod.__main__=function(){ 311 312 var prntRslt=function(expected, rslt){ 313 print("expected\t:" + expected); 314 print("calculated\t:" + rslt); 315 if(rslt.equals(expected) == false){ 316 throw "Sets are not Equal:\n" + expected + " != " + rslt; 317 } 318 print(""); 319 }; 329 320 330 321 331 var s1=new mod.Set("0123456"); 322 332 var s2=new mod.Set("3456789"); 323 324 print("%s | %s".format(s1, s2)); 325 prntRslt(new mod.Set("0123456789"), s1.union(s2)); 326 327 print("%s | %s".format(s2, s1)); 328 prntRslt(new mod.Set("0123456789"), s2.union(s1)); 329 330 print("%s & %s".format(s1, s2)); 331 prntRslt(new mod.Set("3456"), s1.intersection(s2)); 332 333 print("%s & %s".format(s2, s1)); 334 prntRslt(new mod.Set("3456"), s2.intersection(s1)); 335 336 print("%s - %s".format(s1, s2)); 337 prntRslt(new mod.Set("012"), s1.difference(s2)); 338 339 print("%s - %s".format(s2, s1)); 340 prntRslt(new mod.Set("789"), s2.difference(s1)); 341 342 print("%s ^ %s".format(s1, s2)); 343 prntRslt(new mod.Set("012789"),s1.symmDifference(s2)); 344 345 print("%s ^ %s".format(s2, s1)); 346 prntRslt(new mod.Set("012789"),s2.symmDifference(s1)); 333 var testing=imprt('testing'); 334 335 print(testing.test(function(){ 336 testing.assertEquals("checking %s | %s".format(s1, s2), 337 new mod.Set("0123456789"), s1.union(s2)) 338 339 testing.assertEquals("checking %s | %s".format(s2, s1), 340 new mod.Set("0123456789"), s2.union(s1)); 341 342 testing.assertEquals("checking %s & %s".format(s1, s2), 343 new mod.Set("3456"), s1.intersection(s2)); 344 345 testing.assertEquals("checking %s & %s".format(s2, s1), 346 new mod.Set("3456"), s2.intersection(s1)); 347 348 testing.assertEquals("checking %s - %s".format(s1, s2), 349 new mod.Set("012"), s1.difference(s2)); 350 351 testing.assertEquals("checking %s - %s".format(s2, s1), 352 new mod.Set("789"), s2.difference(s1)); 353 354 testing.assertEquals("checking %s ^ %s".format(s1, s2), 355 new mod.Set("012789"),s1.symmDifference(s2)); 356 357 testing.assertEquals("checking %s ^ %s".format(s2, s1), 358 new mod.Set("012789"),s2.symmDifference(s1)); 359 })) 347 360 }; 348 361 }); jsolait/trunk/jsolait/lib/testing.js
r5 r8 47 47 }; 48 48 49 50 mod.testModule=function(modName){ 51 var t=(new Date()).getTime(); 52 log("Starting module test"); 53 log("importing '%s' for testing ...".format(modName)); 54 try{ 55 var m = jsolait.imprt(modName); 56 }catch(e){ 57 log(e); 58 log("Premeture exiting of module test because importing of '%s' failed.".format(modName), LogError); 59 return; 60 } 61 62 if(m.test==null){ 63 log("Premeture exiting of module test because %s does not expose a 'test()' method.".format(m), LogError); 64 return; 65 } 66 log("%s imported, running '%s.test()' ...".format(m,modName)); 67 try{ 68 m.test(mod); 69 }catch(e){ 70 log(e+""); 71 log("Premeture exiting of module test due to an error running '%s.test()'".format(modName), LogError); 72 return; 73 } 74 var t= (new Date()).getTime()-t ; 75 log("Testing of module '%s' completed in %s ms.".format(modName, t < 1? "< 1":t)) ; 76 49 mod.Test=Class(function(publ,supr){ 50 publ.__init__=function(testScope){ 51 this.testScope=testScope; 52 } 53 54 publ.run=function(){ 55 this.startTime=(new Date()).getTime(); 56 try{ 57 this.testScope(); 58 }catch(e){ 59 if(e instanceof mod.AssertFailed){ 60 this.error = e; 61 }else{ 62 throw new mod.Exception("Failed to run test.", e); 63 } 64 } 65 this.endTime=(new Date()).getTime(); 66 this.duration=this.endTime-this.startTime; 67 } 68 69 publ.report=function(){ 70 if(this.error){ 71 return "Test has failed after %s ms due to:\n\n%s".format(this.duration, this.error.toTraceString().indent(4)); 72 }else{ 73 return "Test completed in %s ms".format(this.duration); 74 } 75 } 76 77 publ.startTime; 78 publ.endTime; 79 publ.duration; 80 }); 81 82 mod.test=function(testScope){ 83 var t= new mod.Test(testScope); 84 t.run(); 85 return t.report(); 77 86 }; 78 87 88 mod.AssertFailed=Class(mod.Exception, function(publ,supr){ 89 publ.__init__=function(comment, failMsg){ 90 this.failMessage = failMsg; 91 this.comment = comment; 92 supr.__init__.call(this, "%s failed: %s".format(comment, failMsg)); 93 } 94 }); 95 96 mod.assert=function(comment, value, failMsg){ 97 if(typeof comment == 'boolean'){ 98 failMsg=value; 99 value = comment; 100 comment =''; 101 } 102 103 if(value!==true){ 104 throw new mod.AssertFailed(comment, failMsg===undefined ? "Expected true but found: %s".format(value) : failMsg); 105 } 106 } 107 108 mod.assertTrue=function(comment, value){ 109 if(arguments.length==1){ 110 value = comment; 111 comment =''; 112 } 113 mod.assert(comment, value===true, "Expected true but found: %s".format(value)); 114 } 115 116 mod.assertFalse=function(comment, value){ 117 if(arguments.length==1){ 118 value = comment; 119 comment =''; 120 } 121 mod.assert(comment, value===false, "Expected false but found: %s".format(value)); 122 } 123 124 mod.assertEquals=function(comment, value1, value2){ 125 if(arguments.length==2){ 126 value2=value1; 127 value1 = comment; 128 comment =''; 129 } 130 if((value1 != null) && (value1.__equals__) || ((value2 != null) && (value2.__equals__))){ 131 mod.assert(comment, value1.__equals__(value2), "Expected (using __equals__) %s === %s.".format(value1, value2)); 132 }else{ 133 mod.assert(comment, value1 === value2, "Expected %s === %s.".format(value1, value2)); 134 } 135 } 136 137 mod.assertNotEquals=function(comment, value1, value2){ 138 if(arguments.length==2){ 139 value2=value1; 140 value1 = comment; 141 comment =''; 142 } 143 if((value1 != null) && (value1.__equals__) || ((value2 != null) && (value2.__equals__))){ 144 mod.assert(comment, ! value1.__equals__(value2), "Expected (using __equals__) %s !== %s.".format(value1, value2)); 145 }else{ 146 mod.assert(comment, value1 !== value2, "Expected %s !== %s.".format(value1, value2)); 147 } 148 } 149 150 mod.assertNull=function(comment, value){ 151 if(arguments.length==1){ 152 value = comment; 153 comment =''; 154 } 155 mod.assert(comment, value===null, "Expected %s === null.".format(value)); 156 } 157 158 mod.assertNotNull=function(comment, value){ 159 if(arguments.length==1){ 160 value = comment; 161 comment =''; 162 } 163 mod.assert(comment, value !==null, "Expected %s !== null.".format(value)); 164 } 165 166 mod.assertUndefined=function(comment, value){ 167 if(arguments.length==1){ 168 value = comment; 169 comment =''; 170 } 171 mod.assert(comment, value===undefined, "Expected %s === undefined.".format(value)); 172 } 173 174 mod.assertNotUndefined=function(comment, value){ 175 if(arguments.length==1){ 176 value = comment; 177 comment =''; 178 } 179 mod.assert(comment, value!==undefined, "Expected %s !== undefined".format(value)); 180 } 181 182 mod.assertNaN=function(comment, value){ 183 if(arguments.length==1){ 184 value = comment; 185 comment =''; 186 } 187 mod.assert(comment, isNaN(value)===true, "Expected %s === NaN.".format(value)); 188 } 189 190 mod.assertNotNaN=function(comment, value){ 191 if(arguments.length==1){ 192 value = comment; 193 comment =''; 194 } 195 mod.assert(comment, isNaN(value)!==true, "Expected %s !== NaN".format(value)); 196 } 197 198 mod.fail=function(){ 199 200 } 201 79 202 mod.objectKeys=function(obj){ 80 203 var keys=[]; … … 84 207 return keys; 85 208 }; 209 210 mod.__main__=function(){ 211 print(mod.test(function(){ 212 mod.assert(true); 213 mod.assertTrue(true); 214 mod.assertFalse(false); 215 mod.assertNull(null); 216 mod.assertNotNull(undefined); 217 mod.assertNotNull(''); 218 mod.assertNotNull({}); 219 mod.assertNotNull(0); 220 mod.assertUndefined(undefined); 221 mod.assertNotUndefined(null); 222 mod.assertNaN(NaN); 223 mod.assertNotNaN(435); 224 mod.assertEquals(1,1); 225 mod.assertEquals("a","a"); 226 mod.assertEquals(null,null); 227 mod.assertEquals(undefined,undefined); 228 mod.assertEquals(mod,mod); 229 mod.assertNotEquals(1,2); 230 mod.assertNotEquals(null,undefined); 231 mod.assertNotEquals(mod,{}); 232 })); 233 } 86 234 }); 235 236 jsolait/trunk/jsolait/lib/urllib.js
r5 r8 36 36 */ 37 37 publ.__init__=function(trace){ 38 supr (this).__init__("Could not create an HTTP request object", trace);38 supr.__init__.call(this, "Could not create an HTTP request object", trace); 39 39 }; 40 40 }); … … 49 49 */ 50 50 publ.__init__=function(trace){ 51 supr (this).__init__("Opening of HTTP request failed.", trace);51 supr.__init__.call(this, "Opening of HTTP request failed.", trace); 52 52 }; 53 53 }); … … 62 62 */ 63 63 publ.__init__ = function(trace){ 64 supr (this).__init__("Sending of HTTP request failed.", trace);64 supr.__init__.call(this, "Sending of HTTP request failed.", trace); 65 65 }; 66 66 }); jsolait/trunk/jsolait/lib/xml.js
r5 r8 41 41 */ 42 42 publ.__init__=function(trace){ 43 supr (this).__init__("Could not create an XML parser.", trace);43 supr.__init__.call(this, "Could not create an XML parser.", trace); 44 44 }; 45 45 }); … … 54 54 */ 55 55 publ.__init__=function(xml,trace){ 56 supr (this).__init__("Failed parsing XML document.",trace);56 supr.__init__.call(this, "Failed parsing XML document.",trace); 57 57 this.xml = xml; 58 58 }; jsolait/trunk/jsolait/lib/xmlrpc.js
r2 r8 39 39 */ 40 40 publ.__init__= function(status){ 41 supr (this).__init__("The server did not respond with a status 200 (OK) but with: " + status);41 supr.__init__.call(this, "The server did not respond with a status 200 (OK) but with: " + status); 42 42 this.status = status; 43 43 }; … … 57 57 */ 58 58 publ.__init__= function(msg, xml, trace){ 59 supr (this).__init__(msg,trace);59 supr.__init__.call(this, msg,trace); 60 60 this.xml = xml; 61 61 }; … … 73 73 */ 74 74 publ.__init__= function(faultCode, faultString){ 75 supr (this).__init__("XML-RPC Fault: " + faultCode + "\n\n" + faultString);75 supr.__init__.call(this, "XML-RPC Fault: " + faultCode + "\n\n" + faultString); 76 76 this.faultCode = faultCode; 77 77 this.faultString = faultString; … … 812 812 813 813 814 mod. test= function(){814 mod.__main__ = function(){ 815 815 var s = new mod.ServiceProxy("http://jsolait.net/test.py",['echo']); 816 816 print("creating ServiceProxy object using introspection for method construction...\n");
