Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/platforms/rhino/lib/os-platform.js
blob: c186b280c3e29462aa15f6a5763d8bc4adacd855 (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
106
107
108
109
110
111
112
113
114

var io = require('io');

exports.exit = function (status) {
    Packages.java.lang.System.exit(status << 0);
};

exports.sleep = function (seconds) {
    Packages.java.lang.Thread.sleep((seconds * 1000) >>> 0);
};

exports.fork = function () {
};

exports.exec = function () {
};

exports.dup = function () {
};

exports.dup2 = function () {
};

exports.setsid = function () {
};

exports.getpid = function () {
};

var javaRuntime = function () {
    return Packages.java.lang.Runtime.getRuntime();
};

var javaPopen = function (command) {
    return javaRuntime().exec(command);
};

exports.popen = function (command, options) {
    // todo options: "b", {charset, shell}
    if (!options)
        options = {};
    if (typeof command == "string")
        command = ["sh", "-c", command];

    var process = javaPopen(command);

    var stdin = new io.TextOutputStream(new io.IO(null, process.getOutputStream()));
    var stdout = new io.TextInputStream(new io.IO(process.getInputStream()));
    var stderr = new io.TextInputStream(new io.IO(process.getErrorStream()));

    return {
        wait: function () {
            return process.waitFor();
        },
        stdin: stdin,
        stdout: stdout,
        stderr: stderr,
        communicate: function (input, output, errput) {

            if (typeof stdin == "string")
                stdin = new io.StringIO(input);
            else if (!stdin)
                stdin = new io.StringIO();

            if (!input)
                input = new io.StringIO();
            if (!output)
                output = new io.StringIO();
            if (!errput)
                errput = new io.StringIO();

            var inThread = new JavaAdapter(Packages.java.lang.Thread, {
                "run": function () {
                    input.copy(stdin);
                    stdin.close();
                }
            });

            var outThread = new JavaAdapter(Packages.java.lang.Thread, {
                "run": function () {
                    stdout.copy(output);
                    stdout.close();
                }
            });

            var errThread = new JavaAdapter(Packages.java.lang.Thread, {
                "run": function () {
                    stderr.copy(errput);
                    stderr.close();
                }
            });

            inThread.setDaemon(true);
            inThread.start();
            errThread.setDaemon(true);
            errThread.start();
            outThread.setDaemon(true);
            outThread.start();

            inThread.join();
            outThread.join();
            errThread.join();

            var code = process.waitFor();

            return {
                code: code,
                stdout: output,
                stderr: errput
            };
        }
    }
};