1 //<Script type="text/javascript">
6 * // FIXME - I need this to do next() without doccomments..
9 const Identifier = imports.Identifier.Identifier
10 const XObject = imports.XObject.XObject;
14 function Scope(braceN, parent, startTokN, lastIdent, token)
16 if (lastIdent.length) {
17 // println("NEW SCOPE: " + lastIdent);
23 this.identifiers = { };
26 this.ident = lastIdent;
27 this.gid = Scope.gid++;
29 //print("ADD SCOPE(" + this.gid + ") TO "+ (parent ? this.parent.gid : 'TOP') + ' : ' +
30 // (token ? token.toString() : ''));
33 this.parent.subScopes.push(this);
50 identifiers : false, // map of identifiers to {Identifier} objects
56 protectedVars : {}, // only used by to parent..
59 * dump the scope to StdOut...
62 dump : function(indent)
64 indent = indent || '';
66 for (var k in this.identifiers) {
67 str += str.length ? ", " : "";
68 str += k + '=>' + this.identifiers[k].mungedValue;
72 indent + "Scope: " + this.id + "\n" +
73 indent + "Started: " + ( this.token ? this.token.line : 'TOP' ) + "\n" +
74 indent + "- " + str + "\n"
76 this.subScopes.forEach(function(s) {
84 declareIdentifier : function(symbol, token)
87 //print("SCOPE : " + this.gid + " :SYM: " + symbol + " " + token.toString()+"");
89 if (typeof(this.identifiers[symbol])== 'undefined') {
91 this.identifiers[symbol] = new Identifier(symbol, this);
94 if (typeof(token) != 'undefined') { // shoudl this happen?
95 token.identifier = this.identifiers[symbol];
98 if (this.braceN < 0) {
99 // then it's global...
100 this.identifiers[symbol].toMunge = false;
104 this.addToParentScope(symbol);
105 return this.identifiers[symbol];
107 getIdentifier : function(symbol, token) {
108 if (typeof(this.identifiers[symbol])== 'undefined') {
109 if (['String', 'Date'].indexOf(symbol)> -1) {
113 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
116 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
117 return this.identifiers[symbol];
120 addHint : function(varName, varType) {
121 this.hint[varName] = varType;
123 preventMunging : function() {
127 usedsymcache : false,
129 getUsedSymbols : function() {
132 // if (this.usedsymcache !== false) {
133 // return this.usedsymcache;
136 var idents = this.identifiers;
137 for(var i in idents) {
138 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
139 var identifier = this.identifiers[i];
140 var mungedValue = identifier.mungedValue
141 if (!mungedValue.length) {
142 //println(identifier.toSource());
143 mungedValue = identifier.name;
145 result.push(mungedValue);
147 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
148 //this.usedsymcache = result;
152 getAllUsedSymbols :function() {
153 var result = this.getUsedSymbols();
154 var scope = this.parent;
155 while (scope !== false) {
156 //println("addused:"+scope.id);
157 result = result.concat(scope.getUsedSymbols());
158 scope = scope.parent;
160 //println("Done - addused");
163 /** - we need to register short vairalbes so they never get munged into.. */
164 addToParentScope: function(ident)
166 if (ident.length > 2) {
169 var scope = this.parent;
170 while (scope !== false) {
171 //println("addused:"+scope.id);
173 scope.protectedVars[ident] = true;
175 scope = scope.parent;
179 isProtectedVar: function(ident)
181 if (ident.length > 2) {
184 var scope = this.parent;
185 while (scope !== false) {
186 //println("addused:"+scope.id);
188 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
190 scope = scope.parent;
196 * set's all the munged values on the identifiers.
205 // Stop right here if this scope was flagged as unsafe for munging.
206 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
218 // Do not munge symbols in the global scope!
222 for (var ii in this.identifiers) {
225 //print("MUNGE: " + all.join(', '));
227 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
229 var freeSymbols = [];
230 var sy = this.getAllUsedSymbols();
232 var addSyms=function(batch)
234 for(var i =0;i<batch.length;i++) {
235 if (sy.indexOf(batch[i]) > -1) {
238 freeSymbols.push(batch[i]);
245 //println(freeSymbols.toSource());
247 //println("MUNGE: Replacing " + this.id+"</BR>");
248 for (var i in this.identifiers) {
250 // is the identifer in the global scope!?!!?
253 if (!this.identifiers[i].toMunge) {
254 //print("SKIP toMunge==false : " + i)
258 if (this.isProtectedVar(i)) {
259 //print("SKIP PROTECTED: " + i)
265 //if (this.identifiers[i].constructor != Identifier) {
266 // print("SKIP NOT IDENTIFIER : " + i)
269 // println("IDENT:" +i+'</BR>');
271 if (!repsym.length) {
272 if (!freeSymbols.length) {
275 repsym = freeSymbols.shift(); // pop off beginngin???
278 var identifier = this.identifiers[i];
279 //println(typeof(identifier.name));
280 var mungedValue = identifier.name;
282 //println([ repsym,mungedValue ]);
284 if (this.mungeM && repsym.length < mungedValue.length) {
285 //print("REPLACE:"+ mungedValue +" with " + repsym );
286 mungedValue = repsym;
290 identifier.mungedValue = mungedValue;
292 //println("MUNGE: Done " + this.id+"</BR>");
295 //println("Doing sub scopes");
296 for (var j = 0; j < this.subScopes.length; j++) {
297 var ss = this.subScopes[j];
309 XObject.extend(Scope, {
311 builtin : ["NaN","top"],
312 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
319 if (File.exists('/tmp/var_list_ones.js')) {
320 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
321 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
322 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
325 this.ones = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'.split(',');
327 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
328 for(var i = 0; i < a.length; i++) {
329 for(var j = 0; j < n.length; j++) {
330 var tw = a[i] + n[j];
331 if (this.skips.indexOf(tw) < 0) {
336 for(var k = 0; k < n.length; k++) {
337 var thr = a[i] + n[j] + n[k];
338 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
339 if (this.skips.indexOf(thr) < 0) {
341 this.threes.push(thr);
348 //println("done creating var list");
349 //println("threes="+ this.threes.toSource());
355 // init the scope constants..