Skip to content

Add Env#getSupportedMimeTypes() #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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<Env> {

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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,12 @@ protected Class<? extends TruffleLanguage> 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<? extends TruffleLanguage> languageClass) {
PolyglotEngine vm = (PolyglotEngine) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<? extends TruffleLanguage> languageClass) {
return super.findLanguage(vm, languageClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends TruffleLanguage> findLanguage(RootNode n) {
return NODES.findLanguage(n);
Expand Down