X-Git-Url: http://christophe.rhodes.io/gitweb/?p=swankr.git;a=blobdiff_plain;f=swank.R;h=c6a1ec2f0e0afe13d7e90a3910370bbb3dfe49ef;hp=e51abab78fa2490a7599a3327d3889d793fe9d34;hb=4bae7a87342e7c464e3f2c474656661325db5951;hpb=bcf9cc4d21759e1a5f1af6dfd2bd73aa78d2e238 diff --git a/swank.R b/swank.R index e51abab..c6a1ec2 100644 --- a/swank.R +++ b/swank.R @@ -17,31 +17,79 @@ serve <- function(io) { } mainLoop <- function(io) { - dispatch <- function(event) { - str(event) - kind <- event[[1]] - if(kind == quote(`:emacs-rex`)) { - do.call("emacsRex", event[-1]) - } - } - sendToEmacs <- function(obj) { - payload <- writeSexpToString(obj) - writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL) - writeChar(payload, io, eos=NULL) - flush(io) - cat(sprintf("%06x", nchar(payload)), payload, sep="") + while(TRUE) { + tryCatch(dispatch(io, readPacket(io)), + swankTopLevel=function(c) NULL) } - emacsRex <- function(form, pkg, thread, id) { - value <- do.call(eval(form[[1]]), form[-1]) - sendToEmacs(list(quote(`:return`), list(quote(`:ok`), value), id)) +} + +dispatch <- function(io, event, sldbState=NULL) { + str(event) + kind <- event[[1]] + if(kind == quote(`:emacs-rex`)) { + do.call("emacsRex", c(list(io), list(sldbState), event[-1])) } - +} + +sendToEmacs <- function(io, obj) { + str(obj) + payload <- writeSexpToString(obj) + writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL) + writeChar(payload, io, eos=NULL) + flush(io) + cat(sprintf("%06x", nchar(payload)), payload, sep="") +} + +emacsRex <- function(io, sldbState, form, pkg, thread, id, level=0) { + ok <- FALSE + value <- NULL + tryCatch({ + withCallingHandlers({ + value <- do.call(eval(form[[1]]), c(list(io), list(sldbState), form[-1])) + ok <- TRUE + }, error=function(c) { + newSldbState <- makeSldbState(c, if(is.null(sldbState)) 0 else sldbState$level+1, id) + sldbLoop(io, newSldbState, id) })}, + finally=sendToEmacs(io, list(quote(`:return`), if(ok) list(quote(`:ok`), value) else list(quote(`:abort`)), id))) +} + +makeSldbState <- function(condition, level, id) { + ret <- list(condition=condition, level=level, id=id) + class(ret) <- c("sldbState", class(ret)) + ret +} + +sldbLoop <- function(io, sldbState, id) { + sendToEmacs(io, c(list(quote(`:debug`), id, sldbState$level), debuggerInfoForEmacs(sldbState))) + sendToEmacs(io, list(quote(`:debug-activate`), id, sldbState$level, FALSE)) while(TRUE) { - tryCatch(dispatch(readPacket(io)), - swankTopLevel=NULL) + dispatch(io, readPacket(io), sldbState) } } +debuggerInfoForEmacs <- function(sldbState, from=0, to=NULL) { + backtraceForEmacs <- function() { + calls <- rev(sys.calls()) + if(is.null(to)) to <- length(calls) + from <- from+1 + calls <- lapply(calls[from:to], { frameNumber <- from-1; + function (x) { ret <- list(frameNumber, paste(format(x), sep="", collapse=" ")); frameNumber <<- 1+frameNumber; ret }}) + } + computeRestartsForEmacs <- function () { + lapply(computeRestarts(sldbState$condition), + function(x) { + ## this is all a little bit internalsy + restartName <- x[[1]][[1]] + description <- restartDescription(x) + list(restartName, if(is.null(description)) restartName else description) + }) + } + list(list(as.character(sldbState$condition), sprintf(" [%s]", class(sldbState$condition)[[1]]), FALSE), + computeRestartsForEmacs(), + backtraceForEmacs(), + list(sldbState$id)) +} + readPacket <- function(io) { header <- readChunk(io, 6) len <- strtoi(header, base=16) @@ -164,7 +212,7 @@ writeSexpToString <- function(obj) { } string <- paste(string, ")", sep="") }, "symbol"={ string <- paste(string, as.character(obj), sep="") }, - "logical"={ if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }}, + "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }}, "double"={ string <- paste(string, as.character(obj), sep="") }, "integer"={ string <- paste(string, as.character(obj), sep="") }, stop(paste("can't write object ", obj, sep=""))) @@ -174,7 +222,7 @@ writeSexpToString <- function(obj) { writeSexpToStringLoop(obj) } -`swank:connection-info` <- function () { +`swank:connection-info` <- function (io, sldbState) { list(quote(`:pid`), Sys.getpid(), quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "), quote(`:lisp-implementation`), list(quote(`:type`), "R", @@ -182,16 +230,16 @@ writeSexpToString <- function(obj) { quote(`:version`), paste(R.version$major, R.version$minor, sep="."))) } -`swank:swank-require` <- function (contribs) { +`swank:swank-require` <- function (io, sldbState, contribs) { list() } -`swank:create-repl` <- function(env, ...) { +`swank:create-repl` <- function(io, sldbState, env, ...) { list("R", "R") } -`swank:listener-eval` <- function(string) { - val <- eval(parse(text=string)) +`swank:listener-eval` <- function(io, sldbState, string) { + val <- eval(parse(text=string), envir = globalenv()) f <- fifo("") sink(f) print(val) @@ -200,6 +248,22 @@ writeSexpToString <- function(obj) { list(quote(`:values`), paste(lines, collapse="\n")) } -`swank:autodoc` <- function(rawForm, ...) { +`swank:autodoc` <- function(io, sldbState, rawForm, ...) { "No Arglist Information" } + +`swank:throw-to-toplevel` <- function(io, sldbState) { + condition <- simpleCondition("Throw to toplevel") + class(condition) <- c("swankTopLevel", class(condition)) + signalCondition(condition) +} + +`swank:debugger-info-for-emacs` <- function(io, sldbState, from, to) { + debuggerInfoForEmacs(sldbState, from=from, to=to) +} + +`swank:invoke-nth-restart-for-emacs` <- function(io, sldbState, level, n) { + if(sldbState$level == level) { + invokeRestart(computeRestarts()[[n+1]]) + } +}