Search in sources :

Example 1 with TruffleLanguage

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));
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Test(org.junit.Test)

Example 2 with TruffleLanguage

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;
        }
    };
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Field(java.lang.reflect.Field) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) RootNode(com.oracle.truffle.api.nodes.RootNode) Language(org.graalvm.polyglot.Language) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Method(java.lang.reflect.Method) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

Example 3 with TruffleLanguage

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];
}
Also used : TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

Example 4 with TruffleLanguage

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;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Registration(com.oracle.truffle.api.TruffleLanguage.Registration) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

Aggregations

TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)4 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 Registration (com.oracle.truffle.api.TruffleLanguage.Registration)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Language (org.graalvm.polyglot.Language)1 Test (org.junit.Test)1