+}
+
+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))