Changeset 18

Show
Ignore:
Timestamp:
11/15/05 17:01:40 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

added private object container to jsolait calss instances, updated testing with a profiling function

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • jsolait/trunk/jsolait/jsolait.js

    r17 r18  
    2323    It provides the core functionalities  for creating classes, modules and for importing modules. 
    2424     
     25    @author Jan-Klaas Kollhof 
    2526    @version 2.0 
    26     $LastChangedBy$ 
    27     $Date$ 
    28     $Revision$ 
     27    @lastchangedby $LastChangedBy$ 
     28    @lastchangeddate $Date$ 
     29    @revision $Revision$ 
    2930**/ 
    3031 
     
    4647        args[i] = arguments[i]; 
    4748    } 
    48     //last arg should be a classScope 
     49 
    4950    classScope = args.pop(); 
    5051     
    51     //first arg is the class' name if it is a string 
    5252    if((args.length>0) && (typeof args[0] =='string')){ 
    5353        name=args.shift(); 
     
    5555        name="anonymous"; 
    5656    } 
    57     //the rest of the arguments should be base classses 
     57     
    5858    var bases = args; 
    5959     
    6060    //set up the 'public static' fields of the class 
    61     var statc={__isArray__ : false, 
     61    var __class__={__isArray__ : false, 
    6262                     __name__ : name, 
    6363                     __bases__: bases, 
    64                      __hashCount__:0, 
     64                     __id__: Class.__idcount__++, 
     65                     __hash__: function(){ 
     66                        return this.__id__; 
     67                     }, 
    6568                     __str__ : function(){ 
    6669                            return "[class %s]".format(this.__name__); 
    6770                        } 
    6871                    }; 
    69  
     72     
    7073    var baseProtos=[];//stores the prototypes of all the base classes 
    7174    var proto; //the prototype to use for the new class 
     
    7881        //this makes prototype creatin a bit simpler 
    7982        proto.toString=proto.__str__; 
    80         statc.__bases__=[Object]; 
     83        __class__.__bases__=[Object]; 
    8184    }else{ //inherit from all base classes 
    8285        //inheritance is done by  
     
    8790            baseProtos.push(baseClass.prototype); 
    8891            if(baseClass.__proto__ !== undefined){ 
    89                 baseProto = baseClass.__proto__(); 
     92                baseProto = baseClass.__proto__(bases); 
    9093            }else{ 
    9194                baseProto = new baseClass(Class); 
    9295            } 
    93             statc.__isArray__ = statc.__isArray__ || baseClass.__isArray__; 
     96            __class__.__isArray__ = __class__.__isArray__ || baseClass.__isArray__; 
    9497             
    9598            if(i==0){//for the first base class just use it's proto as the final proto 
     
    104107            //extend the new class' static interface 
    105108            for(var key in baseClass){ 
    106                 if(statc[key] === undefined){ 
    107                     statc[key] = baseClass[key]; 
     109                if(__class__[key] === undefined){ 
     110                    __class__[key] = baseClass[key]; 
    108111                } 
    109112            } 
     
    117120        proto.__hash__=function(){ 
    118121            if(this.__id__ === undefined){ 
    119                 this.__id__ = Class.__hashCount__++; 
     122                this.__id__ = Class.__idcount__++; 
    120123            } 
    121124            return this.__id__; 
    122125        }; 
    123126    } 
    124          
    125     //todo: run the class scope as scope(publ, [statc,] baseClassProto1, ... )  
    126     //publ represents the new class' prototype, statc the public static fields of the class(class properties) 
    127     if(classScope.length>baseProtos.length+1){ 
    128         classScope.apply(this,[proto,statc].concat(baseProtos)); 
     127    proto.__class__=__class__; 
     128   
     129    var privId = '__priv__' + __class__.__id__; 
     130     
     131    //run teh class setup function provided as classScope 
     132    if(classScope.length-1 > baseProtos.length){ 
     133        classScope.apply(this,[proto, privId].concat(baseProtos)); 
    129134    }else{ 
    130135        classScope.apply(this,[proto].concat(baseProtos)); 
     
    142147                    return rslt.__call__.apply(rslt, arguments); 
    143148                }; 
     149                 
     150                var privId='__priv__' + arguments.callee.__id__; 
     151                rslt[privId]={}; 
     152                 
    144153                var proto=arguments.callee.prototype; 
    145154                for(var n in proto){ 
     
    154163            } 
    155164        }; 
    156     }else if(statc.__isArray__){ 
     165    }else if(__class__.__isArray__){ 
    157166        //Since we cannot inherit from Array directly we take the same approach as with the callable above 
    158167        //and just have a constructor which creates an Array  
     
    160169            if(calledBy !== Class){ 
    161170                rslt=[]; 
     171                 
     172                var privId='__priv__' + arguments.callee.__id__; 
     173                rslt[privId]={}; 
     174                 
    162175                var proto=arguments.callee.prototype; 
    163176                for(var n in proto){ 
     
    186199            if(calledBy !== Class){ 
    187200                if(this.__init__){ 
     201                    var privId='__priv__' + arguments.callee.__id__; 
     202                    this[privId] = {}; 
    188203                    this.__init__.apply(this, arguments); 
    189204                }     
     
    200215     
    201216    //apply all the static fileds 
    202     for(var key in statc){ 
    203         NewClass[key] = statc[key]; 
     217    for(var key in __class__){ 
     218        NewClass[key] = __class__[key]; 
    204219    } 
    205     NewClass.toString=statc.__str__; 
     220    NewClass.toString=__class__.__str__; 
    206221     
    207222    return NewClass; 
    208223};     
    209 Class.__hashCount__=0; 
    210  
     224Class.__idcount__=0; 
    211225Class.toString = function(){ 
    212226    return "[object Class]"; 
  • jsolait/trunk/jsolait/lib/testing.js

    r9 r18  
    2828 
    2929Module("testing", "$Revision$", function(mod){ 
    30      
     30      
     31    mod.minProfileTime=500; 
    3132    /** 
    3233        Returns the average time used for executing a function. 
     
    4748    }; 
    4849     
     50    mod.profile=function(min,fn){ 
     51        if(arguments.length==1){ 
     52            fn=min; 
     53            min=mod.minProfileTime; 
     54        } 
     55        var args = []; 
     56        for(var i=2;i<arguments.length;i++){ 
     57            args.push(arguments[i]); 
     58        } 
     59        var cnt=0; 
     60        var t1=(new Date()).getTime(); 
     61        t2=t1; 
     62        while(t2-t1<min){ 
     63            cnt++; 
     64            fn.apply(null, args);     
     65            t2=(new Date()).getTime(); 
     66        } 
     67        return (t2-t1) / cnt; 
     68    }; 
     69     
     70     
    4971    mod.Test=Class(function(publ,supr){ 
    5072        publ.__init__=function(testScope){ 
     
    5375         
    5476        publ.run=function(){ 
     77            this.failed=false; 
     78            this.error=null; 
    5579            this.startTime=(new Date()).getTime(); 
    5680            try{ 
     
    5983                if(e instanceof mod.AssertFailed){ 
    6084                    this.error = e; 
     85                    this.failed=true; 
    6186                }else{ 
    6287                    throw new mod.Exception("Failed to run test.", e); 
     
    7499            } 
    75100        }; 
    76          
     101        publ.failed=false; 
     102        publ.error; 
    77103        publ.startTime; 
    78104        publ.endTime; 
     
    196222    }; 
    197223     
    198     mod.fail=function(){ 
    199          
     224    mod.fail=function(comment){ 
     225        throw new mod.AssertFailed(comment, "Fail was called"); 
    200226    }; 
    201227