Skip to content
Snippets Groups Projects
Commit 38e04878 authored by stepan's avatar stepan
Browse files

More robust approach to handling the .Devices variable

parent c4d87294
Branches
No related tags found
No related merge requests found
...@@ -66,7 +66,7 @@ public final class RGridGraphicsAdapter { ...@@ -66,7 +66,7 @@ public final class RGridGraphicsAdapter {
} }
public static void initialize() { public static void initialize() {
addDevice(NULL_DEVICE); REnvironment.baseEnv().safePut(DOT_DEVICES, RDataFactory.createPairList(NULL_DEVICE));
setCurrentDevice(NULL_DEVICE); setCurrentDevice(NULL_DEVICE);
RContext ctx = RContext.getInstance(); RContext ctx = RContext.getInstance();
ROptions.ContextStateImpl options = ctx.stateROptions; ROptions.ContextStateImpl options = ctx.stateROptions;
...@@ -82,25 +82,41 @@ public final class RGridGraphicsAdapter { ...@@ -82,25 +82,41 @@ public final class RGridGraphicsAdapter {
} }
/** /**
* Fixup .Devices array as someone may have set it to something that is not a (pair) list nor * Fixup .Devices global variable to be a pair list of length at least 1. Non-empty lists are
* RNull, which breaks dev.cur built-in R function and others. * converted to pair lists, anything else causes the variable to be reset back to the initial
* value.
*/ */
@TruffleBoundary @TruffleBoundary
public static void fixupDevicesVariable() { public static RPairList fixupDevicesVariable() {
Object devices = REnvironment.baseEnv().get(DOT_DEVICES); REnvironment baseEnv = REnvironment.baseEnv();
if (!(devices instanceof RPairList || devices instanceof RList)) { Object devices = baseEnv.get(DOT_DEVICES);
// reset the .Devices and .Device variables to initial values if (devices instanceof RPairList) {
REnvironment.baseEnv().safePut(DOT_DEVICES, RNull.instance); return (RPairList) devices;
addDevice(NULL_DEVICE); }
setCurrentDevice(NULL_DEVICE); if (devices instanceof RList) {
RList list = (RList) devices;
if (list.getLength() > 0) {
RPairList head = RDataFactory.createPairList(list.getDataAt(0));
RPairList curr = head;
for (int i = 1; i < list.getLength(); i++) {
RPairList next = RDataFactory.createPairList(list.getDataAt(i));
curr.setCdr(next);
curr = next;
}
baseEnv.safePut(DOT_DEVICES, head);
return head;
}
} }
// reset the .Devices and .Device variables to initial values
RPairList nullDevice = RDataFactory.createPairList(NULL_DEVICE);
baseEnv.safePut(DOT_DEVICES, nullDevice);
setCurrentDevice(NULL_DEVICE);
return nullDevice;
} }
public static void removeDevice(int index) { public static void removeDevice(int index) {
assert index > 0 : "cannot remove null device"; assert index > 0 : "cannot remove null device";
REnvironment baseEnv = REnvironment.baseEnv(); RPairList devices = fixupDevicesVariable();
fixupDevicesVariable();
RPairList devices = (RPairList) baseEnv.get(DOT_DEVICES);
assert index < devices.getLength() : "wrong index in removeDevice"; assert index < devices.getLength() : "wrong index in removeDevice";
RPairList prev = devices; RPairList prev = devices;
for (int i = 0; i < index - 1; ++i) { for (int i = 0; i < index - 1; ++i) {
...@@ -120,12 +136,8 @@ public final class RGridGraphicsAdapter { ...@@ -120,12 +136,8 @@ public final class RGridGraphicsAdapter {
public static void addDevice(String name) { public static void addDevice(String name) {
REnvironment baseEnv = REnvironment.baseEnv(); REnvironment baseEnv = REnvironment.baseEnv();
baseEnv.safePut(DOT_DEVICE, name); baseEnv.safePut(DOT_DEVICE, name);
Object dotDevices = baseEnv.get(DOT_DEVICES); RPairList dotDevices = fixupDevicesVariable();
if (dotDevices instanceof RPairList) { dotDevices.appendToEnd(RDataFactory.createPairList(name));
((RPairList) dotDevices).appendToEnd(RDataFactory.createPairList(name));
} else {
baseEnv.safePut(DOT_DEVICES, RDataFactory.createPairList(name));
}
} }
public static String getDefaultDevice() { public static String getDefaultDevice() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment