Search in sources :

Example 36 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class ProxySPITest method testProxyPrimitive.

@Test
@SuppressWarnings("deprecation")
public void testProxyPrimitive() throws Throwable {
    Context context = Context.create();
    org.graalvm.polyglot.proxy.ProxyPrimitive proxyOuter = new org.graalvm.polyglot.proxy.ProxyPrimitive() {

        public Object asPrimitive() {
            return 42;
        }
    };
    eval(context, proxyOuter, (proxyInner) -> {
        assertEquals(true, Message.IS_BOXED, proxyInner);
        assertEquals(42, Message.UNBOX, proxyInner);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.AS_POINTER, proxyInner);
        assertUnsupported(Message.GET_SIZE, proxyInner);
        assertEmpty(Message.KEYS, proxyInner);
        assertUnsupported(Message.READ, proxyInner);
        assertUnsupported(Message.WRITE, proxyInner);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertUnsupported(Message.createInvoke(0), proxyInner);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.createNew(0), proxyInner);
        assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(false, Message.HAS_KEYS, proxyInner);
        assertEquals(false, Message.HAS_SIZE, proxyInner);
        assertEquals(false, Message.IS_POINTER, proxyInner);
        assertEquals(0, Message.KEY_INFO, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Test(org.junit.Test)

Example 37 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class ProxySPITest method testProxyObjectUnsupported.

@Test
public void testProxyObjectUnsupported() throws Throwable {
    Context context = Context.create();
    ProxyObject proxyOuter = new ProxyObject() {

        public void putMember(String key, Value value) {
            throw new UnsupportedOperationException();
        }

        public boolean hasMember(String key) {
            return true;
        }

        public ProxyArray getMemberKeys() {
            return null;
        }

        public Object getMember(String key) {
            throw new UnsupportedOperationException();
        }
    };
    eval(context, proxyOuter, (proxyInner) -> {
        assertEmpty(Message.KEYS, proxyInner);
        assertUnsupported(Message.READ, proxyInner, "");
        assertUnsupported(Message.WRITE, proxyInner, "", 42);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.GET_SIZE, proxyInner);
        assertUnsupported(Message.READ, proxyInner, 0);
        assertUnsupported(Message.WRITE, proxyInner, 1);
        assertUnsupported(Message.UNBOX, proxyInner);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertUnsupported(Message.AS_POINTER, proxyInner);
        assertUnsupported(Message.createInvoke(0), proxyInner);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.createNew(0), proxyInner);
        assertEquals(false, Message.IS_BOXED, proxyInner);
        assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(true, Message.HAS_KEYS, proxyInner);
        assertEquals(false, Message.HAS_SIZE, proxyInner);
        assertEquals(false, Message.IS_POINTER, proxyInner);
        assertEquals(0, Message.KEY_INFO, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 38 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class ProxySPITest method testProxyError.

@Test
public void testProxyError() throws Throwable {
    Context context = Context.create();
    Proxy proxyOuter = new AllProxy();
    eval(context, proxyOuter, (proxyInner) -> {
        assertHostError(Message.AS_POINTER, proxyInner);
        assertHostError(Message.GET_SIZE, proxyInner);
        assertHostError(Message.KEYS, proxyInner);
        assertHostError(Message.READ, proxyInner, "");
        assertHostError(Message.READ, proxyInner, 42);
        assertHostError(Message.WRITE, proxyInner, "", 42);
        assertHostError(Message.WRITE, proxyInner, 42, 42);
        assertHostError(Message.REMOVE, proxyInner, 10);
        assertHostError(Message.UNBOX, proxyInner);
        assertHostError(Message.createInvoke(0), proxyInner, "");
        assertHostError(Message.createExecute(0), proxyInner);
        assertHostError(Message.createNew(0), proxyInner);
        assertHostError(Message.KEY_INFO, proxyInner, "");
        assertHostError(Message.KEY_INFO, proxyInner, 42);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertEquals(true, Message.IS_BOXED, proxyInner);
        assertEquals(true, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(true, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(true, Message.HAS_KEYS, proxyInner);
        assertEquals(true, Message.HAS_SIZE, proxyInner);
        assertEquals(true, Message.IS_POINTER, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Proxy(org.graalvm.polyglot.proxy.Proxy) Test(org.junit.Test)

Example 39 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class ProxySPITest method testProxyInstantiable.

@Test
public void testProxyInstantiable() throws Throwable {
    Context context = Context.create();
    ProxyInstantiable proxyOuter = new ProxyInstantiable() {

        @Override
        public Object newInstance(Value... t) {
            return t[0].newInstance();
        }
    };
    eval(context, proxyOuter, (proxyInner) -> {
        assertEquals(true, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
        assertUnsupported(Message.createExecute(0), proxyInner, 42);
        assertUnsupported(Message.AS_POINTER, proxyInner);
        assertUnsupported(Message.GET_SIZE, proxyInner);
        assertEmpty(Message.KEYS, proxyInner);
        assertUnsupported(Message.READ, proxyInner);
        assertUnsupported(Message.WRITE, proxyInner);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertUnsupported(Message.UNBOX, proxyInner);
        assertUnsupported(Message.createInvoke(0), proxyInner);
        assertEquals(false, Message.IS_BOXED, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(false, Message.HAS_KEYS, proxyInner);
        assertEquals(false, Message.HAS_SIZE, proxyInner);
        assertEquals(false, Message.IS_POINTER, proxyInner);
        assertEquals(0, Message.KEY_INFO, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyInstantiable(org.graalvm.polyglot.proxy.ProxyInstantiable) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 40 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class SourceAPITest method testCharSequenceNotMaterialized.

@Test
public void testCharSequenceNotMaterialized() throws IOException {
    AtomicBoolean materialized = new AtomicBoolean(false);
    final CharSequence testString = "testString";
    Source source = Source.newBuilder(SourceAPITestLanguage.ID, new CharSequence() {

        public CharSequence subSequence(int start, int end) {
            return testString.subSequence(start, end);
        }

        public int length() {
            return testString.length();
        }

        public char charAt(int index) {
            return testString.charAt(index);
        }

        @Override
        public String toString() {
            materialized.set(true);
            throw new AssertionError("Should not materialize CharSequence.");
        }
    }, "testsource").buildLiteral();
    Context context = Context.create(SourceAPITestLanguage.ID);
    context.eval(source);
    assertEquals(1, source.getLineCount());
    assertTrue(equalsCharSequence(testString, source.getCharacters()));
    assertTrue(equalsCharSequence(testString, source.getCharacters(1)));
    assertEquals(0, source.getLineStartOffset(1));
    assertNull(source.getURL());
    assertNotNull(source.getName());
    assertNull(source.getPath());
    assertEquals(6, source.getColumnNumber(5));
    assertEquals(SourceAPITestLanguage.ID, source.getLanguage());
    assertEquals(testString.length(), source.getLength());
    assertFalse(source.isInteractive());
    assertFalse(source.isInternal());
    // consume reader CharSequence should not be materialized
    CharBuffer charBuffer = CharBuffer.allocate(source.getLength());
    Reader reader = source.getReader();
    reader.read(charBuffer);
    charBuffer.position(0);
    assertEquals(testString, charBuffer.toString());
    assertFalse(materialized.get());
}
Also used : Context(org.graalvm.polyglot.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CharBuffer(java.nio.CharBuffer) Reader(java.io.Reader) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12