From 71eeb93b7f19439a580dc82bd792dae12accbae2 Mon Sep 17 00:00:00 2001 From: Mick Jordan <mick.jordan@oracle.com> Date: Mon, 18 Aug 2014 10:22:00 -0700 Subject: [PATCH] fastr package updates --- .../r/nodes/builtin/fastr/FastRRunDump.java | 2 +- .../r/nodes/builtin/fastr/FastRSetField.java | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRSetField.java diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRRunDump.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRRunDump.java index 56916e8820..6fe3ee5cb9 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRRunDump.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRRunDump.java @@ -40,7 +40,7 @@ import com.oracle.truffle.r.runtime.data.*; * Run a FastR function, and dump its AST to IGV before and after running. If no function is passed, * this builtin does not do anything. */ -@RBuiltin(name = "fastr.rundump", parameterNames = {"function"}, kind = PRIMITIVE) +@RBuiltin(name = "fastr.rundump", parameterNames = {"func"}, kind = PRIMITIVE) public abstract class FastRRunDump extends RInvisibleBuiltinNode { // TODO Make this more versatile by allowing actual function calls with arguments to be diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRSetField.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRSetField.java new file mode 100644 index 0000000000..9410d78a72 --- /dev/null +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRSetField.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014, 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.nodes.builtin.fastr; + +import java.lang.reflect.*; + +import com.oracle.truffle.api.dsl.*; +import com.oracle.truffle.api.frame.*; +import com.oracle.truffle.r.nodes.builtin.*; +import com.oracle.truffle.r.runtime.*; +import com.oracle.truffle.r.runtime.RError.*; +import com.oracle.truffle.r.runtime.data.*; +import com.oracle.truffle.r.runtime.data.model.*; + +@RBuiltin(name = "fastr.setfield", kind = RBuiltinKind.PRIMITIVE, parameterNames = {"field", "value"}) +public abstract class FastRSetField extends RInvisibleBuiltinNode { + + @Specialization + RNull setField(VirtualFrame frame, RAbstractStringVector vec, Object value) { + controlVisibility(); + String qualFieldName = vec.getDataAt(0); + int lx = qualFieldName.lastIndexOf('.'); + String simpleName = qualFieldName.substring(lx + 1); + String className = qualFieldName.substring(0, lx); + if (!className.startsWith("com")) { + className = "com.oracle.truffle.r." + className; + } + try { + Class<?> klass = Class.forName(className); + Field field = klass.getDeclaredField(simpleName); + field.setAccessible(true); + Class<?> fieldType = field.getType(); + switch (fieldType.getSimpleName()) { + case "boolean": + if (value instanceof Byte) { + field.setBoolean(null, RRuntime.fromLogical((byte) value)); + } else { + error(frame, qualFieldName); + } + } + } catch (Exception ex) { + throw RError.error(frame, Message.GENERIC, ex.getMessage()); + } + return RNull.instance; + } + + private static void error(VirtualFrame frame, String fieldName) throws RError { + throw RError.error(frame, Message.GENERIC, "value is wrong type for %s", fieldName); + } + +} -- GitLab