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

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

d3

File size: 2.8 KB
Line 
1// This file is compiled into the jar and executed automatically on startup.
2var __this__ = this;
3var require = (function() {
4    var cached = {};
5    var currentPath = java.lang.System.getProperty('user.dir');
6    var paths = [currentPath];
7   
8    function normalize(id) {
9                var file;
10        id = id + '.js';
11        if (/^\.\.?/.test(id)) {
12            // relative path
13            file = new java.io.File(currentPath, id);
14            if (file.isFile()) {
15                return file.toURL();
16            }
17        } else {
18            for (var i = 0, len = paths.length; i < len; ++i) {
19                file = new java.io.File(paths[i], id);
20                if (file.isFile()) {
21                    return file.toURL();
22                }
23            }
24            // try to get it from the jar as a last resort
25            /*var url = rhoop.getClass().getResource('/' + id);
26            if (url !== null) {
27                return url;
28            }*/
29        }
30        return undefined;
31    };
32   
33    function read(connection) {
34        var stream = connection.getInputStream();
35        var bytes = java.lang.reflect.Array.newInstance(
36                java.lang.Byte.TYPE, 4096);
37        var bytesStream = new java.io.ByteArrayOutputStream();
38        var bytesRead;
39        while ((bytesRead = stream.read(bytes)) >= 0) {
40            if (bytesRead > 0) {
41                bytesStream.write(bytes, 0, bytesRead);
42            }
43        }
44        return String(bytesStream.toString());
45    };
46   
47    function require(id) {
48                //print('require :'+ id);
49        var url = normalize(id);
50        if (!url) {
51            throw new Error("couldn't find module \"" + id + "\"");
52        }
53        id = String(url.toString());
54        if (!cached.hasOwnProperty(id)) {
55            var source = read(url.openConnection());
56            source = source.replace(/^\#\!.*/, '');
57            source = (
58                "(function (require, exports, module) { " + source + "\n});");
59            cached[id] = {
60                exports: {},
61                module: {
62                    id: id,
63                    uri: id
64                }
65            };
66            var previousPath = currentPath;
67            try {
68                currentPath = id.substr(0, id.lastIndexOf('/')) || '.';
69                var ctx = org.mozilla.javascript.Context.getCurrentContext();
70                var func = ctx.evaluateString({}, source, id, 1, null);
71                func(require, cached[id].exports, cached[id].module);
72            } finally {
73                currentPath = previousPath;
74            }
75        }
76                /*
77                print('returning exports for id: '+id+' '+cached[id].exports);
78                for(var prop in cached[id].exports){
79                        print('export: '+prop);
80                }
81                */
82        return cached[id].exports;
83    };
84   
85    require.paths = paths;
86   
87    return require;
88}());
89var __argv__ = arguments;
90require('./platform/rhino');
91require('./window');
Note: See TracBrowser for help on using the repository browser.