Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 2178210

Browse files
author
Face Kapow
committed
js/modules, fix 'self', change some of the wording in the docs
1 parent a320c6f commit 2178210

12 files changed

+367
-379
lines changed

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Contributing to runtime.js
22

3-
Firstly, thanks for deciding to contribute! :+1:
3+
First off, thanks for deciding to contribute! :+1:
44

55
## Got an issue?
66

77
* Make sure the issue isn't already reported by checking in [Issues](https://github.com/runtimejs/runtime/issues).
88
* Include the version of the kernel and JavaScript library in the issue description.
9-
* Include a description of what happens or what the issue/bug does and what the expected behavior is.
9+
* Include a description of what happens and what the expected behavior is.
1010

1111
## Cool feature request?
1212

docs/code-style-exceptions.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can still use `for-in`, just be sure you know [the catch](https://developer.
1616

1717
You *can* use 'dangling' underscores to denote a private member on an object or class.
1818
Some APIs were written before this style was adopted which use underscores for private members and probably won't be changed for compatability.
19-
However, for any new APIs, it'd be prefered to use a [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) instead, like:
19+
However, for any new APIs, it'd be preferred to use a [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) instead, like:
2020

2121
```js
2222
const somePrivateVarName = Symbol('somePrivateVarName');
@@ -30,3 +30,7 @@ class Demo {
3030
}
3131
}
3232
```
33+
34+
## Using `get` and `set`
35+
36+
You can (and should) use ES6 `get` and `set`, it's already used in various runtime.js APIs.

js/__loader.js

-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ ${content}
225225
constants: 'constants-browserify',
226226
fs: './modules/fs.js',
227227
os: './modules/os.js',
228-
__errors__: './modules/errors.js',
229228
net: './modules/net.js',
230229
dns: './modules/dns.js',
231230
punycode: 'punycode',
@@ -273,6 +272,5 @@ ${content}
273272
process.termout = new TermoutStream();
274273
process.termerr = new TermerrStream();
275274
loader.require('console');
276-
Object.assign(global, loader.require('__errors__'));
277275
loader.require('/');
278276
})();

js/driver/virtio/vring/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ class VRing {
111111
return true;
112112
}
113113
getBuffer() {
114-
const hasUnprocessed = self.usedRing.hasUnprocessedBuffers();
114+
const hasUnprocessed = this.usedRing.hasUnprocessedBuffers();
115115
if (!hasUnprocessed) return null;
116116

117117
const used = this.usedRing.getUsedDescriptor();
118118
if (used === null) return null;
119119

120120
const descriptorId = used.id;
121-
const buffer = self.descriptorTable.getBuffer(descriptorId);
121+
const buffer = this.descriptorTable.getBuffer(descriptorId);
122122
const len = used.len;
123123

124124
this.availableRing.setEventIdx(this.usedRing.lastUsedIndex + 1);
@@ -128,7 +128,7 @@ class VRing {
128128
console.log('VRING ERROR: buffer is not a Uint8Array');
129129
console.log('used.descriptor id ', descriptorId);
130130
console.log('used.len ', len);
131-
console.log('last used index ', self.usedRing.lastUsedIndex);
131+
console.log('last used index ', this.usedRing.lastUsedIndex);
132132
return null;
133133
}
134134

js/modules/console.js

+31-12
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,27 @@
1414
'use strict';
1515

1616
const util = require('util');
17+
const stream = require('stream');
1718

1819
class Console {
1920
constructor(stdout, stderr) {
20-
if (!stdout) throw new Error('Console: Must provide a stdout stream to the constructor.');
21+
if (!stdout || (!(stdout instanceof stream.Writable) && !(stdout instanceof stream.Duplex))) {
22+
throw new TypeError('Console expects a writable stream instance');
23+
}
2124
this._stdout = stdout;
22-
if (stderr) this._stderr = stderr;
25+
if (stderr) {
26+
if (!(stderr instanceof stream.Writable) && !(stderr instanceof stream.Duplex)) {
27+
throw new TypeError('Console expects writable stream instances');
28+
}
29+
this._stderr = stderr;
30+
}
2331
this._labels = {};
2432
}
2533
assert(val, ...data) {
2634
if (!val) throw new Error(util.format(...data));
2735
}
28-
dir(obj, opts) {
29-
opts = opts || {};
36+
dir(obj, optsOpt = {}) {
37+
const opts = optsOpt;
3038
opts.customInspect = true;
3139
this._stdout.write(util.inspect(obj, opts));
3240
}
@@ -37,18 +45,16 @@ class Console {
3745
log(...data) {
3846
this._stdout.write(`${util.format(...data)}\n`);
3947
}
40-
time(label) {
41-
if (!label) label = 'undefined';
48+
time(label = 'undefined') {
4249
this._labels[label] = Date.now();
4350
}
44-
timeEnd(label) {
45-
if (!label) label = 'undefined'
46-
if (!this._labels[label]) throw new Error('Console.timeEnd: Label does not exist.');
47-
this._stdout.write(`${label}: ${Date.now()-this._labels[label]}ms\n`);
51+
timeEnd(label = 'undefined') {
52+
if (!this._labels[label]) process.emitWarning(`No such label ${label} for console.timeEnd()`);
53+
this._stdout.write(`${label}: ${Date.now() - this._labels[label]}ms\n`);
4854
}
4955
trace(...data) {
5056
let trace = (new Error()).stack;
51-
let arr = trace.split('\n');
57+
const arr = trace.split('\n');
5258
arr[0] = 'Trace';
5359
if (data.length > 0) arr[0] += `: ${util.format(...data)}`;
5460
trace = arr.join('\n');
@@ -62,5 +68,18 @@ class Console {
6268
}
6369
}
6470

65-
module.exports = Console;
6671
global.console = new Console(process.stdout, process.stderr);
72+
73+
const bound = [
74+
'assert',
75+
'dir',
76+
'error',
77+
'log',
78+
'time',
79+
'timeEnd',
80+
'trace',
81+
'info',
82+
'warn',
83+
];
84+
for (const item of bound) module.exports[item] = console[item].bind(console);
85+
module.exports.Console = Console;

js/modules/dns.js

+69-69
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,48 @@
1313
// limitations under the License.
1414
'use strict';
1515

16-
Object.assign(exports, {
17-
NODATA: 'ENODATA',
18-
BADFAMILY: 'EBADFAMILY',
19-
FORMERR: 'EFORMERR',
20-
SERVFAIL: 'ESERVFAIL',
21-
NOTFOUND: 'ENOTFOUND',
22-
NOTIMP: 'ENOTIMP',
23-
REFUSED: 'EREFUSED',
24-
BADQUERY: 'EBADQUERY',
25-
BADNAME: 'EBADNAME',
26-
BADRESP: 'EBADRESP',
27-
CONNREFUSED: 'ECONNREFUSED',
28-
TIMEOUT: 'ETIMEOUT',
29-
EOF: 'EEOF',
30-
FILE: 'EFILE',
31-
NOMEM: 'ENOMEM',
32-
DESTRUCTION: 'EDESTRUCTION',
33-
BADSTR: 'EBADSTR',
34-
BADFLAGS: 'EBADFLAGS',
35-
NONAME: 'ENONAME',
36-
BADHINTS: 'EBADHINTS',
37-
NOTINITIALIZED: 'ENOTINITIALIZED',
38-
LOADIPHLPAPI: 'ELOADIPHLPAPI',
39-
ADDRGETNETWORKPARAMS: 'EADDRGETNETWORKPARAMS',
40-
CANCELLED: 'ECANCELLED'
41-
});
16+
const { SystemError } = require('./errors');
4217

43-
let servers = [
44-
'8.8.8.8'
18+
const names = [
19+
'NODATA',
20+
'BADFAMILY',
21+
'FORMERR',
22+
'SERVFAIL',
23+
'NOTFOUND',
24+
'NOTIMP',
25+
'REFUSED',
26+
'BADQUERY',
27+
'BADNAME',
28+
'BADRESP',
29+
'CONNREFUSED',
30+
'TIMEOUT',
31+
'EOF',
32+
'FILE',
33+
'NOMEM',
34+
'DESTRUCTION',
35+
'BADSTR',
36+
'BADFLAGS',
37+
'NONAME',
38+
'BADHINTS',
39+
'NOTINITIALIZED',
40+
'LOADIPHLPAPI',
41+
'ADDRGETNETWORKPARAMS',
42+
'CANCELLED',
4543
];
44+
for (const name of names) exports[name] = `E${name}`;
4645

47-
function lookup(hostname, opts, cb) {
46+
const servers = [
47+
'8.8.8.8',
48+
];
49+
50+
const throwIPv6Err = (cb) => {
51+
const err = new SystemError('runtime doesn\'t support IPv6', exports.BADFAMILY);
52+
if (cb) return cb(err);
53+
throw err;
54+
};
55+
56+
const lookup = (hostname, optsOpt, cb) => {
57+
const opts = optsOpt;
4858
if (opts.family && opts.family === 6) return throwIPv6Err(cb);
4959
opts.query = opts.query || 'A';
5060
if (hostname === 'localhost' && opts.query === 'A') {
@@ -54,24 +64,23 @@ function lookup(hostname, opts, cb) {
5464
if (opts.addrOnly) {
5565
if (cb) cb(null, ['127.0.0.1']);
5666
} else {
57-
if (cb) cb(null, [{address: '127.0.0.1', family: 4}]);
67+
if (cb) cb(null, [{ address: '127.0.0.1', family: 4 }]);
5868
}
5969
}
6070
return;
6171
}
6272
runtime.dns.resolve(hostname, {
63-
query: opts.query
64-
}, function(err, data) {
73+
query: opts.query,
74+
}, (err, data) => {
6575
if (err) {
6676
if (cb) cb(err, null, null);
6777
return;
6878
}
69-
var res;
70-
var ret = [];
71-
for (var i = 0; i < data.results.length; i++) {
72-
res = data.results[i];
79+
const ret = [];
80+
for (const i of Object.keys(data.results)) {
81+
const res = data.results[i];
7382
if (!opts.all && i === 0) {
74-
var addr = res.address.join('.');
83+
const addr = res.address.join('.');
7584
if (cb) cb(null, addr, 4);
7685
break;
7786
} else {
@@ -82,10 +91,12 @@ function lookup(hostname, opts, cb) {
8291
} else {
8392
ret.push({
8493
address: res.address.join('.'),
85-
family: 4
94+
family: 4,
8695
});
8796
}
8897
break;
98+
default:
99+
break;
89100
}
90101
}
91102
}
@@ -95,56 +106,45 @@ function lookup(hostname, opts, cb) {
95106
}
96107
if (cb) cb(null, ret);
97108
});
98-
}
99-
100-
function throwIPv6Err(cb) {
101-
var err = new SystemError('runtime doesn\'t support IPv6', exports.BADFAMILY);
102-
if (cb) return cb(err);
103-
throw err;
104-
}
109+
};
105110

106-
exports.getServers = function() {
107-
return servers;
108-
}
111+
exports.getServers = () => servers;
109112

110-
exports.lookup = function(hostname, opts, cb) {
113+
exports.lookup = (hostname, optsOpt, cbOpt) => {
114+
let opts = optsOpt;
115+
let cb = cbOpt;
111116
if (typeof opts === 'function') {
112117
cb = opts;
113118
opts = null;
114119
}
115120
if (typeof opts === 'undefined' || opts === null) opts = {};
116121
if (typeof opts === 'number' || opts instanceof Number) {
117122
opts = {
118-
family: opts
123+
family: opts,
119124
};
120125
}
121126

122127
return lookup(hostname, opts, cb);
123-
}
128+
};
124129

125-
exports.resolve4 = function(hostname, cb) {
126-
return lookup(hostname, {
127-
all: true,
128-
addrOnly: true
129-
}, cb);
130-
}
130+
exports.resolve4 = (hostname, cb) => lookup(hostname, {
131+
all: true,
132+
addrOnly: true,
133+
}, cb);
131134

132-
exports.resolve6 = function(hostname, cb) {
133-
throwIPv6Err(cb);
134-
}
135+
exports.resolve6 = (hostname, cb) => throwIPv6Err(cb);
135136

136-
exports.resolve = function(hostname, rrtype, cb) {
137+
exports.resolve = (hostname, rrtypeOpt, cbOpt) => {
138+
let rrtype = rrtypeOpt;
139+
let cb = cbOpt;
137140
if (typeof rrtype === 'function') {
138141
cb = rrtype;
139142
rrtype = null;
140143
}
141144
if (typeof rrtype === 'undefined' || rrtype === null) rrtype = 'A';
142-
switch (rrtype) {
143-
case 'A':
144-
return exports.resolve4(hostname, cb);
145-
break;
146-
case 'AAAA':
147-
return exports.resolve6(hostname, cb);
148-
break;
145+
if (rrtype === 'A') {
146+
return exports.resolve4(hostname, cb);
147+
} else if (rrtype === 'AAAA') {
148+
return exports.resolve6(hostname, cb);
149149
}
150-
}
150+
};

js/modules/errors.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ exports.Error = Error;
1717

1818
class SystemError {
1919
constructor(message, errcode, call) {
20-
var msg = '';
20+
let msg = '';
2121
if (errcode) msg += `${errcode}: `;
2222
if (message) msg += message;
2323
if (call) msg += `, ${call}`;
24-
var err = new Error(msg);
24+
const err = new Error(msg);
2525
err.code = errcode || '';
2626
err.syscall = call || '';
2727
return err;

0 commit comments

Comments
 (0)