Skip to content

Instantly share code, notes, and snippets.

@micmath
Created September 14, 2011 13:48
Show Gist options
  • Save micmath/1216599 to your computer and use it in GitHub Desktop.
Save micmath/1216599 to your computer and use it in GitHub Desktop.
/**
* Attaches the script represented by the URL to the current
* environment. Right now only supports browser loading,
* but can be redefined in other environments to do the right thing.
* @param {String} url the url of the script to attach.
* @param {String} contextName the name of the context that wants the script.
* @param {moduleName} the name of the module that is associated with the script.
* @param {Function} [callback] optional callback, defaults to require.onScriptLoad
* @param {String} [type] optional type, defaults to text/javascript
*/
req.attach = function (url, contextName, moduleName, callback, type) {
var node, loaded, context;
if (typeof java !== 'undefined') {
context = contexts[contextName];
loaded = context.loaded;
loaded[moduleName] = false;
(function() {
var jurl = new java.net.URL( url );
var con = jurl.openConnection();
con.setDoOutput( true );
var IO = new JavaImporter(java.io);
var wr = new IO.OutputStreamWriter(con.getOutputStream());
var rd = new IO.BufferedReader(new IO.InputStreamReader(con.getInputStream()));
var line;
var srcLines = [];
while ((line = rd.readLine()) != null) {
srcLines.push(line);
}
try { wr.close(); } catch(e) { /*ignore*/}
try { rd.close(); } catch(e) { /*ignore*/}
var src = srcLines.join('\n');
console.log(src);
(1,eval)(src);
})();
//Account for anonymous modules
context.completeLoad(moduleName);
} else if (isBrowser) {
//In the browser so use a script tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment