Search in sources :

Example 1 with OptionDescriptor

use of org.graalvm.options.OptionDescriptor in project graal by oracle.

the class LanguageSPITest method testLazyOptionInit.

@Test
@SuppressWarnings("all")
public void testLazyOptionInit() {
    AtomicInteger getOptionDescriptors = new AtomicInteger(0);
    AtomicInteger iterator = new AtomicInteger(0);
    AtomicInteger get = new AtomicInteger(0);
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        final OptionKey<String> option = new OptionKey<>("");

        final List<OptionDescriptor> descriptors;

        {
            descriptors = new ArrayList<>();
            descriptors.add(OptionDescriptor.newBuilder(option, ProxyLanguage.ID + ".option").build());
        }

        @Override
        protected OptionDescriptors getOptionDescriptors() {
            getOptionDescriptors.incrementAndGet();
            return new OptionDescriptors() {

                public Iterator<OptionDescriptor> iterator() {
                    iterator.incrementAndGet();
                    return descriptors.iterator();
                }

                public OptionDescriptor get(String optionName) {
                    get.incrementAndGet();
                    for (OptionDescriptor optionDescriptor : descriptors) {
                        if (optionDescriptor.getName().equals(optionName)) {
                            return optionDescriptor;
                        }
                    }
                    return null;
                }
            };
        }
    });
    Context c = Context.create();
    c.close();
    assertEquals(0, getOptionDescriptors.get());
    assertEquals(0, iterator.get());
    assertEquals(0, get.get());
    c = Context.newBuilder(ProxyLanguage.ID).option(ProxyLanguage.ID + ".option", "foobar").build();
    assertEquals(1, getOptionDescriptors.get());
    assertEquals(1, get.get());
    boolean assertionsEnabled = false;
    assert assertionsEnabled = true;
    if (assertionsEnabled) {
        // with assertions enabled we assert the options.
        assertEquals(1, iterator.get());
    } else {
        // for valid options we should not need the iterator
        assertEquals(0, iterator.get());
    }
    c.close();
    // test lazyness when using meta-data
    getOptionDescriptors.set(0);
    iterator.set(0);
    get.set(0);
    c = Context.create();
    OptionDescriptors descriptors = c.getEngine().getLanguages().get(ProxyLanguage.ID).getOptions();
    assertEquals(1, getOptionDescriptors.get());
    if (assertionsEnabled) {
        // with assertions enabled we assert the options.
        assertEquals(1, iterator.get());
    } else {
        // for valid options we should not need the iterator
        assertEquals(0, iterator.get());
    }
    assertEquals(0, get.get());
    for (OptionDescriptor descriptor : descriptors) {
    }
    assertEquals(1, getOptionDescriptors.get());
    if (assertionsEnabled) {
        // with assertions enabled we assert the options.
        assertEquals(2, iterator.get());
    } else {
        // for valid options we should not need the iterator
        assertEquals(1, iterator.get());
    }
    assertEquals(0, get.get());
    assertNotNull(descriptors.get(ProxyLanguage.ID + ".option"));
    assertEquals(1, getOptionDescriptors.get());
    assertEquals(1, get.get());
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) OptionDescriptor(org.graalvm.options.OptionDescriptor) ArrayList(java.util.ArrayList) OptionDescriptors(org.graalvm.options.OptionDescriptors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) OptionKey(org.graalvm.options.OptionKey) Test(org.junit.Test)

Example 2 with OptionDescriptor

use of org.graalvm.options.OptionDescriptor in project graal by oracle.

the class OptionProcessorTest method testOptionsInstrument.

@Test
public void testOptionsInstrument() {
    Engine engine = Engine.create();
    OptionDescriptors descriptors = engine.getInstruments().get("optiontestinstr1").getOptions();
    OptionDescriptor descriptor;
    OptionDescriptor descriptor1;
    OptionDescriptor descriptor2;
    descriptor1 = descriptor = descriptors.get("optiontestinstr1.StringOption1");
    assertNotNull(descriptor);
    assertTrue(descriptor.isDeprecated());
    assertSame(OptionCategory.USER, descriptor.getCategory());
    assertEquals("StringOption1 help", descriptor.getHelp());
    assertSame(OptionTestInstrument1.StringOption1, descriptor.getKey());
    descriptor2 = descriptor = descriptors.get("optiontestinstr1.StringOption2");
    assertNotNull(descriptor);
    assertEquals("StringOption2 help", descriptor.getHelp());
    assertFalse(descriptor.isDeprecated());
    assertSame(OptionCategory.EXPERT, descriptor.getCategory());
    assertSame(OptionTestInstrument1.StringOption2, descriptor.getKey());
    Iterator<OptionDescriptor> iterator = descriptors.iterator();
    assertTrue(iterator.hasNext());
    assertEquals(descriptor1, iterator.next());
    assertTrue(iterator.hasNext());
    assertEquals(descriptor2, iterator.next());
    assertFalse(iterator.hasNext());
    assertNull(descriptors.get("optiontestinstr1.StringOption3"));
}
Also used : OptionDescriptor(org.graalvm.options.OptionDescriptor) OptionDescriptors(org.graalvm.options.OptionDescriptors) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 3 with OptionDescriptor

use of org.graalvm.options.OptionDescriptor in project graal by oracle.

the class OptionValuesImpl method failNotFound.

static RuntimeException failNotFound(OptionDescriptors allOptions, String key) {
    Iterable<OptionDescriptor> matches = fuzzyMatch(allOptions, key);
    Formatter msg = new Formatter();
    msg.format("Could not find option with name %s.", key);
    Iterator<OptionDescriptor> iterator = matches.iterator();
    if (iterator.hasNext()) {
        msg.format("%nDid you mean one of the following?");
        for (OptionDescriptor match : matches) {
            msg.format("%n    %s=<%s>", match.getName(), match.getKey().getType().getName());
        }
    }
    throw new IllegalArgumentException(msg.toString());
}
Also used : OptionDescriptor(org.graalvm.options.OptionDescriptor) Formatter(java.util.Formatter)

Example 4 with OptionDescriptor

use of org.graalvm.options.OptionDescriptor in project graal by oracle.

the class Launcher method printLanguageOptions.

private static void printLanguageOptions(Engine engine, OptionCategory optionCategory) {
    Map<Language, List<PrintableOption>> languagesOptions = new HashMap<>();
    List<Language> languages = sortedLanguages(engine);
    for (Language language : languages) {
        List<PrintableOption> options = new ArrayList<>();
        for (OptionDescriptor descriptor : language.getOptions()) {
            if (descriptor.getCategory().ordinal() == optionCategory.ordinal()) {
                options.add(asPrintableOption(descriptor));
            }
        }
        if (!options.isEmpty()) {
            languagesOptions.put(language, options);
        }
    }
    if (!languagesOptions.isEmpty()) {
        System.out.println();
        System.out.println("Language Options:");
        for (Language language : languages) {
            List<PrintableOption> options = languagesOptions.get(language);
            if (options != null) {
                printOptions(options, "  " + language.getName() + ":", 4);
            }
        }
    }
}
Also used : Language(org.graalvm.polyglot.Language) HashMap(java.util.HashMap) OptionDescriptor(org.graalvm.options.OptionDescriptor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with OptionDescriptor

use of org.graalvm.options.OptionDescriptor in project graal by oracle.

the class Launcher method printInstrumentOptions.

private static void printInstrumentOptions(Engine engine, OptionCategory optionCategory) {
    Map<Instrument, List<PrintableOption>> instrumentsOptions = new HashMap<>();
    List<Instrument> instruments = sortedInstruments(engine);
    for (Instrument instrument : instruments) {
        List<PrintableOption> options = new ArrayList<>();
        for (OptionDescriptor descriptor : instrument.getOptions()) {
            if (descriptor.getCategory().ordinal() == optionCategory.ordinal()) {
                options.add(asPrintableOption(descriptor));
            }
        }
        if (!options.isEmpty()) {
            instrumentsOptions.put(instrument, options);
        }
    }
    if (!instrumentsOptions.isEmpty()) {
        System.out.println();
        System.out.println("Tool options:");
        for (Instrument instrument : instruments) {
            List<PrintableOption> options = instrumentsOptions.get(instrument);
            if (options != null) {
                printOptions(options, "  " + instrument.getName() + ":", 4);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) OptionDescriptor(org.graalvm.options.OptionDescriptor) Instrument(org.graalvm.polyglot.Instrument) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

OptionDescriptor (org.graalvm.options.OptionDescriptor)12 Engine (org.graalvm.polyglot.Engine)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 OptionDescriptors (org.graalvm.options.OptionDescriptors)3 Language (org.graalvm.polyglot.Language)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Instrument (org.graalvm.polyglot.Instrument)2 TruffleContext (com.oracle.truffle.api.TruffleContext)1 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)1 Formatter (java.util.Formatter)1 Iterator (java.util.Iterator)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OptionCategory (org.graalvm.options.OptionCategory)1 OptionKey (org.graalvm.options.OptionKey)1 Context (org.graalvm.polyglot.Context)1