use of com.oracle.truffle.api.TruffleLanguage in project graal by oracle.
the class EngineTest method testLanguageAccess.
@Test
public void testLanguageAccess() {
final PolyglotEngine.Builder builder = createBuilderInternal();
ForkingLanguageChannel channel = new ForkingLanguageChannel(builder::build);
PolyglotEngine vm = builder.config(ForkingLanguage.MIME_TYPE, "channel", channel).build();
vm.eval(Source.newBuilder("").name("").mimeType(ForkingLanguage.MIME_TYPE).build()).get();
RootNode root = new RootNode(channel.language) {
@Override
public Object execute(VirtualFrame frame) {
return null;
}
};
try {
// no access using a TruffleLanguage hack
root.getLanguage(TruffleLanguage.class);
fail();
} catch (ClassCastException e) {
}
Class<?> oClass = Object.class;
Class<? extends TruffleLanguage> lang = (Class<? extends TruffleLanguage>) oClass;
try {
// no access using a TruffleLanguage class cast
root.getLanguage(lang);
fail();
} catch (ClassCastException e) {
}
oClass = SecretInterfaceType.class;
Class<? extends TruffleLanguage> secretInterface = (Class<? extends TruffleLanguage>) oClass;
try {
// no access using secret interface
root.getLanguage(secretInterface);
fail();
} catch (ClassCastException e) {
}
// this should work as expected
assertNotNull(root.getLanguage(ForkingLanguage.class));
}
use of com.oracle.truffle.api.TruffleLanguage in project graal by oracle.
the class SourceSectionFilterTest method createRootNode.
static RootNode createRootNode(Engine engine, final SourceSection section, final Boolean internal, Node... children) throws Exception {
Language language = engine.getLanguages().get(InstrumentationTestLanguage.ID);
Field impl = Language.class.getDeclaredField("impl");
ReflectionUtils.setAccessible(impl, true);
Object polyglotLanguage = impl.get(language);
Method ensureInitialized = polyglotLanguage.getClass().getDeclaredMethod("ensureInitialized");
ReflectionUtils.setAccessible(ensureInitialized, true);
ensureInitialized.invoke(polyglotLanguage);
Field info = polyglotLanguage.getClass().getDeclaredField("info");
ReflectionUtils.setAccessible(info, true);
LanguageInfo languageInfo = (LanguageInfo) info.get(polyglotLanguage);
Field spi = LanguageInfo.class.getDeclaredField("spi");
ReflectionUtils.setAccessible(spi, true);
TruffleLanguage<?> truffleLanguage = (TruffleLanguage<?>) spi.get(languageInfo);
return new RootNode(truffleLanguage) {
@Node.Children
Node[] rootChildren = children;
@Override
protected boolean isInstrumentable() {
return true;
}
@Override
public SourceSection getSourceSection() {
return section;
}
@Override
public boolean isInternal() {
if (internal == null) {
return super.isInternal();
} else {
return internal;
}
}
@Override
public Object execute(VirtualFrame frame) {
return null;
}
};
}
use of com.oracle.truffle.api.TruffleLanguage in project graal by oracle.
the class PolyglotEngine method getLanguageImpl.
private Language getLanguageImpl(Class<? extends TruffleLanguage<?>> languageClass) {
int indexValue = languageIndexMap.get(languageClass);
if (indexValue == -1) {
CompilerDirectives.transferToInterpreterAndInvalidate();
Language language = findLanguage(languageClass, false, true);
indexValue = language.shared.languageId;
languageIndexMap.put(languageClass, indexValue);
}
return languageArray[indexValue];
}
use of com.oracle.truffle.api.TruffleLanguage in project graal by oracle.
the class LanguageRegistrationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
generateFile(registrations);
registrations.clear();
return true;
}
for (Element e : roundEnv.getElementsAnnotatedWith(Registration.class)) {
Registration annotation = e.getAnnotation(Registration.class);
if (annotation != null && e.getKind() == ElementKind.CLASS) {
if (!e.getModifiers().contains(Modifier.PUBLIC)) {
emitError("Registered language class must be public", e);
continue;
}
if (e.getEnclosingElement().getKind() != ElementKind.PACKAGE && !e.getModifiers().contains(Modifier.STATIC)) {
emitError("Registered language inner-class must be static", e);
continue;
}
TypeMirror truffleLang = processingEnv.getTypeUtils().erasure(processingEnv.getElementUtils().getTypeElement(TruffleLanguage.class.getName()).asType());
if (!processingEnv.getTypeUtils().isAssignable(e.asType(), truffleLang)) {
emitError("Registered language class must subclass TruffleLanguage", e);
continue;
}
boolean foundConstructor = false;
for (ExecutableElement constructor : ElementFilter.constructorsIn(e.getEnclosedElements())) {
if (!constructor.getModifiers().contains(Modifier.PUBLIC)) {
continue;
}
if (!constructor.getParameters().isEmpty()) {
continue;
}
foundConstructor = true;
break;
}
Element singletonElement = null;
for (Element mem : e.getEnclosedElements()) {
if (!mem.getModifiers().contains(Modifier.PUBLIC)) {
continue;
}
if (mem.getKind() != ElementKind.FIELD) {
continue;
}
if (!mem.getModifiers().contains(Modifier.FINAL)) {
continue;
}
if (!"INSTANCE".equals(mem.getSimpleName().toString())) {
continue;
}
if (processingEnv.getTypeUtils().isAssignable(mem.asType(), truffleLang)) {
singletonElement = mem;
break;
}
}
if (singletonElement != null) {
emitWarning("Using a singleton field is deprecated. Please provide a public no-argument constructor instead.", singletonElement);
} else {
if (!foundConstructor) {
emitError("A TruffleLanguage subclass must have a public no argument constructor.", e);
} else {
assertNoErrorExpected(e);
}
}
registrations.add((TypeElement) e);
}
}
return true;
}
Aggregations