Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/platforms/rhino/bootstrap.js
blob: 4c19cb6a739ac5afcea0be6f298e04d59739b025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(function(global, evalGlobal) {

    /*
        this is a minimal platform-specific thunk for narwhal.js
        that brings the NARWHAL_PATH environment variable into the global
        scope using Rhino's special access to Java.
    */

    var moduleScopingEnabled = false;

    /* this gets used for several fixtures */
    var context = Packages.org.mozilla.javascript.Context.getCurrentContext();

    var prefix = "";
    if (typeof NARWHAL_HOME != "undefined") {
        prefix = NARWHAL_HOME;
        delete NARWHAL_HOME;
    } else {
        prefix = String(Packages.java.lang.System.getenv("NARWHAL_HOME") || "");
    }

    var packagePrefixes = [prefix];

    if (typeof SEA != "undefined") {
        packagePrefixes.push(SEA);
    }

    // TODO: enable this via a command line switch
    context.setOptimizationLevel(-1);

    var isFile = function (path) {
        try { return new java.io.File(path).isFile(); } catch (e) {}
        return false;
    };

    var read = function (path) {
        var path = new java.io.File(path),
            stream = new java.io.FileInputStream(path);
        try {
            var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, path.length());
            stream.read(buffer);
            return String(new java.lang.String(buffer, "UTF-8"));
        } finally {
            stream.close();
        }
    };
    
    var evaluate = function (text, name, lineNo) {
        var scope;
        
        if (moduleScopingEnabled) {
            scope = new Object();
            scope.__parent__ = null;
            scope.__proto__ = global;
        } else {
            scope = global;
        }
        
        return context.compileFunction(
            scope,
            "function(require,exports,module,system,print){"+text+"\n// */\n}",
            name,
            lineNo,
            null
        );
    };

    delete global.print;
    var print = function (string) {
        Packages.java.lang.System.out.println(String(string));
    };

    var narwhal = context.evaluateReader(
        global,
        new Packages.java.io.FileReader(prefix + "/narwhal.js"),
        "narwhal.js",
        1,
        null
    );

    var debug = +String(Packages.java.lang.System.getenv("NARWHAL_DEBUG"));
    var verbose = +String(Packages.java.lang.System.getenv("NARWHAL_VERBOSE"));
    var os = String(Packages.java.lang.System.getProperty("os.name"));

    narwhal({
        global: global,
        evalGlobal: evalGlobal,
        platform: 'rhino',
        platforms: ['rhino', 'default'],
        os: os,
        print: print,
        fs: {
            read: read,
            isFile: isFile
        },
        prefix: prefix,
        packagePrefixes: packagePrefixes,
        evaluate: evaluate,
        debug: debug,
        verbose: verbose
    });
        
})(this, function () {
    return eval(arguments[0]);
});