Skip to content
Snippets Groups Projects
Commit 6a202cf5 authored by stepan's avatar stepan
Browse files

New builtin .fastr.try(func) that catches internal errors

parent 0c3da3bf
Branches
No related tags found
No related merge requests found
......@@ -116,6 +116,8 @@ import com.oracle.truffle.r.nodes.builtin.fastr.FastRTree;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRTreeNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRTreeStats;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRTreeStatsNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRTry;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRTryNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastrDqrls;
import com.oracle.truffle.r.nodes.builtin.fastr.FastrDqrlsNodeGen;
import com.oracle.truffle.r.nodes.unary.UnaryNotNode;
......@@ -344,6 +346,7 @@ public class BasePackage extends RBuiltinPackage {
add(FastrDqrls.class, FastrDqrlsNodeGen::create);
add(FastRDebug.class, FastRDebugNodeGen::create);
add(FastRIdentity.class, FastRIdentityNodeGen::create);
add(FastRTry.class, FastRTryNodeGen::create);
add(FastRInspect.class, FastRInspectNodeGen::create);
add(FastRInterop.Eval.class, FastRInteropFactory.EvalNodeGen::create);
add(FastRInterop.Export.class, FastRInteropFactory.ExportNodeGen::create);
......
/*
* Copyright (c) 2016, 2016, 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 static com.oracle.truffle.r.runtime.builtins.RBehavior.COMPLEX;
import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.r.nodes.access.FrameSlotNode;
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
import com.oracle.truffle.r.nodes.function.RCallBaseNode;
import com.oracle.truffle.r.nodes.function.RCallNode;
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.builtins.RBuiltin;
import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
import com.oracle.truffle.r.runtime.data.RFunction;
/**
* Allows to be 100% robust even in the case of FastR errors like runtime exceptions. The argument
* must be a single parameter-less function. The return value is true on success, otherwise error
* message.
*/
@RBuiltin(name = ".fastr.try", kind = PRIMITIVE, parameterNames = {""}, behavior = COMPLEX)
public abstract class FastRTry extends RBuiltinNode {
private final Object argsIdentifier = new Object();
@Child private RCallBaseNode call = RCallNode.createExplicitCall(argsIdentifier);
@Child private FrameSlotNode slot = FrameSlotNode.createTemp(argsIdentifier, true);
@Specialization
public Object tryFunc(VirtualFrame frame, RFunction func) {
FrameSlot frameSlot = slot.executeFrameSlot(frame);
try {
frame.setObject(frameSlot, RArgsValuesAndNames.EMPTY);
call.execute(frame, func);
} catch (Throwable ex) {
return String.format("Exception %s, message: %s", ex.getClass().getSimpleName(), ex.getMessage());
} finally {
frame.setObject(frameSlot, null);
}
return RRuntime.LOGICAL_TRUE;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment