source: Dev/trunk/d3/lib/env-js/envjs/johnson.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 1.8 KB
Line 
1// This file must be executed automatically on startup for ruby/v8 rubyracer support.
2var __this__ = this;
3
4var require = (function() {
5    var cached = {};
6    var currentPath = Ruby.ENV['PWD'];
7    var paths = [currentPath];
8   
9    function normalize(id) {
10                var file;
11        id = id + '.js';
12        if (/^\.\.?/.test(id)) {
13            // relative path
14                        try{ return Ruby.File.open(id, 'r') }catch(e){}
15        } else {
16            for (var i = 0, len = paths.length; i < len; ++i) {
17                            try{ return Ruby.File.open(paths[i]+'/'+id, 'r') }catch(e){}
18            }
19        }
20        return undefined;
21    };
22   
23   
24    function require(id) {
25                //print('require :'+ id);
26        var file = normalize(id);
27        if (!file) {
28            throw new Error("couldn't find module \"" + id + "\"");
29        }
30        if (!cached.hasOwnProperty(id)) {
31                        //print('loading '+id);
32            var source = file.read();
33            source = source.replace(/^\#\!.*/, '');
34            source = "(function (require, exports, module) { "+source+"\n});";
35            cached[id] = {
36                exports: {},
37                module: {
38                    id: id,
39                    uri: id
40                }
41            };
42            var previousPath = currentPath;
43            try {
44                currentPath = id.substr(0, id.lastIndexOf('/')) || '.';
45                var func = global.evaluate( source, id );
46                func(require, cached[id].exports, cached[id].module);
47            } finally {
48                currentPath = previousPath;
49            }
50        }
51                /*
52                print('returning exports for id: '+id+' '+cached[id].exports);
53                for(var prop in cached[id].exports){
54                        print('export: '+prop);
55                }
56                */
57        return cached[id].exports;
58    };
59   
60    require.paths = paths;
61   
62    return require;
63})();
64
65require('./platform/johnson');
66require('./window');
Note: See TracBrowser for help on using the repository browser.