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 list(list(as.character(sldbState$condition), sprintf(" [%s]", class(sldbState$condition)[[1]]), FALSE),
79 lapply(computeRestarts(), function(x) list(x[[1]][[1]], x[[1]][[1]])),
82 # lapply(calls[from:to], function(x) paste(format(x), sep="", collapse=" ")))
85 readPacket <- function(io) {
86 header <- readChunk(io, 6)
87 len <- strtoi(header, base=16)
88 payload <- readChunk(io, len)
89 readSexpFromString(payload)
92 readChunk <- function(io, len) {
93 buffer <- readChar(io, len)
94 if(nchar(buffer) != len) {
95 stop("short read in readChunk")
100 readSexpFromString <- function(string) {
104 char <- substr(string, pos, pos)
110 if(pos > nchar(string))
111 stop("EOF during read")
112 obj <- readNumberOrSymbol()
113 if(obj == quote(`.`)) {
114 stop("Consing dot not implemented")
119 skipWhitespace <- function() {
120 while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
124 readList <- function() {
129 char <- substr(string, pos, pos)
135 if(length(obj) == 1 && obj == quote(`.`)) {
136 stop("Consing dot not implemented")
138 ret <- c(ret, list(obj))
143 readString <- function() {
145 addChar <- function(c) { ret <<- paste(ret, c, sep="") }
148 char <- substr(string, pos, pos)
150 "\""={ pos <<- pos + 1; break },
151 "\\"={ pos <<- pos + 1
152 char2 <- substr(string, pos, pos)
156 stop("Unrecognized escape character")) },
161 readNumberOrSymbol <- function() {
163 if(nchar(token)==0) {
164 stop("End of file reading token")
165 } else if(grepl("^[0-9]+$", token)) {
167 } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
173 readToken <- function() {
176 char <- substr(string, pos, pos)
179 } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
182 token <- paste(token, char, sep="")
191 writeSexpToString <- function(obj) {
192 writeSexpToStringLoop <- function(obj) {
194 "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
195 "list"={ string <- paste(string, "(", sep="")
199 string <- paste(string, writeSexpToString(obj[[i]]), sep="")
201 string <- paste(string, " ", sep="")
205 string <- paste(string, ")", sep="") },
206 "symbol"={ string <- paste(string, as.character(obj), sep="") },
207 "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
208 "double"={ string <- paste(string, as.character(obj), sep="") },
209 "integer"={ string <- paste(string, as.character(obj), sep="") },
210 stop(paste("can't write object ", obj, sep="")))
214 writeSexpToStringLoop(obj)
217 `swank:connection-info` <- function (io, sldbState) {
218 list(quote(`:pid`), Sys.getpid(),
219 quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
220 quote(`:lisp-implementation`), list(quote(`:type`), "R",
222 quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
225 `swank:swank-require` <- function (io, sldbState, contribs) {
229 `swank:create-repl` <- function(io, sldbState, env, ...) {
233 `swank:listener-eval` <- function(io, sldbState, string) {
234 val <- eval(parse(text=string))
239 lines <- readLines(f)
240 list(quote(`:values`), paste(lines, collapse="\n"))
243 `swank:autodoc` <- function(io, sldbState, rawForm, ...) {
244 "No Arglist Information"
247 `swank:throw-to-toplevel` <- function(io, sldbState) {
248 condition <- simpleCondition("Throw to toplevel")
249 class(condition) <- c("swankTopLevel", class(condition))
250 signalCondition(condition)
253 `swank:debugger-info-for-emacs` <- function(io, sldbState, from, to) {
254 debuggerInfoForEmacs(sldbState, from=from, to=to)