
com.oracle.truffle.api.interop.ForeignAccess Maven / Gradle / Ivy
/*
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.api.interop;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.impl.ReadOnlyArrayList;
import com.oracle.truffle.api.nodes.Node;
import java.util.List;
/**
* Encapsulates types of access to {@link TruffleObject}. If you want to expose your own objects to
* foreign language implementations, you need to implement {@link TruffleObject} and its
* {@link TruffleObject#getForeignAccess()} method. To create instance of ForeignAccess
* , use one of the factory methods available in this class.
*/
public final class ForeignAccess {
private final Factory factory;
private final Thread initThread;
private ForeignAccess(Factory faf) {
this.factory = faf;
this.initThread = Thread.currentThread();
CompilerAsserts.neverPartOfCompilation();
}
/**
* Creates new instance of {@link ForeignAccess} that delegates to provided factory.
*
* @param baseClass the super class of all {@link TruffleObject}s handled by this factory (if
* null
than the second interface also needs to implement
* {@link Factory})
* @param factory the factory that handles access requests to {@link Message}s known as of
* version 1.0
* @return new instance wrapping factory
*/
public static ForeignAccess create(final Class extends TruffleObject> baseClass, final Factory10 factory) {
if (baseClass == null) {
Factory f = (Factory) factory;
assert f != null;
}
return new ForeignAccess(new DelegatingFactory(baseClass, factory));
}
/**
* Creates new instance of {@link ForeignAccess} that delegates to provided factory.
*
* @param factory the factory that handles various access requests {@link Message}s.
* @return new instance wrapping factory
*/
public static ForeignAccess create(Factory factory) {
return new ForeignAccess(factory);
}
/**
* Executes {@link Message#createNode() foreign node}.
*
* @param foreignNode the createNode created by {@link Message#createNode()}
* @param frame the call frame
* @param receiver foreign object to receive the message passed to {@link Message#createNode()}
* method
* @param arguments parameters for the receiver
* @return return value, if any
* @throws ClassCastException if the createNode has not been created by
* {@link Message#createNode()} method.
* @throws IllegalAccessError if the receiver
does not support the
* {@link Message#createNode() message represented} by foreignNode
*/
public static Object execute(Node foreignNode, VirtualFrame frame, TruffleObject receiver, Object... arguments) {
ForeignObjectAccessHeadNode fn = (ForeignObjectAccessHeadNode) foreignNode;
return fn.executeForeign(frame, receiver, arguments);
}
/**
* Read only access to foreign call arguments inside of a frame.
*
* @param frame the frame that was called via
* {@link #execute(com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.VirtualFrame, com.oracle.truffle.api.interop.TruffleObject, java.lang.Object...) }
* @return read-only list of parameters passed to the frame
*/
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy