Search in sources :

Example 31 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class NodeDefaultsTest method testViewDefaults.

@Test
@SuppressWarnings("hiding")
public void testViewDefaults() {
    try (Context context = Context.create()) {
        ProxyLanguage language = new ProxyLanguage();
        ProxyLanguage.setDelegate(language);
        context.initialize(ProxyLanguage.ID);
        context.enter();
        Frame frame = Truffle.getRuntime().createMaterializedFrame(new Object[] {});
        Node n = new TestNode(ProxyLanguage.get(null), frame.getFrameDescriptor());
        NodeLibrary l = createLibrary(NodeLibrary.class, n);
        Object v = 42 * 42;
        // Integer is not associated with a language
        assertFails(() -> l.getView(n, frame, v), AssertionError.class);
        Object pv = new ProxyLanguageValue();
        assertSame(pv, l.getView(n, frame, pv));
    }
}
Also used : Context(org.graalvm.polyglot.Context) NodeLibrary(com.oracle.truffle.api.interop.NodeLibrary) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Frame(com.oracle.truffle.api.frame.Frame) Node(com.oracle.truffle.api.nodes.Node) ProbeNode(com.oracle.truffle.api.instrumentation.ProbeNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) RootNode(com.oracle.truffle.api.nodes.RootNode) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test)

Example 32 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class MultiClassLoaderTest method before.

@Before
public void before() {
    context = Context.newBuilder().allowAllAccess(true).build();
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected LanguageContext createContext(Env contextEnv) {
            env = contextEnv;
            return super.createContext(contextEnv);
        }

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            String req = request.getSource().getCharacters().toString();
            if (req.startsWith("get:")) {
                String name = req.substring(4);
                RootCallTarget reader = new RootNode(ProxyLanguage.get(null)) {

                    @Override
                    public Object execute(VirtualFrame frame) {
                        Object obj = frame.getArguments()[0];
                        try {
                            Object hash = InteropLibrary.getFactory().getUncached(obj).readMember(obj, "hash");
                            return InteropLibrary.getFactory().getUncached(hash).readMember(hash, name);
                        } catch (UnsupportedMessageException | UnknownIdentifierException e) {
                            throw new IllegalStateException(e);
                        }
                    }
                }.getCallTarget();
                return RootNode.createConstantNode(new CatcherObject(reader)).getCallTarget();
            }
            throw new IllegalArgumentException();
        }
    });
    context.initialize(ProxyLanguage.ID);
    context.enter();
    assertNotNull(env);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) Env(com.oracle.truffle.api.TruffleLanguage.Env) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) CatcherObject(com.oracle.truffle.api.test.host.HostExceptionTest.CatcherObject) CatcherObject(com.oracle.truffle.api.test.host.HostExceptionTest.CatcherObject) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Before(org.junit.Before)

Example 33 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class CPUSamplerTest method testSampleContextInitialization.

@Test
public void testSampleContextInitialization() {
    RootNode dummy = RootNode.createConstantNode(42);
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected void initializeContext(LanguageContext c) throws Exception {
            for (int i = 0; i < 50; i++) {
                Thread.sleep(1);
                TruffleSafepoint.pollHere(dummy);
            }
        }
    });
    sampler.setPeriod(1);
    sampler.setSampleContextInitialization(true);
    sampler.setCollecting(true);
    context.initialize(ProxyLanguage.ID);
    sampler.setCollecting(false);
    Map<TruffleContext, CPUSamplerData> data = sampler.getData();
    assertEquals(1, data.size());
    assertEquals(0, searchInitializeContext(data).size());
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) TruffleContext(com.oracle.truffle.api.TruffleContext) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test)

Example 34 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class CPUSamplerTest method testInitializeContext.

@Test
public void testInitializeContext() {
    RootNode dummy = RootNode.createConstantNode(42);
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected void initializeContext(LanguageContext c) throws Exception {
            for (int i = 0; i < 50; i++) {
                Thread.sleep(1);
                TruffleSafepoint.pollHere(dummy);
            }
        }
    });
    sampler.setPeriod(1);
    sampler.clearData();
    sampler.setCollecting(true);
    context.initialize(ProxyLanguage.ID);
    sampler.setCollecting(false);
    Map<TruffleContext, CPUSamplerData> data = sampler.getData();
    assertEquals(1, data.size());
    assertEquals(0, searchInitializeContext(data).size());
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) TruffleContext(com.oracle.truffle.api.TruffleContext) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test)

Example 35 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class IteratorTest method testArrayWithUnreadableElements.

@Test
public void testArrayWithUnreadableElements() {
    String[] items = new String[10];
    String[] expected = new String[items.length / 2];
    for (int i = 0; i < items.length; i++) {
        items[i] = Integer.toString(i);
        if (i < expected.length) {
            expected[i] = items[i];
        }
    }
    setupEnv(createContext(verifyingHandler), new ProxyLanguage() {

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            return createAST(languageInstance, new Array(items, expected.length, Array.UNLIMITED));
        }
    });
    verifyingHandler.expect(expected);
    assertFails(() -> context.eval(ProxyLanguage.ID, "Test"), PolyglotException.class, (pe) -> {
        assertEquals(NonReadableElementError.MESSAGE, pe.getMessage());
        assertTrue(pe.isGuestException());
        assertFalse(pe.isInternalError());
    });
}
Also used : ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) CallTarget(com.oracle.truffle.api.CallTarget) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) FrameSlotTypeException(com.oracle.truffle.api.frame.FrameSlotTypeException) ConcurrentModificationException(java.util.ConcurrentModificationException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) StopIterationException(com.oracle.truffle.api.interop.StopIterationException) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) NoSuchElementException(java.util.NoSuchElementException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Aggregations

ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)64 Test (org.junit.Test)47 CallTarget (com.oracle.truffle.api.CallTarget)32 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)28 PolyglotException (org.graalvm.polyglot.PolyglotException)26 RootNode (com.oracle.truffle.api.nodes.RootNode)21 Source (org.graalvm.polyglot.Source)21 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)20 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)20 Context (org.graalvm.polyglot.Context)17 TruffleContext (com.oracle.truffle.api.TruffleContext)16 Before (org.junit.Before)16 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)14 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)10 ArrayList (java.util.ArrayList)10 ExecutionException (java.util.concurrent.ExecutionException)10 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)9 ExecutorService (java.util.concurrent.ExecutorService)9