Skip to content
Snippets Groups Projects
Commit adb36381 authored by Tomas Stupka's avatar Tomas Stupka
Browse files

[GR-2798] Adjusted graalvm package to changes in interop.

PullRequest: fastr/1480
parents 757442b0 de6ff121
No related branches found
No related tags found
No related merge requests found
...@@ -59,7 +59,7 @@ ping <- function() { ...@@ -59,7 +59,7 @@ ping <- function() {
} }
#' Start the GraalVM agent. The agent is normally started automatically upon the first #' Start the GraalVM agent. The agent is normally started automatically upon the first
#' code ecxecution. #' code execution.
#' @export #' @export
graalvm.start <- function() { graalvm.start <- function() {
if (!ping()) { if (!ping()) {
...@@ -70,14 +70,14 @@ graalvm.start <- function() { ...@@ -70,14 +70,14 @@ graalvm.start <- function() {
serverScriptPath <- attr(packageDescription("graalvm"), "file") serverScriptPath <- attr(packageDescription("graalvm"), "file")
serverScriptPath <- substr(serverScriptPath, 1, nchar(serverScriptPath)-16) serverScriptPath <- substr(serverScriptPath, 1, nchar(serverScriptPath)-16)
serverScriptPath <- paste0(serverScriptPath, "data/server.js") serverScriptPath <- paste0(serverScriptPath, "data/server.js")
libEnvVar <- paste0("R_LIBS=", getOption("graalvm.rlibs")) envVars <- paste0("R_LIBS=", getOption("graalvm.rlibs"), "; R_HOME=", gHome, "/jre/languages/R")
gHost <- getOption("graalvm.host"); gHost <- getOption("graalvm.host");
gPort <- getOption("graalvm.port"); gPort <- getOption("graalvm.port");
javaOpts <- getOption("graalvm.javaOpts"); javaOpts <- getOption("graalvm.javaOpts");
nodeLaunchCmd <- paste0(libEnvVar, " ", gHome, "/bin/node ", javaOpts, " ", serverScriptPath, " ", gHost, " ", gPort, " &") nodeLaunchCmd <- paste0(envVars, " ", gHome, "/bin/node --jvm --polyglot", javaOpts, " ", serverScriptPath, " ", gHost, " ", gPort, " &")
system(nodeLaunchCmd, ignore.stdout = TRUE, ignore.stderr = TRUE) system(nodeLaunchCmd, ignore.stdout = TRUE, ignore.stderr = TRUE)
attempts <- 0L attempts <- 0L
...@@ -114,27 +114,27 @@ graalvm.stop <- function() { ...@@ -114,27 +114,27 @@ graalvm.stop <- function() {
} }
#' Execute code by GraalVM using the language interpreter that corresponds #' Execute code by GraalVM using the language interpreter that corresponds
#' to the language mimetype. #' to the language languageId.
#' #'
#' @param code the code to be executed. It must be a language element as long as the target #' @param code the code to be executed. It must be a language element as long as the target
#' language is R, otherwise it must be a string. #' language is R, otherwise it must be a string.
#' @param echo controls whether this function returns the result of the interpreted code. #' @param echo controls whether this function returns the result of the interpreted code.
#' The default value is TRUE. #' The default value is TRUE.
#' @param mimetype The mimetype of the target language. Currently supported values are #' @param languageId The languageId of the target language. Currently supported values are
#' "application/x-r", "text/javascript" and "application/x-ruby". #' "R", "js" and "ruby".
#' @family execution functions #' @family execution functions
#' @examples #' @examples
#' g(runif(10^3)) #' g(runif(10^3))
#' g(runif(10^8), echo = FALSE) # We do not want that the result is returned due to its size #' g(runif(10^8), echo = FALSE) # We do not want that the result is returned due to its size
#' g("1 < 2", mimetype = "text/javascript") #' g("1 < 2", languageId = "js")
#' @export #' @export
g <- function(code, echo = TRUE, mimetype = "application/x-r") { g <- function(code, echo = TRUE, languageId = "R") {
if (mimetype == "application/x-r") { if (languageId == "R") {
code <- deparse(substitute(code)) code <- deparse(substitute(code))
} else { } else {
if (!is.character(code)) stop("The code argument must a character vector") if (!is.character(code)) stop("The code argument must a character vector")
} }
send(code, echo, mimetype) send(code, echo, languageId)
} }
#' Execute R code. #' Execute R code.
...@@ -144,7 +144,7 @@ g <- function(code, echo = TRUE, mimetype = "application/x-r") { ...@@ -144,7 +144,7 @@ g <- function(code, echo = TRUE, mimetype = "application/x-r") {
#' @export #' @export
g.r <- function(code, echo = TRUE) { g.r <- function(code, echo = TRUE) {
if (!is.character(code)) stop("The code argument must a character vector") if (!is.character(code)) stop("The code argument must a character vector")
send(code, echo, "application/x-r") send(code, echo, "R")
} }
#' Execute JavaScript code. #' Execute JavaScript code.
...@@ -154,7 +154,7 @@ g.r <- function(code, echo = TRUE) { ...@@ -154,7 +154,7 @@ g.r <- function(code, echo = TRUE) {
#' @export #' @export
g.js <- function(code, echo = TRUE) { g.js <- function(code, echo = TRUE) {
if (!is.character(code)) stop("The code argument must a character vector") if (!is.character(code)) stop("The code argument must a character vector")
send(code, echo, "text/javascript") send(code, echo, "js")
} }
#' Execute Ruby code. #' Execute Ruby code.
...@@ -164,7 +164,7 @@ g.js <- function(code, echo = TRUE) { ...@@ -164,7 +164,7 @@ g.js <- function(code, echo = TRUE) {
#' @export #' @export
g.rb <- function(code, echo = TRUE) { g.rb <- function(code, echo = TRUE) {
if (!is.character(code)) stop("The code argument must a character vector") if (!is.character(code)) stop("The code argument must a character vector")
send(code, echo, "application/x-ruby") send(code, echo, "ruby")
} }
#' Assign a value to a paired variable. The value is assigned both locally and remotely. #' Assign a value to a paired variable. The value is assigned both locally and remotely.
...@@ -194,10 +194,10 @@ gset <- function(var, value = var) { ...@@ -194,10 +194,10 @@ gset <- function(var, value = var) {
stop(paste("Unpaired variable ", varName)) stop(paste("Unpaired variable ", varName))
} }
deparsedValue = NULL deparsedValue = NULL
if (meta$mimetype == "application/x-r") { if (meta$languageId == "R") {
deparsedValue <- paste(deparse(substitute(value)), collapse="\n") deparsedValue <- paste(deparse(substitute(value)), collapse="\n")
} }
setVar(varName, value, deparsedValue, meta$mimetype) setVar(varName, value, deparsedValue, meta$languageId)
} else { } else {
stop(paste("Undefined variable ", varName)) stop(paste("Undefined variable ", varName))
} }
...@@ -207,19 +207,19 @@ gset <- function(var, value = var) { ...@@ -207,19 +207,19 @@ gset <- function(var, value = var) {
#' #'
#' @family paired variables #' @family paired variables
#' @export #' @export
gset.r <- function(var, value) setVar(deparse(substitute(var)), value, paste(deparse(substitute(value)), collapse="\n"), "application/x-r") gset.r <- function(var, value) setVar(deparse(substitute(var)), value, paste(deparse(substitute(value)), collapse="\n"), "R")
#' Assign the value to the paired variable in Graal JS and locally. #' Assign the value to the paired variable in Graal JS and locally.
#' #'
#' @family paired variables #' @family paired variables
#' @export #' @export
gset.js <- function(var, value) setVar(deparse(substitute(var)), value, NULL, "text/javascript") gset.js <- function(var, value) setVar(deparse(substitute(var)), value, NULL, "js")
#' Assign the value to the paired variable in Graal Ruby and locally. #' Assign the value to the paired variable in Graal Ruby and locally.
#' #'
#' @family paired variables #' @family paired variables
#' @export #' @export
gset.rb <- function(var, value) setVar(deparse(substitute(var)), value, NULL, "application/x-ruby") gset.rb <- function(var, value) setVar(deparse(substitute(var)), value, NULL, "ruby")
#' Retrieve the variable defined in a GraalVM language. The local variable must have been #' Retrieve the variable defined in a GraalVM language. The local variable must have been
#' initialized by one of the language specific gget.* or ggset.* functions. #' initialized by one of the language specific gget.* or ggset.* functions.
...@@ -233,7 +233,7 @@ gget <- function(var) { ...@@ -233,7 +233,7 @@ gget <- function(var) {
if (is.null(meta)) { if (is.null(meta)) {
stop(paste("Unpaired variable ", varName)) stop(paste("Unpaired variable ", varName))
} }
getVar(varName, meta$mimetype) getVar(varName, meta$languageId)
} else { } else {
stop(paste("Undefined variable ", varName)) stop(paste("Undefined variable ", varName))
} }
...@@ -243,95 +243,95 @@ gget <- function(var) { ...@@ -243,95 +243,95 @@ gget <- function(var) {
#' #'
#' @family paired variables #' @family paired variables
#' @export #' @export
gget.r <- function(var) getVar(deparse(substitute(var)), "application/x-r") gget.r <- function(var) getVar(deparse(substitute(var)), "R")
#' Retrieve the variable defined in GraalVM JS. #' Retrieve the variable defined in GraalVM JS.
#' #'
#' @family paired variables #' @family paired variables
#' @export #' @export
gget.js <- function(var) getVar(deparse(substitute(var)), "text/javascript") gget.js <- function(var) getVar(deparse(substitute(var)), "js")
#' Retrieve the variable defined in GraalVM Ruby. #' Retrieve the variable defined in GraalVM Ruby.
#' #'
#' @family paired variables #' @family paired variables
#' @export #' @export
gget.rb <- function(var) getVar(deparse(substitute(var)), "application/x-ruby") gget.rb <- function(var) getVar(deparse(substitute(var)), "ruby")
setVar <- function(varName, value, deparsedValue, mimetype="application/x-r") { setVar <- function(varName, value, deparsedValue, languageId="R") {
localValue <- value localValue <- value
meta <- NULL meta <- NULL
if (exists(varName)) { if (exists(varName)) {
meta <- attr(var, "graalvm") meta <- attr(var, "graalvm")
} }
if (is.null(meta)) { if (is.null(meta)) {
meta <- list(varName = varName, mimetype = mimetype) meta <- list(varName = varName, languageId = languageId)
attr(localValue, "graalvm") <- meta attr(localValue, "graalvm") <- meta
} }
if (meta$mimetype == "application/x-r") { if (meta$languageId == "R") {
code <- paste0(meta$varName, "<-", deparsedValue) code <- paste0(meta$varName, "<-", deparsedValue)
} else if (meta$mimetype == "text/javascript") { } else if (meta$languageId == "js") {
code <- paste0(meta$varName, "=", toJSON(value)) code <- paste0(meta$varName, "=", toJSON(value))
} else if (meta$mimetype == "application/x-ruby") { } else if (meta$languageId == "ruby") {
code <- paste0("$", meta$varName, "=", toJSON(value)) code <- paste0("$", meta$varName, "=", toJSON(value))
} else { } else {
stop(paste("Unsupported language mimetype:", mimetype)) stop(paste("Unsupported language languageId:", languageId))
} }
send(code, FALSE, meta$mimetype) send(code, FALSE, meta$languageId)
assign(varName, localValue, inherits = TRUE) assign(varName, localValue, inherits = TRUE)
} }
getVar <- function(varName, mimetype="application/x-r") { getVar <- function(varName, languageId="R") {
meta <- NULL meta <- NULL
if (exists(varName)) { if (exists(varName)) {
meta <- attr(var, "graalvm") meta <- attr(var, "graalvm")
} }
if (is.null(meta)) { if (is.null(meta)) {
meta <- list(varName = varName, mimetype = mimetype) meta <- list(varName = varName, languageId = languageId)
} }
if (meta$mimetype == "application/x-r") { if (meta$languageId == "R") {
code <- meta$varName code <- meta$varName
} else if (meta$mimetype == "text/javascript") { } else if (meta$languageId == "js") {
code <- meta$varName code <- meta$varName
} else if (meta$mimetype == "application/x-ruby") { } else if (meta$languageId == "ruby") {
code <- paste0("$", meta$varName) code <- paste0("$", meta$varName)
} else { } else {
stop(paste("Unsupported language mimetype:", mimetype)) stop(paste("Unsupported language languageId:", languageId))
} }
value <- send(code, TRUE, meta$mimetype) value <- send(code, TRUE, meta$languageId)
if (!is.null(value)) { if (!is.null(value)) {
meta <- list("varName" = varName, "mimetype" = mimetype) meta <- list("varName" = varName, "languageId" = languageId)
attr(value, "graalvm") <- meta attr(value, "graalvm") <- meta
} }
assign(varName, value, inherits = TRUE) assign(varName, value, inherits = TRUE)
} }
send <- function(code, echo, mimetype) { send <- function(code, echo, languageId) {
tryCatch(sendAttempt(code, echo, mimetype), error = function(e) { tryCatch(sendAttempt(code, echo, languageId), error = function(e) {
# try to restart the agent and invoke it agains # try to restart the agent and invoke it agains
graalvm.start() graalvm.start()
sendAttempt(code, echo, mimetype) sendAttempt(code, echo, languageId)
}) })
} }
sendAttempt <- function(code, echo, mimetype) { sendAttempt <- function(code, echo, languageId) {
code <- paste(code, collapse="\n") code <- paste(code, collapse="\n")
if (!graalvmEnv$status) { if (!graalvmEnv$status) {
graalvm.start() graalvm.start()
} }
h <- new_handle(failonerror = FALSE) h <- new_handle(failonerror = FALSE)
handle_setform(h, code=code, echo=as.character(echo), mimetype=mimetype) handle_setform(h, code=code, echo=as.character(echo), languageId=languageId)
url <- commandURL("") url <- commandURL("")
resp <- curl_fetch_memory(url, handle = h) resp <- curl_fetch_memory(url, handle = h)
respData <- rawToChar(resp$content) respData <- rawToChar(resp$content)
respData <- strsplit(respData, "\r\n")[[1]] respData <- strsplit(respData, "\r\n")[[1]]
if (mimetype == "application/x-r") { if (languageId == "R") {
respObj <- eval(parse(text=respData)) respObj <- eval(parse(text=respData))
} else if (mimetype == "text/javascript") { } else if (languageId == "js") {
respObj <- fromJSON(respData) respObj <- fromJSON(respData)
} else if (mimetype == "application/x-ruby") { } else if (languageId == "ruby") {
respObj <- fromJSON(respData) respObj <- fromJSON(respData)
} }
......
...@@ -33,5 +33,5 @@ def toJSON(res) ...@@ -33,5 +33,5 @@ def toJSON(res)
Truffle::Interop.to_java_string(res.to_json) Truffle::Interop.to_java_string(res.to_json)
end end
Truffle::Interop.export('storeExpr', method(:storeExpr)) Polyglot.export('storeExpr', method(:storeExpr))
Truffle::Interop.export('rubyToJSON', method(:toJSON)) Polyglot.export('rubyToJSON', method(:toJSON))
\ No newline at end of file \ No newline at end of file
...@@ -73,19 +73,19 @@ function evalJS(code, echo) { ...@@ -73,19 +73,19 @@ function evalJS(code, echo) {
} }
var rHandlerScript = fs.readFileSync( __dirname + "/handler.fr", "utf8"); var rHandlerScript = fs.readFileSync( __dirname + "/handler.fr", "utf8");
Interop.eval("application/x-r", rHandlerScript); Polyglot.eval("R", rHandlerScript);
rParser = Interop.import('parser'); rParser = Polyglot.import('parser');
rResult = Interop.import('result'); rResult = Polyglot.import('result');
rIsError = Interop.import('isError'); rIsError = Polyglot.import('isError');
deparseObject = Interop.import('deparseObject'); deparseObject = Polyglot.import('deparseObject');
function evalR(code, echo) { function evalR(code, echo) {
rParser(code); rParser(code);
if (echo) { if (echo) {
Interop.eval("application/x-r", "err <- TRUE; out <- tryCatch({ err <- TRUE; r <- eval(exp); err <- FALSE; r }, error = function(e) e$message)"); Polyglot.eval("R", "err <- TRUE; out <- tryCatch({ err <- TRUE; r <- eval(exp); err <- FALSE; r }, error = function(e) e$message)");
} else { } else {
Interop.eval("application/x-r", "err <- TRUE; out <- tryCatch({ err <- TRUE; eval(exp); err <- FALSE; NULL }, error = function(e) e$message)"); Polyglot.eval("R", "err <- TRUE; out <- tryCatch({ err <- TRUE; eval(exp); err <- FALSE; NULL }, error = function(e) e$message)");
} }
var res = {} var res = {}
res.data = rResult(); res.data = rResult();
...@@ -94,9 +94,9 @@ function evalR(code, echo) { ...@@ -94,9 +94,9 @@ function evalR(code, echo) {
} }
var rubyHandlerScript = fs.readFileSync( __dirname + "/handler.rb", "utf8"); var rubyHandlerScript = fs.readFileSync( __dirname + "/handler.rb", "utf8");
Interop.eval("application/x-ruby", rubyHandlerScript); Polyglot.eval("ruby", rubyHandlerScript);
rubyStoreExpr = Interop.import('storeExpr'); rubyStoreExpr = Polyglot.import('storeExpr');
rubyToJSON = Interop.import('rubyToJSON'); rubyToJSON = Polyglot.import('rubyToJSON');
function evalRuby(code, echo) { function evalRuby(code, echo) {
rubyStoreExpr(code); rubyStoreExpr(code);
...@@ -107,9 +107,9 @@ function evalRuby(code, echo) { ...@@ -107,9 +107,9 @@ function evalRuby(code, echo) {
-1 -1
end`; end`;
if (echo) { if (echo) {
r = Interop.eval("application/x-ruby", code); r = Polyglot.eval("ruby", code);
} else { } else {
r = Interop.eval("application/x-ruby", code); r = Polyglot.eval("ruby", code);
} }
var res = {} var res = {}
rJSON = rubyToJSON(r); rJSON = rubyToJSON(r);
...@@ -137,16 +137,16 @@ function processPost(request, response) { ...@@ -137,16 +137,16 @@ function processPost(request, response) {
//console.log("Params: " + JSON.stringify(params)); //console.log("Params: " + JSON.stringify(params));
var echo = params.echo === "TRUE"; var echo = params.echo === "TRUE";
var res; var res;
if (params.mimetype == "application/x-r") { if (params.languageId == "R") {
res = evalR(params.code, echo); res = evalR(params.code, echo);
} else if (params.mimetype == "text/javascript") { } else if (params.languageId == "js") {
res = evalJS(params.code, echo); res = evalJS(params.code, echo);
} else if (params.mimetype == "application/x-ruby") { } else if (params.languageId == "ruby") {
res = evalRuby(params.code, echo); res = evalRuby(params.code, echo);
} else { } else {
res = { res = {
isError : true, isError : true,
data : deparseObject("Unsupported language: " + params.mimetype) data : deparseObject("Unsupported language: " + params.languageId)
} }
} }
if (res.isError) { if (res.isError) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment