Changeset 72

Show
Ignore:
Timestamp:
07/28/06 17:10:49 (2 years ago)
Author:
Jan-Klaas Kollhof
Message:

simplifying some code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/experimental/jsolait/jsolait.js

    r70 r72  
    3030**/ 
    3131 
    32 jsolait=(function(){ 
    33     jsolait = {}; 
    34      
    35     jsolait.__name__='jsolait'; 
    36      
    37     jsolait.__version__="$Revision$"; 
    38      
    39     jsolait.__str__ =function(){ 
     32jsolait=(function(publ){{with(publ){ 
     33     
     34    publ.__name__='jsolait'; 
     35     
     36    publ.__version__="$Revision$"; 
     37     
     38    publ.__str__ =function(){ 
    4039        return "[module '%s' version: %s]".format(this.__name__, (this.__version__+'').replace(/\$Revision:\s(\d+) \$/, "Rev.$1")); 
    4140    }; 
    42     jsolait.toString=jsolait.__str__; 
     41    publ.toString=__str__; 
    4342     
    4443    ///The location where jsolait is installed. 
    4544    //do not edit the following lines, it will be replaced by the build script 
    4645    /*@baseURI begin*/ 
    47     jsolait.baseURI="./jsolait"; 
     46    publ.baseURI="./jsolait"; 
    4847    /*@baseURI end*/ 
    4948         
     
    5150    //do not edit the following lines, it will be replaced by the build script 
    5251    /*@moduleSourceURIs begin*/ 
    53     jsolait.moduleSourceURIs={}; 
     52    publ.moduleSourceURIs={}; 
    5453    /*@moduleSourceURIs end*/ 
    5554     
     
    5958        they may contain StringFormating symbols e.g '%(baseURI)s/lib' 
    6059    **/ 
    61     jsolait.moduleSearchURIs = [".", "%(baseURI)s/lib"]; 
     60    publ.moduleSearchURIs = [".", "%(baseURI)s/lib"]; 
    6261    
    63     jsolait.packagesURI = "%(baseURI)s/packages"; 
    64      
    65     jsolait.modules={}; 
     62    publ.packagesURI = "%(baseURI)s/packages"; 
     63     
     64    publ.modules={}; 
     65  
     66    /** 
     67        Returns a string representation of an object. 
     68        @param obj  The object to return a string repr. of. 
     69        @return A string repr. the object. 
     70    **/ 
     71    publ.str = String; 
     72 
     73    /** 
     74        Return a String containing a printable representation of an object which can be used with eval() to create an equal object. 
     75        Objects can cosutumize the the value being returned by repr(obj) by providing a obj.__repr__() method which is called by repr(obj). 
     76        @param obj  The object to create a repr. String from. 
     77        @return A representation of the object. 
     78    **/ 
     79    publ.repr = function(obj){ 
     80        if(obj == null){ 
     81            return null; 
     82        }else if(obj.__repr__){ 
     83            return obj.__repr__(); 
     84        }else{ 
     85            switch(typeof obj){ 
     86                case "string": 
     87                    obj = obj.replace(/\\/g,"\\\\").replace(/\"/g,"\\\"").replace(/\n/g, "\\n").replace(/\r/g,"\\r"); 
     88                    return '"' + obj + '"'; 
     89                case "boolean":case"number": 
     90                    return "" + obj; 
     91                case "object": 
     92                    var out = []; 
     93                    if(obj == null){ 
     94                        return "null"; 
     95                    }else if(obj instanceof Array){ 
     96                        for(var i=0;i<obj.length;i++){ 
     97                            out.push(repr(obj[i])); 
     98                        } 
     99                        return "[" + out.join(",") + "]"; 
     100                    }else if(obj instanceof Object){ 
     101                        for(var key in obj){ 
     102                            out.push(repr(key) + ":" + repr(obj[key])); 
     103                        } 
     104                        return "{" + out.join(",") + "}"; 
     105                    } 
     106            } 
     107        } 
     108    }; 
     109 
     110    /** 
     111        Returns a unique id for an object. 
     112        The same object will always return the same id.  
     113        Most objects are id-able. The following steps are taken to find an id. 
     114        If the obj has an __id__ property that id will be returned. (all jsolait classes have an __id__ property) 
     115        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) 
     116        If the obj is a String  the string prefixed with $ is returned. 
     117        If the obj is a Number the number prefixed with a # is returned as a string. 
     118        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. 
     119         
     120        @param obj The object to get the id for. 
     121        @param forceId=false if true it forces id() to set an __id__ property onto a non id-able object, making it id-able. 
     122        @return A String containing a id value for the obj. 
     123    **/ 
     124    publ.id = function(obj, forceId){ 
     125        switch(typeof obj.__id__){ 
     126             
     127            case "undefined": 
     128                if(obj instanceof String || typeof obj == 'string'){ 
     129                    return '$' + obj; 
     130                }else if(obj instanceof Number || typeof obj == 'number'){ 
     131                    return '#' + obj; 
     132                }else if(forceId){ 
     133                    obj.__id__ = '@' + (Class.__idcount__++); 
     134                    return obj.__id__; 
     135                }else{ 
     136                    throw new Exception('Objec cannot be IDed: %s'.format(obj)); 
     137                } 
     138             
     139            case "function": 
     140                return obj.__id__(); 
     141             
     142            default: //string 
     143                return obj.__id__; 
     144        } 
     145    }; 
     146 
     147    /** 
     148        Returns a bound method. 
     149        A bound method is a function which is bound to a specific object. 
     150        Calling the bound method will call the given function with the this-object inside that function's scope being the object specified. 
     151         
     152        @param obj  The object the function should be bound to. 
     153        @param fn   A function object the obj will be bound to. 
     154        @return A method which when run executes the function with the this-object being the obj specified. 
     155    **/ 
     156    publ.bind = function(obj, fn){ 
     157        return function(){ 
     158            return fn.apply(obj, arguments); 
     159        }; 
     160    }; 
     161 
     162    /** 
     163        Returns if an object is an instance of a specified class or of a direct or indirect subclass thereof. 
     164         
     165        It also works for traditional javascript inheritance(i.e. SomeClass=function(){}; SomeClass.prototype=new SuperClass(); ...). 
     166        Internaly it first checks using instanceof if that fails it uses isinstance(obj.constructor, cls). 
     167        There are some differences between using isinstance and instanceof. 
     168        i.e.  
     169        (123 instanceof Number) == false; 
     170        isinstance(123, Number) == true; 
     171        ('abc' instanceof String) == false; 
     172        isinstance('abc', String) == true; 
     173            
     174        @param obj   The object to test. 
     175        @param cls     The class to test against. 
     176        @return True if the object is an instance of cls. False otherwise. 
     177    **/ 
     178    publ.isinstance=function(obj, cls){ 
     179        if(obj instanceof cls){ 
     180            return true; 
     181        }else{ 
     182            return issubclass(obj.constructor, cls); 
     183        } 
     184    }; 
     185 
     186    /** 
     187        Returns if a cls is a direct or indirect subclass of another. 
     188         
     189        A class is always a subclass of itself and Object is the base for all classes. 
     190        A class is a subclass of baseclass if it's prototype is an instance of baseclass. 
     191        A class is a subclass of baseclass if any of it's __bases__ is a subclass of baseclass. 
     192        If there are no __bases__ defined there is no way to findout about inheritance besides  
     193        the prototype chain which was checked by instanceof before, so false is returned. 
     194         
     195        @param cls  The class to test. 
     196        @param baseclass  The assumed superclass. 
     197        @return True if cls is a subclass of baseclass otherwise false. 
     198    **/ 
     199    publ.issubclass=function(cls, baseclass){ 
     200        if(baseclass === Object || cls===baseclass || (cls.prototype instanceof baseclass)){ 
     201            return true; 
     202        }else{ 
     203            var bases = cls.__bases__; 
     204            if(bases != null){ 
     205                for(var i=0;i<bases.length;i++){ 
     206                    if(bases[i] === baseclass){ 
     207                        return true; 
     208                    } 
     209                } 
     210                for(var i=0;i<bases.length;i++){ 
     211                    if(issubclass(bases[i], baseclass)){ 
     212                        return true; 
     213                    } 
     214                } 
     215            } 
     216            return false; 
     217        } 
     218    }; 
     219     
    66220     
    67221    /** 
     
    76230                                                the super class' wrapper for calling inherited methods. 
    77231    **/ 
    78     var Class=function(name, base1, classScope){ 
     232    publ.Class=function(name, base1, classScope){ 
    79233        var args=[]; 
    80234        for(var i=0;i<arguments.length;i++){ 
     
    251405    Class.__str__=Class.toString = function(){return "[object Class]";}; 
    252406    Class.__createProto__=function(){ throw "Can't use Class as a base class.";}; 
    253     jsolait.Class = Class; 
    254407     
    255408    Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callable interface instead using YourClass::__call__.";}; 
     
    261414    String.__str__ =String.toString=function(){return "[class String]";}; 
    262415     
    263     jsolait.Exception=Class(function(publ){ 
     416    publ.Exception=Class(function(publ){ 
    264417        /** 
    265418            Initializes a new Exception. 
     
    302455        publ.message; 
    303456        ///The module the Exception belongs to. 
    304         publ.module=jsolait
     457        publ.module=publ
    305458        ///The error which caused the Exception or undefined. 
    306459        publ.trace; 
    307460    }); 
    308461     
    309     jsolait.ModuleClass=Class(function(publ){ 
     462    publ.ModuleClass=Class(function(publ){ 
    310463        publ.__name__; 
    311464        publ.__version__; 
     
    330483        @return HTTP request object. 
    331484    **/ 
    332     jsolait.getHTTPRequestObject=function() { 
     485    publ.getHTTPRequestObject=function() { 
    333486        var obj; 
    334487        try{ //to get the mozilla httprequest object 
     
    344497                        obj = new ActiveXObject("microsoft.XMLHTTP"); 
    345498                    }catch(e){ 
    346                         throw new jsolait.Exception("Unable to get an HTTP request object."); 
     499                        throw new Exception("Unable to get an HTTP request object."); 
    347500                    } 
    348501                } 
     
    355508        Thrown when a file could not be loaded. 
    356509    **/ 
    357     jsolait.LoadURIFailed=Class(jsolait.Exception, function(publ, priv,supr){ 
     510    publ.LoadURIFailed=Class(Exception, function(publ, priv,supr){ 
    358511        /** 
    359512            Initializes a new LoadURIFailed Exception. 
     
    376529        @return                 The content of the file. 
    377530    **/ 
    378     jsolait.loadURI=function(uri, headers)
     531    publ.loadURI=function(uri, headers)
    379532        headers = (headers !== undefined) ? headers : []; 
    380533        try{ 
    381             var xmlhttp = jsolait.getHTTPRequestObject(); 
     534            var xmlhttp = getHTTPRequestObject(); 
    382535            xmlhttp.open("GET", uri, false); 
    383536            for(var i=0;i< headers.length;i++){ 
     
    386539            xmlhttp.send(""); 
    387540        }catch(e){ 
    388             throw new jsolait.LoadURIFailed(uri, e); 
     541            throw new LoadURIFailed(uri, e); 
    389542        } 
    390543        //todo: the status checking needs testing 
     
    393546            return s; 
    394547        }else{ 
    395              throw new jsolait.LoadURIFailed(uri, new jsolait.Exception("Server did not respond with status code 200 but with: " + xmlhttp.status)); 
    396         } 
    397     }; 
    398          
     548             throw new LoadURIFailed(uri, new Exception("Server did not respond with status code 200 but with: " + xmlhttp.status)); 
     549        } 
     550    }; 
     551 
     552     
     553    /** 
     554        Returns the possible locations of a module's source file. 
     555        A module's source file location is determined by treating each module name as a directory. 
     556        Only the last one is assumed to point to a file. 
     557         
     558    **/ 
     559    publ.getSearchURIsForModuleName=function(name){ 
     560        var sourceURI; 
     561         
     562        var searchURIs = []; 
     563         
     564        if(moduleSourceURIs[name] != undefined){ 
     565            searchURIs.push(moduleSourceURIs[name].format(jsolait)); 
     566        }else{ 
     567            name = name.split('.'); 
     568            if(name.length>1){ 
     569                if(moduleSourceURIs[name[0]] != undefined){ 
     570                    var uri = moduleSourceURIs[name[0]].format(jsolait); 
     571                    searchURIs.push("%s/%s.js".format(uri, name.slice(1).join('/'))); 
     572                } 
     573                searchURIs.push("%s/%s.js".format(packagesURI.format(jsolait),name.join('/'))); 
     574            } 
     575             
     576            for(var i=0;i<moduleSearchURIs.length; i++){ 
     577                searchURIs.push("%s/%s.js".format(moduleSearchURIs[i].format(jsolait), name.join("/"))); 
     578            } 
     579            name =  name.join("."); 
     580        } 
     581        return searchURIs; 
     582    }; 
     583     
    399584    /** 
    400585        Thrown when a module could not be found. 
    401586    **/ 
    402     jsolait.LoadModuleFailed=Class(jsolait.Exception, function(publ, supr){ 
     587    publ.LoadModuleFailed=Class(Exception, function(publ, supr){ 
    403588        /** 
    404589            Initializes a new LoadModuleFailed Exception. 
     
    417602        publ.moduleURIs; 
    418603    }); 
    419  
    420604     
    421605    /** 
    422606       Loads a module given its name(someModule.someSubModule). 
    423        A module's file location is determined by treating each module name as a directory. 
    424        Only the last one is assumed to point to a file. 
    425        If the module's URL is not known (i.e the module name was not found in jsolait.knownModuleURIs) 
    426        then it will be searched using all URIs found in jsolait.moduleSearchURIs. 
     607       jsolait.getSearchURIsForModuleName() will be used to determine the possible locations for the source of the module. 
     608       
    427609       @param name   The name of the module to load. 
    428610       @return           The module object. 
    429611    **/ 
    430     jsolait.loadModule = function(name){ 
    431  
    432         if(jsolait.modules[name]){ //module already loaded 
    433             return jsolait.modules[name]; 
     612    publ.loadModule = function(name){ 
     613 
     614        if(modules[name]){ //module already loaded 
     615            return modules[name]; 
    434616        }else{ 
    435617            var src,sourceURI; 
    436618             
    437             var searchURIs = []; 
    438              
    439             if(jsolait.moduleSourceURIs[name] != undefined){ 
    440                 searchURIs.push(jsolait.moduleSourceURIs[name].format(jsolait)); 
    441             }else{ 
    442                 name = name.split('.'); 
    443                 if(name.length>1){ 
    444                     if(jsolait.moduleSourceURIs[name[0]] != undefined){ 
    445                         var uri = jsolait.moduleSourceURIs[name[0]].format(jsolait); 
    446                         searchURIs.push("%s/%s.js".format(uri, name.slice(1).join('/'))); 
    447                     } 
    448                     searchURIs.push("%s/%s.js".format(jsolait.packagesURI.format(jsolait),name.join('/'))); 
    449                 } 
    450                  
    451                 for(var i=0;i<jsolait.moduleSearchURIs.length; i++){ 
    452                     searchURIs.push("%s/%s.js".format(jsolait.moduleSearchURIs[i].format(jsolait), name.join("/"))); 
    453                 } 
    454                 name =  name.join("."); 
    455             } 
     619            var searchURIs =getSearchURIsForModuleName(name); 
    456620             
    457621            var failedURIs=[]; 
     
    459623                try{ 
    460624                    sourceURI = searchURIs[i]; 
    461                     src = jsolait.loadURI(sourceURI); 
     625                    src = loadURI(sourceURI); 
    462626                    break; 
    463627                }catch(e){ 
     
    467631             
    468632            if(src == null){ 
    469                 throw new jsolait.LoadModuleFailed(name, failedURIs); 
     633                throw new LoadModuleFailed(name, failedURIs); 
    470634            }else{ 
    471635                try{//interpret the script 
    472                     var m = jsolait.createModuleFromSource(name, src, sourceURI); 
     636                    var m = createModuleFromSource(name, src, sourceURI); 
    473637                    return m; 
    474638                }catch(e){ 
    475                     throw new jsolait.LoadModuleFailed(name, [sourceURI], e); 
     639                    throw new LoadModuleFailed(name, [sourceURI], e); 
    476640                } 
    477641            } 
     
    479643    }; 
    480644            
    481     jsolait.__imprt__ = function(name, destinationScope){ 
     645    publ.__imprt__ = function(name, destinationScope){ 
    482646        var n=name.replace(/\s/g,"").split(":"); 
    483647        name = n[0]; 
     
    488652        } 
    489653         
    490         var m = jsolait.loadModule(name); 
     654        var m = loadModule(name); 
    491655         
    492656        if(items.length > 0){ 
     
    507671    }; 
    508672     
    509     jsolait.CreateModuleFailed=Class(jsolait.Exception, function(publ, supr){ 
     673    publ.CreateModuleFailed=Class(Exception, function(publ, supr){ 
    510674        /** 
    511675            Initializes a new CreateModuleFailed Exception. 
     
    521685    }); 
    522686     
    523     jsolait.createModule=function(name, source, sourceURI, modFn){ 
    524         var newMod = new jsolait.ModuleClass(name, source, sourceURI); 
     687    publ.createModule=function(name, source, sourceURI, modFn){ 
     688        var newMod = new ModuleClass(name, source, sourceURI); 
    525689         
    526690        var privateScope={imprt: function(imp){ 
    527691                var s = arguments.callee.scope; 
    528                 jsolait.__imprt__(imp, s); 
     692                __imprt__(imp, s); 
    529693            }}; 
    530694        privateScope.imprt.scope=privateScope; 
    531         privateScope.Exception = Class(jsolait.Exception, new Function()); 
     695        privateScope.Exception = Class(Exception, new Function()); 
    532696        privateScope.Exception.prototype.module = newMod; 
    533         
     697         
     698        var locals={ 
     699            str:str, 
     700            repr:repr, 
     701            id:id, 
     702            bind:bind, 
     703            isinstance:isinstance, 
     704            issubclass:issubclass, 
     705            jsolait:jsolait, 
     706            Class:Class 
     707        }; 
     708         
    534709        try{//to run the module source 
    535             modFn.call(newMod, newMod, privateScope, jsolait.builtin); 
     710            modFn.call(newMod, newMod, privateScope, locals); 
    536711        }catch(e){ 
    537             throw new jsolait.CreateModuleFailed(newMod, e); 
     712            throw new CreateModuleFailed(newMod, e); 
    538713        } 
    539714         
     
    541716        applyNames(privateScope); 
    542717         
    543         jsolait.modules[name] = newMod; 
     718        modules[name] = newMod; 
    544719        return newMod; 
    545720    }; 
    546721     
    547     jsolait.createModuleFromSource=function(name, source, sourceURI){ 
     722    publ.createModuleFromSource=function(name, source, sourceURI){ 
    548723        var modFn = new Function("publ,priv,__builtin__", "with(__builtin__){with(publ){with(priv){\n" + source + "\n}}}"); 
    549         return jsolait.createModule(name, source, sourceURI, modFn); 
     724        return createModule(name, source, sourceURI, modFn); 
    550725    };      
    551726     
    552     jsolait.Module = function(name, modFn){ 
    553         return jsolait.createModule(name, '', '', modFn); 
     727    publ.Module = function(name, modFn){ 
     728        return createModule(name, str(modFn), '', modFn); 
    554729    }; 
    555730     
     
    563738    }; 
    564739 
    565 //---------------------------------------------------builtins ------------------------------------------------------- 
    566     /** 
    567         Returns a string representation of an object. 
    568         @param obj  The object to return a string repr. of. 
    569         @return A string repr. the object. 
    570     **/ 
    571     var str = String; 
    572  
    573     /** 
    574         Return a String containing a printable representation of an object which can be used with eval() to create an equal object. 
    575         Objects can cosutumize the the value being returned by repr(obj) by providing a obj.__repr__() method which is called by repr(obj). 
    576         @param obj  The object to create a repr. String from. 
    577         @return A representation of the object. 
    578     **/ 
    579     var repr = function(obj){ 
    580         if(obj == null){ 
    581             return null; 
    582         }else if(obj.__repr__){ 
    583             return obj.__repr__(); 
    584         }else{ 
    585             switch(typeof obj){ 
    586                 case "string": 
    587                     obj = obj.replace(/\\/g,"\\\\").replace(/\"/g,"\\\"").replace(/\n/g, "\\n").replace(/\r/g,"\\r"); 
    588                     return '"' + obj + '"'; 
    589                 case "boolean":case"number": 
    590                     return "" + obj; 
    591                 case "object": 
    592                     var out = []; 
    593                     if(obj == null){ 
    594                         return "null"; 
    595                     }else if(obj instanceof Array){ 
    596                         for(var i=0;i<obj.length;i++){ 
    597                             out.push(repr(obj[i])); 
    598                         } 
    599                         return "[" + out.join(",") + "]"; 
    600                     }else if(obj instanceof Object){ 
    601                         for(var key in obj){ 
    602                             out.push(repr(key) + ":" + repr(obj[key])); 
    603                         } 
    604                         return "{" + out.join(",") + "}"; 
    605                     } 
    606             } 
    607         } 
    608     }; 
    609  
    610     /** 
    611         Returns a unique id for an object. 
    612         The same object will always return the same id.  
    613         Most objects are id-able. The following steps are taken to find an id. 
    614         If the obj has an __id__ property that id will be returned. (all jsolait classes have an __id__ property) 
    615         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) 
    616         If the obj is a String  the string prefixed with $ is returned. 
    617         If the obj is a Number the number prefixed with a # is returned as a string. 
    618         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. 
    619          
    620         @param obj The object to get the id for. 
    621         @param forceId=false if true it forces id() to set an __id__ property onto a non id-able object, making it id-able. 
    622         @return A String containing a id value for the obj. 
    623     **/ 
    624     var id = function(obj, forceId){ 
    625         switch(typeof obj.__id__){ 
    626              
    627             case "undefined": 
    628                 if(obj instanceof String || typeof obj == 'string'){ 
    629                     return '$' + obj; 
    630                 }else if(obj instanceof Number || typeof obj == 'number'){ 
    631                     return '#' + obj; 
    632                 }else if(forceId){ 
    633                     obj.__id__ = '@' + (Class.__idcount__++); 
    634                     return obj.__id__; 
    635                 }else{ 
    636                     throw new jsolait.Exception('Objec cannot be IDed: %s'.format(obj)); 
    637                 } 
    638              
    639             case "function": 
    640                 return obj.__id__(); 
    641              
    642             default: //string 
    643                 return obj.__id__; 
    644         } 
    645     }; 
    646  
    647     /** 
    648         Returns a bound method. 
    649         A bound method is a function which is bound to a specific object. 
    650         Calling the bound method will call the given function with the this-object inside that function's scope being the object specified. 
    651          
    652         @param obj  The object the function should be bound to. 
    653         @param fn   A function object the obj will be bound to. 
    654         @return A method which when run executes the function with the this-object being the obj specified. 
    655     **/ 
    656     var bind = function(obj, fn){ 
    657         return function(){ 
    658             return fn.apply(obj, arguments); 
    659         }; 
    660     }; 
    661  
    662     /** 
    663         Returns if an object is an instance of a specified class or of a direct or indirect subclass thereof. 
    664          
    665         It also works for traditional javascript inheritance(i.e. SomeClass=function(){}; SomeClass.prototype=new SuperClass(); ...). 
    666         Internaly it first checks using instanceof if that fails it uses isinstance(obj.constructor, cls). 
    667         There are some differences between using isinstance and instanceof. 
    668         i.e.  
    669         (123 instanceof Number) == false; 
    670         isinstance(123, Number) == true; 
    671         ('abc' instanceof String) == false; 
    672         isinstance('abc', String) == true; 
    673             
    674         @param obj   The object to test. 
    675         @param cls     The class to test against. 
    676         @return True if the object is an instance of cls. False otherwise. 
    677     **/ 
    678     var isinstance=function(obj, cls){ 
    679         if(obj instanceof cls){ 
    680             return true; 
    681         }else{ 
    682             return issubclass(obj.constructor, cls); 
    683         } 
    684     }; 
    685  
    686     /** 
    687         Returns if a cls is a direct or indirect subclass of another. 
    688          
    689         A class is always a subclass of itself and Object is the base for all classes. 
    690         A class is a subclass of baseclass if it's prototype is an instance of baseclass. 
    691         A class is a subclass of baseclass if any of it's __bases__ is a subclass of baseclass. 
    692         If there are no __bases__ defined there is no way to findout about inheritance besides  
    693         the prototype chain which was checked by instanceof before, so false is returned. 
    694          
    695         @param cls  The class to test. 
    696         @param baseclass  The assumed superclass. 
    697         @return True if cls is a subclass of baseclass otherwise false. 
    698     **/ 
    699     var issubclass=function(cls, baseclass){ 
    700         if(baseclass === Object || cls===baseclass || (cls.prototype instanceof baseclass)){ 
    701             return true; 
    702         }else{ 
    703             var bases = cls.__bases__; 
    704             if(bases != null){ 
    705                 for(var i=0;i<bases.length;i++){ 
    706                     if(bases[i] === baseclass){ 
    707                         return true; 
    708                     } 
    709                 } 
    710                 for(var i=0;i<bases.length;i++){ 
    711                     if(issubclass(bases[i], baseclass)){ 
    712                         return true; 
    713                     } 
    714                 } 
    715             } 
    716             return false; 
    717         } 
    718     }; 
    719      
    720     jsolait.builtin={ 
    721         str:str, 
    722         repr:repr, 
    723         id:id, 
    724         bind:bind, 
    725         isinstance:isinstance, 
    726         issubclass:issubclass, 
    727         jsolait:jsolait, 
    728         Class:Class 
    729     }; 
    730      
     740 
    731741//---------------------------------------------------String Format ------------------------------------------------------- 
    732742    /** 
     
    825835        if(sf){ 
    826836            if(sf.join("") != this){ 
    827                 throw new jsolait.Exception("Unsupported formating string."); 
    828             } 
    829         }else{ 
    830             throw new jsolait.Exception("Unsupported formating string."); 
     837                throw new Exception("Unsupported formating string."); 
     838            } 
     839        }else{ 
     840            throw new Exception("Unsupported formating string."); 
    831841        } 
    832842        var rslt=""; 
     
    843853            }else if(s=="%s"){ //making %s faster 
    844854                if(cnt>=arguments.length){ 
    845                     throw new jsolait.Exception("Not enough arguments for format string."); 
     855                    throw new Exception("Not enough arguments for format string."); 
    846856                }else{ 
    847857                    obj=arguments[cnt]; 
     
    860870                        obj = arguments[0][frmt.key]; 
    861871                    }else{ 
    862                         throw new jsolait.Exception("Object or associative array expected as formating value."); 
     872                        throw new Exception("Object or associative array expected as formating value."); 
    863873                    } 
    864874                }else{//get the current value 
    865875                    if(cnt>=arguments.length){ 
    866                         throw new jsolait.Exception("Not enough arguments for format string."); 
     876                        throw new Exception("Not enough arguments for format string."); 
    867877                    }else{ 
    868878                        obj=arguments[cnt]; 
     
    889899                            s=pad(obj, frmt.paddingFlag, frmt.minLength); 
    890900                        }else{ 
    891                             throw new jsolait.Exception("Character of length 1 required."); 
     901                            throw new Exception("Character of length 1 required."); 
    892902                        } 
    893903                    }else{ 
    894                         throw new jsolait.Exception("Character or Byte required."); 
     904                        throw new Exception("Character or Byte required."); 
    895905                    } 
    896906                }else if(typeof obj == "number"){ 
     
    951961                    s=pad(s, frmt.paddingFlag, frmt.minLength);//do padding and justifiing 
    952962                }else{ 
    953                     throw new jsolait.Exception("Number required."); 
     963                    throw new Exception("Number required."); 
    954964                } 
    955965            } 
     
    10081018    }; 
    10091019     
    1010     applyNames(jsolait); 
    1011     return jsolait; 
    1012 }()); 
     1020    applyNames(publ); 
     1021     
     1022    return publ; 
     1023}}}({})); 
  • branches/experimental/jsolait/jsolait.wsf

    r71 r72  
    4646        var ForReading = 1, ForWriting = 2; 
    4747         
    48         var print = __builtin__.print = function(m){ 
     48        print = function(m){ 
    4949            var s=[]; 
    5050            for(var i=0;i<arguments.length;i++){ 
     
    5555        } 
    5656          
    57         var pprint= __builtin__.pprint=function(m, indent){ 
     57        pprint= function(m, indent){ 
    5858            var m = m.split("\n"); 
    5959             
  • branches/experimental/jsolait/lib/asyncimprt.js

    r71 r72  
     1 
     2 
    13var applyNames=function(container){ 
    24    for(var n in container){ 
     
    6365};      
    6466 
    65 publ.getSearchURIsForModuleName=function(name){ 
    66     var sourceURI; 
    67      
    68     var searchURIs = []; 
    69      
    70     if(jsolait.moduleSourceURIs[name] != undefined){ 
    71         searchURIs.push(jsolait.moduleSourceURIs[name].format(jsolait)); 
    72     }else{ 
    73         name = name.split('.'); 
    74         if(name.length>1){ 
    75             if(jsolait.moduleSourceURIs[name[0]] != undefined){ 
    76                 var uri = jsolait.moduleSourceURIs[name[0]].format(jsolait); 
    77                 searchURIs.push("%s/%s.js".format(uri, name.slice(1).join('/'))); 
    78             } 
    79             searchURIs.push("%s/%s.js".format(jsolait.packagesURI.format(jsolait),name.join('/'))); 
    80         } 
    81          
    82         for(var i=0;i<jsolait.moduleSearchURIs.length; i++){ 
    83             searchURIs.push("%s/%s.js".format(jsolait.moduleSearchURIs[i].format(jsolait), name.join("/"))); 
    84         } 
    85         name =  name.join("."); 
    86     } 
    87     return searchURIs; 
    88 }; 
     67 
    8968 
    9069publ.loadModule = function(name, returncb){ 
     
    9372        returncb(jsolait.modules[name], null); 
    9473    }else{ 
    95         var searchURIs = getSearchURIsForModuleName(name); 
     74        var searchURIs = jsolait.getSearchURIsForModuleName(name); 
    9675        var failedURIs=[]; 
    9776        loadURI(searchURIs.shift(), function(src, sourceURI, err){