@Override public Object toGuestValue(Object obj, Object languageContext) { return JavaInterop.asTruffleValue(obj); }
/** * Adds a global symbol (named value) to be exported by the {@link PolyglotEngine engine} * being built. Any guest {@link Language Language} can <em>import</em> this symbol, which * takes precedence over any symbols exported under the same name by languages. Any number * of symbols may be added; in case of name-collision only the last one added will be * exported. The namespace of exported global symbols is immutable once the engine is built. * <p> * See {@linkplain PolyglotEngine "Truffle-Java Interoperation"} for the implications of * exporting Java data to guest languages. The following example demonstrates the export of * both a Java class and a Java object: * * {@link com.oracle.truffle.api.vm.PolyglotEngineSnippets#configureJavaInterop} * * The <code>mul</code> and <code>compose</code> objects are then available to any guest * language. * * @param name name of the global symbol to register * @param obj value of the symbol to export * @return this builder * @see PolyglotEngine#findGlobalSymbol(String) * @throws IllegalArgumentException if the object isn't of primitive type and cannot be * converted to {@link TruffleObject} * @since 0.9 */ public Builder globalSymbol(String name, Object obj) { final Object truffleReady = JavaInterop.asTruffleValue(obj); globals.put(name, truffleReady); return this; }
@Override protected void initializeContext(Env context) throws Exception { context.exportSymbol("env", JavaInterop.asTruffleValue(context)); }
@Override protected Object executeImpl(VirtualFrame frame) { Object[] callArgs = frame.getArguments(); final TruffleObject function = receiverType.cast(callArgs[0]); final Object[] args = (Object[]) callArgs[1]; unwrapArgs(engine, args); try { if (executeNode == null) { CompilerDirectives.transferToInterpreterAndInvalidate(); executeNode = insert(Message.createExecute(0).createNode()); } Object tmp = ForeignAccess.sendExecute(executeNode, function, args); if (tmp == null) { tmp = JavaInterop.asTruffleValue(null); } Object result = returnConvertNode.convert(tmp); // TODO we must check here that the language returns a valid value. return result; } catch (InteropException e) { CompilerDirectives.transferToInterpreter(); throw e.raise(); } } }