diff --git a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/IsMimeTypeSupportedTest.java b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/IsMimeTypeSupportedTest.java new file mode 100644 index 000000000000..f32fb7ef58ae --- /dev/null +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/IsMimeTypeSupportedTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 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.api.vm; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +import com.oracle.truffle.api.source.Source; + +public class IsMimeTypeSupportedTest { + + private static final String MIME_TYPE = "application/x-test-mime-type-supported"; + + @Test + public void isMimeSupported() throws IOException { + PolyglotEngine vm = PolyglotEngine.newBuilder().build(); + assertEquals(true, vm.eval(Source.fromText(MIME_TYPE, "supported").withMimeType(MIME_TYPE)).as(Boolean.class)); + assertEquals(false, vm.eval(Source.fromText("application/x-this-language-does-not-exist", "unsupported").withMimeType(MIME_TYPE)).as(Boolean.class)); + } + +} diff --git a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/IsMimeTypeSupportedTestLanguage.java b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/IsMimeTypeSupportedTestLanguage.java new file mode 100644 index 000000000000..d6ebe01b5e73 --- /dev/null +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/IsMimeTypeSupportedTestLanguage.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 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.api.vm; + +import java.io.IOException; + +import com.oracle.truffle.api.CallTarget; +import com.oracle.truffle.api.TruffleLanguage; +import com.oracle.truffle.api.TruffleLanguage.Env; +import com.oracle.truffle.api.frame.MaterializedFrame; +import com.oracle.truffle.api.instrument.Visualizer; +import com.oracle.truffle.api.instrument.WrapperNode; +import com.oracle.truffle.api.nodes.Node; +import com.oracle.truffle.api.source.Source; + +@TruffleLanguage.Registration(name = "Hash", mimeType = "application/x-test-mime-type-supported", version = "1.0") +public class IsMimeTypeSupportedTestLanguage extends TruffleLanguage { + + public static final IsMimeTypeSupportedTestLanguage INSTANCE = new IsMimeTypeSupportedTestLanguage(); + + @Override + protected Env createContext(com.oracle.truffle.api.TruffleLanguage.Env env) { + return env; + } + + @Override + protected CallTarget parse(final Source code, Node context, String... argumentNames) throws IOException { + final String mimeType = code.getCode(); + + return new CallTarget() { + + public Object call(Object... arguments) { + return findContext(createFindContextNode()).isMimeTypeSupported(mimeType); + } + + }; + } + + @Override + protected Object findExportedSymbol(Env context, String globalName, boolean onlyExplicit) { + throw new UnsupportedOperationException(); + } + + @Override + protected Object getLanguageGlobal(Env context) { + throw new UnsupportedOperationException(); + } + + @Override + protected boolean isObjectOfLanguage(Object object) { + throw new UnsupportedOperationException(); + } + + @Override + protected Visualizer getVisualizer() { + throw new UnsupportedOperationException(); + } + + @Override + protected boolean isInstrumentable(Node node) { + throw new UnsupportedOperationException(); + } + + @Override + protected WrapperNode createWrapperNode(Node node) { + throw new UnsupportedOperationException(); + } + + @Override + protected Object evalInContext(Source source, Node node, MaterializedFrame mFrame) throws IOException { + throw new UnsupportedOperationException(); + } + +} diff --git a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java index 5427dc54c011..cb9c5394666b 100644 --- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java +++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java @@ -985,6 +985,12 @@ protected Class findLanguage(Probe probe) { return super.findLanguage(probe); } + @Override + protected boolean isMimeTypeSupported(Object obj, String mimeType) { + final PolyglotEngine vm = (PolyglotEngine) obj; + return vm.findLanguage(mimeType) != null; + } + @Override protected Env findLanguage(Object obj, Class languageClass) { PolyglotEngine vm = (PolyglotEngine) obj; diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java index a68cbec38e09..24985bbfa8ba 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java @@ -387,6 +387,19 @@ public Object importSymbol(String globalName) { return API.importSymbol(vm, lang, globalName); } + /** + * Allows it to be determined if this {@link com.oracle.truffle.api.vm.PolyglotEngine} can + * execute code written in a language with a given MIME type. + * + * @see Source#withMimeType(String) + * @see #parse(Source, String...) + * + * @return a boolean that indicates if the MIME type is supported + */ + public boolean isMimeTypeSupported(String mimeType) { + return API.isMimeTypeSupported(vm, mimeType); + } + /** * Evaluates source of (potentially different) language. The {@link Source#getMimeType() * MIME type} is used to identify the {@link TruffleLanguage} to use to perform the @@ -525,6 +538,11 @@ protected Object findExportedSymbol(TruffleLanguage.Env env, String globalName, return env.langCtx.findExportedSymbol(globalName, onlyExplicit); } + @Override + protected boolean isMimeTypeSupported(Object vm, String mimeType) { + return super.isMimeTypeSupported(vm, mimeType); + } + @Override protected Env findLanguage(Object vm, Class languageClass) { return super.findLanguage(vm, languageClass); diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java index a623d7e76ecb..46d61d701d65 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java @@ -204,6 +204,10 @@ protected WrapperNode createWrapperNode(Node node, TruffleLanguage language) return API.createWrapperNode(node, language); } + protected boolean isMimeTypeSupported(Object vm, String mimeType) { + return SPI.isMimeTypeSupported(vm, mimeType); + } + @SuppressWarnings("rawtypes") protected Class findLanguage(RootNode n) { return NODES.findLanguage(n);