1 swank <- function(port=4005) {
2 acceptConnections(port, FALSE)
5 startSwank <- function(portFile) {
6 acceptConnections(FALSE, portFile)
9 acceptConnections <- function(port, portFile) {
10 s <- socketConnection(host="localhost", server=TRUE, port=port, open="r+b")
15 serve <- function(io) {
19 mainLoop <- function(io) {
21 tryCatch(dispatch(io, readPacket(io)),
22 swankTopLevel=function(c) NULL)
26 dispatch <- function(io, event, sldbState=NULL) {
29 if(kind == quote(`:emacs-rex`)) {
30 do.call("emacsRex", c(list(io), list(sldbState), event[-1]))
34 sendToEmacs <- function(io, obj) {
36 payload <- writeSexpToString(obj)
37 writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL)
38 writeChar(payload, io, eos=NULL)
40 cat(sprintf("%06x", nchar(payload)), payload, sep="")
43 emacsRex <- function(io, sldbState, form, pkg, thread, id, level=0) {
48 value <- do.call(eval(form[[1]]), c(list(io), list(sldbState), form[-1]))
50 }, error=function(c) {
51 newSldbState <- makeSldbState(c, if(is.null(sldbState)) 0 else sldbState$level+1, id)
52 sldbLoop(io, newSldbState, id) })},
53 finally=sendToEmacs(io, list(quote(`:return`), if(ok) list(quote(`:ok`), value) else list(quote(`:abort`)), id)))
56 makeSldbState <- function(condition, level, id) {
57 ret <- list(condition=condition, level=level, id=id)
58 class(ret) <- c("sldbState", class(ret))
62 sldbLoop <- function(io, sldbState, id) {
63 sendToEmacs(io, c(list(quote(`:debug`), id, sldbState$level), debuggerInfoForEmacs(sldbState)))
64 sendToEmacs(io, list(quote(`:debug-activate`), id, sldbState$level, FALSE))
66 dispatch(io, readPacket(io), sldbState)
70 debuggerInfoForEmacs <- function(sldbState, from=0, to=NULL) {
71 backtraceForEmacs <- function() {
73 if(is.null(to)) to <- length(calls)
75 calls <- lapply(calls[from:to], { frameNumber <- from-1;
76 function (x) { ret <- list(frameNumber, paste(format(x), sep="", collapse=" ")); frameNumber <<- 1+frameNumber; ret }})
78 computeRestartsForEmacs <- function () {
79 lapply(computeRestarts(sldbState$condition),
81 ## this is all a little bit internalsy
82 restartName <- x[[1]][[1]]
83 description <- restartDescription(x)
84 list(restartName, if(is.null(description)) restartName else description)
87 list(list(as.character(sldbState$condition), sprintf(" [%s]", class(sldbState$condition)[[1]]), FALSE),
88 computeRestartsForEmacs(),
93 readPacket <- function(io) {
94 header <- readChunk(io, 6)
95 len <- strtoi(header, base=16)
96 payload <- readChunk(io, len)
97 readSexpFromString(payload)
100 readChunk <- function(io, len) {
101 buffer <- readChar(io, len)
102 if(nchar(buffer) != len) {
103 stop("short read in readChunk")
108 readSexpFromString <- function(string) {
112 char <- substr(string, pos, pos)
118 if(pos > nchar(string))
119 stop("EOF during read")
120 obj <- readNumberOrSymbol()
121 if(obj == quote(`.`)) {
122 stop("Consing dot not implemented")
127 skipWhitespace <- function() {
128 while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
132 readList <- function() {
137 char <- substr(string, pos, pos)
143 if(length(obj) == 1 && obj == quote(`.`)) {
144 stop("Consing dot not implemented")
146 ret <- c(ret, list(obj))
151 readString <- function() {
153 addChar <- function(c) { ret <<- paste(ret, c, sep="") }
156 char <- substr(string, pos, pos)
158 "\""={ pos <<- pos + 1; break },
159 "\\"={ pos <<- pos + 1
160 char2 <- substr(string, pos, pos)
164 stop("Unrecognized escape character")) },
169 readNumberOrSymbol <- function() {
171 if(nchar(token)==0) {
172 stop("End of file reading token")
173 } else if(grepl("^[0-9]+$", token)) {
175 } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
181 readToken <- function() {
184 char <- substr(string, pos, pos)
187 } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
190 token <- paste(token, char, sep="")
199 writeSexpToString <- function(obj) {
200 writeSexpToStringLoop <- function(obj) {
202 "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
203 "list"={ string <- paste(string, "(", sep="")
207 string <- paste(string, writeSexpToString(obj[[i]]), sep="")
209 string <- paste(string, " ", sep="")
213 string <- paste(string, ")", sep="") },
214 "symbol"={ string <- paste(string, as.character(obj), sep="") },
215 "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
216 "double"={ string <- paste(string, as.character(obj), sep="") },
217 "integer"={ string <- paste(string, as.character(obj), sep="") },
218 stop(paste("can't write object ", obj, sep="")))
222 writeSexpToStringLoop(obj)
225 `swank:connection-info` <- function (io, sldbState) {
226 list(quote(`:pid`), Sys.getpid(),
227 quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
228 quote(`:lisp-implementation`), list(quote(`:type`), "R",
230 quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
233 `swank:swank-require` <- function (io, sldbState, contribs) {
237 `swank:create-repl` <- function(io, sldbState, env, ...) {
241 `swank:listener-eval` <- function(io, sldbState, string) {
242 val <- eval(parse(text=string))
247 lines <- readLines(f)
248 list(quote(`:values`), paste(lines, collapse="\n"))
251 `swank:autodoc` <- function(io, sldbState, rawForm, ...) {
252 "No Arglist Information"
255 `swank:throw-to-toplevel` <- function(io, sldbState) {
256 condition <- simpleCondition("Throw to toplevel")
257 class(condition) <- c("swankTopLevel", class(condition))
258 signalCondition(condition)
261 `swank:debugger-info-for-emacs` <- function(io, sldbState, from, to) {
262 debuggerInfoForEmacs(sldbState, from=from, to=to)