-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* + routines.cpp * move R functions to a private environment so don't have to fake-namespce them * basic handle_error * initial execute() * rtools.hpp * using evaluate::evaluate * Do more in R execute()
- Loading branch information
1 parent
ef8ab39
commit 0935763
Showing
9 changed files
with
223 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#define R_NO_REMAP | ||
|
||
#include "R.h" | ||
#include "Rinternals.h" | ||
|
||
namespace xeus_r { | ||
namespace r { | ||
|
||
SEXP r_pairlist(SEXP head) { | ||
return Rf_cons(head, R_NilValue); | ||
} | ||
|
||
SEXP r_call(SEXP head) { | ||
return Rf_lcons(head, R_NilValue); | ||
} | ||
|
||
template<class... Types> | ||
SEXP r_pairlist(SEXP head, Types... tail) { | ||
PROTECT(head); | ||
head = Rf_cons(head, r_pairlist(tail...)); | ||
UNPROTECT(1); | ||
return head; | ||
} | ||
|
||
template<class... Types> | ||
SEXP r_call(SEXP head, Types... tail) { | ||
PROTECT(head); | ||
head = Rf_lcons(head, r_pairlist(tail...)); | ||
UNPROTECT(1); | ||
return head; | ||
} | ||
|
||
template<class... Types> | ||
SEXP invoke_xeusr_fn(const char* f, Types... args) { | ||
SEXP sym_xeus_call = Rf_install(".xeus_call"); | ||
|
||
SEXP call = PROTECT(r_call(sym_xeus_call, Rf_mkString(f), args...)); | ||
SEXP result = Rf_eval(call, R_GlobalEnv); | ||
|
||
UNPROTECT(1); | ||
return result; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
publish_stream <- function(name, text) { | ||
invisible(.Call("xeusr_publish_stream", name, text, PACKAGE = "(embedding)")) | ||
} | ||
|
||
handle_message <- function(msg) { | ||
publish_stream("stderr", conditionMessage(msg)) | ||
} | ||
|
||
handle_warning <- function(w) { | ||
call <- conditionCall(w) | ||
call <- if (is.null(call)) '' else sprintf(' in %s', deparse(call)[[1]]) | ||
msg <- sprintf('Warning message%s:\n%s\n', call, dQuote(conditionMessage(w))) | ||
|
||
publish_stream("stderr", msg) | ||
} | ||
|
||
handle_error <- function(e) { | ||
sys_calls <- sys.calls() | ||
stack <- capture.output(traceback(sys_calls, max.lines = 1L)) | ||
|
||
evalue <- paste(conditionMessage(e), collapse = "\n") | ||
trace_back <- c( | ||
cli::col_red("--- Error"), | ||
evalue, | ||
"", | ||
cli::col_red("--- Traceback (most recent call last)"), | ||
stack | ||
) | ||
publish_execution_error(ename = "ERROR", evalue = evalue, trace_back) | ||
} | ||
|
||
handle_value <- function(execution_counter) function(obj, visible) { | ||
if (!visible) return() | ||
|
||
# only doing text-plain for now | ||
data <- list( | ||
"text/plain" = capture.output(print(obj)) | ||
) | ||
|
||
publish_execution_result(execution_counter, data) | ||
} | ||
|
||
handle_graphics <- function(plotobj) { | ||
|
||
} | ||
|
||
publish_execution_error <- function(ename, evalue, trace_back = character()) { | ||
invisible(.Call("xeusr_publish_execution_error", ename, evalue, trace_back)) | ||
} | ||
|
||
publish_execution_result <- function(execution_count, data, metadata = NULL) { | ||
invisible(.Call("xeusr_publish_execution_result", as.integer(execution_count), jsonlite::toJSON(data), jsonlite::toJSON(metadata))) | ||
} | ||
|
||
execute <- function(code, execution_counter) { | ||
parsed <- tryCatch( | ||
parse(text = code, srcfile = glue::glue("[{execution_counter}]")), | ||
error = function(e) { | ||
msg <- paste(conditionMessage(e), collapse = "\n") | ||
publish_execution_error("PARSE ERROR", msg) | ||
e | ||
} | ||
) | ||
if (inherits(parsed, "error")) return() | ||
|
||
output_handler <- evaluate::new_output_handler( | ||
text = function(txt) publish_stream("stdout", txt), | ||
graphics = handle_graphics, | ||
message = handle_message, | ||
warning = handle_warning, | ||
error = handle_error, | ||
value = handle_value(execution_counter) | ||
) | ||
|
||
evaluate::evaluate( | ||
code, | ||
envir = globalenv(), | ||
output_handler = output_handler, | ||
stop_on_error = 1L | ||
) | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,32 @@ | ||
local({ | ||
|
||
options("cli.num_colors" = 256L) | ||
|
||
xeus_env <- if ("tools:xeusr" %in% search()) { | ||
asEnvironment("tools:xeusr") | ||
} else { | ||
attach(new.env(), "tools:xeusr", pos = 2L) | ||
public_env <- new.env() | ||
private_env <- new.env(parent = public_env) | ||
assign(".xeusr_private_env", private_env, envir = public_env) | ||
|
||
attach(public_env, "tools:xeusr", pos = 2L) | ||
} | ||
|
||
xeus_private_env <- get(".xeusr_private_env", envir = xeus_env) | ||
|
||
here <- file.path( | ||
dirname(Sys.which("xr")), | ||
"..", "share", "jupyter", "kernels", "xr", "resources" | ||
) | ||
|
||
files <- setdiff(list.files(here), "setup.R") | ||
|
||
xeus_env$.xeus_call <- function(fn, ...) { | ||
get(fn, envir = xeus_private_env)(...) | ||
} | ||
|
||
for (f in files) { | ||
sys.source(file.path(here, f), envir = xeus_env) | ||
sys.source(file.path(here, f), envir = xeus_private_env) | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#define R_NO_REMAP | ||
|
||
#include "R.h" | ||
#include "Rinternals.h" | ||
#include "R_ext/Rdynload.h" | ||
|
||
#include "xeus-r/xinterpreter.hpp" | ||
#include "nlohmann/json.hpp" | ||
|
||
namespace xeus_r { | ||
namespace routines { | ||
|
||
SEXP publish_stream(SEXP name_, SEXP text_) { | ||
auto name = CHAR(STRING_ELT(name_, 0)); | ||
auto text = CHAR(STRING_ELT(text_, 0)); | ||
xeus_r::get_interpreter()->publish_stream(name, text); | ||
|
||
return R_NilValue; | ||
} | ||
|
||
SEXP publish_execution_error(SEXP ename_, SEXP evalue_, SEXP trace_back_) { | ||
auto ename = CHAR(STRING_ELT(ename_, 0)); | ||
auto evalue = CHAR(STRING_ELT(evalue_, 0)); | ||
|
||
auto n = XLENGTH(trace_back_); | ||
std::vector<std::string> trace_back(n); | ||
for (decltype(n) i = 0; i < n; i++) { | ||
trace_back[i] = CHAR(STRING_ELT(trace_back_, i)); | ||
} | ||
|
||
xeus_r::get_interpreter()->publish_execution_error(ename, evalue, std::move(trace_back)); | ||
|
||
return R_NilValue; | ||
} | ||
|
||
SEXP publish_execution_result(SEXP execution_count_, SEXP data_, SEXP metadata_) { | ||
int execution_count = INTEGER_ELT(execution_count_, 0); | ||
auto data = nl::json::parse(CHAR(STRING_ELT(data_, 0))); | ||
auto metadata = nl::json::parse(CHAR(STRING_ELT(metadata_, 0))); | ||
|
||
xeus_r::get_interpreter()->publish_execution_result( | ||
execution_count, std::move(data), std::move(metadata) | ||
); | ||
|
||
return R_NilValue; | ||
} | ||
|
||
} | ||
|
||
void register_r_routines() { | ||
DllInfo *info = R_getEmbeddingDllInfo(); | ||
|
||
static const R_CallMethodDef callMethods[] = { | ||
{"xeusr_publish_stream" , (DL_FUNC) &routines::publish_stream , 2}, | ||
{"xeusr_publish_execution_error" , (DL_FUNC) &routines::publish_execution_error , 3}, | ||
{"xeusr_publish_execution_result", (DL_FUNC) &routines::publish_execution_result, 3}, | ||
{NULL, NULL, 0} | ||
}; | ||
|
||
R_registerRoutines(info, NULL, callMethods, NULL, NULL); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters