Skip to content

Commit f88fb39

Browse files
committed
[GR-13204] GitHub issue #46: ClassCastException in pdftools package.
PullRequest: fastr/1860
2 parents 679d443 + eae16ce commit f88fb39

File tree

23 files changed

+176
-114
lines changed

23 files changed

+176
-114
lines changed

com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/interop/RAbstractVectorAccessFactory.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
2323
package com.oracle.truffle.r.engine.interop;
2424

2525
import java.util.List;
26+
import java.util.function.Supplier;
2627

2728
import com.oracle.truffle.api.CallTarget;
2829
import com.oracle.truffle.api.CompilerDirectives;
@@ -472,7 +473,16 @@ public Object execute(VirtualFrame frame) {
472473
});
473474
}
474475

475-
static class Check extends RootNode {
476+
public static final Supplier<RootNode> CHECK_FACTORY = new CheckFactory();
477+
478+
private static final class CheckFactory implements Supplier<RootNode> {
479+
@Override
480+
public RootNode get() {
481+
return new Check();
482+
}
483+
}
484+
485+
private static final class Check extends RootNode {
476486

477487
Check() {
478488
super(null);

com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/interop/RForeignAccessFactoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -122,7 +122,7 @@ public ForeignAccess getForeignAccess(RTruffleObject obj) {
122122
} else if (obj instanceof RInteropNA) {
123123
return RInteropNAMRForeign.ACCESS;
124124
} else if (obj instanceof RAbstractAtomicVector) {
125-
return ForeignAccess.create(new RAbstractVectorAccessFactory(), new RAbstractVectorAccessFactory.Check());
125+
return ForeignAccess.createAccess(new RAbstractVectorAccessFactory(), RAbstractVectorAccessFactory.CHECK_FACTORY);
126126
} else {
127127
ForeignAccess access = FFI_RForeignAccessFactoryImpl.getForeignAccess(obj);
128128
if (access != null) {

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -320,35 +320,8 @@ public Object Rf_getAttrib(Object obj, Object name) {
320320
}
321321

322322
@Override
323-
@TruffleBoundary
324323
public void Rf_setAttrib(Object obj, Object name, Object val) {
325-
if (obj == RNull.instance) {
326-
return;
327-
}
328-
if (obj instanceof RAttributable) {
329-
RAttributable attrObj = (RAttributable) obj;
330-
String nameAsString;
331-
if (name instanceof RSymbol) {
332-
nameAsString = ((RSymbol) name).getName();
333-
} else {
334-
nameAsString = RRuntime.asString(name);
335-
assert nameAsString != null;
336-
}
337-
nameAsString = Utils.intern(nameAsString);
338-
if (val == RNull.instance) {
339-
if ("class" == nameAsString) {
340-
removeClassAttr(attrObj);
341-
} else {
342-
removeAttr(attrObj, nameAsString);
343-
}
344-
} else if ("class" == nameAsString) {
345-
attrObj.initAttributes().define(nameAsString, val);
346-
} else {
347-
attrObj.setAttr(nameAsString, val);
348-
}
349-
} else {
350-
throw RInternalError.shouldNotReachHere();
351-
}
324+
throw implementedAsNode();
352325
}
353326

354327
@TruffleBoundary

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/AttributesAccessNodes.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,15 +34,18 @@
3434
import com.oracle.truffle.api.profiles.ConditionProfile;
3535
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodesFactory.ATTRIBNodeGen;
3636
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodesFactory.CopyMostAttribNodeGen;
37+
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodesFactory.RfSetAttribNodeGen;
3738
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodesFactory.SetAttribNodeGen;
3839
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodesFactory.TAGNodeGen;
3940
import com.oracle.truffle.r.nodes.attributes.CopyOfRegAttributesNode;
4041
import com.oracle.truffle.r.nodes.attributes.GetAttributeNode;
4142
import com.oracle.truffle.r.nodes.attributes.GetAttributesNode;
43+
import com.oracle.truffle.r.nodes.attributes.RemoveAttributeNode;
4244
import com.oracle.truffle.r.nodes.attributes.SetAttributeNode;
4345
import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.GetRowNamesAttributeNode;
4446
import com.oracle.truffle.r.nodes.function.opt.UpdateShareableChildValueNode;
4547
import com.oracle.truffle.r.nodes.unary.CastNode;
48+
import com.oracle.truffle.r.nodes.unary.InternStringNode;
4649
import com.oracle.truffle.r.runtime.ArgumentsSignature;
4750
import com.oracle.truffle.r.runtime.RError;
4851
import com.oracle.truffle.r.runtime.RError.Message;
@@ -91,10 +94,58 @@ private String castToString(Object name) {
9194
}
9295
}
9396

97+
/**
98+
* Sets a single attribute.
99+
*/
100+
public abstract static class RfSetAttribNode extends FFIUpCallNode.Arg3 {
101+
@Child private CastNode castNameNode;
102+
103+
public static RfSetAttribNode create() {
104+
return RfSetAttribNodeGen.create();
105+
}
106+
107+
@Specialization(guards = "!isNull(value)")
108+
protected Object setValue(RAttributable target, Object name, Object value,
109+
@Cached("create()") SetAttributeNode setAttribNode) {
110+
setAttribNode.execute(target, (String) getCastNameNode().doCast(name), value);
111+
return RNull.instance;
112+
}
113+
114+
@Specialization
115+
protected Object setValue(@SuppressWarnings("unused") RNull target, @SuppressWarnings("unused") Object name, @SuppressWarnings("unused") Object value) {
116+
return RNull.instance;
117+
}
118+
119+
@Specialization
120+
protected Object unsetValue(RAttributable target, Object name, @SuppressWarnings("unused") RNull nullVal,
121+
@Cached("create()") RemoveAttributeNode removeAttributeNode) {
122+
removeAttributeNode.execute(target, (String) getCastNameNode().doCast(name));
123+
return RNull.instance;
124+
}
125+
126+
@Fallback
127+
protected Object others(Object target, Object name, Object value) {
128+
throw unsupportedTypes("Rf_setAttrib", target, name, value);
129+
}
130+
131+
protected static boolean isNull(Object o) {
132+
return o == RNull.instance;
133+
}
134+
135+
private CastNode getCastNameNode() {
136+
if (castNameNode == null) {
137+
CompilerDirectives.transferToInterpreterAndInvalidate();
138+
castNameNode = insert(newCastBuilder().asStringVector().findFirst().buildCastNode());
139+
}
140+
return castNameNode;
141+
}
142+
}
143+
94144
public abstract static class ATTRIB extends FFIUpCallNode.Arg1 {
95145

96146
@Specialization
97147
public Object doAttributable(RAttributable obj,
148+
@Cached("create()") InternStringNode internStringNode,
98149
@Cached("createWithCompactRowNames()") GetAttributesNode getAttributesNode) {
99150
Object resultObj = getAttributesNode.execute(obj);
100151
if (resultObj == RNull.instance) {
@@ -104,14 +155,14 @@ public Object doAttributable(RAttributable obj,
104155
RList list = (RList) resultObj;
105156
Object result = RNull.instance;
106157
RStringVector names = list.getNames();
107-
assert names.getLength() == list.getLength();
158+
assert names != null && names.getLength() == list.getLength();
108159
for (int i = list.getLength() - 1; i >= 0; i--) {
109160
Object item = list.getDataAt(i);
110161
String name = names.getDataAt(i);
111162
if (name.equals(RRuntime.ROWNAMES_ATTR_KEY)) {
112163
item = GetRowNamesAttributeNode.ensureRowNamesCompactFormat(item);
113164
}
114-
RSymbol symbol = RDataFactory.createSymbol(name);
165+
RSymbol symbol = RDataFactory.createSymbol(internStringNode.execute(name));
115166
result = RDataFactory.createPairList(item, result, symbol);
116167
}
117168
return result;
@@ -208,6 +259,10 @@ public static CopyMostAttrib create() {
208259
}
209260
}
210261

262+
/**
263+
* Overrides the attributes pairlist of given object with a new pairlist. In FastR, we have to
264+
* convert the pairlist to our representation.
265+
*/
211266
public abstract static class SetAttribNode extends FFIUpCallNode.Arg2 {
212267

213268
public static SetAttribNode create() {

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -277,7 +277,7 @@ public abstract static class NamesGetsNode extends FFIUpCallNode.Arg2 {
277277

278278
@Specialization
279279
Object doNewObject(Object vec, Object val) {
280-
setNamesNode.execute(vec, val);
280+
setNamesNode.setAttr(vec, val);
281281
return vec;
282282
}
283283

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/StdUpCallsRFFI.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
2929
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodes.ATTRIB;
3030
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodes.CopyMostAttrib;
3131
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodes.GetAttrib;
32+
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodes.RfSetAttribNode;
3233
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodes.SetAttribNode;
3334
import com.oracle.truffle.r.ffi.impl.nodes.AttributesAccessNodes.TAG;
3435
import com.oracle.truffle.r.ffi.impl.nodes.CoerceNodes.AsCharacterFactor;
@@ -216,6 +217,7 @@ public interface StdUpCallsRFFI {
216217
@RFFIUpCallNode(GetAttrib.class)
217218
Object Rf_getAttrib(@RFFIResultOwner Object obj, Object name);
218219

220+
@RFFIUpCallNode(RfSetAttribNode.class)
219221
void Rf_setAttrib(Object obj, Object name, Object val);
220222

221223
int Rf_inherits(Object x, @RFFICstring String clazz);

com.oracle.truffle.r.ffi.processor/src/com/oracle/truffle/r/ffi/processor/FFIProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -223,7 +223,7 @@ private void generateCallClass(ExecutableElement m) throws IOException {
223223
String callName = name + "Call";
224224
JavaFileObject fileObj = processingEnv.getFiler().createSourceFile("com.oracle.truffle.r.ffi.impl.upcalls." + callName);
225225
Writer w = fileObj.openWriter();
226-
w.append("// GENERATED by FFIProcessor; DO NOT EDIT\n");
226+
w.append("// GENERATED by com.oracle.truffle.r.ffi.processor.FFIProcessor class; DO NOT EDIT\n");
227227
w.append("\n");
228228
w.append("package com.oracle.truffle.r.ffi.impl.upcalls;\n");
229229
w.append("\n");
@@ -406,7 +406,7 @@ private void generateCallClass(ExecutableElement m) throws IOException {
406406
w.append(" }\n");
407407
w.append(" }\n");
408408
w.append("\n");
409-
w.append(" private static final ForeignAccess ACCESS = ForeignAccess.create(new " + callName + "Factory(), null);\n");
409+
w.append(" private static final ForeignAccess ACCESS = ForeignAccess.createAccess(new " + callName + "Factory(), null);\n");
410410
w.append("\n");
411411
w.append(" @Override\n");
412412
w.append(" public ForeignAccess getForeignAccess() {\n");

com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinomNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 1995, 1996 Robert Gentleman and Ross Ihaka
33
* Copyright (c) 1997-2012, The R Core Team
44
* Copyright (c) 2003-2008, The R Foundation
5-
* Copyright (c) 2016, 2018, Oracle and/or its affiliates
5+
* Copyright (c) 2016, 2019, Oracle and/or its affiliates
66
*
77
* This program is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
@@ -142,7 +142,7 @@ protected RIntVector doMultinom(int n, int size, RAbstractDoubleVector probs,
142142
Object probsNames = getNamesNode.execute(probs.getAttributes());
143143
updateSharedAttributeNode.execute(probs, probsNames);
144144
Object[] dimnamesData = new Object[]{probsNames, RNull.instance};
145-
setDimNamesNode.execute(resultVec.getAttributes(), RDataFactory.createList(dimnamesData));
145+
setDimNamesNode.setAttr(resultVec.getAttributes(), RDataFactory.createList(dimnamesData));
146146
}
147147
return resultVec;
148148
}

com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/utils/TypeConvert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -227,7 +227,7 @@ protected Object typeConvert(RAbstractStringVector x, RAbstractStringVector naSt
227227
}
228228
}
229229
RIntVector res = RDataFactory.createIntVector(data, complete);
230-
setLevelsAttrNode.execute(res, RDataFactory.createStringVector(levels.keySet().toArray(new String[0]), RDataFactory.COMPLETE_VECTOR));
230+
setLevelsAttrNode.setAttr(res, RDataFactory.createStringVector(levels.keySet().toArray(new String[0]), RDataFactory.COMPLETE_VECTOR));
231231
return RVector.setVectorClassAttr(res, RDataFactory.createStringVector("factor"));
232232
}
233233
}

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/EnvFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -432,7 +432,7 @@ protected static Object updateEnvironment(RAbstractContainer obj, REnvironment e
432432
@TruffleBoundary
433433
protected static Object updateEnvironment(RAttributable obj, REnvironment env,
434434
@Cached("createSetEnvAttrNode()") SetFixedAttributeNode setEnvAttrNode) {
435-
setEnvAttrNode.execute(obj, env);
435+
setEnvAttrNode.setAttr(obj, env);
436436
return obj;
437437
}
438438

0 commit comments

Comments
 (0)