This repository has been archived by the owner on Feb 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
system.js
154 lines (138 loc) · 3.19 KB
/
system.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @fileoverview System module compliant with the [CommonJS
* System/1.0](http://wiki.commonjs.org/wiki/System/1.0)
* specification. Additional non-standard methods for sleeping on
* the current thread, spawning a new thread and spawning
* processes are provided.
*/
var Fiber = require('fibers');
var io = require('./io');
var Stream = io.Stream, TextStream = io.TextStream;
/**
* A TextStream to read from stdin.
*
* @name stdin
*/
exports.stdin = new TextStream(new Stream(process.stdin));
/**
* A TextStream to write to stdout.
*
* @name stdout
*/
exports.stdout = new TextStream(new Stream(process.stdout));
/**
* A TextStream to write to stderr.
*
* @name stderr
*/
exports.stderr = new TextStream(new Stream(process.stderr));
/**
* A utility function to write to stdout.
*/
exports.print = function() {
exports.stdout.print.apply(exports.stdout, arguments);
};
/**
* An array of strings representing the command line arguments passed to the
* running script.
*/
exports.args = process.argv || [];
/**
* An object containing our environment variables.
*/
exports.env = process.env;
/**
* Terminates the current process.
*
* @param {number}
* status The exit status, defaults to 0.
*/
exports.exit = function(status) {
process.exit(status);
};
// non-standard methods below
/**
* Used to register a callback which is called when the current process
* terminates.
*
* @param {Function}
* callback The callback to call.
*/
exports.onexit = function(callback) {
process.on('SIGINT', callback);
};
/**
* Spawns a new thread.
*
* @param {Function}
* run entry point of the new thread.
*/
exports.spawn = function(run) {
// TODO bring in line with RingoJS
return Fiber(run).run();
};
exports.parallel = function(fibers) {
var fiber = Fiber.current;
var index = 0;
fibers.forEach(function(r) {
Fiber(function() {
fiber.run([index ++, r()]);
}).run();
});
var result = [];
while(index --) {
var r = Fiber.yield();
result[r[0]] = r[1];
}
return result;
}
/**
* Suspends the current process for the specified number of milliseconds.
*
* @param {Number}
* milliseconds The number of milliseconds to sleep.
*/
exports.sleep = function(milliseconds) {
// TODO bring in line with RingoJS
var fiber = Fiber.current;
setTimeout(function() {
fiber.run();
}, milliseconds);
Fiber.yield();
};
global.getResource = function(name) {
// TODO cache resources?
var fs = require('fs-base');
var resource = {
lastModified : function() {
return fs.lastModified(name);
},
length : fs.exists(name) ? fs.size(name) : 0,
getInputStream : function() {
return fs.openRaw(name);
},
getContent : function() {
return this.content;
},
exists : function() {
return fs.exists(name);
}
};
Object.defineProperty(resource, "content", {
get : function() {
return fs.openRaw(name).read().decodeToString();
}
});
return resource;
}
global.getRepository = function(name) {
var fs = require('fs-base');
return {
_base : name,
getResource : function(name) {
return getResource(fs.normal(fs.join(this._base, name)));
},
setRoot : function() {
}
};
}