Skip to content
Snippets Groups Projects
Commit 0cf42b8d authored by stepan's avatar stepan
Browse files

Foreign access message resolution for RS4Object

parent 1fd6c9ec
Branches
No related tags found
No related merge requests found
......@@ -22,6 +22,8 @@
*/
package com.oracle.truffle.r.engine.interop;
import static com.oracle.truffle.r.engine.interop.Utils.javaToRPrimitive;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
......@@ -32,7 +34,6 @@ import com.oracle.truffle.r.engine.TruffleRLanguage;
import com.oracle.truffle.r.nodes.access.vector.ElementAccessMode;
import com.oracle.truffle.r.nodes.access.vector.ExtractVectorNode;
import com.oracle.truffle.r.nodes.access.vector.ReplaceVectorNode;
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.context.RContext.RCloseable;
import com.oracle.truffle.r.runtime.env.REnvironment;
......@@ -82,20 +83,7 @@ public class REnvironmentMR {
@SuppressWarnings("try")
protected Object access(VirtualFrame frame, REnvironment receiver, String field, Object valueObj) {
try (RCloseable c = RContext.withinContext(TruffleRLanguage.INSTANCE.actuallyFindContext0(findContext))) {
Object value = valueObj;
if (value instanceof Short) {
value = (int) ((Short) value).shortValue();
} else if (value instanceof Float) {
float floatValue = ((Float) value).floatValue();
value = new Double(floatValue);
} else if (value instanceof Boolean) {
boolean booleanValue = ((Boolean) value).booleanValue();
value = booleanValue ? RRuntime.LOGICAL_TRUE : RRuntime.LOGICAL_FALSE;
} else if (value instanceof Character) {
value = (int) ((Character) value).charValue();
} else if (value instanceof Byte) {
value = (int) ((Byte) value).byteValue();
}
Object value = javaToRPrimitive(valueObj);
Object x = extract.apply(frame, receiver, new Object[]{field}, value);
return x;
}
......
......@@ -40,6 +40,7 @@ import com.oracle.truffle.r.runtime.data.RInteger;
import com.oracle.truffle.r.runtime.data.RList;
import com.oracle.truffle.r.runtime.data.RNull;
import com.oracle.truffle.r.runtime.data.RPairList;
import com.oracle.truffle.r.runtime.data.RS4Object;
import com.oracle.truffle.r.runtime.data.RSymbol;
import com.oracle.truffle.r.runtime.data.RTruffleObject;
import com.oracle.truffle.r.runtime.data.RUnboundValue;
......@@ -107,6 +108,8 @@ public final class RForeignAccessFactoryImpl implements RForeignAccessFactory {
return MakeResultImplMRForeign.ACCESS;
} else if (obj instanceof TruffleNFI_PCRE.TruffleNFI_GetCaptureNamesNode.CaptureNamesImpl) {
return CaptureNamesImplMRForeign.ACCESS;
} else if (obj instanceof RS4Object) {
return RS4ObjectMRForeign.ACCESS;
} else {
if (obj instanceof RAbstractVector) {
return ForeignAccess.create(RAbstractVector.class, new RAbstractVectorAccessFactory());
......
......@@ -22,6 +22,8 @@
*/
package com.oracle.truffle.r.engine.interop;
import static com.oracle.truffle.r.engine.interop.Utils.javaToRPrimitive;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
......@@ -35,7 +37,6 @@ import com.oracle.truffle.r.nodes.access.vector.ElementAccessMode;
import com.oracle.truffle.r.nodes.access.vector.ExtractVectorNode;
import com.oracle.truffle.r.nodes.access.vector.ReplaceVectorNode;
import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.GetNamesAttributeNode;
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.context.RContext.RCloseable;
import com.oracle.truffle.r.runtime.data.RFunction;
......@@ -87,20 +88,7 @@ public class RListMR {
@SuppressWarnings("try")
protected Object access(VirtualFrame frame, RList receiver, String field, Object valueObj) {
try (RCloseable c = RContext.withinContext(TruffleRLanguage.INSTANCE.actuallyFindContext0(findContext))) {
Object value = valueObj;
if (value instanceof Short) {
value = (int) ((Short) value).shortValue();
} else if (value instanceof Float) {
float floatValue = ((Float) value).floatValue();
value = new Double(floatValue);
} else if (value instanceof Boolean) {
boolean booleanValue = ((Boolean) value).booleanValue();
value = booleanValue ? RRuntime.LOGICAL_TRUE : RRuntime.LOGICAL_FALSE;
} else if (value instanceof Character) {
value = (int) ((Character) value).charValue();
} else if (value instanceof Byte) {
value = (int) ((Byte) value).byteValue();
}
Object value = javaToRPrimitive(valueObj);
Object x = replace.apply(frame, receiver, new Object[]{field}, value);
return x;
}
......
/*
* Copyright (c) 2017, 2017, 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 2 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 2 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
* 2 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.engine.interop;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.CanResolve;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.r.engine.TruffleRLanguage;
import com.oracle.truffle.r.nodes.attributes.ArrayAttributeNode;
import com.oracle.truffle.r.nodes.attributes.GetAttributeNode;
import com.oracle.truffle.r.nodes.attributes.SetAttributeNode;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.context.RContext.RCloseable;
import com.oracle.truffle.r.runtime.data.RAttributesLayout.RAttribute;
import com.oracle.truffle.r.runtime.data.RDataFactory;
import com.oracle.truffle.r.runtime.data.RS4Object;
@MessageResolution(receiverType = RS4Object.class, language = TruffleRLanguage.class)
public class RS4ObjectMR {
@Resolve(message = "IS_BOXED")
public abstract static class RS4ObjectIsBoxedNode extends Node {
protected Object access(@SuppressWarnings("unused") RS4Object receiver) {
return false;
}
}
@Resolve(message = "HAS_SIZE")
public abstract static class RS4ObjectHasSizeNode extends Node {
protected Object access(@SuppressWarnings("unused") RS4Object receiver) {
return false;
}
}
@Resolve(message = "IS_NULL")
public abstract static class RS4ObjectIsNullNode extends Node {
protected Object access(@SuppressWarnings("unused") RS4Object receiver) {
return false;
}
}
@Resolve(message = "READ")
public abstract static class RS4ObjectReadNode extends Node {
@Child private GetAttributeNode getAttributeNode = GetAttributeNode.create();
@Child private Node findContext = TruffleRLanguage.INSTANCE.actuallyCreateFindContextNode();
@SuppressWarnings("try")
protected Object access(VirtualFrame frame, RS4Object receiver, String field) {
try (RCloseable c = RContext.withinContext(TruffleRLanguage.INSTANCE.actuallyFindContext0(findContext))) {
return getAttributeNode.execute(receiver, field);
}
}
}
@Resolve(message = "WRITE")
public abstract static class RS4ObjectWriteNode extends Node {
@Child private SetAttributeNode setAttributeNode = SetAttributeNode.create();
@Child private Node findContext = TruffleRLanguage.INSTANCE.actuallyCreateFindContextNode();
@SuppressWarnings("try")
protected Object access(VirtualFrame frame, RS4Object receiver, String field, Object valueObj) {
try (RCloseable c = RContext.withinContext(TruffleRLanguage.INSTANCE.actuallyFindContext0(findContext))) {
Object value = Utils.javaToRPrimitive(valueObj);
setAttributeNode.execute(receiver, field, value);
return value;
}
}
}
@Resolve(message = "KEYS")
public abstract static class RS4ObjectKeysNode extends Node {
@Child private Node findContext = TruffleRLanguage.INSTANCE.actuallyCreateFindContextNode();
@Child private ArrayAttributeNode arrayAttrAccess = ArrayAttributeNode.create();
@SuppressWarnings("try")
protected Object access(RS4Object receiver) {
try (RCloseable c = RContext.withinContext(TruffleRLanguage.INSTANCE.actuallyFindContext0(findContext))) {
RAttribute[] attributes = arrayAttrAccess.execute(receiver.getAttributes());
String[] data = new String[attributes.length];
for (int i = 0; i < data.length; i++) {
data[i] = attributes[i].getName();
}
return RDataFactory.createStringVector(data, RDataFactory.COMPLETE_VECTOR);
}
}
}
@CanResolve
public abstract static class RS4ObjectCheck extends Node {
protected static boolean test(TruffleObject receiver) {
return receiver instanceof RS4Object;
}
}
}
/*
* Copyright (c) 2017, 2017, 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 2 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 2 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
* 2 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.engine.interop;
import com.oracle.truffle.r.runtime.RRuntime;
class Utils {
static Object javaToRPrimitive(Object valueObj) {
Object value = valueObj;
if (value instanceof Short) {
value = (int) ((Short) value).shortValue();
} else if (value instanceof Float) {
float floatValue = ((Float) value).floatValue();
value = new Double(floatValue);
} else if (value instanceof Boolean) {
boolean booleanValue = ((Boolean) value).booleanValue();
value = booleanValue ? RRuntime.LOGICAL_TRUE : RRuntime.LOGICAL_FALSE;
} else if (value instanceof Character) {
value = (int) ((Character) value).charValue();
} else if (value instanceof Byte) {
value = (int) ((Byte) value).byteValue();
}
return value;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment