Skip to content

Commit

Permalink
Get rid of String concatenation in checkAndRunStartupShutdownFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-s committed Dec 13, 2018
1 parent 94855a9 commit 5852b5b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.RSource;
import com.oracle.truffle.r.runtime.ReturnException;
import com.oracle.truffle.r.runtime.RootWithBody;
import com.oracle.truffle.r.runtime.RootBodyNode;
import com.oracle.truffle.r.runtime.RootWithBody;
import com.oracle.truffle.r.runtime.ThreadTimings;
import com.oracle.truffle.r.runtime.Utils;
import com.oracle.truffle.r.runtime.Utils.DebugExitException;
Expand Down Expand Up @@ -171,7 +171,7 @@ private void initializeNonShared() {
} catch (ParseException e) {
throw new RInternalError(e, "error while parsing system profile from %s", RProfile.systemProfile().getName());
}
checkAndRunStartupShutdownFunction(".OptRequireMethods");
checkAndRunStartupShutdownFunction(".OptRequireMethods", ".OptRequireMethods()");

suppressWarnings = false;
Source siteProfile = context.stateRProfile.siteProfile();
Expand All @@ -192,37 +192,25 @@ private void initializeNonShared() {
}
if (context.getStartParams().restore()) {
// call sys.load.image(".RData", RCmdOption.QUIET
checkAndRunStartupShutdownFunction("sys.load.image", new String[]{"\".RData\"", context.getStartParams().isQuiet() ? "TRUE" : "FALSE"});
checkAndRunStartupShutdownFunction("sys.load.image", "sys.load.image('.RData'," + (context.getStartParams().isQuiet() ? "TRUE" : "FALSE") + ')');
}
checkAndRunStartupShutdownFunction(".First");
checkAndRunStartupShutdownFunction(".First.sys");
checkAndRunStartupShutdownFunction(".First", ".First()");
checkAndRunStartupShutdownFunction(".First.sys", ".First.sys()");

StartupTiming.timestamp("After Profiles Loaded");
}
}

@Override
public void checkAndRunStartupShutdownFunction(String name, String... args) {
public void checkAndRunStartupShutdownFunction(String name, String code) {
// sanity check: code should be invocation of the function, so it should contain
// "{name}(some-args)"
assert code.contains("(") && code.contains(name);
Object func = REnvironment.globalEnv().findFunction(name);
if (func != null) {
String call = name;
if (args.length == 0) {
call += "()";
} else {
call += "(";
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
call += args[i];
if (i != args.length - 1) {
call += ", ";
}
}
}
call += ")";
}
// Should this print the result?
try {
parseAndEval(RSource.fromTextInternal(call, RSource.Internal.STARTUP_SHUTDOWN), globalFrame, false);
parseAndEval(RSource.fromTextInternal(code, RSource.Internal.STARTUP_SHUTDOWN), globalFrame, false);
} catch (ParseException e) {
throw new RInternalError(e, "error while parsing startup function");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static void stdCleanUp(SA_TYPE saveActionIn, int status, boolean runLast)
* we do not have an efficient way to tell if the global environment is "dirty", so
* we save always
*/
RContext.getEngine().checkAndRunStartupShutdownFunction("sys.save.image", new String[]{"\".RData\""});
RContext.getEngine().checkAndRunStartupShutdownFunction("sys.save.image", "sys.save.image('.RData')");
// TODO: write out history
break;
case NOSAVE:
Expand Down Expand Up @@ -130,9 +130,9 @@ public static void stdCleanUp(SA_TYPE saveActionIn, int status, boolean runLast)
}

private static void runDotLast() {
RContext.getEngine().checkAndRunStartupShutdownFunction(".Last");
RContext.getEngine().checkAndRunStartupShutdownFunction(".Last", ".Last()");
// TODO errors should return to toplevel if interactive
RContext.getEngine().checkAndRunStartupShutdownFunction(".Last.sys");
RContext.getEngine().checkAndRunStartupShutdownFunction(".Last.sys", ".Last.sys()");
}

private static final class UserDefinedCleanUpRootNode extends RootNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ default Object eval(RPairList expr, REnvironment envir, RCaller caller) {

/**
* Checks for the existence of (startup/shutdown) function {@code name} and, if present, invokes
* the function with the given {@code args}.
* it using the given code.
*/
void checkAndRunStartupShutdownFunction(String name, String... args);
void checkAndRunStartupShutdownFunction(String name, String code);

/**
* Wraps the Truffle AST in {@code body} in an anonymous function and returns a
Expand Down

0 comments on commit 5852b5b

Please sign in to comment.