Changeset 61
- Timestamp:
- 04/27/06 15:39:01 (2 years ago)
- Files:
-
- trunk/jsolait/jsolait.js (modified) (3 diffs)
- trunk/jsolait/lib/jsonrpc.js (modified) (1 diff)
- trunk/jsolait/lib/sets.js (modified) (5 diffs)
- trunk/test/test_core.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/jsolait/jsolait.js
r60 r61 64 64 __bases__: bases, 65 65 __id__: '@' + classID, 66 __hash__: function(){67 return this.__id__;68 },69 66 __str__ : function(){ 70 67 return "[class %s]".format(this.__name__); … … 111 108 } 112 109 } 113 //make sure all jsolait objects have a hash method 114 if(proto.__hash__ === undefined){ 115 proto.__hash__=function(){ 116 if(this.__id__ === undefined){ 117 this.__id__ = '@' + (Class.__idcount__++); 118 } 110 //make sure all jsolait objects have a id method 111 if(proto.__id__ === undefined){ 112 proto.__id__=function(){ 113 this.__id__ = '@' + (Class.__idcount__++); 119 114 return this.__id__; 120 115 }; … … 270 265 271 266 /** 272 Returns a hash valuefor an object.273 The same object will always return the same hash.274 Most objects are hashable. The following steps are taken to find a hash value:267 Returns a unique id for an object. 268 The same object will always return the same id. 269 Most objects are id-able. The following steps are taken to find an id. 275 270 If the obj has an __id__ property that id will be returned. (all jsolait classes have an __id__ property) 276 If the obj has a __ hash__ method the return value of that method will be returned.(all jsolait objects have a __hash__ method which sets an __id__ property)271 If the obj has a __id__ method the return value of that method will be returned.(all jsolait objects have a __id__ method which sets an __id__ property) 277 272 If the obj is a String the string prefixed with $ is returned. 278 273 If the obj is a Number the number prefixed with a # is returned as a string. 279 All other objects are not safely hashable and an exception is thrown unless forceId is true. In that case the object will get a unique __id__ property applied which is returned. 280 281 @param obj The object to hash. 282 @param forceId=false if true it forces hash() to set an __id__ property onto a non hashable object, making it hashable. 283 @return A String containing a hash value for the obj. 284 **/ 285 hash = function(obj, forceId){ 286 if(obj.__id__ != null){ 287 return obj.__id__; 288 }else if(obj.__hash__){ 289 return obj.__hash__(); 290 }else if(obj instanceof String || typeof obj == 'string'){ 291 return '$' + obj; 292 }else if(obj instanceof Number || typeof obj == 'number'){ 293 return '#' + obj; 294 }else if(forceId){ 295 obj.__id__ = '@' + (Class.__idcount__++); 296 return obj.__id__; 297 }else{ 298 throw new jsolait.Exception('Objec cannot be hashed: %s'.format(obj)); 274 All other objects are not safely id-able and an exception is thrown unless forceId is true. In that case the object will get a unique __id__ property applied which is returned. 275 276 @param obj The object to get the id for. 277 @param forceId=false if true it forces id() to set an __id__ property onto a non id-able object, making it id-able. 278 @return A String containing a id value for the obj. 279 **/ 280 id = function(obj, forceId){ 281 switch(typeof obj.__id__){ 282 283 case "undefined": 284 if(obj instanceof String || typeof obj == 'string'){ 285 return '$' + obj; 286 }else if(obj instanceof Number || typeof obj == 'number'){ 287 return '#' + obj; 288 }else if(forceId){ 289 obj.__id__ = '@' + (Class.__idcount__++); 290 return obj.__id__; 291 }else{ 292 throw new jsolait.Exception('Objec cannot be IDed: %s'.format(obj)); 293 } 294 295 case "function": 296 return obj.__id__(); 297 298 default: //string 299 return obj.__id__; 299 300 } 300 301 }; trunk/jsolait/lib/jsonrpc.js
r59 r61 471 471 publ._sendRequest=function(method, params, callback){ 472 472 var r = new mod.PendingRequest(callback); 473 this._pendingRequests[ hash(r)] = r;474 var data = mod.marshall({method:method, params:params, id: hash(r)});473 this._pendingRequests[id(r)] = r; 474 var data = mod.marshall({method:method, params:params, id:id(r)}); 475 475 this._sendMessage(data); 476 476 return r; trunk/jsolait/lib/sets.js
r57 r61 46 46 /** 47 47 The Set class. 48 All Items added to a set must be hashable using jsolait's hash() function.48 All Items added to a set must be id-able using jsolait's id() function. 49 49 **/ 50 50 mod.Set=Class(function(publ, supr){ … … 78 78 **/ 79 79 publ.add=function(item){ 80 this.items[ hash(item)] = item;80 this.items[id(item)] = item; 81 81 return item; 82 82 }; … … 91 91 **/ 92 92 publ.remove=function(item){ 93 var h = hash(item);93 var h = id(item); 94 94 if(this.items[h] === undefined){ 95 95 throw new mod.ItemNotFoundInSet(this, item); … … 107 107 **/ 108 108 publ.discard=function(item){ 109 var h = hash(item);109 var h = id(item); 110 110 item = this.items[h]; 111 111 delete this.items[h]; … … 119 119 **/ 120 120 publ.contains=function(item){ 121 return (this.items[ hash(item)] !== undefined);121 return (this.items[id(item)] !== undefined); 122 122 }; 123 123 /** trunk/test/test_core.js
r50 r61 50 50 51 51 52 testing.assertEquals(" hash()", hash("a"), "$a");53 testing.assertEquals(" hash()", hash("$a"), "$$a");54 testing.assertEquals(" hash()", hash(123), "#123");55 testing.assertEquals(" hash()", hash(mod), mod.__id__);52 testing.assertEquals("id()", id("a"), "$a"); 53 testing.assertEquals("id()", id("$a"), "$$a"); 54 testing.assertEquals("id()", id(123), "#123"); 55 testing.assertEquals("id()", id(mod), mod.__id__); 56 56 57 57 logger.log("testing core functions ...");
