Skip to content

Commit

Permalink
Allow env-js to be require'd.
Browse files Browse the repository at this point in the history
This fixes a bug with the usage of `require` in env-js, such that env-js now
uses proper relative paths rather than unshifting "." onto `require.paths`. The
behavior of `require` is documented here:

  <http://nodejs.org/docs/v0.4.2/api/modules.html#all_Together...>

With this change in place, it's now possible to use env-js simply by require'ing
it from a script, rather than having env-js host and eval.

Also, this commit changes the initialization of env-js, such that the initial
document is immediately available when the env-js module is loaded. And, the
event loop is disabled so that env-js shuts down cleanly. This probably breaks
some parts of env-js, but it makes it much easier to use so I'm going to play
with it a bit.
  • Loading branch information
mbostock committed Mar 4, 2011
1 parent 3568eca commit 7df63b3
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 148 deletions.
39 changes: 39 additions & 0 deletions lib/env-js/bin/envjs
@@ -0,0 +1,39 @@
#!/bin/sh
# Usage: envjs <platorm> file.js [file2.js ...]
###########################################################################
ENVJS_PLATFORM='node'

# first arguments ($1)
# may be a platform or is a file and the default platorm is used
if [ -n "$1" ]; then
if [ -n "$1" ]; then ENVJS_PLATFORM="$1"; shift; fi
fi

# Run envjs with the given platform
###########################################################################
case "$ENVJS_PLATFORM" in

"node")
node envjs/node.js $@
;;
"rhino")
java -Xmx512M -jar rhino/js.jar -opt -1 envjs/rhino.js $@
;;
"rhino-debug")
java -cp rhino/js.jar org.mozilla.javascript.tools.debugger.Main envjs/rhino.js $@
;;
"spyd")
python envjs/spydermonkey.py envjs/spydermonkey.js $@
;;
"rubyracer")
ruby -rrubygems envjs/rubyracer.rb envjs/rubyracer.js $@
;;
"johnson")
ruby -rrubygems envjs/johnson.rb envjs/johnson.js $@
;;
*)
# platform default means $1 was actually a file
node envjs/node.js $ENVJS_PLATFORM $@
;;
esac

6 changes: 3 additions & 3 deletions lib/env-js/envjs/console.js
Expand Up @@ -4,10 +4,10 @@
* @Console
*/

var Envjs = Envjs || require('envjs/platform/core').Envjs;
var Envjs = Envjs || require('./platform/core').Envjs;

/*
* Envjs console.1.3.pre03
* Envjs console.1.3.pre03
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
Expand All @@ -28,7 +28,7 @@ var Envjs = Envjs || require('envjs/platform/core').Envjs;
try{
console.log();
}catch(e){


function escapeHTML(value)
{
Expand Down
6 changes: 3 additions & 3 deletions lib/env-js/envjs/css.js
Expand Up @@ -18,9 +18,9 @@ var CSS2Properties,
*/

var Envjs = Envjs || require('envjs/platform/core').Envjs,
Document = require('envjs/dom').Document,
HTMLElement = require('envjs/html').HTMLElement;
var Envjs = Envjs || require('./platform/core').Envjs,
Document = require('./dom').Document,
HTMLElement = require('./html').HTMLElement;
/*
* Envjs css.1.3.pre03
* Pure JavaScript Browser Environment
Expand Down
4 changes: 2 additions & 2 deletions lib/env-js/envjs/dom.js
Expand Up @@ -41,8 +41,8 @@ var Attr,
*/

var Envjs = Envjs || require('envjs/platform/core').Envjs,
After = After || require('envjs/platform/core').After;
var Envjs = Envjs || require('./platform/core').Envjs,
After = After || require('./platform/core').After;

/*
* Envjs dom.1.3.pre03
Expand Down
6 changes: 3 additions & 3 deletions lib/env-js/envjs/event.js
Expand Up @@ -20,9 +20,9 @@ var Event,
*/

var Envjs = Envjs || require('envjs/platform/core').Envjs,
After = After || require('envjs/platform/core').After,
Document = Document || require('envjs/dom').Document;
var Envjs = Envjs || require('./platform/core').Envjs,
After = After || require('./platform/core').After,
Document = Document || require('./dom').Document;
/*
* Envjs event.1.3.pre03
* Pure JavaScript Browser Environment
Expand Down
14 changes: 7 additions & 7 deletions lib/env-js/envjs/html.js
Expand Up @@ -68,13 +68,13 @@
__loadLink__;
*/

var Envjs = Envjs || require('envjs/platform/core').Envjs,
After = After || require('envjs/platform/core').After,
Document = Document || require('envjs/dom').Document,
Element = Element || require('envjs/dom').Element,
NodeList = NodeList || require('envjs/dom').NodeList,
Node = Node || require('envjs/dom').Node,
Event = Event || require('envjs/event').Event;
var Envjs = Envjs || require('./platform/core').Envjs,
After = After || require('./platform/core').After,
Document = Document || require('./dom').Document,
Element = Element || require('./dom').Element,
NodeList = NodeList || require('./dom').NodeList,
Node = Node || require('./dom').Node,
Event = Event || require('./event').Event;

/*
* Envjs html.1.3.pre03
Expand Down
4 changes: 2 additions & 2 deletions lib/env-js/envjs/johnson.js
Expand Up @@ -62,5 +62,5 @@ var require = (function() {
return require;
})();

require('envjs/platform/johnson');
require('envjs/window');
require('./platform/johnson');
require('./window');
8 changes: 5 additions & 3 deletions lib/env-js/envjs/node.js
@@ -1,3 +1,5 @@
require.paths.unshift('.');
require('envjs/platform/node');
require('envjs/window');
require('./platform/node');
Envjs.eventLoop = function() {}; // disabled for clean shutdown

require('./window');
new Window(__this__); // initialize a blank window
26 changes: 13 additions & 13 deletions lib/env-js/envjs/parser.js
Expand Up @@ -9,19 +9,19 @@
*
*/

var Envjs = Envjs || require('envjs/platform/core').Envjs,
After = After || require('envjs/platform/core').After,
DOMImplementation = DOMImplementation || require('envjs/dom').DOMImplementation,
Document = Document || require('envjs/dom').Document,
Element = Element || require('envjs/dom').Element,
NodeList = NodeList || require('envjs/dom').NodeList,
Node = Node || require('envjs/dom').Node,
HTMLDocument = HTMLDocument || require('envjs/html').HTMLDocument,
HTMLElement = HTMLElement || require('envjs/html').HTMLElement,
setTimeout = setTimeout || require('envjs/timer').setTimeout,
clearTimeout = clearTimeout || require('envjs/timer').clearTimeout,
setInterval = setInterval || require('envjs/timer').setInterval,
clearInterval = clearInterval || require('envjs/timer').clearInterval;
var Envjs = Envjs || require('./platform/core').Envjs,
After = After || require('./platform/core').After,
DOMImplementation = DOMImplementation || require('./dom').DOMImplementation,
Document = Document || require('./dom').Document,
Element = Element || require('./dom').Element,
NodeList = NodeList || require('./dom').NodeList,
Node = Node || require('./dom').Node,
HTMLDocument = HTMLDocument || require('./html').HTMLDocument,
HTMLElement = HTMLElement || require('./html').HTMLElement,
setTimeout = setTimeout || require('./timer').setTimeout,
clearTimeout = clearTimeout || require('./timer').clearTimeout,
setInterval = setInterval || require('./timer').setInterval,
clearInterval = clearInterval || require('./timer').clearInterval;
/*
* Envjs parser.1.3.pre03
* Pure JavaScript Browser Environment
Expand Down
6 changes: 3 additions & 3 deletions lib/env-js/envjs/platform/core.js
Expand Up @@ -43,7 +43,7 @@ var log = {
};

if (typeof console == "undefined") {
console = require('envjs/console').console;
console = require('./console').console;
}

//eg "Mozilla"
Expand Down Expand Up @@ -980,7 +980,7 @@ Envjs.emit = function(event /*, arg1, arg2, etc*/ ){
eventQueue.push({event:event, args:arguments});
};

setTimeout = require('envjs/timer').setTimeout;
setTimeout = require('./../timer').setTimeout;

var $warming = 10;

