| 58 | | //the next arg should be the superclass |
|---|
| 59 | | if(args.length > 0){ |
|---|
| 60 | | superClass = args.shift(); |
|---|
| | 57 | //the rest of the arguments should be base classses |
|---|
| | 58 | var bases = args; |
|---|
| | 59 | |
|---|
| | 60 | //set up the 'public static' fields of the class |
|---|
| | 61 | var statc={__isArray__ : false, |
|---|
| | 62 | __name__ : name, |
|---|
| | 63 | __bases__: bases, |
|---|
| | 64 | __hashCount__:0, |
|---|
| | 65 | __str__ : function(){ |
|---|
| | 66 | return "[class %s]".format(this.__name__); |
|---|
| | 67 | } |
|---|
| | 68 | }; |
|---|
| | 69 | |
|---|
| | 70 | var baseProtos=[];//stores the prototypes of all the base classes |
|---|
| | 71 | var proto; //the prototype to use for the new class |
|---|
| | 72 | if(bases.length==0){//use Object as base |
|---|
| | 73 | proto={}; |
|---|
| | 74 | proto.__str__ = function(){ |
|---|
| | 75 | return "[%s %s]".format(this.__class__.prototype.__call__ === undefined ? 'object' : 'callable', this.__class__.__name__); |
|---|
| | 76 | }; |
|---|
| | 77 | //because toString is not apearing in a for in loop we will just use __str__ and always assign it to toString |
|---|
| | 78 | //this makes prototype creatin a bit simpler |
|---|
| | 79 | proto.toString=proto.__str__; |
|---|
| | 80 | statc.__bases__=[Object]; |
|---|
| | 81 | }else{ //inherit from all base classes |
|---|
| | 82 | //inheritance is done by |
|---|
| | 83 | var baseProto; |
|---|
| | 84 | for(var i=0;i<bases.length;i++){ |
|---|
| | 85 | var baseClass = bases[i]; |
|---|
| | 86 | //remember the base prototypes |
|---|
| | 87 | baseProtos.push(baseClass.prototype); |
|---|
| | 88 | if(baseClass.__proto__ !== undefined){ |
|---|
| | 89 | baseProto = baseClass.__proto__(); |
|---|
| | 90 | }else{ |
|---|
| | 91 | baseProto = new baseClass(Class); |
|---|
| | 92 | } |
|---|
| | 93 | statc.__isArray__ = statc.__isArray__ || baseClass.__isArray__; |
|---|
| | 94 | |
|---|
| | 95 | if(i==0){//for the first base class just use it's proto as the final proto |
|---|
| | 96 | proto = baseProto; |
|---|
| | 97 | }else{//for all others extend(do not override) the final proto with the properties in the baseProto |
|---|
| | 98 | for(var key in baseProto){ |
|---|
| | 99 | if(proto[key] === undefined){ |
|---|
| | 100 | proto[key] = baseProto[key]; |
|---|
| | 101 | } |
|---|
| | 102 | } |
|---|
| | 103 | } |
|---|
| | 104 | //extend the new class' static interface |
|---|
| | 105 | for(var key in baseClass){ |
|---|
| | 106 | if(statc[key] === undefined){ |
|---|
| | 107 | statc[key] = baseClass[key]; |
|---|
| | 108 | } |
|---|
| | 109 | } |
|---|
| | 110 | } |
|---|
| | 111 | //make sure the toString points to __str__, this will make overwriting __str__ after object construction impossible (todo ?) |
|---|
| | 112 | //but will be faster than having toString call __str__, also overwriting methods unless they are ment to be overridden is not cool anyways. |
|---|
| | 113 | proto.toString=proto.__str__; |
|---|
| | 114 | } |
|---|
| | 115 | //make sure all jsolait objects have a hash method |
|---|
| | 116 | if(proto.__hash__ === undefined){ |
|---|
| | 117 | proto.__hash__=function(){ |
|---|
| | 118 | if(this.__id__ === undefined){ |
|---|
| | 119 | this.__id__ = Class.__hashCount__++; |
|---|
| | 120 | } |
|---|
| | 121 | return this.__id__; |
|---|
| | 122 | }; |
|---|
| | 123 | } |
|---|
| | 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)); |
|---|
| 64 | | var mixinClasses=args; |
|---|
| 65 | | |
|---|
| 66 | | //this is the constructor for the new objects created from the new class. |
|---|
| 67 | | //if and only if it is NOT used for prototyping/subclassing the __init__ method of the newly created object will be called. |
|---|
| 68 | | //todo: this breaks mozilla's implementation of instanceof which returns true for every jsolait object and any jsolait class |
|---|
| 69 | | //a workaround would be using new Function |
|---|
| 70 | | var NewClass = function(calledBy){ |
|---|
| 71 | | if(calledBy !== Class){ |
|---|
| 72 | | //simulate Array and Function subclassing |
|---|
| 73 | | if(NewClass.prototype instanceof Array || NewClass.prototype instanceof Function){ |
|---|
| 74 | | var rslt; |
|---|
| 75 | | if(NewClass.prototype instanceof Array){ |
|---|
| 76 | | rslt=[]; |
|---|
| 77 | | }else{ |
|---|
| 78 | | rslt = function(){ |
|---|
| 79 | | return rslt.__call__.apply(rslt, arguments); |
|---|
| 80 | | }; |
|---|
| 81 | | } |
|---|
| 82 | | //transfer all properties defined in teh class to the new object(as it is empty) |
|---|
| 83 | | for(var n in NewClass.prototype){ |
|---|
| 84 | | rslt[n] = NewClass.prototype[n]; |
|---|
| 85 | | } |
|---|
| 86 | | //these props are not copied in the above loop |
|---|
| 87 | | rslt.constructor = NewClass; |
|---|
| 88 | | rslt.toString = NewClass.prototype.toString; |
|---|
| 89 | | |
|---|
| 90 | | rslt.__init__.apply(rslt,arguments); |
|---|
| | 132 | |
|---|
| | 133 | //allthough a single constructor would suffice for generating normal objects, Arrays and callables, |
|---|
| | 134 | //we use 3 different ones. This will minimize the code inside the constructor and therefore |
|---|
| | 135 | //minimize object construction time |
|---|
| | 136 | if(proto.__call__){ |
|---|
| | 137 | //if the callable interface is implemented we need a class constructor |
|---|
| | 138 | //which generates a function upon construction |
|---|
| | 139 | var NewClass = function(calledBy){ |
|---|
| | 140 | if(calledBy !== Class){ |
|---|
| | 141 | var rslt = function(){ |
|---|
| | 142 | return rslt.__call__.apply(rslt, arguments); |
|---|
| | 143 | }; |
|---|
| | 144 | var proto=arguments.callee.prototype; |
|---|
| | 145 | for(var n in proto){ |
|---|
| | 146 | rslt[n] = proto[n]; |
|---|
| | 147 | } |
|---|
| | 148 | rslt.constructor = arguments.callee; |
|---|
| | 149 | rslt.toString = proto.__str__; |
|---|
| | 150 | if(rslt.__init__){ |
|---|
| | 151 | rslt.__init__.apply(rslt, arguments); |
|---|
| | 152 | } |
|---|
| 92 | | }else{ |
|---|
| 93 | | //todo should a constructor be able to return something? |
|---|
| 94 | | this.__init__.apply(this, arguments); |
|---|
| 95 | | } |
|---|
| 96 | | } |
|---|
| 97 | | }; |
|---|
| 98 | | //This will create a new prototype object of the new class. |
|---|
| 99 | | NewClass.__createProto__ = function(){ |
|---|
| 100 | | return new NewClass(Class); |
|---|
| 101 | | }; |
|---|
| 102 | | //setting class properties for the new class. |
|---|
| 103 | | NewClass.__super__ = superClass; |
|---|
| 104 | | NewClass.__name__= name; |
|---|
| 105 | | NewClass.toString = function(){ |
|---|
| 106 | | return "[class %s]".format(NewClass.__name__); |
|---|
| 107 | | }; |
|---|
| 108 | | |
|---|
| 109 | | //see if the super class can create prototypes. (creating an object without calling __init__()) |
|---|
| 110 | | if(superClass.__createProto__!==undefined){ |
|---|
| 111 | | NewClass.prototype = superClass.__createProto__(); |
|---|
| 112 | | }else{//just create an object of the super class |
|---|
| 113 | | NewClass.prototype = new superClass(); |
|---|
| | 154 | } |
|---|
| | 155 | }; |
|---|
| | 156 | }else if(statc.__isArray__){ |
|---|
| | 157 | //Since we cannot inherit from Array directly we take the same approach as with the callable above |
|---|
| | 158 | //and just have a constructor which creates an Array |
|---|
| | 159 | var NewClass = function(calledBy){ |
|---|
| | 160 | if(calledBy !== Class){ |
|---|
| | 161 | rslt=[]; |
|---|
| | 162 | var proto=arguments.callee.prototype; |
|---|
| | 163 | for(var n in proto){ |
|---|
| | 164 | rslt[n] = proto[n]; |
|---|
| | 165 | } |
|---|
| | 166 | rslt.constructor = proto; |
|---|
| | 167 | rslt.toString = proto.__str__; |
|---|
| | 168 | if(rslt.__init__){ |
|---|
| | 169 | rslt.__init__.apply(rslt, arguments); |
|---|
| | 170 | }else{//implement Array's defaul behavior |
|---|
| | 171 | if(arguments.lengt==1){ |
|---|
| | 172 | rslt.length=arguments[0]; |
|---|
| | 173 | }else{ |
|---|
| | 174 | for(var i=0;i<arguments.length;i++){ |
|---|
| | 175 | rslt.push(arguments[i]); |
|---|
| | 176 | } |
|---|
| | 177 | } |
|---|
| | 178 | } |
|---|
| | 179 | return rslt; |
|---|
| | 180 | } |
|---|
| | 181 | }; |
|---|
| | 182 | }else{ |
|---|
| | 183 | //this is a 'normal' object constructor which does nothing but call the __init__ method |
|---|
| | 184 | //unless it does not exsit or the constructor was used for prototyping |
|---|
| | 185 | var NewClass = function(calledBy){ |
|---|
| | 186 | if(calledBy !== Class){ |
|---|
| | 187 | if(this.__init__){ |
|---|
| | 188 | this.__init__.apply(this, arguments); |
|---|
| | 189 | } |
|---|
| | 190 | } |
|---|
| | 191 | }; |
|---|
| 116 | | NewClass.prototype.constructor = NewClass; |
|---|
| 117 | | |
|---|
| 118 | | |
|---|
| 119 | | //make sure the new class has an __init__ method if it inherits from Object, Array or Function |
|---|
| 120 | | switch(superClass){ |
|---|
| 121 | | case Object: |
|---|
| 122 | | NewClass.prototype.__init__=function(){}; |
|---|
| 123 | | NewClass.prototype.toString = function(){ |
|---|
| 124 | | return "[object %s]".format(this.constructor.__name__); |
|---|
| 125 | | }; |
|---|
| 126 | | //todo |
|---|
| 127 | | NewClass.prototype.__hash__=function(){ |
|---|
| 128 | | if(this.__id__==null){ |
|---|
| 129 | | this.__id__ = '#auto#' + (Class.hashCount++); |
|---|
| 130 | | } |
|---|
| 131 | | return this.__id__; |
|---|
| 132 | | }; |
|---|
| 133 | | break; |
|---|
| 134 | | case Array: |
|---|
| 135 | | //immitate a call to new Array() |
|---|
| 136 | | NewClass.prototype.__init__=function(){ |
|---|
| 137 | | if (arguments.length==0){ |
|---|
| 138 | | }else if(arguments.lengt==1){ |
|---|
| 139 | | this.length=arguments[0]; |
|---|
| 140 | | }else{ |
|---|
| 141 | | for(var i=0;i<arguments.length;i++){ |
|---|
| 142 | | this.push(arguments[i]); |
|---|
| 143 | | } |
|---|
| 144 | | } |
|---|
| 145 | | }; |
|---|
| 146 | | break; |
|---|
| 147 | | case Function: |
|---|
| 148 | | //Subclasses of Function do not impl. Function's default behavior |
|---|
| 149 | | //as this would not make much sense, we allow subclassing of functions so people can create callable objects |
|---|
| 150 | | NewClass.prototype.__init__=function(){}; |
|---|
| 151 | | //needs to be overwritten by users |
|---|
| 152 | | NewClass.prototype.__call__=function(){}; |
|---|
| 153 | | NewClass.prototype.toString=function(){ |
|---|
| 154 | | return "[callable %s]".format(this.constructor.__name__); |
|---|
| 155 | | //todo: return Function.prototype.toString.call(this); |
|---|
| 156 | | }; |
|---|
| 157 | | break; |
|---|
| | 195 | proto.constructor = NewClass; |
|---|
| | 196 | proto.__class__= NewClass;//no, it is not needed, just like __str__ is not, but it is nicer than constructor |
|---|
| | 197 | |
|---|
| | 198 | //this is where the inheritance realy happens |
|---|
| | 199 | NewClass.prototype = proto; |
|---|
| | 200 | |
|---|
| | 201 | //apply all the static fileds |
|---|
| | 202 | for(var key in statc){ |
|---|
| | 203 | NewClass[key] = statc[key]; |
|---|
| 159 | | |
|---|
| 160 | | //mixin classes overwrite props/methods of the new class |
|---|
| 161 | | for(var i=0;i<mixinClasses.length;i++){ |
|---|
| 162 | | var mixin = mixinClasses[i].prototype; |
|---|
| 163 | | for(var n in mixin){ |
|---|
| 164 | | if(n != "__init__"){ |
|---|
| 165 | | NewClass.prototype[n] = mixin[n]; |
|---|
| 166 | | } |
|---|
| 167 | | } |
|---|
| 168 | | } |
|---|
| 169 | | |
|---|
| 170 | | //execute the scope of the class |
|---|
| 171 | | switch(classScope.length){ |
|---|
| 172 | | case 3: //publ, statc, supr |
|---|
| 173 | | classScope(NewClass.prototype, NewClass, superClass.prototype); |
|---|
| 174 | | break; |
|---|
| 175 | | default://publ, supr |
|---|
| 176 | | classScope(NewClass.prototype, superClass.prototype); |
|---|
| 177 | | break; |
|---|
| 178 | | } |
|---|
| | 205 | NewClass.toString=statc.__str__; |
|---|