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

implemented set/getPrompt in RContext.ConsoleIO

parent 796f4cad
Branches
No related tags found
No related merge requests found
Showing
with 103 additions and 30 deletions
......@@ -97,6 +97,19 @@ public final class EmbeddedConsoleHandler extends DelegatingConsoleHandler {
}
}
@SuppressWarnings("try")
@Override
public String getPrompt() {
try (ContextClose ignored = inContext()) {
if (!isOverridden("R_ReadConsole")) {
return getDelegate().getPrompt();
} else {
// TODO: get prompt
return "";
}
}
}
@Override
public InputStream createInputStream() {
return new InputStream() {
......
......@@ -49,6 +49,13 @@ public abstract class ConsoleHandler {
*/
public abstract void setPrompt(String prompt);
/**
* Get the R prompt.
*
* @return the prompt
*/
public abstract String getPrompt();
public void setContext(@SuppressWarnings("unused") Context context) {
// ignore by default
}
......
......@@ -65,6 +65,11 @@ public class DefaultConsoleHandler extends ConsoleHandler {
this.prompt = prompt;
}
@Override
public String getPrompt() {
return prompt;
}
@Override
public int getCurrentLineIndex() {
return currentLine;
......
......@@ -81,6 +81,11 @@ public class JLineConsoleHandler extends ConsoleHandler {
console.setPrompt(noPrompt ? "" : prompt != null ? prompt : "");
}
@Override
public String getPrompt() {
return console.getPrompt();
}
public void clearHistory() {
history.clear();
}
......
......@@ -37,7 +37,10 @@ import org.graalvm.polyglot.Context.Builder;
import com.oracle.truffle.r.launcher.RCmdOptions.Client;
import com.oracle.truffle.r.launcher.RCmdOptions.RCmdOption;
import static com.oracle.truffle.r.launcher.RCommand.fatal;
import java.io.File;
import java.io.IOException;
import org.graalvm.polyglot.Source;
public abstract class RAbstractLauncher extends AbstractLanguageLauncher implements Closeable {
......@@ -87,6 +90,12 @@ public abstract class RAbstractLauncher extends AbstractLanguageLauncher impleme
this.context = contextBuilder.allowAllAccess(true).allowHostAccess(useJVM).arguments("R", getArguments()).in(consoleHandler.createInputStream()).out(
outStream).err(errStream).build();
this.consoleHandler.setContext(context);
try {
Source src = Source.newBuilder("R", ".fastr.set.consoleHandler", "<set-console-handler>").internal(true).build();
context.eval(src).execute(consoleHandler);
} catch (IOException e) {
throw fatal(e, "error while setting console handler");
}
}
@Override
......
......@@ -55,6 +55,11 @@ class StringConsoleHandler extends ConsoleHandler {
this.prompt = prompt;
}
@Override
public String getPrompt() {
return prompt;
}
@Override
public int getCurrentLineIndex() {
return currentLine;
......
......@@ -131,6 +131,8 @@ import com.oracle.truffle.r.nodes.builtin.fastr.FastRRefCountInfo;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRRefCountInfoNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRRegisterFunctions;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRRegisterFunctionsNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRSetConsoleHandler;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRSetConsoleHandlerNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRSlotAssign;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRSlotAssignNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRSourceInfo;
......@@ -493,6 +495,7 @@ public class BasePackage extends RBuiltinPackage {
add(FastRRefCountInfo.class, FastRRefCountInfoNodeGen::create);
add(FastRPkgSource.class, FastRPkgSourceNodeGen::create);
add(FastRSourceInfo.class, FastRSourceInfoNodeGen::create);
add(FastRSetConsoleHandler.class, FastRSetConsoleHandlerNodeGen::create);
add(FastRStackTrace.class, FastRStackTraceNodeGen::create);
add(FastRSlotAssign.class, FastRSlotAssignNodeGen::create);
add(FastRSyntaxTree.class, FastRSyntaxTreeNodeGen::create);
......
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.nodes.builtin.fastr;
import static com.oracle.truffle.r.runtime.builtins.RBehavior.COMPLEX;
import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
import com.oracle.truffle.r.runtime.builtins.RBuiltin;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.data.RNull;
@RBuiltin(name = ".fastr.set.consoleHandler", kind = PRIMITIVE, parameterNames = {"handler"}, behavior = COMPLEX)
public abstract class FastRSetConsoleHandler extends RBuiltinNode.Arg1 {
static {
Casts.noCasts(FastRSetConsoleHandler.class);
}
@Specialization
@TruffleBoundary
protected RNull setHandler(TruffleObject handler) {
RContext.getInstance().getConsole().setHandler(handler);
return RNull.instance;
}
}
......@@ -948,15 +948,16 @@ public final class RContext {
}
private final Node read = Message.READ.createNode();
private final Node write = Message.WRITE.createNode();
private final Node execute = Message.createExecute(1).createNode();
@TruffleBoundary
public String getPrompt() {
if (handler != null) {
Object result;
try {
result = ForeignAccess.sendRead(read, handler, "prompt");
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
Object f = ForeignAccess.sendRead(read, handler, "getPrompt");
result = ForeignAccess.sendExecute(execute, (TruffleObject) f);
} catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException | ArityException | ClassCastException e) {
throw new RInternalError(e, "error while reading prompt");
}
return (String) result;
......@@ -968,38 +969,14 @@ public final class RContext {
public void setPrompt(String prompt) {
if (handler != null) {
try {
ForeignAccess.sendWrite(write, handler, "prompt", prompt);
} catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException e) {
Object f = ForeignAccess.sendRead(read, handler, "setPrompt");
ForeignAccess.sendExecute(execute, (TruffleObject) f, prompt);
} catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException | ArityException | ClassCastException e) {
throw new RInternalError(e, "error while writing prompt");
}
}
}
@TruffleBoundary
public String getHistory() {
if (handler != null) {
Object result;
try {
result = ForeignAccess.sendRead(read, handler, "history");
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
throw new RInternalError(e, "error while reading history");
}
return (String) result;
}
return "";
}
@TruffleBoundary
public void setHistory(String history) {
if (handler != null) {
try {
ForeignAccess.sendWrite(write, handler, "history", history);
} catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException e) {
throw new RInternalError(e, "error while writing history");
}
}
}
public InputStream getStdin() {
return env.in();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment