From a089699c8915636ba4f158d77dba9b012bc93208 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 4 Mar 2011 19:02:28 +0300 Subject: build custom layer of Dojo to speed up loading of tt-rss (refs #293) --- lib/dojo/_base/_loader/hostenv_rhino.js | 313 +++++++++++++++++++------------- 1 file changed, 184 insertions(+), 129 deletions(-) (limited to 'lib/dojo/_base/_loader/hostenv_rhino.js') diff --git a/lib/dojo/_base/_loader/hostenv_rhino.js b/lib/dojo/_base/_loader/hostenv_rhino.js index 9cd882713..ee9ad8b43 100644 --- a/lib/dojo/_base/_loader/hostenv_rhino.js +++ b/lib/dojo/_base/_loader/hostenv_rhino.js @@ -5,149 +5,204 @@ */ +/* +* Rhino host environment +*/ + if(dojo.config["baseUrl"]){ -dojo.baseUrl=dojo.config["baseUrl"]; + dojo.baseUrl = dojo.config["baseUrl"]; }else{ -dojo.baseUrl="./"; + dojo.baseUrl = "./"; } -dojo.locale=dojo.locale||String(java.util.Locale.getDefault().toString().replace("_","-").toLowerCase()); -dojo._name="rhino"; -dojo.isRhino=true; -if(typeof print=="function"){ -console.debug=print; + +dojo.locale = dojo.locale || String(java.util.Locale.getDefault().toString().replace('_','-').toLowerCase()); +dojo._name = 'rhino'; +dojo.isRhino = true; + +if(typeof print == "function"){ + console.debug = print; } + if(!("byId" in dojo)){ -dojo.byId=function(id,_1){ -if(id&&(typeof id=="string"||id instanceof String)){ -if(!_1){ -_1=document; -} -return _1.getElementById(id); -} -return id; -}; -} -dojo._isLocalUrl=function(_2){ -var _3=(new java.io.File(_2)).exists(); -if(!_3){ -var _4; -try{ -_4=(new java.net.URL(_2)).openStream(); -_4.close(); -} -finally{ -if(_4&&_4.close){ -_4.close(); -} -} -} -return _3; -}; -dojo._loadUri=function(_5,cb){ -try{ -var _6; -try{ -_6=dojo._isLocalUrl(_5); -} -catch(e){ -return false; -} -if(cb){ -var _7=(_6?readText:readUri)(_5,"UTF-8"); -if(!eval("'‏'").length){ -_7=String(_7).replace(/[\u200E\u200F\u202A-\u202E]/g,function(_8){ -return "\\u"+_8.charCodeAt(0).toString(16); -}); -} -cb(eval("("+_7+")")); -}else{ -load(_5); -} -return true; -} -catch(e){ -return false; -} -}; -dojo.exit=function(_9){ -quit(_9); -}; -function readText(_a,_b){ -_b=_b||"utf-8"; -var jf=new java.io.File(_a); -var is=new java.io.FileInputStream(jf); -return dj_readInputStream(is,_b); -}; -function readUri(_c,_d){ -var _e=(new java.net.URL(_c)).openConnection(); -_d=_d||_e.getContentEncoding()||"utf-8"; -var is=_e.getInputStream(); -return dj_readInputStream(is,_d); -}; -function dj_readInputStream(is,_f){ -var _10=new java.io.BufferedReader(new java.io.InputStreamReader(is,_f)); -try{ -var sb=new java.lang.StringBuffer(); -var _11=""; -while((_11=_10.readLine())!==null){ -sb.append(_11); -sb.append(java.lang.System.getProperty("line.separator")); -} -return sb.toString(); + dojo.byId = function(id, doc){ + if(id && (typeof id == "string" || id instanceof String)){ + if(!doc){ doc = document; } + return doc.getElementById(id); + } + return id; // assume it's a node + } } -finally{ -_10.close(); -} -}; -dojo._getText=function(uri,_12){ -try{ -var _13=dojo._isLocalUrl(uri); -var _14=(_13?readText:readUri)(uri,"UTF-8"); -if(_14!==null){ -_14+=""; + +dojo._isLocalUrl = function(/*String*/ uri) { + // summary: + // determines if URI is local or not. + + var local = (new java.io.File(uri)).exists(); + if(!local){ + var stream; + //Try remote URL. Allow this method to throw, + //but still do cleanup. + try{ + // try it as a file first, URL second + stream = (new java.net.URL(uri)).openStream(); + // close the stream so we don't leak resources + stream.close(); + }finally{ + if(stream && stream.close){ + stream.close(); + } + } + } + return local; } -return _14; + +// see comments in spidermonkey loadUri +dojo._loadUri = function(uri, cb){ + try{ + var local; + try{ + local = dojo._isLocalUrl(uri); + }catch(e){ + // no debug output; this failure just means the uri was not found. + return false; + } + + //FIXME: Use Rhino 1.6 native readFile/readUrl if available? + if(cb){ + var contents = (local ? readText : readUri)(uri, "UTF-8"); + + // patch up the input to eval until https://bugzilla.mozilla.org/show_bug.cgi?id=471005 is fixed. + if(!eval("'\u200f'").length){ + contents = String(contents).replace(/[\u200E\u200F\u202A-\u202E]/g, function(match){ + return "\\u" + match.charCodeAt(0).toString(16); + }) + } + + cb(eval('('+contents+')')); + }else{ + load(uri); + } + return true; + }catch(e){ + console.debug("rhino load('" + uri + "') failed. Exception: " + e); + return false; + } } -catch(e){ -if(_12){ -return null; -}else{ -throw e; + +dojo.exit = function(exitcode){ + quit(exitcode); } + +// reading a file from disk in Java is a humiliating experience by any measure. +// Lets avoid that and just get the freaking text +function readText(path, encoding){ + encoding = encoding || "utf-8"; + // NOTE: we intentionally avoid handling exceptions, since the caller will + // want to know + var jf = new java.io.File(path); + var is = new java.io.FileInputStream(jf); + return dj_readInputStream(is, encoding); } -}; -dojo.doc=typeof document!="undefined"?document:null; -dojo.body=function(){ -return document.body; -}; -if(typeof setTimeout=="undefined"||typeof clearTimeout=="undefined"){ -dojo._timeouts=[]; -clearTimeout=function(idx){ -if(!dojo._timeouts[idx]){ -return; + +function readUri(uri, encoding){ + var conn = (new java.net.URL(uri)).openConnection(); + encoding = encoding || conn.getContentEncoding() || "utf-8"; + var is = conn.getInputStream(); + return dj_readInputStream(is, encoding); } -dojo._timeouts[idx].stop(); -}; -setTimeout=function(_15,_16){ -var def={sleepTime:_16,hasSlept:false,run:function(){ -if(!this.hasSlept){ -this.hasSlept=true; -java.lang.Thread.currentThread().sleep(this.sleepTime); + +function dj_readInputStream(is, encoding){ + var input = new java.io.BufferedReader(new java.io.InputStreamReader(is, encoding)); + try { + var sb = new java.lang.StringBuffer(); + var line = ""; + while((line = input.readLine()) !== null){ + sb.append(line); + sb.append(java.lang.System.getProperty("line.separator")); + } + return sb.toString(); + } finally { + input.close(); + } } -try{ -_15(); + +dojo._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){ + // summary: Read the contents of the specified uri and return those contents. + // uri: + // A relative or absolute uri. + // fail_ok: + // Default false. If fail_ok and loading fails, return null + // instead of throwing. + // returns: The response text. null is returned when there is a + // failure and failure is okay (an exception otherwise) + try{ + var local = dojo._isLocalUrl(uri); + var text = (local ? readText : readUri)(uri, "UTF-8"); + if(text !== null){ + //Force JavaScript string. + text += ""; + } + return text; + }catch(e){ + if(fail_ok){ + return null; + }else{ + throw e; + } + } } -catch(e){ + +// summary: +// return the document object associated with the dojo.global +dojo.doc = typeof document != "undefined" ? document : null; + +dojo.body = function(){ + return document.body; } -}}; -var _17=new java.lang.Runnable(def); -var _18=new java.lang.Thread(_17); -_18.start(); -return dojo._timeouts.push(_18)-1; -}; + +// Supply setTimeout/clearTimeout implementations if they aren't already there +// Note: this assumes that we define both if one is not provided... there might +// be a better way to do this if there is a use case where one is defined but +// not the other +if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){ + dojo._timeouts = []; + clearTimeout = function(idx){ + if(!dojo._timeouts[idx]){ return; } + dojo._timeouts[idx].stop(); + } + + setTimeout = function(func, delay){ + // summary: provides timed callbacks using Java threads + + var def={ + sleepTime:delay, + hasSlept:false, + + run:function(){ + if(!this.hasSlept){ + this.hasSlept=true; + java.lang.Thread.currentThread().sleep(this.sleepTime); + } + try{ + func(); + }catch(e){ + console.debug("Error running setTimeout thread:" + e); + } + } + }; + + var runnable = new java.lang.Runnable(def); + var thread = new java.lang.Thread(runnable); + thread.start(); + return dojo._timeouts.push(thread)-1; + } } + +//Register any module paths set up in djConfig. Need to do this +//in the hostenvs since hostenv_browser can read djConfig from a +//script tag's attribute. if(dojo.config["modulePaths"]){ -for(var param in dojo.config["modulePaths"]){ -dojo.registerModulePath(param,dojo.config["modulePaths"][param]); -} + for(var param in dojo.config["modulePaths"]){ + dojo.registerModulePath(param, dojo.config["modulePaths"][param]); + } } -- cgit v1.2.3