forked from swank-js/swank-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swank-handler.js
459 lines (410 loc) · 13.9 KB
/
swank-handler.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// -*- mode: js2; js-run: "swank-handler-tests.js" -*-
//
// Copyright (c) 2010 Ivan Shvedunov. All rights reserved.
// Copyright (c) 2012 Robert Krahn. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
var EventEmitter = require("events").EventEmitter;
var Script = require('vm').Script;
var evalcx = Script.runInContext;
var util = require("util");
var url = require("url");
var assert = require("assert");
var lisp = require("./lisp");
var S = lisp.S, list = lisp.list, consp = lisp.consp, car = lisp.car, cdr = lisp.cdr,
repr = lisp.repr, fromLisp = lisp.fromLisp, toLisp = lisp.toLisp;
var Completion = require("./completion").Completion;
var DEFAULT_SLIME_VERSION = "2012-02-12";
function Handler (executive) {
this.executive = executive;
var self = this;
this.executive.on("output", function (str) { self.output(str); });
this.executive.on("newPackage", function (name) { self.newPackage(name); });
};
util.inherits(Handler, EventEmitter);
Handler.prototype.receive = function receive (message) {
// FIXME: error handling
console.log("Handler.prototype.receive(): %s", repr(message).replace(/\n/, "\\n"));
if (!consp(message) || car(message) != S(":emacs-rex")) {
console.log("bad message: %s", message);
return;
}
var d, expr;
try {
d = fromLisp(message, ["S:op", ">:form",
["S:name", "R*:args"],
"_:package", "_:threadId", "N:id"]);
} catch (e) {
if (e instanceof TypeError) {
console.log("failed to parse %s: %s", message, e);
return; // FIXME
}
throw e;
}
var r = { status: ":ok", result: null };
var self = this;
var cont = function cont () {
self.sendResponse({ r: r, id: d.id },
[S(":return"), ">:r", ["S:status", "_:result"], "N:id"]);
};
switch (d.form.name) {
case "swank:connection-info":
this.executive.connectionInfo(
function (info) {
console.log("info = %j", info);
r.result = toLisp(
info,
{ "pid": "N:pid",
"encoding": { name: "encoding",
spec: { "coding-systems": "@:codingSystems" } },
"package": { name: "packageSpec", spec: { name: "s", prompt: "s" } },
"lisp-implementation": {
name: "implementation",
spec: { type: "s", name: "s", version: "s" } },
"version": "s:version" });
cont();
});
return;
case "swank:create-repl":
r.result = toLisp(this.executive.createRepl(), ["s:packageName", "s:prompt"]);
break;
case "swank:autodoc":
r.result = S(":not-available");
break;
case "js:list-remotes":
// FIXME: support 'list of similar elements' type spec
r.result = toLisp(
this.executive.listRemotes().map(
function (item) {
return toLisp(item, ["N:index", "K:kind", "s:id", "B:isActive"]);
}, this));
break;
case "js:select-remote":
if (d.form.args.length != 2) {
console.log("bad args len for SWANK:SELECT-REMOTE -- %s", d.form.args.length);
return; // FIXME
}
// FIXME: get rid of spaghetti
var remoteIndex, sticky;
try {
// FIXME: args should be a cons / NIL
remoteIndex = fromLisp(d.form.args[0], "N");
sticky = fromLisp(d.form.args[1]);
} catch (e) {
if (e instanceof TypeError) {
console.log("can't parse arg -- %s", d.form.args[0]);
return; // FIXME
}
throw e;
}
this.executive.selectRemote(remoteIndex, sticky);
break;
case "js:set-target-url":
case "js:set-slime-version":
if (d.form.args.length != 1) {
console.log("bad args len for JS:SET-TARGET-URL -- %s", d.form.args.length);
return; // FIXME
}
try {
expr = fromLisp(d.form.args[0], "s");
} catch (e) {
if (e instanceof TypeError) {
console.log("can't parse arg -- %s", d.form.args[0]);
return; // FIXME
}
throw e;
}
this.executive[d.form.name == "js:set-target-url" ? "setTargetUrl" : "setSlimeVersion"](expr);
break;
case "swank:interactive-eval":
case "swank:interactive-eval-region":
case "swank:listener-eval":
if (d.form.args.length != 1) {
console.log("bad args len for SWANK:LISTENER-EVAL -- %s", d.form.args.length);
return; // FIXME
}
try {
expr = fromLisp(d.form.args[0], "s");
} catch (e) {
if (e instanceof TypeError) {
console.log("can't parse arg -- %s", d.form.args[0]);
return; // FIXME
}
throw e;
}
this.executive.listenerEval(
expr, function (values) {
if (values.length)
r.result = toLisp({ values: values }, [S(":values"), "R:values"]);
cont();
});
return;
case "swank:fuzzy-completions":
// TBD
r.result = toLisp([[], []], "@");
cont();
return;
case "swank:simple-completions":
var prefix = d.form.args[0];
var objStr = fromLisp(prefix, "@");
console.log("Called " + "swank:simple-completions: " + objStr);
this.executive.listenerCompletion(
objStr, function (result) {
r.result = toLisp([ result.values, result.partial ], "@");
cont();
});
return;
default:
// FIXME: handle unknown commands
}
cont();
};
Handler.prototype.output = function output (str) {
this.sendResponse([S(":write-string"), str]);
};
Handler.prototype.newPackage = function newPackage (name) {
this.sendResponse([S(":new-package"), name, name]);
};
Handler.prototype.sendResponse = function sendResponse(response, spec)
{
this.emit("response", repr(toLisp(response, spec || "@")));
};
function Remote () {};
util.inherits(Remote, EventEmitter);
Remote.prototype.prompt = function prompt () {
return "JS";
};
Remote.prototype.kind = function kind () {
throw new Error("must override Remote.prototype.kind()");
};
Remote.prototype.id = function id () {
throw new Error("must override Remote.prototype.id()");
};
Remote.prototype.evaluate = function evaluate (id, str) {
throw new Error("must override Remote.prototype.evaluate()");
};
Remote.prototype.completer = function completer () {
return new Completion();
};
Remote.prototype.completion = function completion (id, str) {
console.log("complete: " + str);
this.sendResult(id, this.completer().complete(str));
};
Remote.prototype.fullName = function fullName () {
return "(" + this.kind() + ") " + this.id();
};
Remote.prototype.disconnect = function disconnect () {
this.emit("disconnect");
};
Remote.prototype.detachSelf = function detachSelf () {
this.removeAllListeners("output");
this.removeAllListeners("disconnect");
this.removeAllListeners("result");
};
Remote.prototype.output = function output (str) {
this.emit("output", String(str));
};
Remote.prototype.setIndex = function setIndex (n) {
this._index = n;
};
Remote.prototype.index = function index () {
return this._index;
};
Remote.prototype.sendResult = function sendResult (id, values) {
this.emit("result", id, values);
};
function DefaultRemote () {
this.context = Script.createContext();
for (var i in global) this.context[i] = global[i];
this.context.module = module;
this.context.require = require;
var self = this;
this.context._swank = {
output: function output (arg) {
self.output(arg);
}
};
}
util.inherits(DefaultRemote, Remote);
DefaultRemote.prototype.completer = function completer () {
return new Completion(
{
global: this.context,
evaluate: function (str) {
return evalcx(str, this.context, "repl");
}.bind(this)
});
};
DefaultRemote.prototype.prompt = function prompt () {
return "NODE";
};
DefaultRemote.prototype.kind = function kind () {
return "direct";
};
DefaultRemote.prototype.id = function id () {
return "node.js";
};
DefaultRemote.prototype.evaluate = function evaluate (id, str) {
var r;
try {
r = evalcx(str, this.context, "repl");
} catch (e) {
r = undefined;
this.output(e.stack);
}
this.sendResult(id, r === undefined ? [] : [util.inspect(r)]);
};
// TBD: rename Executive to Dispatcher
function Executive (options) {
options = options || {};
assert.ok(options.hasOwnProperty("config") && options.config);
this.config = options.config;
this.pid = options.hasOwnProperty("pid") ? options.pid : null;
this.remotes = [];
this.attachRemote(new DefaultRemote());
this.activeRemote = this.remotes[0];
this.pendingRequests = {};
};
util.inherits(Executive, EventEmitter);
Executive.nextId = 1; // request id counter is global in order to avoid inter-connection conflicts
Executive.nextRemoteIndex = 1;
Executive.prototype.attachRemote = function attachRemote (remote) {
assert.ok(this.remotes.indexOf(remote) < 0);
remote.setIndex(Executive.nextRemoteIndex++);
var self = this;
remote.on(
"output", function (str) {
if (remote == self.activeRemote)
self.emit("output", str);
});
remote.on(
"disconnect", function (str) {
self.handleDisconnectRemote(remote);
});
remote.on(
"result", function (id, values) {
if (!self.pendingRequests[id]) {
self.emit("output", "WARNING: received late result from " + remote.fullName() + "\n");
return;
}
try {
self.pendingRequests[id](values);
} finally {
delete self.pendingRequests[id];
}
});
this.remotes.push(remote);
this.emit("output", "Remote attached: " + remote.fullName() + "\n");
this.config.get(
"stickyRemote",
function (stickyRemote) {
if (stickyRemote !== null &&
(!self.activeRemote || self.activeRemote.fullName() != stickyRemote) &&
remote.fullName() == stickyRemote)
self.selectRemote(remote.index(), true, true);
});
};
Executive.prototype.handleDisconnectRemote = function handleDisconnectRemote (remote) {
remote.detachSelf();
var index = this.remotes.indexOf(remote);
if (index < 0) {
this.emit("output", "WARNING: disconnectRemote() called for an unknown remote: " + remote.fullName() + "\n");
return;
}
this.remotes.splice(index, 1);
this.emit("output", "Remote detached: " + remote.fullName() + "\n");
if (remote == this.activeRemote)
this.selectRemote(this.remotes[0].index(), false, true);
};
Executive.prototype.connectionInfo = function connectionInfo (cont) {
var self = this;
var prompt = this.activeRemote.prompt();
this.config.get(
"slimeVersion",
function (slimeVersion) {
cont({ pid: self.pid === null ? process.pid : self.pid,
encoding: { codingSystems: [ "utf-8-unix" ] },
packageSpec: { name: prompt, prompt: prompt },
implementation: { type: "JS", name: "JS", version: "1.5" },
version: slimeVersion || DEFAULT_SLIME_VERSION });
});
};
Executive.prototype.createRepl = function createRepl () {
var prompt = this.activeRemote.prompt();
return { packageName: prompt, prompt: prompt };
};
Executive.prototype.listenerEval = function listenerEval (str, cont) {
var id = Executive.nextId++;
this.pendingRequests[id] = cont;
this.activeRemote.evaluate(id, str);
};
Executive.prototype.listenerCompletion = function listenerCompletion(str, cont) {
var id = Executive.nextId++;
this.pendingRequests[id] = cont;
this.activeRemote.completion(id, str);
};
Executive.prototype.listRemotes = function listRemotes () {
return this.remotes.map(
function (remote) {
return { index: remote.index(), kind: remote.kind(), id: remote.id(),
isActive: remote === this.activeRemote };
}, this);
};
Executive.prototype.selectRemote = function selectRemote (index, sticky, auto) {
// TBD: sticky support (should autoselect the remote with message upon attachment)
for (var i = 0; i < this.remotes.length; ++i) {
var remote = this.remotes[i];
if (remote.index() == index) {
if (remote == this.activeRemote) {
this.emit("output", "WARNING: remote already selected: " + remote.fullName() + "\n");
return;
}
this.activeRemote = remote;
if (!auto)
this.config.set("stickyRemote", sticky ? remote.fullName() : null);
this.emit("newPackage", remote.prompt());
this.emit("output", "Remote selected" + (auto ? " (auto)" : sticky ? " (sticky)" : "") +
": " + remote.fullName() + "\n");
return;
}
}
this.emit("output", "WARNING: bad remote index\n");
};
Executive.prototype.setTargetUrl = function setTargetUrl (targetUrl) {
var parsedUrl = null;
try {
parsedUrl = url.parse(targetUrl);
} catch (e) {}
if (parsedUrl && parsedUrl.hostname)
this.config.set("targetUrl", targetUrl);
else
this.emit("output", "WARNING: the URL must contain host and port\n");
};
Executive.prototype.setSlimeVersion = function setSlimeVersion (slimeVersion) {
this.config.set("slimeVersion", slimeVersion);
};
exports.Handler = Handler;
exports.Remote = Remote;
exports.Executive = Executive;