Expand Down Expand Up @@ -1375,7 +1375,7 @@ Envjs.exchangeHTMLDocument = function(doc, text, url) {
frame = doc.__ownerFrame__,
i;
try {
HTMLParser = HTMLParser || require('envjs/parser').HTMLParser;
HTMLParser = HTMLParser || require('./../parser').HTMLParser;
//do some cleanup so we can reuse the document
doc.baseURI = url;
log.debug('reseting document indexes');
Expand Down
36 changes: 18 additions & 18 deletions lib/env-js/envjs/platform/johnson.js
@@ -1,7 +1,7 @@
var __context__ = __this__;

var Envjs = Envjs ||
require('envjs/platform/core').Envjs;
var Envjs = Envjs ||
require('./core').Envjs;
require('local_settings');

Envjs.platform = "Johnson SpiderMonkey";
Expand All @@ -13,7 +13,7 @@ Envjs.argv = Ruby.ARGV;
Envjs.exit = function(){ Ruby['exit!'](); };

/*
* Envjs johnson-env.1.3.pre03
* Envjs johnson-env.1.3.pre03
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
Expand Down Expand Up @@ -41,7 +41,7 @@ function __extend__(a,b) {
a[i] = b[i];
}
}
}
}
return a;
}

Expand All @@ -67,7 +67,7 @@ Envjs.repl = function(){
console.log('Envjs REPL Not Available');
};
(function(){

var log = Envjs.logger('Envjs.Platform.Johnson');

Envjs.eval = function(context, source, name){
Expand All @@ -84,12 +84,12 @@ Envjs.eval = function(context, source, name){
/**
* synchronizes thread modifications
* @param {Function} fn
*/
*/
var lock = new Ruby.Mutex();

//NOTES:
//context['sync'] = lambda{|wrapper|
// Proc.new{|*args|lock.synchronize {wrapper['fn'].call(*args)}}
//context['sync'] = lambda{|wrapper|
// Proc.new{|*args|lock.synchronize {wrapper['fn'].call(*args)}}
//}
//context['sync'] = lambda{|fn| Proc.new{|*args| fn.call(*args) }}
Envjs.sync = function(fn){
Expand All @@ -116,7 +116,7 @@ Envjs.sleep = function(milliseconds){
return Ruby.sleep(1.0*milliseconds/1000.0);
};
(function(){

var log = Envjs.logger('Envjs.Platform.Johnson');

//Since we're running in spydermonkey I guess we can safely assume
Expand Down Expand Up @@ -151,7 +151,7 @@ Envjs.proxy = function(scope, parent) {
};

})();(function(){

var log = Envjs.logger('Envjs.XMLHttpRequest.Johnson');

/**
Expand Down Expand Up @@ -207,7 +207,7 @@ Envjs.readFromFile = function( url ){
data = file.read();
return data;
};


/**
* Used to delete a local file
Expand Down Expand Up @@ -236,14 +236,14 @@ Envjs.connection = function(xhr, responseHandler, data){
contentEncoding,
responseXML,
i;

if ( /^file\:/.test(url) ) {
//console.log('establishing file connection');
Envjs.localXHR(url, xhr, connection, data);
} else {
log.debug('establishing http connection %s', xhr.url);
try{

connection = Ruby.Net.HTTP.start(
urlparts.hostname,
Number(urlparts.port||80)
Expand All @@ -252,7 +252,7 @@ Envjs.connection = function(xhr, responseHandler, data){

path = urlparts.path+(urlparts.query?'?'+urlparts.query:'');
switch( xhr.method.toUpperCase() ){
case "GET":
case "GET":
request = new Ruby.Net.HTTP.Get(path);break;
case "PUT":
request = new Ruby.Net.HTTP.Put(path);break;
Expand All @@ -270,7 +270,7 @@ Envjs.connection = function(xhr, responseHandler, data){
//TODO: add gzip support
//connection.putheader("Accept-Encoding", 'gzip');
//connection.endheaders();

//write data to output stream if required
//TODO: if they have set the request header for a chunked
//request body, implement a chunked output stream
Expand All @@ -295,7 +295,7 @@ Envjs.connection = function(xhr, responseHandler, data){

if(connection){
[response, headers] = HTTPConnection.go(connection, request, xhr.headers, null);

try{
// Stick the response headers into responseHeaders
var keys = headers.keys();
Expand All @@ -314,7 +314,7 @@ Envjs.connection = function(xhr, responseHandler, data){
log.info('%s %s %s %s', xhr.method, xhr.status, xhr.url, xhr.statusText);
contentEncoding = xhr.getResponseHeader('content-encoding') || "utf-8";
responseXML = null;

try{
//console.log('contentEncoding %s', contentEncoding);
if( contentEncoding.toLowerCase() == "gzip" ||
Expand All @@ -339,7 +339,7 @@ Envjs.connection = function(xhr, responseHandler, data){


}

if(responseHandler){
log.debug('calling ajax response handler');
if(!xhr.async){
Expand Down

0 comments on commit 7df63b3

Please sign in to comment.