source: Dev/trunk/d3/lib/env-js/envjs/spydermonkey.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;
3var require = (function() {
4    var cached = {};
5    var currentPath = os.getcwd();
6    var paths = [currentPath];
7   
8    function normalize(id) {
9                var file;
10        id = id + '.js';
11        if (/^\.\.?/.test(id)) {
12            // relative path
13                        try{ return fopen(id, 'r') }catch(e){}
14        } else {
15            for (var i = 0, len = paths.length; i < len; ++i) {
16                            try{ return fopen(paths[i]+'/'+id, 'r') }catch(e){}
17            }
18        }
19        return undefined;
20    };
21   
22   
23    function require(id) {
24                //print('require :'+ id);
25        var file = normalize(id);
26        if (!file) {
27            throw new Error("couldn't find module \"" + id + "\"");
28        }
29        if (!cached.hasOwnProperty(id)) {
30                        //print('loading '+id);
31            var source = file.read();
32            source = source.replace(/^\#\!.*/, '');
33            source = "(function (require, exports, module) { "+source+"\n});";
34            cached[id] = {
35                exports: {},
36                module: {
37                    id: id,
38                    uri: id
39                }
40            };
41            var previousPath = currentPath;
42            try {
43                currentPath = id.substr(0, id.lastIndexOf('/')) || '.';
44                var func = global.execute( source );
45                func(require, cached[id].exports, cached[id].module);
46            } finally {
47                currentPath = previousPath;
48            }
49        }
50                /*
51                print('returning exports for id: '+id+' '+cached[id].exports);
52                for(var prop in cached[id].exports){
53                        print('export: '+prop);
54                }
55                */
56        return cached[id].exports;
57    };
58   
59    require.paths = paths;
60   
61    return require;
62})();
63require('./platform/spydermonkey');
64require('./window');
Note: See TracBrowser for help on using the repository browser.