UglifyJS/lib/browser-cola.js

7 lines
218 KiB
JavaScript
Raw Normal View History

2014-07-28 18:51:23 +00:00
!this.Cola&&(this.Cola={});Cola.getSource=function(url){var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.send();return xhr.responseText};Cola.dirname=function(dir){return dir.replace(/\/[^\/]*$/,"")};Cola.notRoot=function(path){return path[0]!="/"&&!/^http/.test(path)};Cola.translate=function(source,opts){var stream=new Cola.OutputStream({beautify:true}),ast;try{ast=Cola.parse(source).toJavaScript(opts);ast.print(stream);return stream.toString()}catch(e){throw e}};Cola.eval=function(source,opts){return eval.call(this,Cola.translate(source,opts))};Cola.bootstraped=false;Cola.bootstrap=function(){if(Cola.bootstraped)return;Cola.bootstraped=true;Array.prototype.forEach.call(document.querySelectorAll('script[type="text/colascript"][src]'),function(script){if(/\.js$/.test(script.src))eval.call(window,Cola.getSource(script.src));else Cola.eval.call(window,Cola.getSource(script.src),{path:Cola.dirname(script.src)})});var event=document.createEvent("HTMLEvents");event.initEvent("DOMContentLoaded",true,true);event.eventName="DOMContentLoaded";window.dispatchEvent(event);event=document.createEvent("HTMLEvents");event.initEvent("load",true,true);event.eventName="load";window.dispatchEvent(event)};"use strict";Cola.array_to_hash=function(a){var ret=Object.create(null);for(var i=0;i<a.length;++i)ret[a[i]]=true;return ret};Cola.slice=function(a,start){return Array.prototype.slice.call(a,start||0)};Cola.characters=function(str){return str.split("")};Cola.member=function(name,array){for(var i=array.length;--i>=0;)if(array[i]==name)return true;return false};Cola.find_if=function(func,array){for(var i=0,n=array.length;i<n;++i){if(func(array[i]))return array[i]}};Cola.repeat_string=function(str,i){if(i<=0)return"";if(i==1)return str;var d=Cola.repeat_string(str,i>>1);d+=d;if(i&1)d+=str;return d};Cola.DefaultsError=function(msg,defs){Error.call(this,msg);this.msg=msg;this.defs=defs};Cola.DefaultsError.prototype=Object.create(Error.prototype);Cola.DefaultsError.prototype.constructor=Cola.DefaultsError;Cola.DefaultsError.croak=function(msg,defs){throw new Cola.DefaultsError(msg,defs)};Cola.defaults=function(args,defs,croak){if(args===true)args={};var ret=args||{};if(croak)for(var i in ret)if(ret.hasOwnProperty(i)&&!defs.hasOwnProperty(i))Cola.DefaultsError.croak("`"+i+"` is not a supported option",defs);for(var i in defs)if(defs.hasOwnProperty(i)){ret[i]=args&&args.hasOwnProperty(i)?args[i]:defs[i]}return ret};Cola.merge=function(obj,ext){for(var i in ext)if(ext.hasOwnProperty(i)){obj[i]=ext[i]}return obj};Cola.noop=function(){};Cola.MAP=function(){function MAP(a,f,backwards){var ret=[],top=[],i;function doit(){var val=f(a[i],i);var is_last=val instanceof Last;if(is_last)val=val.v;if(val instanceof Continue)return false;if(val instanceof AtTop){val=val.v;if(val instanceof Splice){top.push.apply(top,backwards?val.v.slice().reverse():val.v)}else{top.push(val)}}else if(val!==skip){if(val instanceof Splice){ret.push.apply(ret,backwards?val.v.slice().reverse():val.v)}else{ret.push(val)}}return is_last}if(a instanceof Array){if(backwards){for(i=a.length;--i>=0;)if(doit())break;ret.reverse();top.reverse()}else{for(i=0;i<a.length;++i)if(doit())break}}else{for(i in a)if(a.hasOwnProperty(i))if(doit())break}return top.concat(ret)}MAP.at_top=function(val){return new AtTop(val)};MAP.splice=function(val){return new Splice(val)};MAP.last=function(val){return new Last(val)};MAP.continue=function(){return new Continue};var skip=MAP.skip={};function AtTop(val){this.v=val}function Splice(val){this.v=val}function Last(val){this.v=val}function Continue(){}return MAP}();Cola.push_uniq=function(array,el){if(array.indexOf(el)<0)array.push(el)};Cola.string_template=function(text,props){return text.replace(/\{(.+?)\}/g,function(str,p){return props[p]})};Cola.remove=function(array,el){for(var i=array.length;--i>=0;){if(array[i]===el)array.splice(i,1)}};Cola.mergeSort=function(array,cmp){if(array.length<2)return array.slice();function merge(a,b){var r=[],ai=0,bi=0,i=0;while(ai<a.length&&bi<b.length){cmp(a[ai],b[bi])<=0?r[i++]=a[ai++]:r[i++]=b[bi++]}if(ai<a.leng
Cola.AST_Boolean=Cola.DEFNODE("Boolean",null,{$documentation:"Base class for booleans"},Cola.AST_Atom);Cola.AST_False=Cola.DEFNODE("False",null,{$documentation:"The `false` atom",value:false},Cola.AST_Boolean);Cola.AST_True=Cola.DEFNODE("True",null,{$documentation:"The `true` atom",value:true},Cola.AST_Boolean);Cola.TreeWalker=function(callback){this.visit=callback;this.stack=[]};Cola.TreeWalker.prototype={_visit:function(node,descend){this.stack.push(node);var ret=this.visit(node,descend?function(){descend.call(node)}:Cola.noop);if(!ret&&descend){descend.call(node)}this.stack.pop();return ret},parent:function(n){return this.stack[this.stack.length-2-(n||0)]},push:function(node){this.stack.push(node)},pop:function(){return this.stack.pop()},self:function(){return this.stack[this.stack.length-1]},find_parent:function(type){var stack=this.stack;for(var i=stack.length;--i>=0;){var x=stack[i];if(x instanceof type)return x}},has_directive:function(type){return this.find_parent(Cola.AST_Scope).has_directive(type)},in_boolean_context:function(){var stack=this.stack;var i=stack.length,self=stack[--i];while(i>0){var p=stack[--i];if(p instanceof Cola.AST_If&&p.condition===self||p instanceof Cola.AST_Conditional&&p.condition===self||p instanceof Cola.AST_DWLoop&&p.condition===self||p instanceof Cola.AST_For&&p.condition===self||p instanceof Cola.AST_UnaryPrefix&&p.operator=="!"&&p.expression===self){return true}if(!(p instanceof Cola.AST_Binary&&(p.operator=="&&"||p.operator=="||")))return false;self=p}},loopcontrol_target:function(label){var stack=this.stack;if(label)for(var i=stack.length;--i>=0;){var x=stack[i];if(x instanceof Cola.AST_LabeledStatement&&x.label.name==label.name){return x.body}}else for(var i=stack.length;--i>=0;){var x=stack[i];if(x instanceof Cola.AST_Switch||x instanceof Cola.AST_IterationStatement)return x}}};"use strict";!this.Cola&&(this.Cola={});Cola.KEYWORDS="break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var void while with";Cola.cKEYWORDS=Cola.KEYWORDS.replace(" void","")+" when clone isset is isnt class singleton injector";Cola.KEYWORDS_ATOM="false null true";Cola.cKEYWORDS_ATOM=Cola.KEYWORDS_ATOM+" on yes off no";Cola.RESERVED_WORDS="abstract boolean byte char double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield";Cola.cRESERVED_WORDS=Cola.RESERVED_WORDS.replace(" class","")+" "+Cola.cKEYWORDS_ATOM+" "+Cola.cKEYWORDS;Cola.RESERVED_WORDS+=" "+Cola.KEYWORDS_ATOM+" "+Cola.KEYWORDS;Cola.KEYWORDS_BEFORE_EXPRESSION="return new delete throw else case";Cola.cKEYWORDS_BEFORE_EXPRESSION=Cola.KEYWORDS_BEFORE_EXPRESSION+=" when";Cola.KEYWORDS=Cola.makePredicate(Cola.KEYWORDS);Cola.cKEYWORDS=Cola.makePredicate(Cola.cKEYWORDS);Cola.RESERVED_WORDS=Cola.makePredicate(Cola.RESERVED_WORDS);Cola.cRESERVED_WORDS=Cola.makePredicate(Cola.cRESERVED_WORDS);Cola.KEYWORDS_ATOM=Cola.makePredicate(Cola.KEYWORDS_ATOM);Cola.cKEYWORDS_ATOM=Cola.makePredicate(Cola.cKEYWORDS_ATOM);Cola.KEYWORDS_BEFORE_EXPRESSION=Cola.makePredicate(Cola.KEYWORDS_BEFORE_EXPRESSION);Cola.cKEYWORDS_BEFORE_EXPRESSION=Cola.makePredicate(Cola.cKEYWORDS_BEFORE_EXPRESSION);Cola.OPERATOR_CHARS=Cola.makePredicate(Cola.characters("+-*&%=<>!?|~^"));Cola.RE_HEX_NUMBER=/^0x[0-9a-f]+$/i;Cola.RE_OCT_NUMBER=/^0[0-7]+$/;Cola.RE_DEC_NUMBER=/^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i;Cola.OPERATORS=["in","instanceof","typeof","new","delete","++","--","+","-","!","~","&","|","^","*","/","%",">>","<<",">>>","<",">","<=",">=","==","===","!=","!==","?","=","+=","-=","/=","*=","%=",">>=","<<=",">>>=","|=","^=","&=","&&","||","clone","isset","is","isnt","**","%%","?="];Cola.cOPERATORS=Cola.makePredicate(Cola.OPERATORS);Cola.OPERATORS=Cola.OPERATORS.slice(0,Cola.OPERATORS.length-7);Cola.OPERATORS.push("void");Cola.OPERATORS=Cola.makePredicate(Cola.OPERATORS);Cola.COMPARISON=Cola.makePredicate("< > <= >= == === != !==");Cola.WHITESPACE_CHARS=Cola.makePr
};Cola.Parser.prototype.parenthesised=function(){this.expect("(");var exp=this.expression(true);this.expect(")");return exp};Cola.Parser.embed_tokens=function(parser){return function(){var start=this.S.token;var expr=parser.apply(this,arguments);var end=this.prev();expr.start=start;expr.end=end;return expr}};Cola.Parser.prototype.handle_regexp=function(){if(this.is("operator","/")||this.is("operator","/=")){this.S.peeked=null;this.S.token=this.S.input(this.S.token.value.substr(1))}};Cola.Parser.prototype.statement=Cola.Parser.embed_tokens(function(){var tmp,type,_this=this;this.handle_regexp();switch(this.S.token.type){case"string":var dir=this.S.in_directives,stat=this.simple_statement();if(dir&&stat.body instanceof Cola.AST_String&&!this.is("punc",","))return new Cola.AST_Directive({value:stat.body.value});return stat;case"num":case"regexp":case"operator":case"atom":return this.simple_statement();case"name":if(!this.is_js&&this.next_is("name")){type=this.S.token.value,this.next();if(this.next_is("punc","("))return this.function_(Cola.AST_Defun,type);return tmp=this.var_(false,type),this.semicolon(),tmp}if(!this.is_js){var _this=this,balance=1,isfun=false;this.dumpS();this.next();if(this.is("punc","(")){this.next();this.next_until(function(){if(_this.is("punc","("))balance++;else if(_this.is("punc",")"))balance--;return balance==0||_this.is("eof")});isfun=(this.next(),this.is("punc","{")||this.is("punc","=>"))}this.restoreS();if(isfun)return this.function_(Cola.AST_Defun)}return this.next_is("punc",":")?this.labeled_statement():this.simple_statement();case"punc":switch(this.S.token.value){case"@":if(!this.is_js&&this.next_is("name"))return new Cola.AST_Command({name:this.next().value,args:function(name){var args=[];if(name=="require"||name=="include"){while(!this.peek().nlb&&!this.next_is("eof")){this.next();if(!this.is("string"))this.unexpected();args.push(this.S.token.value)}this.next()}else if(name=="use"){this.next();if(!this.is("name"))this.unexpected();args.push(this.S.token.value);if(this.S.token.value=="closure"){this.next();args.push(this.is("punc","{"),new Cola.AST_BlockStatement({start:this.S.token,body:this.block_(!this.is("punc","{")),end:this.prev()}))}else if(this.S.token.value!="asm"&&this.S.token.value!="strict")this.unexpected()}else this.unexpected();return args}.call(this,this.S.token.value)});case"{":if(this.is_js)return new Cola.AST_BlockStatement({start:this.S.token,body:this.block_(),end:this.prev()});this.dumpS();var balance=0,is_object=false;this.next_until(function(){if(_this.is("punc","{"))balance++;else if(_this.is("punc","}"))balance--;return balance==0||_this.is("eof")});is_object=this.next_is("operator");this.restoreS();if(is_object)return this.simple_statement();return new Cola.AST_BlockStatement({start:this.S.token,body:this.block_(),end:this.prev()});case"[":case"(":return this.simple_statement();case";":this.next();return new Cola.AST_EmptyStatement;default:this.unexpected()}case"keyword":switch(tmp=this.S.token.value,this.next(),tmp){case"break":return this.break_cont(Cola.AST_Break);case"continue":return this.break_cont(Cola.AST_Continue);case"debugger":this.semicolon();return new Cola.AST_Debugger;case"do":return new Cola.AST_Do({body:this.in_loop(function(){return this.statement()}),condition:(this.expect_token("keyword","while"),tmp=this.parenthesised(),this.semicolon(),tmp)});case"while":return new Cola.AST_While({condition:this.parenthesised(),body:this.in_loop(function(){return this.statement()})});case"for":return this.for_();case"function":return this.function_(Cola.AST_Defun);case"if":return this.if_();case"return":if(this.S.in_function==0)this.croak("'return' outside of function");return new Cola.AST_Return({value:this.is("punc",";")?(this.next(),null):this.can_insert_semicolon()?null:(tmp=this.expression(true),this.semicolon(),tmp)});case"switch":return new Cola.AST_Switch({expression:this.is("punc","{")&&!this.is_js?new Cola.AST_Noop:this.parenthesised(),body:this.in_loop(this.switch_body_)});case"throw":if(this.S.token.nlb)this.croak("Illegal newline after 'throw'");re
2014-07-28 18:51:23 +00:00
this.mangled_name=null;this.undeclared=false;this.constant=false;this.index=index};Cola.SymbolDef.prototype={unmangleable:function(options){return this.global&&!(options&&options.toplevel)||this.undeclared||!(options&&options.eval)&&(this.scope.uses_eval||this.scope.uses_with)},mangle:function(options){if(!this.mangled_name&&!this.unmangleable(options)){var s=this.scope;if(!options.screw_ie8&&this.orig[0]instanceof Cola.AST_SymbolLambda)s=s.parent_scope;this.mangled_name=s.next_mangled(options,this)}}};Cola.AST_Toplevel.DEFMETHOD("figure_out_scope",function(options){options=Cola.defaults(options,{screw_ie8:false});var self=this;var scope=self.parent_scope=null;var defun=null;var nesting=0;var tw=new Cola.TreeWalker(function(node,descend){if(options.screw_ie8&&node instanceof Cola.AST_Catch){var save_scope=scope;scope=new Cola.AST_Scope(node);scope.init_scope_vars(nesting);scope.parent_scope=save_scope;descend();scope=save_scope;return true}if(node instanceof Cola.AST_Scope){node.init_scope_vars(nesting);var save_scope=node.parent_scope=scope;var save_defun=defun;defun=scope=node;++nesting;descend();--nesting;scope=save_scope;defun=save_defun;return true}if(node instanceof Cola.AST_Directive){node.scope=scope;Cola.push_uniq(scope.directives,node.value);return true}if(node instanceof Cola.AST_With){for(var s=scope;s;s=s.parent_scope)s.uses_with=true;return}if(node instanceof Cola.AST_Symbol){node.scope=scope}if(node instanceof Cola.AST_SymbolLambda){defun.def_function(node)}else if(node instanceof Cola.AST_SymbolDefun){(node.scope=defun.parent_scope).def_function(node)}else if(node instanceof Cola.AST_SymbolVar||node instanceof Cola.AST_SymbolConst){var def=defun.def_variable(node);def.constant=node instanceof Cola.AST_SymbolConst;def.init=tw.parent().value}else if(node instanceof Cola.AST_SymbolCatch){(options.screw_ie8?scope:defun).def_variable(node)}});self.walk(tw);var func=null;var globals=self.globals=new Cola.Dictionary;var tw=new Cola.TreeWalker(function(node,descend){if(node instanceof Cola.AST_Lambda){var prev_func=func;func=node;descend();func=prev_func;return true}if(node instanceof Cola.AST_SymbolRef){var name=node.name;var sym=node.scope.find_variable(name);if(!sym){var g;if(globals.has(name)){g=globals.get(name)}else{g=new Cola.SymbolDef(self,globals.size(),node);g.undeclared=true;g.global=true;globals.set(name,g)}node.thedef=g;if(name=="eval"&&tw.parent()instanceof Cola.AST_Call){for(var s=node.scope;s&&!s.uses_eval;s=s.parent_scope)s.uses_eval=true}if(func&&name=="arguments"){func.uses_arguments=true}}else{node.thedef=sym}node.reference();return true}});self.walk(tw)});Cola.AST_Scope.DEFMETHOD("init_scope_vars",function(nesting){this.directives=[];this.variables=new Cola.Dictionary;this.functions=new Cola.Dictionary;this.uses_with=false;this.uses_eval=false;this.parent_scope=null;this.enclosed=[];this.cname=-1;this.nesting=nesting});Cola.AST_Scope.DEFMETHOD("strict",function(){return this.has_directive("use strict")});Cola.AST_Lambda.DEFMETHOD("init_scope_vars",function(){Cola.AST_Scope.prototype.init_scope_vars.apply(this,arguments);this.uses_arguments=false});Cola.AST_SymbolRef.DEFMETHOD("reference",function(){var def=this.definition();def.references.push(this);var s=this.scope;while(s){Cola.push_uniq(s.enclosed,def);if(s===def.scope)break;s=s.parent_scope}this.frame=this.scope.nesting-def.scope.nesting});Cola.AST_Scope.DEFMETHOD("find_variable",function(name){if(name instanceof Cola.AST_Symbol)name=name.name;return this.variables.get(name)||this.parent_scope&&this.parent_scope.find_variable(name)});Cola.AST_Scope.DEFMETHOD("has_directive",function(value){return this.parent_scope&&this.parent_scope.has_directive(value)||(this.directives.indexOf(value)>=0?this:null)});Cola.AST_Scope.DEFMETHOD("def_function",function(symbol){this.functions.set(symbol.name,this.def_variable(symbol))});Cola.AST_Scope.DEFMETHOD("def_variable",function(symbol){var def;if(!this.variables.has(symbol.name)){def=new Cola.SymbolDef(this,this.variables.size(),symbol);this.variables.set(symbol.name,def);def.global=!this.parent_scope
output.newline()}output.indent();prop.print(output)});output.newline()});else output.print("{}")});DEFPRINT(Cola.AST_ObjectKeyVal,function(self,output){var key=self.key;if(output.option("quote_keys")){output.print_string(key+"")}else if((typeof key=="number"||!output.option("beautify")&&+key+""==key)&&parseFloat(key)>=0){output.print(make_num(key))}else if(Cola.RESERVED_WORDS(key)?output.option("screw_ie8"):Cola.is_identifier_string(key)){output.print_name(key)}else{output.print_string(key)}output.colon();self.value.print(output)});DEFPRINT(Cola.AST_ObjectSetter,function(self,output){output.print("set");output.space();self.key.print(output);self.value._do_print(output,true)});DEFPRINT(Cola.AST_ObjectGetter,function(self,output){output.print("get");output.space();self.key.print(output);self.value._do_print(output,true)});DEFPRINT(Cola.AST_Symbol,function(self,output){var def=self.definition();output.print_name(def?def.mangled_name||def.name:self.name)});DEFPRINT(Cola.AST_Undefined,function(self,output){output.print("void 0")});DEFPRINT(Cola.AST_Hole,Cola.noop);DEFPRINT(Cola.AST_Infinity,function(self,output){output.print("1/0")});DEFPRINT(Cola.AST_NaN,function(self,output){output.print("0/0")});DEFPRINT(Cola.AST_This,function(self,output){output.print("this")});DEFPRINT(Cola.AST_Constant,function(self,output){output.print(self.getValue())});DEFPRINT(Cola.AST_String,function(self,output){output.print_string(self.getValue())});DEFPRINT(Cola.AST_Number,function(self,output){output.print(make_num(self.getValue()))});function regexp_safe_literal(code){return[92,47,46,43,42,63,40,41,91,93,123,125,36,94,58,124,33,10,13,0,65279,8232,8233].indexOf(code)<0}DEFPRINT(Cola.AST_RegExp,function(self,output){var str=self.getValue().toString();if(output.option("ascii_only")){str=output.to_ascii(str)}else if(output.option("unescape_regexps")){str=str.split("\\\\").map(function(str){return str.replace(/\\u[0-9a-fA-F]{4}|\\x[0-9a-fA-F]{2}/g,function(s){var code=parseInt(s.substr(2),16);return regexp_safe_literal(code)?String.fromCharCode(code):s})}).join("\\\\")}output.print(str);var p=output.parent();if(p instanceof Cola.AST_Binary&&/^in/.test(p.operator)&&p.left===self)output.print(" ")});function force_statement(stat,output){if(output.option("bracketize")){if(!stat||stat instanceof Cola.AST_EmptyStatement)output.print("{}");else if(stat instanceof Cola.AST_BlockStatement)stat.print(output);else output.with_block(function(){output.indent();stat.print(output);output.newline()})}else{if(!stat||stat instanceof Cola.AST_EmptyStatement)output.force_semicolon();else stat.print(output)}}function first_in_statement(output){var a=output.stack,i=a.length,node=a[--i],p=a[--i];while(i>0){if(p instanceof Cola.AST_Statement&&p.body===node)return true;if(p instanceof Cola.AST_Seq&&p.car===node||p instanceof Cola.AST_Call&&p.expression===node&&!(p instanceof Cola.AST_New)||p instanceof Cola.AST_Dot&&p.expression===node||p instanceof Cola.AST_Sub&&p.expression===node||p instanceof Cola.AST_Conditional&&p.condition===node||p instanceof Cola.AST_Binary&&p.left===node||p instanceof Cola.AST_UnaryPostfix&&p.expression===node){node=p;p=a[--i]}else{return false}}}function no_constructor_parens(self,output){return self.args.length==0&&!output.option("beautify")}function best_of(a){var best=a[0],len=best.length;for(var i=1;i<a.length;++i){if(a[i].length<len){best=a[i];len=best.length}}return best}function make_num(num){var str=num.toString(10),a=[str.replace(/^0\./,".").replace("e+","e")],m;if(Math.floor(num)===num){if(num>=0){a.push("0x"+num.toString(16).toLowerCase(),"0"+num.toString(8))}else{a.push("-0x"+(-num).toString(16).toLowerCase(),"-0"+(-num).toString(8))}if(m=/^(.*?)(0+)$/.exec(num)){a.push(m[1]+"e"+m[2].length)}}else if(m=/^0?\.(0+)(.*)$/.exec(num)){a.push(m[2]+"e-"+(m[1].length+m[2].length),str.substr(str.indexOf(".")))}return best_of(a)}function make_block(stmt,output){if(stmt instanceof Cola.AST_BlockStatement){stmt.print(output);return}output.with_block(function(){output.indent();stmt.print(output);output.newline()})}function DEFMAP(nodetype,gener
return true}}));hoist_vars=hoist_vars&&var_decl>1;var tt=new Cola.TreeTransformer(function before(node){if(node!==self){if(node instanceof Cola.AST_Directive){dirs.push(node);return make_node(Cola.AST_EmptyStatement,node)}if(node instanceof Cola.AST_Defun&&hoist_funs){hoisted.push(node);return make_node(Cola.AST_EmptyStatement,node)}if(node instanceof Cola.AST_Var&&hoist_vars){node.definitions.forEach(function(def){vars.set(def.name.name,def);++vars_found});var seq=node.to_assignments();var p=tt.parent();if(p instanceof Cola.AST_ForIn&&p.init===node){if(seq==null)return node.definitions[0].name;return seq}if(p instanceof Cola.AST_For&&p.init===node){return seq}if(!seq)return make_node(Cola.AST_EmptyStatement,node);return make_node(Cola.AST_SimpleStatement,node,{body:seq})}if(node instanceof Cola.AST_Scope)return node}});self=self.transform(tt);if(vars_found>0){var defs=[];vars.each(function(def,name){if(self instanceof Cola.AST_Lambda&&Cola.find_if(function(x){return x.name==def.name.name},self.argnames)){vars.del(name)}else{def=def.clone();def.value=null;defs.push(def);vars.set(name,def)}});if(defs.length>0){for(var i=0;i<self.body.length;){if(self.body[i]instanceof Cola.AST_SimpleStatement){var expr=self.body[i].body,sym,assign;if(expr instanceof Cola.AST_Assign&&expr.operator=="="&&(sym=expr.left)instanceof Cola.AST_Symbol&&vars.has(sym.name)){var def=vars.get(sym.name);if(def.value)break;def.value=expr.right;Cola.remove(defs,def);defs.push(def);self.body.splice(i,1);continue}if(expr instanceof Cola.AST_Seq&&(assign=expr.car)instanceof Cola.AST_Assign&&assign.operator=="="&&(sym=assign.left)instanceof Cola.AST_Symbol&&vars.has(sym.name)){var def=vars.get(sym.name);if(def.value)break;def.value=assign.right;Cola.remove(defs,def);defs.push(def);self.body[i].body=expr.cdr;continue}}if(self.body[i]instanceof Cola.AST_EmptyStatement){self.body.splice(i,1);continue}if(self.body[i]instanceof Cola.AST_BlockStatement){var tmp=[i,1].concat(self.body[i].body);self.body.splice.apply(self.body,tmp);continue}break}defs=make_node(Cola.AST_Var,self,{definitions:defs});hoisted.push(defs)}}self.body=dirs.concat(hoisted,self.body)}return self});OPT(Cola.AST_SimpleStatement,function(self,compressor){if(compressor.option("side_effects")){if(!self.body.has_side_effects(compressor)){compressor.warn("Dropping side-effect-free statement [{file}:{line},{col}]",self.start);return make_node(Cola.AST_EmptyStatement,self)}}return self});OPT(Cola.AST_DWLoop,function(self,compressor){var cond=self.condition.evaluate(compressor);self.condition=cond[0];if(!compressor.option("loops"))return self;if(cond.length>1){if(cond[1]){return make_node(Cola.AST_For,self,{body:self.body})}else if(self instanceof Cola.AST_While){if(compressor.option("dead_code")){var a=[];extract_declarations_from_unreachable_code(compressor,self.body,a);return make_node(Cola.AST_BlockStatement,self,{body:a})}}}return self});function if_break_in_loop(self,compressor){function drop_it(rest){rest=as_statement_array(rest);if(self.body instanceof Cola.AST_BlockStatement){self.body=self.body.clone();self.body.body=rest.concat(self.body.body.slice(1));self.body=self.body.transform(compressor)}else{self.body=make_node(Cola.AST_BlockStatement,self.body,{body:rest}).transform(compressor)}if_break_in_loop(self,compressor)}var first=self.body instanceof Cola.AST_BlockStatement?self.body.body[0]:self.body;if(first instanceof Cola.AST_If){if(first.body instanceof Cola.AST_Break&&compressor.loopcontrol_target(first.body.label)===self){if(self.condition){self.condition=make_node(Cola.AST_Binary,self.condition,{left:self.condition,operator:"&&",right:first.condition.negate(compressor)})}else{self.condition=first.condition.negate(compressor)}drop_it(first.alternative)}else if(first.alternative instanceof Cola.AST_Break&&compressor.loopcontrol_target(first.alternative.label)===self){if(self.condition){self.condition=make_node(Cola.AST_Binary,self.condition,{left:self.condition,operator:"&&",right:first.condition})}else{self.condition=first.condition}drop_it(first.body)}}}OPT(Cola.AST_While,function(
};Cola.Constructions.SplatedConditional=function(name,uid,pos,after,length){if(Cola._ColaRuntime$is(name,String))name=new Cola.AST_SymbolRef({name:name});return new Cola.AST_Conditional({condition:new Cola.AST_Binary({operator:"<=",left:new Cola.AST_Number({value:length}),right:new Cola.AST_Dot({expression:name,property:"length"})}),consequent:new Cola.AST_Call({expression:new Cola.AST_Dot({property:"call",expression:new Cola.AST_Dot({property:"slice",expression:new Cola.AST_Array({elements:[]})})}),args:[name,new Cola.AST_Number({value:pos}),new Cola.AST_Assign({operator:"=",left:new Cola.AST_SymbolRef({name:"$_cola"+uid+"i"}),right:new Cola.AST_Binary({operator:"-",left:new Cola.AST_Dot({property:"length",expression:name}),right:new Cola.AST_Number({value:after})})})]}),alternative:new Cola.AST_Seq({car:new Cola.AST_Assign({operator:"=",left:new Cola.AST_SymbolRef({name:"$_cola"+uid+"i"}),right:new Cola.AST_Number({value:pos})}),cdr:new Cola.AST_Array({elements:[]})})})};Cola.Constructions.ValueWithOffset=function(name,uid,cond,pos,after){if(Cola._ColaRuntime$is(name,String))name=new Cola.AST_SymbolRef({name:name});return new Cola.AST_Sub({expression:name,property:cond?new Cola.AST_Number({value:pos}):new Cola.AST_Binary({operator:"+",left:new Cola.AST_SymbolRef({name:"$_cola"+uid+"i"}),right:new Cola.AST_Number({value:after})})})};Cola.Constructions.ValueWithKey=function(cond,name,key){return cond?new Cola.AST_Dot({expression:name,property:key}):new Cola.AST_Sub({expression:name,property:new Cola.AST_String({value:key})})};Cola.Constructions.SplatedVarDef=function(name,pos,after,length){if(Cola._ColaRuntime$is(name,String))name={name:name};return new Cola.AST_VarDef({type:"Array",name:new Cola.AST_SymbolVar(name),value:Cola.Constructions.SplatedConditional("arguments","_",pos,after,length)})};Cola.Constructions.PosedVarDef=function(name,type,pos,aftersplated){if(Cola._ColaRuntime$is(name,String))name={name:name};return new Cola.AST_VarDef({type:type,name:new Cola.AST_SymbolVar(name),value:Cola.Constructions.ValueWithOffset("arguments","_",aftersplated==-1,pos,aftersplated)})};Cola.Constructions.PosedWithDefsVarDef=function(name,type,defval,pos,aftersplated){if(Cola._ColaRuntime$is(name,String))name={name:name};return new Cola.AST_VarDef({type:type,name:new Cola.AST_SymbolVar(name),value:new Cola.AST_Conditional({condition:new Cola.AST_Binary({operator:"!==",left:Cola.Constructions.ValueWithOffset("arguments","_",aftersplated==-1,pos,aftersplated),right:new Cola.AST_SymbolRef({name:"undefined"})}),consequent:Cola.Constructions.ValueWithOffset("arguments","_",aftersplated==-1,pos,aftersplated),alternative:defval})})};Cola.Constructions.NamedVarDef=function(name,type,defval,key){if(Cola._ColaRuntime$is(name,String))name={name:name};return new Cola.AST_VarDef({type:type,name:new Cola.AST_SymbolVar(name),value:new Cola.AST_Conditional({condition:new Cola.AST_Binary({operator:"!==",left:new Cola.AST_Dot({expression:new Cola.AST_SymbolRef({name:"arguments"}),property:key}),right:new Cola.AST_SymbolRef({name:"undefined"})}),consequent:new Cola.AST_Dot({expression:new Cola.AST_SymbolRef({name:"arguments"}),property:key}),alternative:defval})})};Cola.AST_Toplevel.prototype.toJavaScript=function(options){if(this.language=="js")return this;this.language="js";options=Cola.defaults(options,{main_binding:true,main_event:"DOMContentLoaded",parser:{},std:true,path:""});var _ColaRuntime$ast=Cola.parse(Cola.$_cola,{is_js:true}),_ColaRuntime$hash={},_this,required=[],required_hash={},tt=new Cola.TreeTransformer(function(node,descend,in_list){var newNode,props={},parent=this.parent();node=node.clone();if(options.main_binding&&parent instanceof Cola.AST_Toplevel&&node instanceof Cola.AST_Defun&&node.name instanceof Cola.AST_SymbolDefun&&node.name.name=="main"){node.name=new Cola.AST_SymbolLambda(node.name);node=new Cola.AST_Function(node);props={args:[new Cola.AST_String({value:options.main_event}),node,new Cola.AST_False],expression:new Cola.AST_Dot({property:"addEventListener",expression:new Cola.AST_SymbolRef({name:"window"})})};node=n