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

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

d3

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