boot-truffle/src/main/java/net/java/html/boot/truffle/FnRootNode.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Jul 2016 09:35:05 +0200
branchTruffle
changeset 1096 28e373ac1576
permissions -rw-r--r--
Run most of the tests against GraalVM 0.12 successfully
jtulach@1096
     1
/**
jtulach@1096
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@1096
     3
 *
jtulach@1096
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@1096
     5
 *
jtulach@1096
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@1096
     7
 * Other names may be trademarks of their respective owners.
jtulach@1096
     8
 *
jtulach@1096
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@1096
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@1096
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@1096
    12
 * "License"). You may not use this file except in compliance with the
jtulach@1096
    13
 * License. You can obtain a copy of the License at
jtulach@1096
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@1096
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@1096
    16
 * specific language governing permissions and limitations under the
jtulach@1096
    17
 * License.  When distributing the software, include this License Header
jtulach@1096
    18
 * Notice in each file and include the License file at
jtulach@1096
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@1096
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@1096
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@1096
    22
 * accompanied this code. If applicable, add the following below the
jtulach@1096
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@1096
    24
 * your own identifying information:
jtulach@1096
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@1096
    26
 *
jtulach@1096
    27
 * Contributor(s):
jtulach@1096
    28
 *
jtulach@1096
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@1096
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@1096
    31
 *
jtulach@1096
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@1096
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@1096
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@1096
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@1096
    36
 * single choice of license, a recipient has the option to distribute
jtulach@1096
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@1096
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@1096
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@1096
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@1096
    41
 * made subject to such option by the copyright holder.
jtulach@1096
    42
 */
jtulach@1096
    43
package net.java.html.boot.truffle;
jtulach@1096
    44
jtulach@1096
    45
import com.oracle.truffle.api.TruffleLanguage;
jtulach@1096
    46
import com.oracle.truffle.api.frame.VirtualFrame;
jtulach@1096
    47
import com.oracle.truffle.api.interop.ForeignAccess;
jtulach@1096
    48
import com.oracle.truffle.api.interop.InteropException;
jtulach@1096
    49
import com.oracle.truffle.api.interop.Message;
jtulach@1096
    50
import com.oracle.truffle.api.interop.TruffleObject;
jtulach@1096
    51
import com.oracle.truffle.api.nodes.Node;
jtulach@1096
    52
import com.oracle.truffle.api.nodes.RootNode;
jtulach@1096
    53
jtulach@1096
    54
final class FnRootNode extends RootNode {
jtulach@1096
    55
    @Child
jtulach@1096
    56
    private Node execute;
jtulach@1096
    57
    private final TruffleObject fn;
jtulach@1096
    58
jtulach@1096
    59
    FnRootNode(TruffleObject fn, int arity) {
jtulach@1096
    60
        super(TruffleLanguage.class, null, null);
jtulach@1096
    61
        // one more argument for this
jtulach@1096
    62
        this.execute = Message.createExecute(arity + 1).createNode();
jtulach@1096
    63
        this.fn = fn;
jtulach@1096
    64
    }
jtulach@1096
    65
jtulach@1096
    66
    @Override
jtulach@1096
    67
    public Object execute(VirtualFrame frame) {
jtulach@1096
    68
        try {
jtulach@1096
    69
            final Object[] args = frame.getArguments();
jtulach@1096
    70
            Object result = ForeignAccess.sendExecute(execute, frame, fn, args);
jtulach@1096
    71
            return result;
jtulach@1096
    72
        } catch (InteropException ex) {
jtulach@1096
    73
            throw raise(RuntimeException.class, ex);
jtulach@1096
    74
        }
jtulach@1096
    75
    }
jtulach@1096
    76
jtulach@1096
    77
    private static <E extends Throwable> E raise(Class<E> castTo, Throwable t) throws E {
jtulach@1096
    78
        throw (E)t;
jtulach@1096
    79
    }
jtulach@1096
    80
}