-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshell.js
More file actions
164 lines (147 loc) · 4.38 KB
/
shell.js
File metadata and controls
164 lines (147 loc) · 4.38 KB
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
155
156
157
158
159
160
161
162
163
164
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint undef: true es5: true node: true devel: true globalstrict: true
forin: true latedef: false supernew: true */
/*global define: true */
"use strict";
const { Cc, Ci } = require('chrome');
const { Loader } = require('@loader');
const options = require('@packaging');
require('./sandbox');
/* Narwhal shell module */
const EMPTY_MATCH = /^\s*$/,
STATEMENT_MATCH = /^\s*;\s*$/,
NORMAL_PS = 'js> ',
UNFINISHED_PS = ' > ',
FIN = String.fromCharCode(65533, 65533, 65533, 65533, 6)
function handleError(e) {
let result = '', realException = e.cause || e
if (realException) {
result += e + '\nDetails:\n'
for (let key in realException) {
let content = '' + realException[key]
if (content.indexOf('\n') != -1)
content = '\n' + content.replace(/^(?!$)/gm, ' ')
else content = ' ' + content
result += ' ' + key + ':' + content.replace(/\s*\n$/m, '')
}
result += "\n"
}
return result
}
/**
* Internal utility function that is used to generate
* textual representation of things passed to it.
*/
function represent(thing) {
let result
switch (typeof (thing)) {
case 'string':
result = '"' + thing + '"'
break
case 'number':
result = thing
break
case 'object':
if (null === thing) {
result = 'null'
break
}
if (Array.isArray(thing)) {
result = '[' + thing.map(represent).join(',') + ']'
break
}
let names = []
for (let name in thing) names.push(name)
result = '/* ' + thing + ' */'
if (names.length > 0) {
result += ' \n{ '
result += names.slice(0, 7).map(function (name) {
let repr = name + ': '
try {
let $ = thing[name]
repr += 'object' == typeof $ && null !== $ ? '{...}' : represent($)
} catch (e) {
repr += '[Exception!]'
}
return repr
}).join('\n, ')
if (names.length > 7) result += '\n, ....................'
result += '\n}'
}
break
case "function":
result = thing.toString().split('\n').slice(0, 2).join('\n') + '...}'
break
default:
result = '' + thing
}
return result
}
function Shell(stream) {
let buffer = '', ps = NORMAL_PS, result, error
let loader = Loader.new(options);
let sandbox = Loader.require.call(loader, module.path, './sandbox');
let line = 1;
let root = 'resource://' + options.unique_prefix + '/';
let uri = Object.keys(loader.sandboxes).filter(function(uri) {
return uri.substr(-1 * 'sandbox.js'.length) === 'sandbox.js'
}).map(function(uri) uri)[0]
let sandbox = loader.sandboxes[uri];
Object.defineProperties(sandbox.sandbox, {
_: { get: function () result },
__: { get: function () error },
require: { value: function require(id) {
let parts = id.split('/')
let path = id[0] === '/' ? 'file://' + id :
[ parts.shift(), 'lib' ].concat(parts).join('/')
if (path.slice(-3) !== '.js') path = path + '.js'
if (id in loader.modules) path = id
if (path in loader.modules) {
module = loader.modules[path];
} else {
module = loader.modules[path] = {
uri: root + path,
path: path,
id: id,
exports: {},
setExports: function setExports(exports) {
this.exports = exports;
}
}
loader.load(module)
Object.freeze(module)
}
if (typeof(module) === 'function') module = module(this, requirer)
return module.exports
}},
print: { value: function () print.apply(null, arguments) }
});
function print(text) stream.write(text + '\n')
function repl() stream.write(Array.slice(arguments).join('\n' + ps) + '\n')
function prompt() stream.write(ps)
stream.on('data', function (data) {
if (EMPTY_MATCH.test(data)) return prompt()
if (FIN == data) return stream.end()
let isStatement = STATEMENT_MATCH.test(data)
buffer += data
try {
result = sandbox.evaluate(buffer, '@repl')
if (undefined !== result) repl(represent(result))
buffer = ''
ps = NORMAL_PS
} catch (e) {
error = e
if ('SyntaxError' == e.name && !isStatement) {
ps = UNFINISHED_PS
} else {
print(handleError(e))
buffer = ''
ps = NORMAL_PS
}
}
prompt()
})
print('Jetpack repl')
prompt()
}
exports.Shell = Shell